Can you please rewrite this code. I don't want extra classes. Or 90 million subs.
Imports System.Drawing.Drawing2D
Public Class RoomView
Inherits Form
code
Assistant
' 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
' Instance of GridRenderer
Private gridRenderer As New GridRenderer()
' 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()
' Call InitializeComponent to set up form and controls from Designer
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
gridRenderer.CalculateSquareSizes(PicGame.ClientSize.Width, PicGame.ClientSize.Height, SQCOUNTX, SQCOUNTY, WALL_THICKNESS)
e.Graphics.SmoothingMode = SmoothingMode.None
gridRenderer.DrawGrid(e.Graphics, PicGame.ClientSize.Width, PicGame.ClientSize.Height, INDENT, SQCOUNTX, SQCOUNTY, WALL_THICKNESS)
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 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 * (gridRenderer.SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (gridRenderer.SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, WALL_THICKNESS, gridRenderer.SQSizeY)
g.DrawRectangle(highlightPen, highlightRect)
ElseIf highlightedWallY Then
' Horizontal wall
Dim x As Integer = INDENT + highlightedX * (gridRenderer.SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (gridRenderer.SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, gridRenderer.SQSizeX, WALL_THICKNESS)
g.DrawRectangle(highlightPen, highlightRect)
Else
' Square
Dim x As Integer = INDENT + highlightedX * (gridRenderer.SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim y As Integer = INDENT + highlightedY * (gridRenderer.SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim highlightRect As New Rectangle(x, y, gridRenderer.SQSizeX, gridRenderer.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 = gridRenderer.SQSizeX + WALL_THICKNESS
Dim cellSizeY As Integer = gridRenderer.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
' Supporting GridRenderer class
Public Class GridRenderer
' Properties for hatch styles and colors
Public Property wallsHatchStyle As HatchStyle = HatchStyle.Cross
Public Property wallsColor1 As Color = Color.Gray
Public Property wallsColor2 As Color = Color.DarkGray
Public Property floorsHatchStyle As HatchStyle = HatchStyle.SmallCheckerBoard
Public Property floorsColor1 As Color = Color.LightGray
Public Property floorsColor2 As Color = Color.White
code
' Calculated square sizes
Public SQSizeX As Integer
Public SQSizeY As Integer
' Constructor
Public Sub New()
End Sub
' Method to calculate square sizes
Public Sub CalculateSquareSizes(clientWidth As Integer, clientHeight As Integer, SQCOUNTX As Integer, SQCOUNTY As Integer, WALL_THICKNESS As Integer)
SQSizeX = (clientWidth - 2 * 20 - (SQCOUNTX + 1) * WALL_THICKNESS) \ SQCOUNTX
SQSizeY = (clientHeight - 2 * 20 - (SQCOUNTY + 1) * WALL_THICKNESS) \ SQCOUNTY
End Sub
' Method to draw the grid
Public Sub DrawGrid(g As Graphics, clientWidth As Integer, clientHeight As Integer, INDENT As Integer, SQCOUNTX As Integer, SQCOUNTY As Integer, WALL_THICKNESS As Integer)
' Drawing logic for walls and floors
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 walls (vertical)
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 walls (horizontal)
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
End Class
Here is the rewritten code without extra classes and with fewer subroutines:
vb.net
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
This version of the code eliminates the extra GridRenderer class by integrating its functionality directly into the RoomView class. It reduces the number of methods while keeping the logic clear and maintaining the functionality of drawing the grid, handling mouse interactions, and highlighting.