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
code
Assistant
' 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
Certainly! The issue you're experiencing stems from the MouseMove event handler not correctly handling the detection of walls along the Y-axis (WallY). Currently, your code only checks for walls along the X-axis (WallX), which is why hovering over grid squares works but hovering over walls appears random.
In your MouseMove event handler, you need to add logic to detect when the mouse is over a horizontal wall (WallY). Here's how you can modify your MouseMove event handler to include detection for both WallX and WallY:
vb
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, WallY, and squares
If mouseX >= squareStartX AndAlso mouseX < squareStartX + WALL_THICKNESS Then
' Left wall of the current square
highlightedWallX = True
STB.Text = $"WallX({highlightedX},{highlightedY})"
ElseIf mouseX > squareEndX AndAlso mouseX <= squareEndX + WALL_THICKNESS Then
' Right wall of the current square
highlightedWallX = True
STB.Text = $"WallX({highlightedX + 1},{highlightedY})"
ElseIf mouseY >= squareStartY AndAlso mouseY < squareStartY + WALL_THICKNESS Then
' Top wall of the current square
highlightedWallY = True
STB.Text = $"WallY({highlightedX},{highlightedY})"
ElseIf mouseY > squareEndY AndAlso mouseY <= squareEndY + WALL_THICKNESS Then
' Bottom wall of the current square
highlightedWallY = True
STB.Text = $"WallY({highlightedX},{highlightedY + 1})"
Else
STB.Text = $"Square({highlightedX},{highlightedY})"
End If
' Always redraw to update the highlight
PicGame.Invalidate()
' Debugging coordinates
Bar1.Text = "X " & mouseX.ToString() & ", Y " & mouseY.ToString()
End Sub
Explanation:
Detecting WallY: The added ElseIf statements check if the mouse is within the WALL_THICKNESS area above (Top wall) or below (Bottom wall) the square, thereby detecting WallY.
Updating highlightedWallY: When the mouse is over a horizontal wall, highlightedWallY is set to True, and the appropriate wall coordinate is displayed.
Adjustments for Wall Indices:
For the right and bottom walls, we increment the highlightedX or highlightedY by 1 because those walls are shared with the adjacent square.
Redrawing: Updating the highlight by invalidating the PicGame control ensures that the highlight reflects the current mouse position.
Additional Notes:
Drawing Method Adjustments: Ensure your DrawHighlight method uses highlightedWallY appropriately to draw the highlight for horizontal walls.
Consistency: Always ensure that your wall detection logic matches how you draw and store walls in your application.
By making these changes, your application should now correctly detect and highlight walls when the mouse hovers over them, providing a consistent user experience.