// Called when a tile is left clicked in fill mode
        private void fillTilePressed(Image tile, MouseButtonEventArgs e)
        {
            // If the selected image has not been created yet create it
                if (fillSelectedImage == null)
                {
                    // Draw a box around the current selection
                        fillSelectedImage = new Image();
                        fillSelectedImage.Source = selectImage;
                        Grid.SetColumn(fillSelectedImage, Grid.GetColumn(tile));
                        Grid.SetRow(fillSelectedImage, Grid.GetRow(tile));
                        fillSelectedImage.Width = tileSize;
                        fillSelectedImage.Height = tileSize;
                        mazeGrid.Children.Add(fillSelectedImage);
                        fillSelectedImage.Visibility = Visibility.Hidden;
                }

                // Check which point is being selected
                    if (fillSelectedImage.Visibility == Visibility.Hidden)
                    {
                        // Draw the box around the first selection
                            Grid.SetColumn(fillSelectedImage, Grid.GetColumn(tile));
                            Grid.SetRow(fillSelectedImage, Grid.GetRow(tile));
                            fillSelectedImage.Width = tileSize;
                            fillSelectedImage.Height = tileSize;
                            fillSelectedImage.Visibility = Visibility.Visible;

                    }
                    else
                    {
                        // Get the parameters of the whole selection, hide the old image, and set fill mode off
                            int x1 = Math.Min(Grid.GetColumn(fillSelectedImage), Grid.GetColumn(tile)),
                                y1 = Math.Min(Grid.GetRow(fillSelectedImage), Grid.GetRow(tile)),
                                x2 = Math.Max(Grid.GetColumn(fillSelectedImage), Grid.GetColumn(tile)) + 1,
                                y2 = Math.Max(Grid.GetRow(fillSelectedImage), Grid.GetRow(tile)) + 1;
                            clearMode();

                        // Fill the selection with walls or floors depending on the mouse button clicked
                            for (int x = x1; x < x2; x++)
                            {
                                for (int y = y1; y < y2; y++)
                                {
                                    // Create commands only of the tiles that are going to be changed
                                        if (tiles[x][y] != (e.RightButton == MouseButtonState.Pressed))
                                        {
                                            InvertableCommand command = new ChangeTileCommand(x, y, e.RightButton == MouseButtonState.Pressed, setTile);
                                            currentCommands.Push(command);
                                            command.Execute();
                                        }
                                }
                            }

                        // Add the commands to the undo stack
                            storeCurrentCommands();
                    }
        }
        // Detects when the user presses on the grid of the maze
        private void tilePressed(object sender, MouseEventArgs e)
        {
            // If Shift click is pressed start fill mode
                if (e is MouseButtonEventArgs && (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)))
                {
                    Mouse.OverrideCursor = Cursors.Cross;
                    fillMode = true;
                }

            // If Alt click is pressed set exit using a command
                if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
                {
                    if (e is MouseButtonEventArgs)
                    {
                        InvertableCommand command = new EntranceCommand(Grid.GetColumn(entranceImage), Grid.GetRow(entranceImage), Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), changeEntrance);
                        currentCommands.Push(command);
                        command.Execute();
                    }
                }

            // If Crtl click is pressed set exit using a command
                else if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    if (e is MouseButtonEventArgs)
                    {
                        InvertableCommand command = new EntranceCommand(Grid.GetColumn(exitImage), Grid.GetRow(exitImage), Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), changeExit);
                        currentCommands.Push(command);
                        command.Execute();
                    }
                }

            // Check if fill mode or exit mode is on and mouse button was clicked
                else if (fillMode && e is MouseButtonEventArgs)
                    fillTilePressed((Image)sender, (MouseButtonEventArgs)e);
                else if (entranceMode && e is MouseButtonEventArgs)
                {
                    InvertableCommand command = new EntranceCommand(Grid.GetColumn(entranceImage), Grid.GetRow(entranceImage), Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), changeEntrance);
                    currentCommands.Push(command);
                    command.Execute();
                }
                else if (exitMode && e is MouseButtonEventArgs)
                {
                    InvertableCommand command = new EntranceCommand(Grid.GetColumn(exitImage), Grid.GetRow(exitImage), Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), changeExit);
                    currentCommands.Push(command);
                    command.Execute();
                }
                else
                {
                    // Change the tile of the currently selected tile
                    if (e.RightButton == MouseButtonState.Pressed && tiles[Grid.GetColumn((Image)sender)][Grid.GetRow((Image)sender)] != true)
                    {
                        InvertableCommand command = new ChangeTileCommand(Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), true, setTile);
                        currentCommands.Push(command);
                        command.Execute();
                    }
                    else if (e.LeftButton == MouseButtonState.Pressed && tiles[Grid.GetColumn((Image)sender)][Grid.GetRow((Image)sender)] != false)
                    {
                        InvertableCommand command = new ChangeTileCommand(Grid.GetColumn((Image)sender), Grid.GetRow((Image)sender), false, setTile);
                        currentCommands.Push(command);
                        command.Execute();
                    }
                }
        }