Private Sub HandleSelectionMode(e As MouseEventArgs)
        ' Handle selecting a square in selection mode
        Dim clickedX As Integer = (e.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
        Dim clickedY As Integer = (e.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

        ' Determine if the user clicked on a wall or a floor
        Dim isWall As Boolean = highlightedWallX OrElse highlightedWallY
        Dim itemType As String = If(isWall, If(highlightedWallX, "VerticalWall", "HorizontalWall"), "Floor")

        ' Determine the item at the selected point
        Dim item As String
        If isWall Then
            If highlightedWallX Then
                ' Check vertical wall items
                If clickedX >= 0 AndAlso clickedX < verticalWallItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < verticalWallItems(clickedX).Length Then
                    item = If(verticalWallItems(clickedX)(clickedY) IsNot Nothing, verticalWallItems(clickedX)(clickedY).ImageKey, "Nothing")
                End If
            ElseIf highlightedWallY Then
                ' Check horizontal wall items
                If clickedX >= 0 AndAlso clickedX < horizontalWallItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < horizontalWallItems(clickedX).Length Then
                    item = If(horizontalWallItems(clickedX)(clickedY) IsNot Nothing, horizontalWallItems(clickedX)(clickedY).ImageKey, "Nothing")
                End If
            End If
        Else
            ' Check floor items
            If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso
   clickedY >= 0 AndAlso clickedY < placedItems(clickedX).Length Then
                item = If(placedItems(clickedX)(clickedY) IsNot Nothing, placedItems(clickedX)(clickedY).ImageKey, "Nothing")
            End If
        End If

        ' Check selection constraints
        Dim triggerEditor As frmTriggerEditor = CType(SelectionRequestedByForm, frmTriggerEditor)
        If SelectionRequestedFor = "TriggeringPoint" Then
            If triggerEditor.AllowFloorSelectionOnly AndAlso isWall Then
                Return
            End If
        ElseIf SelectionRequestedFor = "TriggerActionPoint" Then
            ' Allow clicking anything for TriggerActionPoint
        ElseIf SelectionRequestedFor = "TriggerItemToRemove" Then
            If triggerEditor.AllowWallSelectionOnly AndAlso Not isWall Then
                Return
            End If
        End If

        ' Pass the selected point, item, and item type back to the requesting form
        If TypeOf SelectionRequestedByForm Is frmTriggerEditor Then
            If SelectionRequestedFor = "TriggeringPoint" Then
                triggerEditor.SetTriggeringPoint(New Point(clickedX, clickedY))
            ElseIf SelectionRequestedFor = "TriggerActionPoint" Then
                triggerEditor.SetTriggerActionPoint(New Point(clickedX, clickedY), itemType)
            End If
        End If

        TurnOffSelectionMode()

        ' Bring FrmTriggerEditor to the foreground
        If TypeOf SelectionRequestedByForm Is frmTriggerEditor Then
            triggerEditor.BringToFront() ' Bring FrmTriggerEditor to the front
            Dim unused = triggerEditor.Focus() ' Focus on FrmTriggerEditor
        End If
    End Sub

    Private Sub TurnOffSelectionMode()
        isInSelectionMode = False
        selectionRectangle = Rectangle.Empty
        SelectionMode = False
        SelectionRequestedByForm = Nothing
        SelectionRequestedFor = Nothing
        ResetTheCursor()

        ' Reset the cycle
        isSelectionExpandedToWalls = False
        isSelectionExpandedToFloors = False
    End Sub
#Region " Mouse Up, Down, Move "
    Private Sub PicGame_MouseDown(sender As Object, e As MouseEventArgs) Handles picGame.MouseDown
        If SelectionMode Then
            HandleSelectionMode(e)
            Return
        End If

        If e.Button = MouseButtons.Left Then
            ' Handle actions when the left mouse button is clicked
            HandleGateClicked(e.X, e.Y)
            HandleBaddyClicked(e.X, e.Y) ' Ensure this line is added

            ' Start the selection process
            isSelecting = True
            selectionStartPoint = e.Location
            selectionEndPoint = e.Location
            selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)

            ' Reset selection data
            selectionData = Nothing
            selectionStartCell = New Point(-1, -1)
            copiedSelectionData = Nothing
            copiedSelectionType = ""

            ' Check if selection starts from a wall or the space between walls
            Dim offsetX As Integer = (selectionStartPoint.X - INDENT) Mod (SQSizeX + WALL_THICKNESS)
            Dim offsetY As Integer = (selectionStartPoint.Y - INDENT) Mod (SQSizeY + WALL_THICKNESS)
            selectionStartedOnWall = (offsetX < WALL_THICKNESS) OrElse (offsetY < WALL_THICKNESS)

            ' Adjust the selection start point to snap to the grid
            selectionStartPoint = GetGridAlignedPoint(selectionStartPoint)

            ' Determine if the user starts the selection on a floor or a wall
            Dim clickedX As Integer = (e.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
            Dim clickedY As Integer = (e.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

            If clickedX >= 0 AndAlso clickedX < SQCOUNTX AndAlso clickedY >= 0 AndAlso clickedY < SQCOUNTY Then
                If Not IsMouseOverWall(e.X, e.Y) Then
                    ' The starting selection is on the floor
                    SnapToFloor = True
                Else
                    ' Starting on a wall
                    SnapToFloor = False
                End If
            End If
        ElseIf e.Button = MouseButtons.Right Then
            ' Hide properties displays
            UcBaddyProps.Visible = False
            krKeys.Visible = False
        End If

        If isSelectionReadyToDrag Then
            If selectionRectangle.Contains(e.Location) Then
                If e.Button = MouseButtons.Left Then
                    SaveStateForUndo()
                    isDraggingSelection = True
                    selectionOffset = New Point(e.X - selectionRectangle.Location.X, e.Y - selectionRectangle.Location.Y)
                    selectionStartCell = New Point(
                (selectionRectangle.Location.X - INDENT) \ (SQSizeX + WALL_THICKNESS),
                (selectionRectangle.Location.Y - INDENT) \ (SQSizeY + WALL_THICKNESS))
                    picGame.Capture = True
                    Cursor = Cursors.SizeAll
                ElseIf e.Button = MouseButtons.Right Then
                    ' Show context menu if right-clicking inside the selection
                    Dim menuPosition As Point = e.Location
                    ctmMenu.Show(picGame, menuPosition)
                End If
            End If
        ElseIf isInSelectionMode Then
            ' Start selecting a new area
            If e.Button = MouseButtons.Left Then
                isSelecting = True
                selectionStartPoint = e.Location
                selectionEndPoint = e.Location
                selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)

                ' Reset selection data
                selectionData = Nothing
                selectionStartCell = New Point(-1, -1)
                copiedSelectionData = Nothing
                copiedSelectionType = ""

                ' Reset the cycle
                isSelectionExpandedToWalls = False
                isSelectionExpandedToFloors = False

                ' Adjust the selection start point to snap to the grid
                selectionStartPoint = GetGridAlignedPoint(selectionStartPoint)
            ElseIf e.Button = MouseButtons.Right Then
                If selectionRectangle.Contains(e.Location) Then
                    Dim menuPosition As Point = e.Location
                    ctmMenu.Show(picGame, menuPosition)
                End If
            Else
                TurnOffSelectionMode()
            End If
        Else
            ' Normal mouse down processing
            If e.Button = MouseButtons.Right Then
                isRightMouseDown = True
                rightMouseDownPosition = e.Location
                rightMouseDownTime = Date.Now
                If Not isActionInProgress Then
                    isActionInProgress = True
                    SaveStateForUndo()
                End If
            ElseIf e.Button = MouseButtons.Left Then
                isLeftMouseDown = True
                UpdateHighlight(e.X, e.Y)
                Dim itemPickedUp As Boolean = False

                If isMouseInsideGrid Then
                    If Not isActionInProgress Then
                        isActionInProgress = True
                        SaveStateForUndo()
                    End If

                    If highlightedWallX Then
                        ' Handle vertical wall item pickup
                        If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
               highlightedY >= 0 AndAlso highlightedY < verticalWallItems(0).Length Then
                            If verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                                isDraggingItem = True
                                draggedItem = verticalWallItems(highlightedX)(highlightedY)
                                draggedItemIsWall = True
                                draggedItemIsVerticalWall = True
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                verticalWallItems(highlightedX)(highlightedY) = Nothing
                                verticalWalls(highlightedX)(highlightedY) = True
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    ElseIf highlightedWallY Then
                        ' Handle horizontal wall item pickup
                        If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
               highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(0).Length Then
                            If horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                                isDraggingItem = True
                                draggedItem = horizontalWallItems(highlightedX)(highlightedY)
                                draggedItemIsWall = True
                                draggedItemIsVerticalWall = False
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                horizontalWallItems(highlightedX)(highlightedY) = Nothing
                                horizontalWalls(highlightedX)(highlightedY) = True
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    Else
                        ' Handle floor item pickup
                        If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
               highlightedY >= 0 AndAlso highlightedY < placedItems(0).Length Then
                            If placedItems(highlightedX)(highlightedY) IsNot Nothing Then
                                isDraggingItem = True
                                draggedItem = placedItems(highlightedX)(highlightedY)
                                draggedItemIsWall = False
                                draggedItemIsVerticalWall = False
                                draggedItemOriginalX = highlightedX
                                draggedItemOriginalY = highlightedY
                                placedItems(highlightedX)(highlightedY) = Nothing
                                picGame.Capture = True
                                itemPickedUp = True
                            End If
                        End If
                    End If

                    If itemPickedUp Then
                        redoStack.Clear()
                    Else
                        ' Add wall or edge style handling
                        If rdoEdge.Checked Then
                            isLeftMouseDown = True
                            SetEdgeStyleAtMousePosition(e.X, e.Y)
                        Else
                            isLeftMouseDown = True
                            AddWallAtMousePosition(e.X, e.Y)
                        End If
                    End If
                End If
            End If
        End If

        ' Check if the krKeys control is visible and the click is outside the cell with the gate
        If krKeys.Visible And IsOnGate = False Then
            Dim clickedX As Integer = (e.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
            Dim clickedY As Integer = (e.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

            If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < placedItems(0).Length Then
                Dim clickedItem As ItemInfo = placedItems(clickedX)(clickedY)
                If clickedItem Is Nothing OrElse clickedItem.ImageKey <> "picGate" Then
                    krKeys.Visible = False
                End If
            Else
                krKeys.Visible = False
            End If
        End If

        ' Check if the UcBaddyProps control is visible and the click is outside the cell with the Baddy
        If UcBaddyProps.Visible AndAlso Not IsOnBaddy Then
            Dim clickedX As Integer = (e.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
            Dim clickedY As Integer = (e.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

            If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < placedItems(0).Length Then
                Dim clickedItem As ItemInfo = placedItems(clickedX)(clickedY)
                If Not clickedItem.ImageKey.StartsWith("picBaddy") Then
                    UcBaddyProps.Visible = False
                End If
            Else
                UcBaddyProps.Visible = False
            End If
        End If
    End Sub
    Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles picGame.MouseMove
        isCursorOnPicGame = True

        If isDraggingSelection Then
            ' Existing code for dragging selection
            If isCopying Then
                If copiedSelectionData Is Nothing Then
                    Return
                End If
            Else
                If selectionData Is Nothing Then
                    CaptureSelectionData()
                End If
            End If

            ' Calculate new cell positions
            Dim newX As Integer = e.X - selectionOffset.X
            Dim newY As Integer = e.Y - selectionOffset.Y

            Dim cellX As Integer = (newX - INDENT) \ (SQSizeX + WALL_THICKNESS)
            Dim cellY As Integer = (newY - INDENT) \ (SQSizeY + WALL_THICKNESS)

            ' Ensure cellX and cellY are within bounds
            If isCopying Then
                cellX = Math.Max(0, Math.Min(SQCOUNTX - copiedSelectionData.selectionWidth, cellX))
                cellY = Math.Max(0, Math.Min(SQCOUNTY - copiedSelectionData.selectionHeight, cellY))
                selectionRectangle.Width = (copiedSelectionData.selectionWidth * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS
                selectionRectangle.Height = (copiedSelectionData.selectionHeight * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS
            Else
                cellX = Math.Max(0, Math.Min(SQCOUNTX - selectionData.selectionWidth, cellX))
                cellY = Math.Max(0, Math.Min(SQCOUNTY - selectionData.selectionHeight, cellY))
                selectionRectangle.Width = (selectionData.selectionWidth * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS
                selectionRectangle.Height = (selectionData.selectionHeight * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS
            End If

            ' Update selection rectangle position
            selectionRectangle.X = INDENT + (cellX * (SQSizeX + WALL_THICKNESS))
            selectionRectangle.Y = INDENT + (cellY * (SQSizeY + WALL_THICKNESS))

            ' Store cell positions for use in MouseUp
            draggedSelectionCellX = cellX
            draggedSelectionCellY = cellY

        ElseIf isDraggingItem Then
            ' Update the mouse position to the current cursor position
            mouseX = e.X
            mouseY = e.Y

            ' Snap the item to the nearest wall and center it
            UpdateHighlight(e.X, e.Y)
            If draggedItemIsWall Then
                If draggedItemIsVerticalWall Then
                    mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) - (WALL_THICKNESS \ 2) + 4
                    mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeY \ 2)
                Else
                    mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeX \ 2)
                    mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) - (WALL_THICKNESS \ 2) + 4
                End If
            Else
                mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeX \ 2)
                mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeY \ 2)
            End If

        ElseIf isInSelectionMode Then
            If isSelecting Then
                Dim newEndPoint = e.Location

                ' Adjust the snap behavior based on where we started the selection
                If SnapToFloor Then
                    selectionStartPoint = GetGridAlignedPoint(selectionStartPoint)
                    newEndPoint = GetGridAlignedPoint(newEndPoint, isFloor:=True)
                Else
                    selectionStartPoint = GetGridAlignedPoint(selectionStartPoint, isWall:=True)
                    newEndPoint = GetGridAlignedPoint(newEndPoint, isWall:=True)
                End If

                selectionRectangle = GetRectangle(selectionStartPoint, newEndPoint)
            End If
        Else
            UpdateHighlight(e.X, e.Y)

            If isRightMouseDown Then
                If rdoEdge.Checked Then
                    If isMouseInsideGrid Then
                        RemoveEdgeStyleAtMousePosition(e.X, e.Y)
                    End If
                Else
                    Dim deltaX As Integer = e.X - rightMouseDownPosition.X
                    Dim deltaY As Integer = e.Y - rightMouseDownPosition.Y
                    Dim distanceSquared As Integer = (deltaX * deltaX) + (deltaY * deltaY)
                    If distanceSquared > CLICK_DRAG_THRESHOLD_SQUARED Then
                        RemoveObjectAtMousePosition(e.X, e.Y, removeWalls:=True, removeItems:=False)
                    End If
                End If
            End If
            If isLeftMouseDown Then
                If rdoEdge.Checked Then
                    If isMouseInsideGrid Then
                        SetEdgeStyleAtMousePosition(e.X, e.Y)
                    End If
                Else
                    AddWallAtMousePosition(e.X, e.Y)
                End If
            End If
        End If

        UpdateStatusBar(e.X, e.Y)
    End Sub
    Private Sub PicGame_MouseUp(sender As Object, e As MouseEventArgs) Handles picGame.MouseUp
        If isDraggingSelection Then
            ' Completing selection dragging
            If e.Button = MouseButtons.Left Then
                ' Save the state before modifying the data model
                If Not isRestoringState Then
                    SaveStateForUndo()
                End If

                isDraggingSelection = False
                picGame.Capture = False
                Cursor = Cursors.Default

                If selectionRectangle.IsEmpty Then
                    Return
                End If

                ' When the mouse is released, we don’t change the selection rectangle. Use the stored
                ' selection data and the adjusted rectangle from when it was being drawn.
                ' Use the captured selection data to snap to the grid.
                Dim startCell As New Point(
        (selectionRectangle.X - INDENT) \ (SQSizeX + WALL_THICKNESS),
        (selectionRectangle.Y - INDENT) \ (SQSizeY + WALL_THICKNESS))

                PasteSelectionDataTo(startCell, False, copiedSelectionType = "Edge")

                isCopying = False
                isSelectionReadyToDrag = False

                ' Reset selection-related variables to prevent issues with future operations
                selectionData = Nothing
                selectionStartCell = New Point(-1, -1)
                copiedSelectionData = Nothing
                copiedSelectionType = ""
                isCopying = False
                isSelectionReadyToDrag = False
                isDraggingSelection = False

                ' Clear the redo stack since we've made a new change
                redoStack.Clear()
            End If
        ElseIf isInSelectionMode AndAlso isSelecting Then
            If e.Button = MouseButtons.Left Then
                ' Finalizing the selection rectangle
                isSelecting = False
                selectionEndPoint = e.Location

                ' Finalize selection snapping based on the type of click
                selectionEndPoint = If(SnapToFloor, GetGridAlignedPoint(selectionEndPoint, isFloor:=True), GetGridAlignedPoint(selectionEndPoint, isWall:=True))

                selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)

                ' Check if the selection needs to be adjusted
                selectionRectangle = AdjustSelectionRectangleToWalls(selectionRectangle)

                ' Reset selection-related variables
                selectionData = Nothing
                selectionStartCell = New Point(-1, -1)
                copiedSelectionData = Nothing
                copiedSelectionType = ""
                isCopying = False
                isSelectionReadyToDrag = False
                isDraggingSelection = False

                If selectionRectangle.IsEmpty Then
                    TurnOffSelectionMode()
                End If
            End If
        Else
            ' Normal mouse up processing
            If e.Button = MouseButtons.Left Then
                If isDraggingItem Then
                    ' Save the state before modifying the data model
                    If Not isRestoringState Then
                        SaveStateForUndo()
                    End If

                    isDraggingItem = False
                    picGame.Capture = False
                    Cursor.Current = Cursors.Default

                    ' Snap the item to the grid position
                    UpdateHighlight(e.X, e.Y)
                    If isMouseInsideGrid Then
                        If draggedItemIsWall Then
                            If draggedItemIsVerticalWall Then
                                If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
                   verticalWallItems(highlightedX) IsNot Nothing AndAlso
                   highlightedY >= 0 AndAlso highlightedY < verticalWallItems(0).Length Then

                                    If Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) OrElse IsFlipperItem(draggedItem.ImageKey) Then
                                        verticalWallItems(highlightedX)(highlightedY) = draggedItem
                                        verticalWalls(highlightedX)(highlightedY) = False
                                    Else
                                        ReturnItemToOriginalLocation()
                                    End If
                                End If
                            Else
                                If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
                   horizontalWallItems(highlightedX) IsNot Nothing AndAlso
                   highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(0).Length Then

                                    If Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) OrElse IsFlipperItem(draggedItem.ImageKey) Then
                                        horizontalWallItems(highlightedX)(highlightedY) = draggedItem
                                        horizontalWalls(highlightedX)(highlightedY) = False
                                    Else
                                        ReturnItemToOriginalLocation()
                                    End If
                                End If
                            End If
                        Else
                            If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
                   highlightedY >= 0 AndAlso highlightedY < placedItems(0).Length Then

                                placedItems(highlightedX)(highlightedY) = draggedItem
                            End If
                        End If
                    End If

                    ' Clear redo stack since we've made a new change
                    redoStack.Clear()

                    ' Invalidate the picGame control to refresh the display

                End If

                isLeftMouseDown = False
                isActionInProgress = False

            ElseIf e.Button = MouseButtons.Right Then
                If isRightMouseDown Then
                    isRightMouseDown = False

                    If rdoEdge.Checked Then
                        UpdateHighlight(e.X, e.Y)
                        If isMouseInsideGrid Then
                            If Not highlightedWallX AndAlso Not highlightedWallY Then
                                If highlightedX >= 0 AndAlso highlightedX < SQCOUNTX AndAlso highlightedY >= 0 AndAlso highlightedY < SQCOUNTY Then
                                    floorsUseEdgeStyle(highlightedX)(highlightedY) = False

                                End If
                            End If
                        End If
                    Else
                        Dim deltaX As Integer = e.X - rightMouseDownPosition.X
                        Dim deltaY As Integer = e.Y - rightMouseDownPosition.Y
                        Dim distanceSquared As Integer = (deltaX * deltaX) + (deltaY * deltaY)
                        If distanceSquared < CLICK_DRAG_THRESHOLD_SQUARED Then
                            RemoveObjectAtMousePosition(e.X, e.Y, removeWalls:=False, removeItems:=True)
                        End If
                    End If

                    isActionInProgress = False
                End If

                ' Check if the right-click is outside the selected area
                If isInSelectionMode AndAlso Not selectionRectangle.Contains(e.Location) Then
                    TurnOffSelectionMode()
                End If
            End If
        End If
    End Sub

    Private Sub Form_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
        Dim screenPos As Point = PointToScreen(New Point(e.X, e.Y))
        Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)

        isCursorOnPicGame = picGame.ClientRectangle.Contains(picGameClientPoint)

        If isDraggingItem Then
            mouseX = picGameClientPoint.X
            mouseY = picGameClientPoint.Y

            If isCursorOnPicGame Then
                UpdateHighlight(mouseX, mouseY)
                isMouseInsideGrid = True

                ' Snap the item to the nearest wall and center it
                If draggedItemIsWall Then
                    If draggedItemIsVerticalWall Then
                        mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) - (WALL_THICKNESS \ 2) + 4
                        mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeY \ 2)
                    Else
                        mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeX \ 2)
                        mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) - (WALL_THICKNESS \ 2) + 4
                    End If
                Else
                    mouseX = INDENT + (highlightedX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeX \ 2)
                    mouseY = INDENT + (highlightedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2) + (SQSizeY \ 2)
                End If
            Else
                isMouseInsideGrid = False
            End If

            ' Invalidate only the picGame control
        End If

        ' Force the highlight color to toggle based on mouse movement
        Dim currentTime As Date = Date.Now
        If (currentTime - lastMouseMoveTime).TotalMilliseconds >= DEBOUNCE_INTERVAL Then
            highlightColor = If(highlightColor = Color.Black, Color.White, Color.Black)
            lastMouseMoveTime = currentTime
        End If

        UpdateStatusBar(e.X, e.Y)
    End Sub

    Private Sub Form_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
        If isDraggingItem AndAlso e.Button = MouseButtons.Left Then
            ' Before modifying the data model, save the state
            If Not isRestoringState Then
                SaveStateForUndo()
            End If

            isDraggingItem = False
            Capture = False
            Cursor.Current = Cursors.Default

            Dim screenPos As Point = PointToScreen(New Point(e.X, e.Y))
            Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)

            If picGame.ClientRectangle.Contains(picGameClientPoint) Then
                mouseX = picGameClientPoint.X
                mouseY = picGameClientPoint.Y
                UpdateHighlight(mouseX, mouseY)

                If isMouseInsideGrid Then
                    If draggedItemIsWall Then
                        If draggedItemIsVerticalWall Then
                            If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
                           verticalWallItems(highlightedX) IsNot Nothing AndAlso
                           highlightedY >= 0 AndAlso highlightedY < verticalWallItems(0).Length Then

                                If Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) OrElse IsFlipperItem(draggedItem.ImageKey) Then
                                    verticalWallItems(highlightedX)(highlightedY) = draggedItem
                                    verticalWalls(highlightedX)(highlightedY) = False
                                Else
                                    ReturnItemToOriginalLocation()
                                End If
                            End If
                        Else
                            If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
                           horizontalWallItems(highlightedX) IsNot Nothing AndAlso
                           highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(0).Length Then

                                If Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) OrElse IsFlipperItem(draggedItem.ImageKey) Then
                                    horizontalWallItems(highlightedX)(highlightedY) = draggedItem
                                    horizontalWalls(highlightedX)(highlightedY) = False
                                Else
                                    ReturnItemToOriginalLocation()
                                End If
                            End If
                        End If
                    Else
                        ' Existing code for placing non-wall items
                        If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
                       highlightedY >= 0 AndAlso highlightedY < placedItems(0).Length Then

                            placedItems(highlightedX)(highlightedY) = draggedItem
                        End If
                    End If
                Else
                    ReturnItemToOriginalLocation()
                End If
            Else
                ReturnItemToOriginalLocation()
            End If

            draggedItem = Nothing

            If isActionInProgress Then
                isActionInProgress = False
                redoStack.Clear()
            End If

            redoStack.Clear()
        End If
    End Sub

    Private Sub PicBlock_MouseDown(sender As Object, e As MouseEventArgs) Handles picBlock1.MouseDown, picBlock2.MouseDown, picBaddy1.MouseDown, picBaddy2.MouseDown, picLaser1.MouseDown, picLaser2.MouseDown, picFlipper1.MouseDown, picFlipper2.MouseDown, picFlipper3.MouseDown, picFlipper4.MouseDown, picBaddy3.MouseDown, picBaddy4.MouseDown, picCoin.MouseDown, picGate.MouseDown, picKey.MouseDown, picStart.MouseDown, picFinish.MouseDown, picBlockStopper1.MouseDown, picBall.MouseDown, picBlockSwitch.MouseDown, picBomb.MouseDown, picWarp.MouseDown, picWarp2.MouseDown, picWarp3.MouseDown, picVolcano.MouseDown
        TurnOffSelectionMode()
        Dim pic = CType(sender, PictureBox)

        isDraggingItem = True

        ' Create a new PlacedItem with the ImageKey
        draggedItem = New ItemInfo(pic.Name, New Point(0, 0), Guid.NewGuid.ToString, isDoodad:=IsDoodadItem(pic.Name))

        ' Determine if it's a wall item
        draggedItemIsWall = IsVerticalItem(pic.Name) OrElse IsHorizontalItem(pic.Name)
        draggedItemIsVerticalWall = IsVerticalItem(pic.Name)

        ' Since it's being dragged from the toolbox, there is no original position
        draggedItemOriginalX = -1
        draggedItemOriginalY = -1

        ' Capture the mouse events at the form level
        Capture = True

        Cursor.Current = Cursors.Hand

        ' Initialize the mouse position to the current cursor position
        Dim mousePos = picGame.PointToClient(Cursor.Position)
        mouseX = mousePos.X
        mouseY = mousePos.Y
    End Sub
