Imports System.Drawing.Drawing2D Public Class RoomView Inherits Form ' Constants for…
- User
Imports System.Drawing.Drawing2D
Public Class RoomView Inherits Form
code - Assistant
Imports System.Drawing.Drawing2D
Public Class RoomView Inherits Form
' Constants for grid dimensions and indentation
Private Const INDENT As Integer = 20
Private Const SQCOUNTX As Integer = 15
Private Const SQCOUNTY As Integer = 11
Private Const WALL_THICKNESS As Integer = 10
' Properties for hatch styles and colors
Private wallsHatchStyle As HatchStyle = HatchStyle.Cross
Private wallsColor1 As Color = Color.Gray
Private wallsColor2 As Color = Color.DarkGray
Private floorsHatchStyle As HatchStyle = HatchStyle.SmallCheckerBoard
Private floorsColor1 As Color = Color.LightGray
Private floorsColor2 As Color = Color.White
' Calculated square sizes
Private SQSizeX As Integer
Private SQSizeY As Integer
' Mouse state variables
Private isRightMouseDown As Boolean = False
Private isLeftMouseDown As Boolean = False
Private isMouseInsideGrid As Boolean = False
' Variables for highlighting
Private highlightedX As Integer
Private highlightedY As Integer
Private highlightedWallX As Boolean = False
Private highlightedWallY As Boolean = False
Private highlightColor As Color = Color.Black
Private highlightTimer As Timer
' Constructor
Public Sub New()
' Initialize the form components (assumes you have a PictureBox named 'PicGame')
InitializeComponent()
' Initialize the highlight timer
highlightTimer = New Timer()
highlightTimer.Interval = 500 ' Flash every 500 milliseconds
AddHandler highlightTimer.Tick, AddressOf HighlightTimer_Tick
highlightTimer.Start()
End Sub
' Event handler for the highlight timer tick
Private Sub HighlightTimer_Tick(sender As Object, e As EventArgs)
' Toggle the highlight color between black and white
If highlightColor = Color.Black Then
highlightColor = Color.White
Else
highlightColor = Color.Black
End If
PicGame.Invalidate() ' Trigger a redraw on the PictureBox
End Sub
' PicGame event handlers, assuming the PictureBox name is 'PicGame'
Private Sub PicGame_Paint(sender As Object, e As PaintEventArgs) Handles PicGame.Paint
' Calculate square size and draw the grid
CalculateSquareSizes(PicGame.ClientSize.Width, PicGame.ClientSize.Height)
e.Graphics.SmoothingMode = SmoothingMode.None
DrawGrid(e.Graphics)
DrawHighlight(e.Graphics)
End Sub
Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles PicGame.MouseMove
' Logic for mouse movement within the grid
HandleMouseMove(e)
End Sub
Private Sub PicGame_MouseDown(sender As Object, e As MouseEventArgs) Handles PicGame.MouseDown
' Set mouse button states
If e.Button = MouseButtons.Right Then
isRightMouseDown = True
ElseIf e.Button = MouseButtons.Left Then
isLeftMouseDown = True
End If
End Sub
Private Sub PicGame_MouseUp(sender As Object, e As MouseEventArgs) Handles PicGame.MouseUp
' Reset mouse button states
If e.Button = MouseButtons.Right Then
isRightMouseDown = False
ElseIf e.Button = MouseButtons.Left Then
isLeftMouseDown = False
End If
End Sub
' Method to calculate square sizes
Private Sub CalculateSquareSizes(clientWidth As Integer, clientHeight As Integer)
SQSizeX = (clientWidth - 2 * INDENT - (SQCOUNTX + 1) * WALL_THICKNESS) \ SQCOUNTX
SQSizeY = (clientHeight - 2 * INDENT - (SQCOUNTY + 1) * WALL_THICKNESS) \ SQCOUNTY
End Sub
' Method to draw the grid
Private Sub DrawGrid(g As Graphics)
Dim xOrigin As Integer = INDENT
Dim yOrigin As Integer = INDENT
' Draw floors
For x As Integer = 0 To SQCOUNTX - 1
For y As Integer = 0 To SQCOUNTY - 1
Dim floorRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
SQSizeX,
SQSizeY)
Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
g.FillRectangle(floorBrush, floorRect)
End Using
Next
Next
' Draw vertical walls
For x As Integer = 0 To SQCOUNTX
For y As Integer = 0 To SQCOUNTY - 1
Dim wallRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
WALL_THICKNESS,
SQSizeY)
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
Next
Next
' Draw horizontal walls
For x As Integer = 0 To SQCOUNTX - 1
For y As Integer = 0 To SQCOUNTY
Dim wallRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
SQSizeX,
WALL_THICKNESS)
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
Next
Next
End Sub
' Method to draw the highlight
Private Sub DrawHighlight(g As Graphics)
If Not isMouseInsideGrid Then
Return
End If
Using highlightPen As New Pen(highlightColor, 2)
highlightPen.DashStyle = DashStyle.Dash
If highlightedWallX Then
' Vertical wall
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, WALL_THICKNESS, SQSizeY)
g.DrawRectangle(highlightPen, highlightRect)
ElseIf highlightedWallY Then
' Horizontal wall
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, SQSizeX, WALL_THICKNESS)
g.DrawRectangle(highlightPen, highlightRect)
Else
' Square
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, SQSizeX, SQSizeY)
g.DrawRectangle(highlightPen, highlightRect)
End If
End Using
End Sub
' Method to handle mouse movement within the grid
Private Sub HandleMouseMove(e As MouseEventArgs)
' Initialize as False at the start of the method
isMouseInsideGrid = False
' Adjust for indentation
Dim mouseX As Integer = e.X - INDENT
Dim mouseY As Integer = e.Y - INDENT
' Early exit if the mouse is outside the grid area
If mouseX < 0 OrElse mouseY < 0 Then
Exit Sub
End If
Dim cellSizeX As Integer = SQSizeX + WALL_THICKNESS
Dim cellSizeY As Integer = SQSizeY + WALL_THICKNESS
Dim cellX As Integer = mouseX \ cellSizeX
Dim cellY As Integer = mouseY \ cellSizeY
If cellX >= SQCOUNTX OrElse cellY >= SQCOUNTY Then
Exit Sub
End If
Dim offsetX As Integer = mouseX Mod cellSizeX
Dim offsetY As Integer = mouseY Mod cellSizeY
highlightedWallX = False
highlightedWallY = False
If offsetX < WALL_THICKNESS Then
' Vertical wall
highlightedWallX = True
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
ElseIf offsetY < WALL_THICKNESS Then
' Horizontal wall
highlightedWallY = True
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
ElseIf offsetX >= WALL_THICKNESS AndAlso offsetX < cellSizeX AndAlso offsetY >= WALL_THICKNESS AndAlso offsetY < cellSizeY Then
' Inside the square
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
Else
Exit Sub
End If
' Redraw to update the highlight
PicGame.Invalidate()
End Sub
End Class
Can you add if I drag over a wall with right click it removes the wall. And left click to add a wall.
I have 98 coloured pictureboxes named col1 to col48 and 51 to col98 (Each picturebox has a unique colour) I have 2 radiobuttons rdofloors and rdowalls. I have a listbox called lstHatchStyles which is populated with Hatchstyles (done in designer)
If rdoFloors is checked and I click col1 to col48 the hatchstyles colour1 will change, if I click col51 to col98 colour2 will change. If I selected a hatchstyle from lstHatchstyles the hatchstyle will change repeart the above for the walls when rdoWalls is checked
Yes, I can modify your code to add the requested functionality. Below is the updated code that includes the ability to add or remove walls by dragging over them with the left or right mouse button, respectively. Additionally, I've added the interactions with the colored picture boxes, radio buttons (rdofloors and rdowalls), and the list box lstHatchStyles.
Here's the modified code:
Imports System.Drawing.Drawing2D
Public Class RoomView
Inherits Form
' Constants for grid dimensions and indentation
Private Const INDENT As Integer = 20
Private Const SQCOUNTX As Integer = 15
Private Const SQCOUNTY As Integer = 11
Private Const WALL_THICKNESS As Integer = 10
' Properties for hatch styles and colors
Private wallsHatchStyle As HatchStyle = HatchStyle.Cross
Private wallsColor1 As Color = Color.Gray
Private wallsColor2 As Color = Color.DarkGray
Private floorsHatchStyle As HatchStyle = HatchStyle.SmallCheckerBoard
Private floorsColor1 As Color = Color.LightGray
Private floorsColor2 As Color = Color.White
' Calculated square sizes
Private SQSizeX As Integer
Private SQSizeY As Integer
' Mouse state variables
Private isRightMouseDown As Boolean = False
Private isLeftMouseDown As Boolean = False
Private isMouseInsideGrid As Boolean = False
' Variables for highlighting
Private highlightedX As Integer
Private highlightedY As Integer
Private highlightedWallX As Boolean = False
Private highlightedWallY As Boolean = False
Private highlightColor As Color = Color.Black
Private highlightTimer As Timer
' Arrays to store wall presence
Private verticalWalls(SQCOUNTX, SQCOUNTY - 1) As Boolean ' Vertical walls
Private horizontalWalls(SQCOUNTX - 1, SQCOUNTY) As Boolean ' Horizontal walls
' Constructor
Public Sub New()
' Initialize the form components (assumes you have a PictureBox named 'PicGame')
InitializeComponent()
' Initialize walls (e.g., set all walls to present)
For x As Integer = 0 To SQCOUNTX
For y As Integer = 0 To SQCOUNTY - 1
verticalWalls(x, y) = True
Next
Next
For x As Integer = 0 To SQCOUNTX - 1
For y As Integer = 0 To SQCOUNTY
horizontalWalls(x, y) = True
Next
Next
' Initialize the highlight timer
highlightTimer = New Timer()
highlightTimer.Interval = 500 ' Flash every 500 milliseconds
AddHandler highlightTimer.Tick, AddressOf HighlightTimer_Tick
highlightTimer.Start()
' Attach event handlers to picture boxes
AddColorPictureBoxHandlers(Me)
End Sub
' Recursively add event handlers to picture boxes
Private Sub AddColorPictureBoxHandlers(parent As Control)
For Each ctrl As Control In parent.Controls
If TypeOf ctrl Is PictureBox AndAlso ctrl.Name.StartsWith("col") Then
AddHandler ctrl.Click, AddressOf ColorPictureBox_Click
End If
If ctrl.HasChildren Then
AddColorPictureBoxHandlers(ctrl)
End If
Next
End Sub
' Event handler for picture boxes
Private Sub ColorPictureBox_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = CType(sender, PictureBox)
' Get the color of the clicked picture box
Dim selectedColor As Color = pb.BackColor
If rdofloors.Checked Then
If pb.Name.StartsWith("col") Then
Dim numStr As String = pb.Name.Substring(3)
Dim num As Integer
If Integer.TryParse(numStr, num) Then
If num >= 1 AndAlso num <= 48 Then
' Change floorsColor1
floorsColor1 = selectedColor
ElseIf num >= 51 AndAlso num <= 98 Then
' Change floorsColor2
floorsColor2 = selectedColor
End If
End If
End If
ElseIf rdowalls.Checked Then
If pb.Name.StartsWith("col") Then
Dim numStr As String = pb.Name.Substring(3)
Dim num As Integer
If Integer.TryParse(numStr, num) Then
If num >= 1 AndAlso num <= 48 Then
' Change wallsColor1
wallsColor1 = selectedColor
ElseIf num >= 51 AndAlso num <= 98 Then
' Change wallsColor2
wallsColor2 = selectedColor
End If
End If
End If
End If
' Redraw the grid to update colors
PicGame.Invalidate()
End Sub
' Event handler for lstHatchStyles
Private Sub lstHatchStyles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstHatchStyles.SelectedIndexChanged
' Get the selected HatchStyle
Dim selectedHatchStyleName As String = lstHatchStyles.SelectedItem.ToString()
Dim selectedHatchStyle As HatchStyle = CType([Enum].Parse(GetType(HatchStyle), selectedHatchStyleName), HatchStyle)
If rdofloors.Checked Then
floorsHatchStyle = selectedHatchStyle
ElseIf rdowalls.Checked Then
wallsHatchStyle = selectedHatchStyle
End If
' Redraw the grid to update hatch styles
PicGame.Invalidate()
End Sub
' Event handler for the highlight timer tick
Private Sub HighlightTimer_Tick(sender As Object, e As EventArgs)
' Toggle the highlight color between black and white
If highlightColor = Color.Black Then
highlightColor = Color.White
Else
highlightColor = Color.Black
End If
PicGame.Invalidate() ' Trigger a redraw on the PictureBox
End Sub
' PicGame event handlers, assuming the PictureBox name is 'PicGame'
Private Sub PicGame_Paint(sender As Object, e As PaintEventArgs) Handles PicGame.Paint
' Calculate square size and draw the grid
CalculateSquareSizes(PicGame.ClientSize.Width, PicGame.ClientSize.Height)
e.Graphics.SmoothingMode = SmoothingMode.None
DrawGrid(e.Graphics)
DrawHighlight(e.Graphics)
End Sub
Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles PicGame.MouseMove
' Logic for mouse movement within the grid
HandleMouseMove(e)
End Sub
Private Sub PicGame_MouseDown(sender As Object, e As MouseEventArgs) Handles PicGame.MouseDown
' Set mouse button states
If e.Button = MouseButtons.Right Then
isRightMouseDown = True
ElseIf e.Button = MouseButtons.Left Then
isLeftMouseDown = True
End If
End Sub
Private Sub PicGame_MouseUp(sender As Object, e As MouseEventArgs) Handles PicGame.MouseUp
' Reset mouse button states
If e.Button = MouseButtons.Right Then
isRightMouseDown = False
ElseIf e.Button = MouseButtons.Left Then
isLeftMouseDown = False
End If
End Sub
' Method to calculate square sizes
Private Sub CalculateSquareSizes(clientWidth As Integer, clientHeight As Integer)
SQSizeX = (clientWidth - 2 * INDENT - (SQCOUNTX + 1) * WALL_THICKNESS) \ SQCOUNTX
SQSizeY = (clientHeight - 2 * INDENT - (SQCOUNTY + 1) * WALL_THICKNESS) \ SQCOUNTY
End Sub
' Method to draw the grid
Private Sub DrawGrid(g As Graphics)
Dim xOrigin As Integer = INDENT
Dim yOrigin As Integer = INDENT
' Draw floors
For x As Integer = 0 To SQCOUNTX - 1
For y As Integer = 0 To SQCOUNTY - 1
Dim floorRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
SQSizeX,
SQSizeY)
Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
g.FillRectangle(floorBrush, floorRect)
End Using
Next
Next
' Draw vertical walls
For x As Integer = 0 To SQCOUNTX
For y As Integer = 0 To SQCOUNTY - 1
If verticalWalls(x, y) Then
Dim wallRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2,
WALL_THICKNESS,
SQSizeY)
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
End If
Next
Next
' Draw horizontal walls
For x As Integer = 0 To SQCOUNTX - 1
For y As Integer = 0 To SQCOUNTY
If horizontalWalls(x, y) Then
Dim wallRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
SQSizeX,
WALL_THICKNESS)
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
End If
Next
Next
End Sub
' Method to draw the highlight
Private Sub DrawHighlight(g As Graphics)
If Not isMouseInsideGrid Then
Return
End If
Using highlightPen As New Pen(highlightColor, 2)
highlightPen.DashStyle = DashStyle.Dash
If highlightedWallX Then
' Vertical wall
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, WALL_THICKNESS, SQSizeY)
g.DrawRectangle(highlightPen, highlightRect)
ElseIf highlightedWallY Then
' Horizontal wall
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, SQSizeX, WALL_THICKNESS)
g.DrawRectangle(highlightPen, highlightRect)
Else
' Square
Dim x As Integer = INDENT + highlightedX * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, SQSizeX, SQSizeY)
g.DrawRectangle(highlightPen, highlightRect)
End If
End Using
End Sub
' Method to handle mouse movement within the grid
Private Sub HandleMouseMove(e As MouseEventArgs)
' Initialize as False at the start of the method
isMouseInsideGrid = False
' Adjust for indentation
Dim mouseX As Integer = e.X - INDENT
Dim mouseY As Integer = e.Y - INDENT
' Early exit if the mouse is outside the grid area
If mouseX < 0 OrElse mouseY < 0 Then
Exit Sub
End If
Dim cellSizeX As Integer = SQSizeX + WALL_THICKNESS
Dim cellSizeY As Integer = SQSizeY + WALL_THICKNESS
Dim cellX As Integer = mouseX \ cellSizeX
Dim cellY As Integer = mouseY \ cellSizeY
If cellX >= SQCOUNTX OrElse cellY >= SQCOUNTY Then
Exit Sub
End If
Dim offsetX As Integer = mouseX Mod cellSizeX
Dim offsetY As Integer = mouseY Mod cellSizeY
highlightedWallX = False
highlightedWallY = False
If offsetX < WALL_THICKNESS Then
' Vertical wall
highlightedWallX = True
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
ElseIf offsetY < WALL_THICKNESS Then
' Horizontal wall
highlightedWallY = True
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
ElseIf offsetX >= WALL_THICKNESS AndAlso offsetX < cellSizeX AndAlso offsetY >= WALL_THICKNESS AndAlso offsetY < cellSizeY Then
' Inside the square
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
Else
Exit Sub
End If
' If mouse button is down and over a wall, add/remove wall
If isLeftMouseDown OrElse isRightMouseDown Then
If highlightedWallX Then
' Vertical wall
If isLeftMouseDown Then
verticalWalls(highlightedX, highlightedY) = True
ElseIf isRightMouseDown Then
verticalWalls(highlightedX, highlightedY) = False
End If
ElseIf highlightedWallY Then
' Horizontal wall
If isLeftMouseDown Then
horizontalWalls(highlightedX, highlightedY) = True
ElseIf isRightMouseDown Then
horizontalWalls(highlightedX, highlightedY) = False
End If
End If
' Redraw to update the wall
PicGame.Invalidate()
End If
' Redraw to update the highlight
PicGame.Invalidate()
End Sub
End Class
Explanation of the Changes:
Adding Wall Data Structures:
verticalWalls and horizontalWalls arrays to store the presence of walls.Modifying the Drawing Code:
DrawGrid method to only draw walls where they are present in the verticalWalls and horizontalWalls arrays.Handling Mouse Interaction:
HandleMouseMove method to check if the mouse is over a wall and whether the left or right mouse button is pressed.True in the arrays).False in the arrays).PicGame.Invalidate() to trigger a redraw when walls are added or removed.Interacting with Colored Picture Boxes:
ColorPictureBox_Click event handler to change the hatch style colors based on which picture box is clicked.AddColorPictureBoxHandlers method, which recursively finds all picture boxes starting with "col".rdofloors or rdowalls is checked and updates the corresponding color properties (floorsColor1, floorsColor2, wallsColor1, or wallsColor2).Handling Hatch Style Selection:
lstHatchStyles_SelectedIndexChanged event handler to update the hatch style when a new style is selected from the list box.floorsHatchStyle or wallsHatchStyle based on which radio button is checked.Redrawing the Grid:
PicGame.Invalidate() is called to redraw the grid with the updated settings.Usage:
Adding/Removing Walls:
Changing Floor or Wall Colors:
rdofloors Radio Button: Click on colored picture boxes (col1 to col48 for Color1, col51 to col98 for Color2) to change floor colors.rdowalls Radio Button: Click on colored picture boxes to change wall colors in the same way.Changing Hatch Styles:
lstHatchStyles: Changes the hatch style of floors or walls based on which radio button is checked.Note: Ensure that all the picture boxes (col1 to col48 and col51 to col98) are properly named and have their BackColor property set to the desired color. Also, make sure that the event handlers are properly attached to your controls, especially if you are adding them via the Designer.
Not yet included in a published release.