Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class RoomView
    ' Constants for grid dimensions and indentation
    Const INDENT As Integer = 20
    Const SQCOUNTX As Integer = 15
    Const SQCOUNTY As Integer = 11
    Const WALL_THICKNESS As Integer = 10

    ' Class-level variables
    ReadOnly SQ(SQCOUNTX, SQCOUNTY) As Integer
    ReadOnly WallX(SQCOUNTX, SQCOUNTY) As Boolean
    ReadOnly WallY(SQCOUNTX, SQCOUNTY) As Boolean
    Dim SQSizeX As Integer
    Dim SQSizeY As Integer
    Dim highlightedX As Integer
    Dim highlightedY As Integer
    Dim oldHighlightedX As Integer
    Dim oldHighlightedY As Integer
    Dim highlightedWallX As Boolean = False
    Dim highlightedWallY As Boolean = False

    ' Constructor
    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        For i As Integer = 0 To SQCOUNTX - 1
            For j As Integer = 0 To SQCOUNTY - 1
                SQ(i, j) = 1
                WallX(i, j) = True  ' Initialize walls to True
                WallY(i, j) = True  ' Initialize walls to True
            Next
        Next
    End Sub

    ' Event handler for PicGame's Paint event
    Private Sub PicGame_Paint(sender As Object, e As PaintEventArgs) Handles PicGame.Paint
        ' Calculate the square size based on the size of the PictureBox
        CalculateSquareSizes()

        ' Draw the grid, walls, and highlighted square
        DrawGrid(e.Graphics)
        DrawWalls(e.Graphics)
        DrawHighlight(e.Graphics)
    End Sub

    ' Event handler for resizing the form
    Private Sub RoomView_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        If PicGame.ClientSize.Width > 0 AndAlso PicGame.ClientSize.Height > 0 Then
            CalculateSquareSizes()
            PicGame.Invalidate() ' Redraw the PictureBox
        End If
    End Sub

    ' Method to calculate square sizes
    Private Sub CalculateSquareSizes()
        If PicGame.ClientSize.Width > 0 AndAlso PicGame.ClientSize.Height > 0 Then
            SQSizeX = (PicGame.ClientSize.Width - 2 * INDENT) \ SQCOUNTX - WALL_THICKNESS
            SQSizeY = (PicGame.ClientSize.Height - 2 * INDENT) \ SQCOUNTY - WALL_THICKNESS
        End If
    End Sub

    ' Method to draw the grid
    Private Sub DrawGrid(g As Graphics)
        If PicGame.ClientSize.Width > 0 AndAlso PicGame.ClientSize.Height > 0 Then
            ' Draw grid squares
            For i As Integer = 0 To SQCOUNTX - 1
                For j As Integer = 0 To SQCOUNTY - 1
                    Dim rect As New Rectangle(INDENT + i * (SQSizeX + WALL_THICKNESS), INDENT + j * (SQSizeY + WALL_THICKNESS), SQSizeX, SQSizeY)
                    g.DrawRectangle(Pens.Green, rect)
                Next
            Next
        End If
    End Sub

    ' Method to draw the walls
    Private Sub DrawWalls(g As Graphics)
        If PicGame.ClientSize.Width > 0 AndAlso PicGame.ClientSize.Height > 0 Then
            ' Draw WallX (left and right walls)
            For i As Integer = 0 To SQCOUNTX - 1
                For j As Integer = 0 To SQCOUNTY - 1
                    Dim x As Integer = INDENT + i * (SQSizeX + WALL_THICKNESS)
                    Dim y As Integer = INDENT + j * (SQSizeY + WALL_THICKNESS)
                    If WallX(i, j) Then
                        g.FillRectangle(Brushes.Red, x - WALL_THICKNESS, y, WALL_THICKNESS, SQSizeY)
                        g.FillRectangle(Brushes.Red, x + SQSizeX, y, WALL_THICKNESS, SQSizeY)
                    End If
                Next
            Next

            ' Draw WallY (top and bottom walls)
            For i As Integer = 0 To SQCOUNTX - 1
                For j As Integer = 0 To SQCOUNTY - 1
                    Dim x As Integer = INDENT + i * (SQSizeX + WALL_THICKNESS)
                    Dim y As Integer = INDENT + j * (SQSizeY + WALL_THICKNESS)
                    If WallY(i, j) Then
                        g.FillRectangle(Brushes.Red, x, y - WALL_THICKNESS, SQSizeX, WALL_THICKNESS)
                        g.FillRectangle(Brushes.Red, x, y + SQSizeY, SQSizeX, WALL_THICKNESS)
                    End If
                Next
            Next
        End If
    End Sub

    ' Method to draw the highlight
    Private Sub DrawHighlight(g As Graphics)
        ' Calculate the coordinates of the highlighted square or wall
        Dim highlightRect As Rectangle
        If highlightedWallX Then
            highlightRect = New Rectangle(INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS, INDENT + highlightedY * (SQSizeY + WALL_THICKNESS), WALL_THICKNESS, SQSizeY)
        ElseIf highlightedWallY Then
            highlightRect = New Rectangle(INDENT + highlightedX * (SQSizeX + WALL_THICKNESS), INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS, SQSizeX, WALL_THICKNESS)
        Else
            highlightRect = New Rectangle(INDENT + highlightedX * (SQSizeX + WALL_THICKNESS), INDENT + highlightedY * (SQSizeY + WALL_THICKNESS), SQSizeX, SQSizeY)
        End If
        ' Draw a thicker black rectangle to highlight the square or wall
        Using highlightPen As New Pen(Color.Black, 5)
            g.DrawRectangle(highlightPen, highlightRect)
        End Using
    End Sub


    ' Event handler for PicGame's MouseMove event
    Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles PicGame.MouseMove
        ' Calculate the square that the mouse is currently over
        Dim mouseX As Integer = e.X
        Dim mouseY As Integer = e.Y
        oldHighlightedX = highlightedX ' Store the old highlighted X position
        oldHighlightedY = highlightedY ' Store the old highlighted Y position

        ' Reset highlighting flags
        highlightedWallX = False
        highlightedWallY = False

        ' Adjust for indentation
        mouseX -= INDENT
        mouseY -= INDENT

        ' Ensure coordinates start at 0
        mouseX = Math.Max(mouseX, 0)
        mouseY = Math.Max(mouseY, 0)

        ' Determine the square coordinates
        highlightedX = mouseX \ (SQSizeX + WALL_THICKNESS)
        highlightedY = mouseY \ (SQSizeY + WALL_THICKNESS)

        ' Calculate exact boundaries for squares
        Dim squareStartX As Integer = highlightedX * (SQSizeX + WALL_THICKNESS) + INDENT
        Dim squareEndX As Integer = squareStartX + SQSizeX
        Dim squareStartY As Integer = highlightedY * (SQSizeY + WALL_THICKNESS) + INDENT
        Dim squareEndY As Integer = squareStartY + SQSizeY

        ' Highlight logic for WallX and squares
        If mouseX >= squareStartX AndAlso mouseX < squareStartX + WALL_THICKNESS Then
            highlightedWallX = True
            STB.Text = $"WallX({highlightedX},{highlightedY})"
        ElseIf mouseX > squareEndX AndAlso mouseX <= squareEndX + WALL_THICKNESS Then
            highlightedWallX = True
            STB.Text = $"WallX({highlightedX},{highlightedY})"
        Else
            STB.Text = $"Square({highlightedX},{highlightedY})"
        End If

        ' Always redraw to update the highlight
        PicGame.Invalidate()

        ' Debugging coordinates
        Bar1.Text = "X " & CStr(mouseX) & ", Y " & CStr(mouseY)
    End Sub





    Private Sub PicGame_Click(sender As Object, e As EventArgs) Handles PicGame.Click
        ' Handle click events if needed
    End Sub
End Class


Can you see what's going on in the mousemove event. When I hover over a wall or a grid square. Said wall or grid should be highlighted. Hovering over a grid square works perfectly, but the walls are very random