#End Region
#Region " Mouse Helpers "
    Private Function GetGridAlignedPoint(point As Point, Optional isFloor As Boolean = False, Optional isWall As Boolean = False) As Point
        Dim cellX As Integer = (point.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
        Dim cellY As Integer = (point.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

        ' Ensure we align correctly based on the type of snapping
        If isFloor Then
            cellX = Math.Max(0, Math.Min(SQCOUNTX - 1, cellX))
            cellY = Math.Max(0, Math.Min(SQCOUNTY - 1, cellY))
        ElseIf isWall Then
            ' To get wall or wall intersection points, adjust this computation
            cellX = Math.Max(0, Math.Min(SQCOUNTX, cellX)) ' Adjust bounds for wall snapping
            cellY = Math.Max(0, Math.Min(SQCOUNTY, cellY)) ' Adjust bounds for wall snapping
        End If

        Dim alignedX As Integer = INDENT + (cellX * (SQSizeX + WALL_THICKNESS))
        Dim alignedY As Integer = INDENT + (cellY * (SQSizeY + WALL_THICKNESS))

        Return New Point(alignedX, alignedY)
    End Function

    Private Function IsMouseOverWall(mouseX As Integer, mouseY As Integer) As Boolean
        ' You can iterate over the vertical and horizontal walls to determine if the mouse is over a wall
        UpdateHighlight(mouseX, mouseY)
        Return highlightedWallX OrElse highlightedWallY
    End Function

    Private Function AdjustSelectionRectangleToWalls(rect As Rectangle) As Rectangle
        ' Determine starting and ending points for cell snapping
        Dim startCellX As Integer = (rect.X - INDENT + (WALL_THICKNESS \ 2)) \ (SQSizeX + WALL_THICKNESS)
        Dim startCellY As Integer = (rect.Y - INDENT + (WALL_THICKNESS \ 2)) \ (SQSizeY + WALL_THICKNESS)
        Dim endCellX As Integer = (rect.Right - INDENT - (WALL_THICKNESS \ 2) - 1) \ (SQSizeX + WALL_THICKNESS)
        Dim endCellY As Integer = (rect.Bottom - INDENT - (WALL_THICKNESS \ 2) - 1) \ (SQSizeY + WALL_THICKNESS)

        ' Make sure to stay within grid bounds
        startCellX = Math.Max(0, Math.Min(SQCOUNTX - 1, startCellX))
        startCellY = Math.Max(0, Math.Min(SQCOUNTY - 1, startCellY))
        endCellX = Math.Max(0, Math.Min(SQCOUNTX - 1, endCellX))
        endCellY = Math.Max(0, Math.Min(SQCOUNTY - 1, endCellY))

        ' Adjust rectangle to snap to walls' positions
        If highlightedWallX OrElse highlightedWallY Then
            ' Adjust snap position for walls
            Dim adjustedRect As New Rectangle(
            INDENT + (startCellX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
            INDENT + (startCellY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
            ((endCellX - startCellX + 1) * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS,
            ((endCellY - startCellY + 1) * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS
        )
            Return adjustedRect
        Else
            ' Otherwise, follow the standard floor area snapping.
            Dim adjustedRect As New Rectangle(
            INDENT + (startCellX * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
            INDENT + (startCellY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
            ((endCellX - startCellX + 1) * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS,
            ((endCellY - startCellY + 1) * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS
        )
            Return adjustedRect
        End If
    End Function
    Private Sub HandleBaddyClicked(mouseX As Integer, mouseY As Integer)
        ' Calculate the cell coordinates based on the mouse position
        Dim clickedX As Integer = (mouseX - INDENT) \ (SQSizeX + WALL_THICKNESS)
        Dim clickedY As Integer = (mouseY - INDENT) \ (SQSizeY + WALL_THICKNESS)

        ' Check if the clicked cell contains a Baddy
        If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso clickedY >= 0 AndAlso clickedY < placedItems(0).Length Then
            Dim clickedItem As ItemInfo = placedItems(clickedX)(clickedY)
            If clickedItem IsNot Nothing AndAlso clickedItem.ImageKey.StartsWith("picBaddy") Then
                IsOnBaddy = True

                ' Calculate the top-right corner of the clicked cell
                Dim cellTopRightX As Integer = INDENT + (clickedX * (SQSizeX + WALL_THICKNESS)) + SQSizeX + 9
                Dim cellTopRightY As Integer = INDENT + (clickedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS * 2) + 38

                ' Set the position and make the control visible
                UcBaddyProps.Location = New Point(cellTopRightX, cellTopRightY)
                UcBaddyProps.Visible = True

                ' Set properties of UcBaddyProps based on the clicked Baddy item
                UcBaddyProps.ItemInfo = clickedItem

                UcBaddyProps.BringToFront() ' Bring the control to the front
            Else
                ' Hide the control if the clicked item is not a Baddy
                UcBaddyProps.Visible = False
            End If
        End If
    End Sub

    Private Sub HandleGateClicked(mouseX As Integer, mouseY As Integer)
        ' Calculate the cell coordinates based on the mouse position
        Dim clickedX As Integer = (mouseX - INDENT) \ (SQSizeX + WALL_THICKNESS)
        Dim clickedY As Integer = (mouseY - INDENT) \ (SQSizeY + WALL_THICKNESS)

        ' Check if the clicked cell contains a gate
        If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso
   clickedY >= 0 AndAlso clickedY < placedItems(0).Length Then

            Dim clickedItem As ItemInfo = placedItems(clickedX)(clickedY)
            If clickedItem IsNot Nothing AndAlso clickedItem.ImageKey = "picGate" Then
                IsOnGate = True
                ' Calculate the top-right corner of the clicked cell
                Dim cellTopRightX As Integer = INDENT + (clickedX * (SQSizeX + WALL_THICKNESS)) + SQSizeX + 7
                Dim cellTopRightY As Integer = INDENT + (clickedY * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS * 2) + 38

                ' Set the position and make the control visible
                krKeys.Location = New Point(cellTopRightX, cellTopRightY)
                krKeys.SelectedGate = clickedItem ' Set the SelectedGate property
                krKeys.Grid = picGame ' Set the Grid property
                krKeys.SetNumKeysValue(clickedItem.KeysRequiredToOpen) ' Set the NumKeys value to the gate's value
                krKeys.Visible = True
            Else
                IsOnGate = False
            End If
        End If
    End Sub

    Private Sub HandleSelectionMode(e As MouseEventArgs)
        ' Handle selecting a square in selection mode
        Dim clickedX As Integer = (e.X - INDENT) \ (SQSizeX + WALL_THICKNESS)
        Dim clickedY As Integer = (e.Y - INDENT) \ (SQSizeY + WALL_THICKNESS)

        ' Determine if the user clicked on a wall or a floor
        Dim isWall As Boolean = highlightedWallX OrElse highlightedWallY
        Dim itemType As String = If(isWall, If(highlightedWallX, "VerticalWall", "HorizontalWall"), "Floor")

        ' Determine the item at the selected point
        Dim item As String
        If isWall Then
            If highlightedWallX Then
                ' Check vertical wall items
                If clickedX >= 0 AndAlso clickedX < verticalWallItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < verticalWallItems(clickedX).Length Then
                    item = If(verticalWallItems(clickedX)(clickedY) IsNot Nothing, verticalWallItems(clickedX)(clickedY).ImageKey, "Nothing")
                End If
            ElseIf highlightedWallY Then
                ' Check horizontal wall items
                If clickedX >= 0 AndAlso clickedX < horizontalWallItems.Length AndAlso
       clickedY >= 0 AndAlso clickedY < horizontalWallItems(clickedX).Length Then
                    item = If(horizontalWallItems(clickedX)(clickedY) IsNot Nothing, horizontalWallItems(clickedX)(clickedY).ImageKey, "Nothing")
                End If
            End If
        Else
            ' Check floor items
            If clickedX >= 0 AndAlso clickedX < placedItems.Length AndAlso
   clickedY >= 0 AndAlso clickedY < placedItems(clickedX).Length Then
                item = If(placedItems(clickedX)(clickedY) IsNot Nothing, placedItems(clickedX)(clickedY).ImageKey, "Nothing")
            End If
        End If

        ' Check selection constraints
        Dim triggerEditor As frmTriggerEditor = CType(SelectionRequestedByForm, frmTriggerEditor)
        If SelectionRequestedFor = "TriggeringPoint" Then
            If triggerEditor.AllowFloorSelectionOnly AndAlso isWall Then
                Return
            End If
        ElseIf SelectionRequestedFor = "TriggerActionPoint" Then
            ' Allow clicking anything for TriggerActionPoint
        ElseIf SelectionRequestedFor = "TriggerItemToRemove" Then
            If triggerEditor.AllowWallSelectionOnly AndAlso Not isWall Then
                Return
            End If
        End If

        ' Pass the selected point, item, and item type back to the requesting form
        If TypeOf SelectionRequestedByForm Is frmTriggerEditor Then
            If SelectionRequestedFor = "TriggeringPoint" Then
                triggerEditor.SetTriggeringPoint(New Point(clickedX, clickedY))
            ElseIf SelectionRequestedFor = "TriggerActionPoint" Then
                triggerEditor.SetTriggerActionPoint(New Point(clickedX, clickedY), itemType)
            End If
        End If

        TurnOffSelectionMode()

        ' Bring FrmTriggerEditor to the foreground
        If TypeOf SelectionRequestedByForm Is frmTriggerEditor Then
            triggerEditor.BringToFront() ' Bring FrmTriggerEditor to the front
            Dim unused = triggerEditor.Focus() ' Focus on FrmTriggerEditor
        End If
    End Sub

    ' Create a new method to reset the cursor
    Private Sub ResetTheCursor()
        picGame.Cursor = Cursors.Default
        Cursor = Cursors.Default
    End Sub
    ' Call this method in TurnOffSelectionMode
    Private Sub TurnOffSelectionMode()
        isInSelectionMode = False
        selectionRectangle = Rectangle.Empty
        SelectionMode = False
        SelectionRequestedByForm = Nothing
        SelectionRequestedFor = Nothing
        ResetTheCursor()

        ' Reset the cycle
        isSelectionExpandedToWalls = False
        isSelectionExpandedToFloors = False
    End Sub

    Private Function IsDoodadItem(imageKey As String) As Boolean
        Return imageKey = "picBlockSwitch" OrElse imageKey.StartsWith("picDoodad")
    End Function
    Private Sub UpdateStatusBar(pixelX As Integer, pixelY As Integer)
        ' Calculate the square coordinates based on the mouse position
        Dim squareX As Integer = (pixelX - INDENT + ((SQSizeX + WALL_THICKNESS) \ 2)) \ (SQSizeX + WALL_THICKNESS)
        Dim squareY As Integer = (pixelY - INDENT + ((SQSizeY + WALL_THICKNESS) \ 2)) \ (SQSizeY + WALL_THICKNESS)
        Dim triggerInfo As String = ""

        ' Determine if the mouse is over a wall
        Dim isOverWall As Boolean = highlightedWallX OrElse highlightedWallY

        ' Determine the content of the square or wall
        Dim content As String = "Empty"
        Dim uniqueId As String = ""

        If isOverWall Then
            If highlightedWallX Then
                ' Check vertical wall items or walls
                If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
   highlightedY >= 0 AndAlso highlightedY < verticalWallItems(0).Length Then
                    If verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                        content = GetItemFullName(verticalWallItems(highlightedX)(highlightedY).ImageKey)
                        uniqueId = verticalWallItems(highlightedX)(highlightedY).UniqueId
                    ElseIf verticalWalls(highlightedX)(highlightedY) Then
                        content = "Wall"
                    End If
                End If
            ElseIf highlightedWallY Then
                ' Check horizontal wall items or walls
                If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
   highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(0).Length Then
                    If horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                        content = GetItemFullName(horizontalWallItems(highlightedX)(highlightedY).ImageKey)
                        uniqueId = horizontalWallItems(highlightedX)(highlightedY).UniqueId
                    ElseIf horizontalWalls(highlightedX)(highlightedY) Then
                        content = "Wall"
                    End If
                End If
            End If
        Else
            ' Check floor items
            If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
   highlightedY >= 0 AndAlso highlightedY < placedItems(0).Length Then
                If placedItems(highlightedX)(highlightedY) IsNot Nothing Then
                    content = GetItemFullName(placedItems(highlightedX)(highlightedY).ImageKey)
                    uniqueId = placedItems(highlightedX)(highlightedY).UniqueId
                ElseIf floorsUseEdgeStyle(highlightedX)(highlightedY) Then
                    content = "Edge"
                End If
            End If
        End If

        ' Update the status bar labels
        stbMouseCoords.Text = $"{pixelX}, {pixelY}"

        If isOverWall Then
            If highlightedWallX Then
                stbGridCoords.Text = $"Vertical Wall: {highlightedX}, {highlightedY}"
            ElseIf highlightedWallY Then
                stbGridCoords.Text = $"Horizontal Wall: {highlightedX}, {highlightedY}"
            End If
        Else
            stbGridCoords.Text = $"Floor: {highlightedX}, {highlightedY}"
        End If

        stbItemType.Text = content
        stbID.Text = uniqueId

        ' Look for triggers associated with this point using GRID COORDINATES
        For Each trigger As Triggers In TriggerData
            ' Check floor triggers
            If trigger.TriggeringPoint.Item1 = highlightedX AndAlso trigger.TriggeringPoint.Item2 = highlightedY Then
                triggerInfo = trigger.TriggerFull
                Exit For
            End If

            ' Check if the TriggerActionPoint is on a wall
            If trigger.TriggerActionPoint.Item1 = highlightedX AndAlso trigger.TriggerActionPoint.Item2 = highlightedY Then
                If trigger.TriggerActionIsWall Then
                    If trigger.TriggerActionIsVerticalWall AndAlso highlightedWallX Then
                        triggerInfo = trigger.TriggerFull
                    ElseIf trigger.TriggerActionIsHorizontalWall AndAlso highlightedWallY Then
                        triggerInfo = trigger.TriggerFull
                    End If
                Else
                    triggerInfo = trigger.TriggerFull
                End If
                Exit For
            End If
        Next

        ' Only update stbTrigger if not over a wall or if the trigger action point is on the correct wall type
        If Not isOverWall Then
            stbTrigger.Text = triggerInfo ' Display the trigger information in the status bar
        Else
            For Each trigger As Triggers In TriggerData
                If trigger.TriggerActionPoint.Item1 = highlightedX AndAlso trigger.TriggerActionPoint.Item2 = highlightedY Then
                    If trigger.TriggerActionIsWall AndAlso
           ((trigger.TriggerActionIsVerticalWall AndAlso highlightedWallX) OrElse
            (trigger.TriggerActionIsHorizontalWall AndAlso highlightedWallY)) Then
                        stbTrigger.Text = triggerInfo ' Display the trigger information in the status bar
                        Exit For
                    End If
                End If
            Next
            If stbTrigger.Text = "" Then
                stbTrigger.Text = ""
            End If
        End If

        ' Apply formatting to the RichTextBox
        ApplyFormatting(stbTrigger, triggerInfo)
    End Sub
    Private Function GetItemTypeAtPosition(x As Integer, y As Integer) As String
        ' Determine if the selected item is a wall, wall item, or floor item
        If highlightedWallX Then
            Return If(verticalWallItems(x)(y) IsNot Nothing, "VerticalWallItem", "VerticalWall")
        ElseIf highlightedWallY Then
            Return If(horizontalWallItems(x)(y) IsNot Nothing, "HorizontalWallItem", "HorizontalWall")
        Else
            Return If(placedItems(x)(y) IsNot Nothing, "FloorItem", "Floor")
        End If
    End Function

    Private Sub HideUcBaddyProps()
        UcBaddyProps.Visible = False
    End Sub

    Public Sub PictureBox_MouseEnter(sender As Object, e As EventArgs)
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
        pb.BackColor = Color.LightBlue
    End Sub

    Public Sub PictureBox_MouseLeave(sender As Object, e As EventArgs)
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
        pb.BackColor = Color.Transparent
    End Sub

    Private Sub AddWallAtMousePosition(mouseX As Integer, mouseY As Integer)
        UpdateHighlight(mouseX, mouseY)

        If highlightedWallX Then
            If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length AndAlso
   highlightedY >= 0 AndAlso highlightedY < verticalWalls(0).Length Then
                verticalWalls(highlightedX)(highlightedY) = True

            End If
        ElseIf highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso
   highlightedY >= 0 AndAlso highlightedY < horizontalWalls(0).Length Then
                horizontalWalls(highlightedX)(highlightedY) = True

            End If
        End If
    End Sub

    Private Sub RemoveObjectAtMousePosition(mouseX As Integer, mouseY As Integer, removeWalls As Boolean, removeItems As Boolean)
        UpdateHighlight(mouseX, mouseY)

        If Not isMouseInsideGrid Then
            Return
        End If

        If highlightedWallX Then
            If highlightedX >= 0 AndAlso highlightedX < verticalWallItems.Length AndAlso
   verticalWallItems(highlightedX) IsNot Nothing AndAlso
   highlightedY >= 0 AndAlso highlightedY < verticalWallItems(0).Length Then

                If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                    verticalWallItems(highlightedX)(highlightedY) = Nothing
                    verticalWalls(highlightedX)(highlightedY) = True

                    If IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        verticalWalls(highlightedX)(highlightedY) = True
                    End If
                ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) Then
                    If Not IsVerticalWallOuterBorder(highlightedX) AndAlso Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        verticalWalls(highlightedX)(highlightedY) = False

                    End If
                End If
            End If
        ElseIf highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < horizontalWallItems.Length AndAlso
   horizontalWallItems(highlightedX) IsNot Nothing AndAlso
   highlightedY >= 0 AndAlso highlightedY < horizontalWallItems(0).Length Then

                If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
                    horizontalWallItems(highlightedX)(highlightedY) = Nothing
                    horizontalWalls(highlightedX)(highlightedY) = True

                    If IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        horizontalWalls(highlightedX)(highlightedY) = True
                    End If
                ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) Then
                    If Not IsHorizontalWallOuterBorder(highlightedY) AndAlso Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
                        horizontalWalls(highlightedX)(highlightedY) = False

                    End If
                End If
            End If
        Else
            If highlightedX >= 0 AndAlso highlightedX < placedItems.Length AndAlso
   placedItems(highlightedX) IsNot Nothing AndAlso
   highlightedY >= 0 AndAlso highlightedY < placedItems(0).Length Then

                If removeItems AndAlso placedItems(highlightedX)(highlightedY) IsNot Nothing Then
                    placedItems(highlightedX)(highlightedY) = Nothing

                End If
            End If
        End If
    End Sub

    Private Sub SetEdgeStyleAtMousePosition(mouseX As Integer, mouseY As Integer)
        UpdateHighlight(mouseX, mouseY)
        If Not highlightedWallX AndAlso Not highlightedWallY Then
            If highlightedX >= 0 AndAlso highlightedX < SQCOUNTX AndAlso highlightedY >= 0 AndAlso highlightedY < SQCOUNTY Then
                floorsUseEdgeStyle(highlightedX)(highlightedY) = True

                If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length AndAlso
           verticalWalls(highlightedX) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < verticalWalls(0).Length Then
                    verticalWallItems(highlightedX)(highlightedY) = Nothing
                    verticalWalls(highlightedX)(highlightedY) = True
                End If

                If highlightedX + 1 >= 0 AndAlso highlightedX + 1 < verticalWalls.Length AndAlso
           verticalWalls(highlightedX + 1) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < verticalWalls(0).Length Then
                    verticalWallItems(highlightedX + 1)(highlightedY) = Nothing
                    verticalWalls(highlightedX + 1)(highlightedY) = True
                End If

                If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso
           horizontalWalls(highlightedX) IsNot Nothing AndAlso
           highlightedY >= 0 AndAlso highlightedY < horizontalWalls(0).Length Then
                    horizontalWallItems(highlightedX)(highlightedY) = Nothing
                    horizontalWalls(highlightedX)(highlightedY) = True
                End If

                If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso
           horizontalWalls(highlightedX) IsNot Nothing AndAlso
           highlightedY + 1 >= 0 AndAlso highlightedY + 1 < horizontalWalls(0).Length Then
                    horizontalWallItems(highlightedX)(highlightedY + 1) = Nothing
                    horizontalWalls(highlightedX)(highlightedY + 1) = True
                End If

            End If
        End If
    End Sub
    Private Function IsHorizontalWallConnectedToEdge(pt As Point) As Boolean
        Dim x As Integer = pt.X
        Dim y As Integer = pt.Y

        If y > 0 AndAlso floorsUseEdgeStyle(x)(y - 1) Then Return True
        If y < SQCOUNTY AndAlso floorsUseEdgeStyle(x)(y) Then Return True

        Return False
    End Function
    Private Function IsVerticalWallConnectedToEdge(pt As Point) As Boolean
        Dim x As Integer = pt.X
        Dim y As Integer = pt.Y

        If x > 0 AndAlso floorsUseEdgeStyle(x - 1)(y) Then Return True
        If x < SQCOUNTX AndAlso floorsUseEdgeStyle(x)(y) Then Return True

        Return False
    End Function
    Private Function GetRectangle(p1 As Point, p2 As Point) As Rectangle
        Dim x As Integer = Math.Min(p1.X, p2.X)
        Dim y As Integer = Math.Min(p1.Y, p2.Y)
        Dim width As Integer = Math.Abs(p1.X - p2.X)
        Dim height As Integer = Math.Abs(p1.Y - p2.Y)
        Return New Rectangle(x, y, width, height)
    End Function

    Private Function GetCellsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedCells As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY - 1
                Dim cellRect As Rectangle = GetCellRectangle(x, y)
                If rect.IntersectsWith(cellRect) Then
                    selectedCells.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedCells
    End Function

    Private Function GetCellRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + (x * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
        yOrigin + (y * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
        SQSizeX,
        SQSizeY)
    End Function

    Private Function GetVerticalWallsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedWalls As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX
            For y As Integer = 0 To SQCOUNTY - 1
                Dim wallRect As Rectangle = GetVerticalWallRectangle(x, y)
                If rect.IntersectsWith(wallRect) Then
                    selectedWalls.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedWalls
    End Function

    Private Function GetVerticalWallRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + (x * (SQSizeX + WALL_THICKNESS)) - (WALL_THICKNESS \ 2),
        yOrigin + (y * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
        WALL_THICKNESS, SQSizeY)
    End Function

    Private Function GetHorizontalWallsInRectangle(rect As Rectangle) As List(Of Point)
        Dim selectedWalls As New List(Of Point)
        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY
                Dim wallRect As Rectangle = GetHorizontalWallRectangle(x, y)
                If rect.IntersectsWith(wallRect) Then
                    selectedWalls.Add(New Point(x, y))
                End If
            Next
        Next
        Return selectedWalls
    End Function

    Private Function GetHorizontalWallRectangle(x As Integer, y As Integer) As Rectangle
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT
        Return New Rectangle(
        xOrigin + (x * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
        yOrigin + (y * (SQSizeY + WALL_THICKNESS)) - (WALL_THICKNESS \ 2),
        SQSizeX, WALL_THICKNESS)
    End Function

    Private Sub ReturnItemToOriginalLocation()
        If draggedItemIsWall Then
            If draggedItemOriginalX >= 0 AndAlso draggedItemOriginalY >= 0 Then
                If draggedItemIsVerticalWall Then
                    verticalWallItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
                    verticalWalls(draggedItemOriginalX)(draggedItemOriginalY) = False
                Else
                    horizontalWallItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
                    horizontalWalls(draggedItemOriginalX)(draggedItemOriginalY) = False
                End If
            End If
        Else
            If draggedItemOriginalX >= 0 AndAlso draggedItemOriginalY >= 0 Then
                placedItems(draggedItemOriginalX)(draggedItemOriginalY) = draggedItem
            End If
        End If
    End Sub
    Private Function IsVerticalItem(imgKey As String) As Boolean
        Return imgKey = "picLaser1" OrElse imgKey = "picFlipper1" OrElse imgKey = "picFlipper2"
    End Function

    Private Function IsHorizontalItem(imgKey As String) As Boolean
        Return imgKey = "picLaser2" OrElse imgKey = "picFlipper3" OrElse imgKey = "picFlipper4"
    End Function
    Private Function IsFlipperItem(item As ItemInfo) As Boolean
        If item Is Nothing Then Return False

        Dim imgKey = item.ImageKey
        Return imgKey = "picFlipper1" OrElse imgKey = "picFlipper2" _
        OrElse imgKey = "picFlipper3" OrElse imgKey = "picFlipper4"
    End Function

    Private Function IsFlipperItem(imgKey As String) As Boolean
        Return imgKey = "picFlipper1" OrElse imgKey = "picFlipper2" _
        OrElse imgKey = "picFlipper3" OrElse imgKey = "picFlipper4"
    End Function

#End Region
Partial Public Class FrmDesigner

    ' Variables related to selection and ctmMenu
    Private isCursorOnPicGame As Boolean = False

    Private selectionStartedOnWall As Boolean
    Private SelectionType As String

    Private isSelectionReadyToDrag As Boolean = False
    Private isDraggingSelection As Boolean = False
    Private selectionOffset As Point ' The offset between mouse position and selection rectangle origin
    Private selectionData As SelectionData
    Private selectionStartCell As Point ' The grid cell coordinates corresponding to selectionRectangle's original position

    Private isRightMouseDown As Boolean = False
    Private isLeftMouseDown As Boolean = False
    Private isMouseInsideGrid As Boolean = False

    Private isSelecting As Boolean = False
    Private isInSelectionMode As Boolean = False
    Private selectionStartPoint As Point
    Private selectionEndPoint As Point
    Private selectionRectangle As Rectangle

    Private copiedSelectionData As SelectionData
    Private copiedSelectionType As String
    Private isCopying As Boolean = False

    Private isSelectionExpandedToWalls As Boolean = False
    Private isSelectionExpandedToFloors As Boolean = False

#Region " Select "

    ' Methods related to ctmMenu
    Private Sub BtnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
        If isInSelectionMode Then
            ' Exiting selection mode
            isInSelectionMode = False
            isSelecting = False
            isSelectionReadyToDrag = False
            selectionRectangle = Rectangle.Empty
            picGame.Cursor = Cursors.Default
            picGame.Invalidate()
        Else
            ' Entering selection mode
            isInSelectionMode = True
            isSelecting = False
            isSelectionReadyToDrag = False
            selectionRectangle = Rectangle.Empty
            picGame.Cursor = Cursors.Cross
        End If
    End Sub

    Private Sub SelectAllItemsOfType(ParamArray itemKeys As String())
        Dim selectedPoints As New List(Of Point)

        For x As Integer = 0 To SQCOUNTX - 1
            For y As Integer = 0 To SQCOUNTY - 1
                Dim localX As Integer = x
                Dim localY As Integer = y
                If placedItems(localX)(localY) IsNot Nothing AndAlso Array.Exists(itemKeys, Function(key) placedItems(localX)(localY).ImageKey = key) Then
                    selectedPoints.Add(New Point(localX, localY))
                End If
            Next
        Next

        If selectedPoints.Count > 0 Then
            Dim minX = selectedPoints.Min(Function(p) p.X)
            Dim minY = selectedPoints.Min(Function(p) p.Y)
            Dim maxX = selectedPoints.Max(Function(p) p.X)
            Dim maxY = selectedPoints.Max(Function(p) p.Y)

            selectionRectangle = New Rectangle(
                INDENT + (minX * (SQSizeX + WALL_THICKNESS)),
                INDENT + (minY * (SQSizeY + WALL_THICKNESS)),
                ((maxX - minX + 1) * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS,
                ((maxY - minY + 1) * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS)

            isInSelectionMode = True
            Cursor = Cursors.Cross
            picGame.Invalidate()
        End If
    End Sub

#End Region

#Region " ctmMenu Opened, ctmMenu Closed "

    Private Sub ctmMenu_Opened(sender As Object, e As EventArgs) Handles ctmMenu.Opened
        AddHandler ctmMenu.Closed, AddressOf ctmMenu_Closed
        ' Check the selection area
        If selectionRectangle.IsEmpty Then
            ' Disable the Clear, Copy, Move menus if there's no selection
            mnuClear.Enabled = False
            mnuCopy.Enabled = False
            Return
        Else
            mnuClear.Enabled = True
            mnuCopy.Enabled = True
        End If

        ' Capture the selection data if not already captured
        If selectionData Is Nothing Then
            CaptureSelectionData()
        End If

        ' Initialize booleans for item categories
        Dim hasEdge As Boolean = False
        Dim hasBaddies As Boolean = False
        Dim hasBlockStoppers As Boolean = False
        Dim hasBlocks As Boolean = False
        Dim hasCoins As Boolean = False
        Dim hasKeys As Boolean = False
        Dim hasWarps As Boolean = False
        Dim hasFlippers As Boolean = False
        Dim hasLasers As Boolean = False
        Dim hasWalls As Boolean = False

        ' Loop through the selection data
        For x As Integer = 0 To selectionData.selectionWidth - 1
            For y As Integer = 0 To selectionData.selectionHeight - 1
                ' Check Edge
                If selectionData.floorsUseEdgeStyle(x)(y) Then
                    hasEdge = True
                End If

                ' Check placedItems
                Dim placedItem As ItemInfo = selectionData.placedItems(x)(y)
                If placedItem IsNot Nothing Then
                    Dim imageKey As String = placedItem.ImageKey
                    If IsBaddieItem(imageKey) Then
                        hasBaddies = True
                    ElseIf IsBlockStopperItem(imageKey) Then
                        hasBlockStoppers = True
                    ElseIf IsBlockItem(imageKey) Then
                        hasBlocks = True
                    ElseIf IsCoinItem(imageKey) Then
                        hasCoins = True
                    ElseIf IsKeyItem(imageKey) Then
                        hasKeys = True
                    ElseIf IsWarpItem(imageKey) Then
                        hasWarps = True
                    ElseIf isFlipperItems(imageKey) Then
                        hasFlippers = True
                    ElseIf IsLaserItem(imageKey) Then
                        hasLasers = True
                    End If
                End If
            Next
        Next

        ' Check vertical walls and wall items
        If selectionData.verticalWalls IsNot Nothing Then
            For x As Integer = 0 To selectionData.verticalWalls.Length - 1
                For y As Integer = 0 To selectionData.verticalWalls(x).Length - 1
                    If selectionData.verticalWalls(x)(y) Then
                        hasWalls = True
                    End If
                    Dim wallItem As ItemInfo = selectionData.verticalWallItems(x)(y)
                    If wallItem IsNot Nothing Then
                        Dim imageKey As String = wallItem.ImageKey
                        If isFlipperItems(imageKey) Then
                            hasFlippers = True
                        ElseIf IsLaserItem(imageKey) Then
                            hasLasers = True
                        End If
                    End If
                Next
            Next
        End If

        ' Check horizontal walls and wall items
        If selectionData.horizontalWalls IsNot Nothing Then
            For x As Integer = 0 To selectionData.horizontalWalls.Length - 1
                For y As Integer = 0 To selectionData.horizontalWalls(x).Length - 1
                    If selectionData.horizontalWalls(x)(y) Then
                        hasWalls = True
                    End If
                    Dim wallItem As ItemInfo = selectionData.horizontalWallItems(x)(y)
                    If wallItem IsNot Nothing Then
                        Dim imageKey As String = wallItem.ImageKey
                        If isFlipperItems(imageKey) Then
                            hasFlippers = True
                        ElseIf IsLaserItem(imageKey) Then
                            hasLasers = True
                        End If
                    End If
                Next
            Next
        End If

        ' Set enabled state of menu items in Clear menu
        mnuClearEdge.Enabled = hasEdge
        mnuClearBaddies.Enabled = hasBaddies
        mnuClearBlockStoppers.Enabled = hasBlockStoppers
        mnuClearBlocks.Enabled = hasBlocks
        mnuClearCoins.Enabled = hasCoins
        mnuClearKeys.Enabled = hasKeys
        mnuClearWarps.Enabled = hasWarps
        mnuClearFlippers.Enabled = hasFlippers
        mnuClearLasers.Enabled = hasLasers
        mnuClearWalls.Enabled = hasWalls
        mnuClearAll.Enabled = True ' "Clear All" is always enabled
    End Sub

    Private Sub ctmMenu_Closed(sender As Object, e As ToolStripDropDownClosedEventArgs)
        If isInSelectionMode Then
            Dim menuPosition = picGame.PointToClient(MousePosition)
            ctmMenu.Show(picGame, menuPosition)
        End If
    End Sub

#End Region
#Region " Copy "
    Private Sub mnuCopy_Click(sender As Object, e As EventArgs) Handles mnuCopy.Click

        If Not isRestoringState Then SaveStateForUndo()
        If selectionRectangle.IsEmpty Then
            Return
        End If

        CaptureSelectionData()
        copiedSelectionData = CloneSelectionData(selectionData)
        isCopying = True

        ' Extract the selection type from the sender's name
        Dim menuItem = CType(sender, ToolStripMenuItem)
        copiedSelectionType = menuItem.Name.Substring(7) ' Remove the "mnuCopy" prefix

        ' Filter the selection data based on the selection type
        Select Case copiedSelectionType
            Case "All"
        ' Do nothing, keep all items
            Case "Baddies"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Baddies"))
            Case "BlockStoppers"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("BlockStoppers"))
            Case "Blocks"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Blocks"))
            Case "Coins"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Coins"))
            Case "Keys"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Keys"))
            Case "Warps"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Warps"))
            Case "Flippers"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Flippers"))
            Case "Lasers"
                FilterSelectionDataForItemTypes(selectionData, GetItemKeysForType("Lasers"))
            Case "Edge"
        ' Handle edge separately if needed
            Case "Walls"
                ' Handle walls separately if needed
            Case Else
                ' Handle other cases
        End Select

        ' Proceed with copying the filtered selection data
        copiedSelectionData = CloneSelectionData(selectionData)

        ' Get cursor position in picGame coordinates
        Dim cursorPosInPicGame = picGame.PointToClient(Cursor.Position)

        ' Adjust selection rectangle to center on the cursor
        selectionRectangle.Location = New Point(
    cursorPosInPicGame.X - (selectionRectangle.Width \ 2),
    cursorPosInPicGame.Y - (selectionRectangle.Height \ 2)
)

        ' Recalculate selectionOffset based on new rectangle position
        selectionOffset = New Point(
    Cursor.Position.X - picGame.PointToScreen(selectionRectangle.Location).X,
    Cursor.Position.Y - picGame.PointToScreen(selectionRectangle.Location).Y
)

        ' Automatically start dragging the selection
        isSelectionReadyToDrag = True
        isDraggingSelection = True
        picGame.Capture = True
        Cursor = Cursors.SizeAll
        picGame.Invalidate()

        ' Handle pasting logic
        AddHandler picGame.MouseUp, Sub(mouseSender As Object, mouseEventArgs As MouseEventArgs)
                                        ' Paste data
                                        PasteSelectionDataTo(New Point(cursorPosInPicGame.X, cursorPosInPicGame.Y), False, copiedSelectionType = "Edge")
                                        isSelectionReadyToDrag = False
                                        isDraggingSelection = False
                                        picGame.Capture = False
                                        Cursor = Cursors.Default
                                        picGame.Invalidate()
                                    End Sub
    End Sub
    Private Sub CaptureSelectionData()
        If selectionRectangle.IsEmpty Then
            Return
        End If

        ' Get start and end cell indices from selection rectangle
        Dim startCellX As Integer = (selectionRectangle.X - INDENT + (WALL_THICKNESS \ 2)) \ (SQSizeX + WALL_THICKNESS)
        Dim startCellY As Integer = (selectionRectangle.Y - INDENT + (WALL_THICKNESS \ 2)) \ (SQSizeY + WALL_THICKNESS)
        Dim endCellX As Integer = (selectionRectangle.Right - INDENT - (WALL_THICKNESS \ 2) - 1) \ (SQSizeX + WALL_THICKNESS)
        Dim endCellY As Integer = (selectionRectangle.Bottom - INDENT - (WALL_THICKNESS \ 2) - 1) \ (SQSizeY + WALL_THICKNESS)

        ' Allow negative indices to handle selections starting outside the grid
        Dim selectionCellWidth As Integer = endCellX - startCellX + 1
        Dim selectionCellHeight As Integer = endCellY - startCellY + 1

        ' Adjust startCellX and startCellY if negative
        Dim adjustedStartCellX As Integer = Math.Max(0, startCellX)
        Dim adjustedStartCellY As Integer = Math.Max(0, startCellY)

        ' Adjust selection dimensions if start cells were negative
        selectionCellWidth -= adjustedStartCellX - startCellX
        selectionCellHeight -= adjustedStartCellY - startCellY

        ' Adjust end cells if they exceed grid boundaries
        adjustedStartCellX = Math.Min(adjustedStartCellX, SQCOUNTX - 1)
        adjustedStartCellY = Math.Min(adjustedStartCellY, SQCOUNTY - 1)
        selectionCellWidth = Math.Min(selectionCellWidth, SQCOUNTX - adjustedStartCellX)
        selectionCellHeight = Math.Min(selectionCellHeight, SQCOUNTY - adjustedStartCellY)

        ' Initialize selectionData
        selectionData = New SelectionData With {
        .selectionWidth = selectionCellWidth,
        .selectionHeight = selectionCellHeight
    }

        ' Capture placed items and floor edge styles
        selectionData.placedItems = New ItemInfo(selectionData.selectionWidth - 1)() {}
        selectionData.floorsUseEdgeStyle = New Boolean(selectionData.selectionWidth - 1)() {}
        For x As Integer = 0 To selectionData.selectionWidth - 1
            selectionData.placedItems(x) = New ItemInfo(selectionData.selectionHeight - 1) {}
            selectionData.floorsUseEdgeStyle(x) = New Boolean(selectionData.selectionHeight - 1) {}
            For y As Integer = 0 To selectionData.selectionHeight - 1
                Dim srcX As Integer = adjustedStartCellX + x
                Dim srcY As Integer = adjustedStartCellY + y
                If srcX >= 0 AndAlso srcX < SQCOUNTX AndAlso srcY >= 0 AndAlso srcY < SQCOUNTY Then
                    selectionData.placedItems(x)(y) = placedItems(srcX)(srcY)
                    selectionData.floorsUseEdgeStyle(x)(y) = floorsUseEdgeStyle(srcX)(srcY)
                Else
                    selectionData.placedItems(x)(y) = Nothing
                    selectionData.floorsUseEdgeStyle(x)(y) = False
                End If
            Next
        Next

        ' Capture internal vertical walls (excluding boundary walls)
        If selectionData.selectionWidth > 1 Then
            Dim numVertWalls As Integer = selectionData.selectionWidth - 2
            selectionData.verticalWalls = New Boolean(numVertWalls)() {}
            selectionData.verticalWallItems = New ItemInfo(numVertWalls)() {}
            For x As Integer = 0 To numVertWalls
                selectionData.verticalWalls(x) = New Boolean(selectionData.selectionHeight - 1) {}
                selectionData.verticalWallItems(x) = New ItemInfo(selectionData.selectionHeight - 1) {}
                For y As Integer = 0 To selectionData.selectionHeight - 1
                    Dim wallX As Integer = adjustedStartCellX + x + 1
                    Dim wallY As Integer = adjustedStartCellY + y
                    If wallX >= 0 AndAlso wallX < verticalWalls.Length AndAlso wallY >= 0 AndAlso wallY < verticalWalls(0).Length Then
                        selectionData.verticalWalls(x)(y) = verticalWalls(wallX)(wallY)
                        selectionData.verticalWallItems(x)(y) = verticalWallItems(wallX)(wallY)
                    Else
                        selectionData.verticalWalls(x)(y) = False
                        selectionData.verticalWallItems(x)(y) = Nothing
                    End If
                Next
            Next
        Else
            ' No internal vertical walls to capture
            selectionData.verticalWalls = Nothing
            selectionData.verticalWallItems = Nothing
        End If

        ' Capture internal horizontal walls (excluding boundary walls)
        If selectionData.selectionHeight > 1 Then
            Dim numHorizWalls As Integer = selectionData.selectionHeight - 2
            selectionData.horizontalWalls = New Boolean(selectionData.selectionWidth - 1)() {}
            selectionData.horizontalWallItems = New ItemInfo(selectionData.selectionWidth - 1)() {}
            For x As Integer = 0 To selectionData.selectionWidth - 1
                selectionData.horizontalWalls(x) = New Boolean(numHorizWalls) {}
                selectionData.horizontalWallItems(x) = New ItemInfo(numHorizWalls) {}
                For y As Integer = 0 To numHorizWalls
                    Dim wallX As Integer = adjustedStartCellX + x
                    Dim wallY As Integer = adjustedStartCellY + y + 1
                    If wallX >= 0 AndAlso wallX < horizontalWalls.Length AndAlso wallY >= 0 AndAlso wallY < horizontalWalls(0).Length Then
                        selectionData.horizontalWalls(x)(y) = horizontalWalls(wallX)(wallY)
                        selectionData.horizontalWallItems(x)(y) = horizontalWallItems(wallX)(wallY)
                    Else
                        selectionData.horizontalWalls(x)(y) = False
                        selectionData.horizontalWallItems(x)(y) = Nothing
                    End If
                Next
            Next
        Else
            ' No internal horizontal walls to capture
            selectionData.horizontalWalls = Nothing
            selectionData.horizontalWallItems = Nothing
        End If

        ' Store the selection start cell
        selectionStartCell = New Point(adjustedStartCellX, adjustedStartCellY)
    End Sub

    Private Sub PasteSelectionDataTo(newCell As Point, removeOldData As Boolean, copyEdge As Boolean)
        If copiedSelectionData Is Nothing Then
            Return
        End If

        ' Ensure the new position is within bounds
        newCell.X = Math.Max(0, Math.Min(SQCOUNTX - copiedSelectionData.selectionWidth, newCell.X))
        newCell.Y = Math.Max(0, Math.Min(SQCOUNTY - copiedSelectionData.selectionHeight, newCell.Y))

        ' Paste placed items and floor edge styles
        For x As Integer = 0 To copiedSelectionData.selectionWidth - 1
            For y As Integer = 0 To copiedSelectionData.selectionHeight - 1
                If newCell.X + x < SQCOUNTX AndAlso newCell.Y + y < SQCOUNTY Then
                    If copiedSelectionData.placedItems(x)(y) IsNot Nothing Then
                        placedItems(newCell.X + x)(newCell.Y + y) = copiedSelectionData.placedItems(x)(y)
                    End If
                    If copyEdge AndAlso copiedSelectionData.floorsUseEdgeStyle(x)(y) Then
                        floorsUseEdgeStyle(newCell.X + x)(newCell.Y + y) = True
                    End If
                End If
            Next
        Next

        ' Paste internal vertical walls, if any
        If copiedSelectionData.verticalWalls IsNot Nothing AndAlso Not removeOldData Then
            For x As Integer = 0 To copiedSelectionData.verticalWalls.Length - 1
                For y As Integer = 0 To copiedSelectionData.verticalWalls(x).Length - 1
                    Dim targetX As Integer = newCell.X + x + 1
                    Dim targetY As Integer = newCell.Y + y

                    If targetX >= 0 AndAlso targetX < verticalWalls.Length AndAlso targetY >= 0 AndAlso targetY < verticalWalls(0).Length Then
                        If copiedSelectionData.verticalWalls(x)(y) Then
                            verticalWalls(targetX)(targetY) = True
                        End If
                        If copiedSelectionData.verticalWallItems(x)(y) IsNot Nothing Then
                            verticalWallItems(targetX)(targetY) = copiedSelectionData.verticalWallItems(x)(y)
                        End If
                    End If
                Next
            Next
        End If

        ' Paste internal horizontal walls, if any
        If copiedSelectionData.horizontalWalls IsNot Nothing AndAlso Not removeOldData Then
            For x As Integer = 0 To copiedSelectionData.horizontalWalls.Length - 1
                For y As Integer = 0 To copiedSelectionData.horizontalWalls(x).Length - 1
                    Dim targetX As Integer = newCell.X + x
                    Dim targetY As Integer = newCell.Y + y + 1

                    If targetX >= 0 AndAlso targetX < horizontalWalls.Length AndAlso targetY >= 0 AndAlso targetY < horizontalWalls(0).Length Then
                        If copiedSelectionData.horizontalWalls(x)(y) Then
                            horizontalWalls(targetX)(targetY) = True
                        End If
                        If copiedSelectionData.horizontalWallItems(x)(y) IsNot Nothing Then
                            horizontalWallItems(targetX)(targetY) = copiedSelectionData.horizontalWallItems(x)(y)
                        End If
                    End If
                Next
            Next
        End If

        ' Update the selection rectangle to the new position
        selectionRectangle = New Rectangle(
INDENT + (newCell.X * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
INDENT + (newCell.Y * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2),
(copiedSelectionData.selectionWidth * (SQSizeX + WALL_THICKNESS)) - WALL_THICKNESS,
(copiedSelectionData.selectionHeight * (SQSizeY + WALL_THICKNESS)) - WALL_THICKNESS
)

        ' Remove old data if moving
        If removeOldData Then
            ClearSelectedArea()
        End If

        picGame.Invalidate()
    End Sub

    Private Function CloneSelectionData(original As SelectionData) As SelectionData
        Dim clone As New SelectionData With {
            .selectionWidth = original.selectionWidth,
            .selectionHeight = original.selectionHeight
        }

        ' Clone placedItems
        If original.placedItems IsNot Nothing Then
            clone.placedItems = original.placedItems.Select(Function(row) row.ToArray()).ToArray()
        End If

        ' Clone floorsUseEdgeStyle
        If original.floorsUseEdgeStyle IsNot Nothing Then
            clone.floorsUseEdgeStyle = original.floorsUseEdgeStyle.Select(Function(row) row.ToArray()).ToArray()
        End If

        ' Clone verticalWalls and verticalWallItems
        If original.verticalWalls IsNot Nothing Then
            clone.verticalWalls = original.verticalWalls.Select(Function(row) CType(row.Clone(), Boolean())).ToArray()
        End If

        If original.verticalWallItems IsNot Nothing Then
            clone.verticalWallItems = original.verticalWallItems.Select(Function(row) row.ToArray()).ToArray()
        End If

        ' Clone horizontalWalls and horizontalWallItems
        If original.horizontalWalls IsNot Nothing Then
            clone.horizontalWalls = original.horizontalWalls.Select(Function(row) CType(row.Clone(), Boolean())).ToArray()
        End If

        If original.horizontalWallItems IsNot Nothing Then
            clone.horizontalWallItems = original.horizontalWallItems.Select(Function(row) row.ToArray()).ToArray()
        End If

        Return clone
    End Function

    Private Sub DrawSelectionData(g As Graphics, baseCellX As Integer, baseCellY As Integer, data As SelectionData)
        Dim xOrigin As Integer = INDENT
        Dim yOrigin As Integer = INDENT

        Dim colorMatrix As New Imaging.ColorMatrix With {
            .Matrix33 = 0.5F  ' Set opacity to 50%
            }
        Dim imgAttributes As New Imaging.ImageAttributes()
        imgAttributes.SetColorMatrix(colorMatrix, Imaging.ColorMatrixFlag.Default, Imaging.ColorAdjustType.Bitmap)

        For x As Integer = 0 To data.selectionWidth - 1
            For y As Integer = 0 To data.selectionHeight - 1
                If data.placedItems(x)(y) IsNot Nothing Then
                    Dim img As Image = GetImageByKey(data.placedItems(x)(y).ImageKey)
                    If img IsNot Nothing Then
                        ImageAnimator.UpdateFrames(img)
                        Dim xPos As Integer = xOrigin + ((baseCellX + x) * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2)
                        Dim yPos As Integer = yOrigin + ((baseCellY + y) * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2)
                        Dim destRect As New Rectangle(xPos, yPos, SQSizeX, SQSizeY)
                        g.DrawImage(img, destRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
                    End If
                End If
            Next
        Next

        ' Draw vertical wall items
        If data.verticalWallItems IsNot Nothing Then
            For x As Integer = 0 To data.verticalWallItems.Length - 1
                For y As Integer = 0 To data.verticalWallItems(x).Length - 1
                    If data.verticalWallItems(x)(y) IsNot Nothing Then
                        Dim img As Image = GetImageByKey(data.verticalWallItems(x)(y).ImageKey)
                        If img IsNot Nothing Then
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = xOrigin + ((baseCellX + x + 1) * (SQSizeX + WALL_THICKNESS)) - (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = yOrigin + ((baseCellY + y) * (SQSizeY + WALL_THICKNESS)) + (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = WALL_THICKNESS
                            Dim wallHeight As Integer = SQSizeY

                            Dim newWidth As Integer = img.Width
                            Dim newHeight As Integer = wallHeight

                            Dim centeredX As Integer = wallX + ((wallWidth - newWidth) \ 2)
                            Dim drawRect As New Rectangle(centeredX, wallY, newWidth, newHeight)
                            g.DrawImage(img, drawRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
                        End If
                    End If
                Next
            Next
        End If

        ' Draw horizontal wall items
        If data.horizontalWallItems IsNot Nothing Then
            For x As Integer = 0 To data.horizontalWallItems.Length - 1
                For y As Integer = 0 To data.horizontalWallItems(x).Length - 1
                    If data.horizontalWallItems(x)(y) IsNot Nothing Then
                        Dim img As Image = GetImageByKey(data.horizontalWallItems(x)(y).ImageKey)
                        If img IsNot Nothing Then
                            ImageAnimator.UpdateFrames(img)
                            Dim wallX As Integer = xOrigin + ((baseCellX + x) * (SQSizeX + WALL_THICKNESS)) + (WALL_THICKNESS \ 2)
                            Dim wallY As Integer = yOrigin + ((baseCellY + y + 1) * (SQSizeY + WALL_THICKNESS)) - (WALL_THICKNESS \ 2)
                            Dim wallWidth As Integer = SQSizeX
                            Dim wallHeight As Integer = WALL_THICKNESS

                            Dim newWidth As Integer = wallWidth
                            Dim newHeight As Integer = img.Height

                            Dim centeredY As Integer = wallY + ((wallHeight - newHeight) \ 2)
                            Dim drawRect As New Rectangle(wallX, centeredY, newWidth, newHeight)
                            g.DrawImage(img, drawRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
                        End If
                    End If
                Next
            Next
        End If
    End Sub

#End Region

    Private Function IsBaddieItem(imageKey As String) As Boolean
        Return imageKey = "picBaddy1" OrElse imageKey = "picBaddy2" OrElse imageKey = "picBaddy3" OrElse imageKey = "picBaddy4"
    End Function

    Private Function IsBlockStopperItem(imageKey As String) As Boolean
        Return imageKey = "picBlockStopper1"
    End Function

    Private Function IsBlockItem(imageKey As String) As Boolean
        Return imageKey = "picBlock1"
    End Function

    Private Function IsCoinItem(imageKey As String) As Boolean
        Return imageKey = "picCoin"
    End Function

    Private Function IsKeyItem(imageKey As String) As Boolean
        Return imageKey = "picKey"
    End Function

    Private Function IsWarpItem(imageKey As String) As Boolean
        Return imageKey = "picWarp" OrElse imageKey = "picWarp2"
    End Function

    Private Function isFlipperItems(imageKey As String) As Boolean
        Return imageKey = "picFlipper1" OrElse imageKey = "picFlipper2" OrElse imageKey = "picFlipper3" OrElse imageKey = "picFlipper4"
    End Function

    Private Function IsLaserItem(imageKey As String) As Boolean
        Return imageKey = "picLaser1" OrElse imageKey = "picLaser2"
    End Function

    Private Sub mnuCancel_Click(sender As Object, e As EventArgs) Handles mnuCancel.Click
        TurnOffSelectionMode()
    End Sub

    Private Function GetItemKeysForType(type As String) As String()
        Select Case type
            Case "Baddies"
                Return New String() {"picBaddy1", "picBaddy2", "picBaddy3", "picBaddy4"}
            Case "BlockStoppers"
                Return New String() {"picBlockStopper1"}
            Case "Blocks"
                Return New String() {"picBlock1"}
            Case "Coins"
                Return New String() {"picCoin"}
            Case "Keys"
                Return New String() {"picKey"}
            Case "Warps"
                Return New String() {"picWarp", "picWarp2"}
            Case "Flippers"
                Return New String() {"picFlipper1", "picFlipper2", "picFlipper3", "picFlipper4"}
            Case "Lasers"
                Return New String() {"picLaser1", "picLaser2"}
            Case Else
                Return New String() {}
        End Select
    End Function

    Private Sub FilterSelectionDataForItemTypes(selectionData As SelectionData, allowedItemKeys As String())
        ' Filter placedItems
        For x As Integer = 0 To selectionData.selectionWidth - 1
            For y As Integer = 0 To selectionData.selectionHeight - 1
                Dim placedItem As ItemInfo = selectionData.placedItems(x)(y)
                If placedItem IsNot Nothing AndAlso Not allowedItemKeys.Contains(placedItem.ImageKey) Then
                    selectionData.placedItems(x)(y) = Nothing
                End If
            Next
        Next

        ' Filter verticalWallItems
        If selectionData.verticalWallItems IsNot Nothing Then
            For x As Integer = 0 To selectionData.verticalWallItems.Length - 1
                For y As Integer = 0 To selectionData.verticalWallItems(x).Length - 1
                    Dim wallItem As ItemInfo = selectionData.verticalWallItems(x)(y)
                    If wallItem IsNot Nothing AndAlso Not allowedItemKeys.Contains(wallItem.ImageKey) Then
                        selectionData.verticalWallItems(x)(y) = Nothing
                    End If
                Next
            Next
        End If

        ' Filter horizontalWallItems
        If selectionData.horizontalWallItems IsNot Nothing Then
            For x As Integer = 0 To selectionData.horizontalWallItems.Length - 1
                For y As Integer = 0 To selectionData.horizontalWallItems(x).Length - 1
                    Dim wallItem As ItemInfo = selectionData.horizontalWallItems(x)(y)
                    If wallItem IsNot Nothing AndAlso Not allowedItemKeys.Contains(wallItem.ImageKey) Then
                        selectionData.horizontalWallItems(x)(y) = Nothing
                    End If
                Next
            Next
        End If

        ' Filter verticalWalls
        If selectionData.verticalWalls IsNot Nothing Then
            For x As Integer = 0 To selectionData.verticalWalls.Length - 1
                For y As Integer = 0 To selectionData.verticalWalls(x).Length - 1
                    If Not allowedItemKeys.Contains("Walls") Then
                        selectionData.verticalWalls(x)(y) = False
                    End If
                Next
            Next
        End If

        ' Filter horizontalWalls
        If selectionData.horizontalWalls IsNot Nothing Then
            For x As Integer = 0 To selectionData.horizontalWalls.Length - 1
                For y As Integer = 0 To selectionData.horizontalWalls(x).Length - 1
                    If Not allowedItemKeys.Contains("Walls") Then
                        selectionData.horizontalWalls(x)(y) = False
                    End If
                Next
            Next
        End If
    End Sub

Can you see why when I select an area and choose mnuCopy. Once I've selected an area the information does not paste. However, if I press right click when pasting the information DOES paste. 

Also. When I right click to paste the information, the walls are not pasting correctly. It pastes as though all walls are filled even though most of them are blank. 
End Class