Private Sub frmDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadMusic()
LoadPremades()
' Populate lstHatchstyles with hatch style names
lstHatchstyles.Items.Clear()
For Each hs As HatchStyle In [Enum].GetValues(GetType(HatchStyle))
lstHatchstyles.Items.Add(hs.ToString())
Next
' Set default selections
UpdateSelections()
End Sub
Private Sub LoadPremades()
If Not Directory.Exists(premadesFolder) Then Directory.CreateDirectory(premadesFolder)
lstPremades.Items.Clear()
For Each file As String In Directory.GetFiles(premadesFolder, "*.Premade")
lstPremades.Items.Add(Path.GetFileNameWithoutExtension(file))
Next
End Sub
Private Sub LoadMusic()
If Not Directory.Exists(musicFolder) Then Directory.CreateDirectory(musicFolder)
cboMusic.Items.Clear()
For Each file As String In Directory.GetFiles(musicFolder, "*.wav")
cboMusic.Items.Add(Path.GetFileNameWithoutExtension(file))
Next
End Sub
Private Sub OnAnimationTick(sender As Object, e As EventArgs)
For Each img In animatedImages
ImageAnimator.UpdateFrames(img)
Next
picGame.Invalidate()
End Sub
Private Sub OnFrameChanged(o As Object, e As EventArgs)
' Required but can be left empty
End Sub
Private Sub HighlightTimer_Tick(sender As Object, e As EventArgs)
highlightColor = If(highlightColor = Color.Black, Color.White, Color.Black)
picGame.Invalidate()
End Sub
Private Sub lstPremades_SelectedIndexChanged(sender As Object, e As EventArgs)
If lstPremades.SelectedIndex >= 0 Then
If Not isRestoringState Then SaveStateForUndo()
Dim selectedPremade As String = lstPremades.SelectedItem.ToString()
LoadSettingsFromFile(Path.Combine(premadesFolder, selectedPremade & ".Premade"))
' Update lstHatchstyles selection after loading settings
UpdateSelections()
picGame.Invalidate()
End If
End Sub
Private Sub mnuClearWalls_Click(sender As Object, e As EventArgs) Handles mnuClearWalls.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
If Not IsVerticalWallConnectedToEdge(pt) Then
verticalWalls(pt.X)(pt.Y) = False
verticalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
If Not IsHorizontalWallConnectedToEdge(pt) Then
horizontalWalls(pt.X)(pt.Y) = False
horizontalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
Private Sub mnuFillWalls_Click(sender As Object, e As EventArgs) Handles mnuFillWalls.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
If pt.X > 0 AndAlso pt.X < SQCOUNTX Then
verticalWalls(pt.X)(pt.Y) = True
End If
Next
For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
If pt.Y > 0 AndAlso pt.Y < SQCOUNTY Then
horizontalWalls(pt.X)(pt.Y) = True
End If
Next
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
Private Sub mnuClearObjects_Click(sender As Object, e As EventArgs) Handles mnuClearObjects.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
For Each pt As Point In GetCellsInRectangle(selectionRectangle)
placedItems(pt.X)(pt.Y) = Nothing
Next
For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
verticalWallItems(pt.X)(pt.Y) = Nothing
Next
For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
horizontalWallItems(pt.X)(pt.Y) = Nothing
Next
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
Private Sub mnuClearEdge_Click(sender As Object, e As EventArgs) Handles mnuClearEdge.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
For Each pt As Point In GetCellsInRectangle(selectionRectangle)
floorsUseEdgeStyle(pt.X)(pt.Y) = False
Next
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
Private Sub mnuAddEdge_Click(sender As Object, e As EventArgs) Handles mnuAddEdge.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
' Loop through each cell within the selection rectangle
For Each pt As Point In GetCellsInRectangle(selectionRectangle)
' Apply the edge style to the floor cell
floorsUseEdgeStyle(pt.X)(pt.Y) = True
' Place walls around the cell if there are no wall items already
' Left vertical wall at (pt.X, pt.Y)
If pt.X >= 0 AndAlso pt.X < verticalWalls.Length Then
If verticalWallItems(pt.X)(pt.Y) Is Nothing Then
verticalWalls(pt.X)(pt.Y) = True
End If
End If
' Right vertical wall at (pt.X + 1, pt.Y)
If pt.X + 1 >= 0 AndAlso pt.X + 1 < verticalWalls.Length Then
If verticalWallItems(pt.X + 1)(pt.Y) Is Nothing Then
verticalWalls(pt.X + 1)(pt.Y) = True
End If
End If
' Top horizontal wall at (pt.X, pt.Y)
If pt.X >= 0 AndAlso pt.X < horizontalWalls.Length AndAlso pt.Y >= 0 AndAlso pt.Y < horizontalWalls(0).Length Then
If horizontalWallItems(pt.X)(pt.Y) Is Nothing Then
horizontalWalls(pt.X)(pt.Y) = True
End If
End If
' Bottom horizontal wall at (pt.X, pt.Y + 1)
If pt.X >= 0 AndAlso pt.X < horizontalWalls.Length AndAlso pt.Y + 1 >= 0 AndAlso pt.Y + 1 < horizontalWalls(0).Length Then
If horizontalWallItems(pt.X)(pt.Y + 1) Is Nothing Then
horizontalWalls(pt.X)(pt.Y + 1) = True
End If
End If
Next
' Clear the selection and update the UI
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
If isInSelectionMode Then
isInSelectionMode = False
Cursor = Cursors.Default
selectionRectangle = Rectangle.Empty
picGame.Invalidate()
Else
isInSelectionMode = True
Cursor = Cursors.Cross
End If
End Sub
Private Sub mnuCancel_Click(sender As Object, e As EventArgs) Handles mnuCancel.Click
' Cancel the selection
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End Sub
Private Sub mnuNew_Click(sender As Object, e As EventArgs) Handles mnuNew.Click
If Not isRestoringState Then SaveStateForUndo()
wallsHatchStyle = HatchStyle.Cross
wallsColor1 = Color.Gray
wallsColor2 = Color.DarkGray
floorsHatchStyle = HatchStyle.SmallCheckerBoard
floorsColor1 = Color.LightGray
floorsColor2 = Color.White
InitializeWalls()
currentFilePath = ""
' Clear level name and music selection
txtName.Text = ""
SelectedMusic = ""
cboMusic.SelectedIndex = -1
' Update selections
UpdateSelections()
picGame.Invalidate()
End Sub
Private Sub mnuSave_Click(sender As Object, e As EventArgs)
If String.IsNullOrEmpty(currentFilePath) Then
mnuSaveAs_Click(sender, e)
Else
SaveRoomToFile(currentFilePath)
End If
End Sub
Private Sub mnuLoad_Click(sender As Object, e As EventArgs)
Dim openDialog As New OpenFileDialog() With {
.InitialDirectory = savesFolder,
.Filter = "Room Files (*.room)|*.room",
.DefaultExt = "room",
.AddExtension = True,
.Title = "Load Room"
}
If openDialog.ShowDialog() = DialogResult.OK Then
If Not isRestoringState Then SaveStateForUndo()
LoadRoomFromFile(openDialog.FileName)
currentFilePath = openDialog.FileName
' Update lstHatchstyles selection
UpdateSelections()
picGame.Invalidate()
End If
End Sub
Private Sub mnuSaveAs_Click(sender As Object, e As EventArgs)
Dim saveDialog As New SaveFileDialog() With {
.InitialDirectory = savesFolder,
.Filter = "Room Files (*.room)|*.room",
.DefaultExt = "room",
.AddExtension = True,
.Title = "Save Room As"
}
If saveDialog.ShowDialog() = DialogResult.OK Then
SaveRoomToFile(saveDialog.FileName)
currentFilePath = saveDialog.FileName
End If
End Sub
Private Sub btnUndo_Click(sender As Object, e As EventArgs) Handles btnUndo.Click
If undoStack.Count > 1 Then
SaveStateForRedo()
undoStack.Pop()
isRestoringState = True
Dim previousState As RoomState = undoStack.Peek()
previousState.RestoreState(Me)
isRestoringState = False
' Update selections
UpdateSelections()
picGame.Invalidate()
End If
End Sub
Private Sub btnRedo_Click(sender As Object, e As EventArgs) Handles btnRedo.Click
If redoStack.Count > 0 Then
SaveStateForUndo()
isRestoringState = True
Dim redoState As RoomState = redoStack.Pop()
redoState.RestoreState(Me)
isRestoringState = False
' Update selections
UpdateSelections()
picGame.Invalidate()
End If
End Sub
Private Sub btnSavePremade_Click(sender As Object, e As EventArgs)
Dim saveDialog As New SaveFileDialog() With {
.InitialDirectory = premadesFolder,
.Filter = "Premade Files (*.Premade)|*.Premade",
.DefaultExt = "Premade",
.AddExtension = True,
.Title = "Save Premade"
}
If saveDialog.ShowDialog() = DialogResult.OK Then
SaveSettingsToFile(saveDialog.FileName)
LoadPremades()
End If
End Sub
Private Sub lstHatchstyles_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstHatchstyles.SelectedIndexChanged
If lstHatchstyles.SelectedItem IsNot Nothing Then
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
If Not isRestoringState Then SaveStateForUndo()
picGame.Invalidate()
End If
End Sub
Private Sub cboMusic_SelectedIndexChanged(sender As Object, e As EventArgs)
SelectedMusic = If(cboMusic.SelectedItem?.ToString(), "")
If Not isRestoringState Then SaveStateForUndo()
End Sub
Private Sub btnAll_Click(sender As Object, e As EventArgs)
If Not isRestoringState Then SaveStateForUndo()
RandomizeWalls()
RandomizeFloors()
' Update the HatchStyle list selection
UpdateSelections()
picGame.Invalidate()
End Sub
Private Sub btnWalls_Click(sender As Object, e As EventArgs)
If Not isRestoringState Then SaveStateForUndo()
RandomizeWalls()
picGame.Invalidate()
End Sub
Private Sub btnFloors_Click(sender As Object, e As EventArgs)
If Not isRestoringState Then SaveStateForUndo()
RandomizeFloors()
picGame.Invalidate()
End Sub
Private Sub rdoWalls_CheckedChanged(sender As Object, e As EventArgs) Handles rdoWalls.CheckedChanged
If rdoWalls.Checked Then
UpdateSelections()
End If
End Sub
Private Sub rdoFloors_CheckedChanged(sender As Object, e As EventArgs) Handles rdoFloors.CheckedChanged
If rdoFloors.Checked Then
UpdateSelections()
End If
End Sub
Private Sub rdoEdge_CheckedChanged(sender As Object, e As EventArgs) Handles rdoEdge.CheckedChanged
If rdoEdge.Checked Then
UpdateSelections()
End If
End Sub
Private Sub PicGame_Paint(sender As Object, e As PaintEventArgs)
CalculateSquareSizes(picGame.ClientSize.Width, picGame.ClientSize.Height)
e.Graphics.SmoothingMode = SmoothingMode.None
DrawGrid(e.Graphics)
DrawHighlight(e.Graphics)
If isDraggingItem AndAlso draggedItem IsNot Nothing Then
Dim img As Image = GetImageByKey(draggedItem.ImageKey)
If img IsNot Nothing Then
ImageAnimator.UpdateFrames(img)
Dim drawRect As Rectangle
If draggedItemIsWall Then
If draggedItemIsVerticalWall Then
' Draw vertical wall item under cursor
Dim wallWidth As Integer = WALL_THICKNESS
Dim wallHeight As Integer = SQSizeY
Dim xPos As Integer = mouseX - WALL_THICKNESS \ 2
Dim yPos As Integer = mouseY - SQSizeY \ 2
' Keep original image width, stretch height
Dim newWidth As Integer = img.Width
Dim newHeight As Integer = wallHeight
' Center horizontally
Dim centeredX As Integer = xPos + (wallWidth - newWidth) \ 2
drawRect = New Rectangle(centeredX, yPos, newWidth, newHeight)
Else
' Draw horizontal wall item under cursor
Dim wallWidth As Integer = SQSizeX
Dim wallHeight As Integer = WALL_THICKNESS
Dim xPos As Integer = mouseX - SQSizeX \ 2
Dim yPos As Integer = mouseY - WALL_THICKNESS \ 2
' Stretch width, keep original image height
Dim newWidth As Integer = wallWidth
Dim newHeight As Integer = img.Height
' Center vertically
Dim centeredY As Integer = yPos + (wallHeight - newHeight) \ 2
drawRect = New Rectangle(xPos, centeredY, newWidth, newHeight)
End If
Else
' Draw floor item under cursor
Dim xPos As Integer = mouseX - SQSizeX \ 2
Dim yPos As Integer = mouseY - SQSizeY \ 2
drawRect = New Rectangle(xPos, yPos, SQSizeX, SQSizeY)
End If
e.Graphics.DrawImage(img, drawRect)
End If
End If
If isInSelectionMode AndAlso Not selectionRectangle.IsEmpty Then
Using selectionBrush As New SolidBrush(Color.FromArgb(64, Color.Blue))
e.Graphics.FillRectangle(selectionBrush, selectionRectangle)
End Using
Using selectionPen As New Pen(Color.Blue, 2) With {.DashStyle = DashStyle.Dash}
e.Graphics.DrawRectangle(selectionPen, selectionRectangle)
End Using
End If
End Sub
Private Sub Form_MouseMove(sender As Object, e As MouseEventArgs)
If isDraggingItem Then
Dim screenPos As Point = Me.PointToScreen(New Point(e.X, e.Y))
Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)
mouseX = picGameClientPoint.X
mouseY = picGameClientPoint.Y
If picGame.ClientRectangle.Contains(picGameClientPoint) Then
UpdateHighlight(mouseX, mouseY)
isMouseInsideGrid = True
Else
isMouseInsideGrid = False
End If
picGame.Invalidate()
End If
End Sub
Private Sub Form_MouseUp(sender As Object, e As MouseEventArgs)
If isDraggingItem AndAlso e.Button = MouseButtons.Left Then
isDraggingItem = False
Me.Capture = False
Cursor.Current = Cursors.Default
Dim screenPos As Point = Me.PointToScreen(New Point(e.X, e.Y))
Dim picGameClientPoint As Point = picGame.PointToClient(screenPos)
If picGame.ClientRectangle.Contains(picGameClientPoint) Then
UpdateHighlight(picGameClientPoint.X, picGameClientPoint.Y)
' Place the item if the drop location is valid
If isMouseInsideGrid Then
If highlightedWallX AndAlso draggedItemIsWall AndAlso draggedItemIsVerticalWall Then
' Place vertical wall item
verticalWallItems(highlightedX)(highlightedY) = draggedItem
verticalWalls(highlightedX)(highlightedY) = False
ElseIf highlightedWallY AndAlso draggedItemIsWall AndAlso Not draggedItemIsVerticalWall Then
' Place horizontal wall item
horizontalWallItems(highlightedX)(highlightedY) = draggedItem
horizontalWalls(highlightedX)(highlightedY) = False
ElseIf Not highlightedWallX AndAlso Not highlightedWallY AndAlso Not draggedItemIsWall Then
' Place floor item
placedItems(highlightedX)(highlightedY) = draggedItem
Else
' Invalid drop location, return item to original location
ReturnItemToOriginalLocation()
End If
Else
' Not inside grid, return item to original location
ReturnItemToOriginalLocation()
End If
Else
' Not over picGame, return item to original location
ReturnItemToOriginalLocation()
End If
draggedItem = Nothing
If isActionInProgress Then
isActionInProgress = False
redoStack.Clear()
End If
picGame.Invalidate()
End If
End Sub
Private Sub PicGame_MouseDown(sender As Object, e As MouseEventArgs) Handles picGame.MouseDown
If isInSelectionMode Then
If e.Button = MouseButtons.Left Then
isSelecting = True
selectionStartPoint = e.Location
selectionEndPoint = e.Location
selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
picGame.Invalidate()
End If
Else
If e.Button = MouseButtons.Right Then
isRightMouseDown = True
rightMouseDownPosition = e.Location
rightMouseDownTime = DateTime.Now
If Not isActionInProgress Then
isActionInProgress = True
If Not isRestoringState Then SaveStateForUndo()
End If
ElseIf e.Button = MouseButtons.Left Then
UpdateHighlight(e.X, e.Y)
Dim itemPickedUp As Boolean = False
If isMouseInsideGrid Then
If Not isActionInProgress Then
isActionInProgress = True
If Not isRestoringState Then SaveStateForUndo()
End If
' Try to pick up an item first
If highlightedWallX Then
If verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
' Pick up vertical wall item
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
ElseIf highlightedWallY Then
If horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
' Pick up horizontal wall item
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
Else
If placedItems(highlightedX)(highlightedY) IsNot Nothing Then
' Pick up floor item
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
If itemPickedUp Then
redoStack.Clear()
picGame.Invalidate()
Else
' If no item was picked up, proceed to set edge style
If rdoEdge.Checked Then
isLeftMouseDown = True
SetEdgeStyleAtMousePosition(e.X, e.Y)
Else
' Existing logic for adding walls or other elements
isLeftMouseDown = True
AddWallAtMousePosition(e.X, e.Y)
End If
End If
End If
End If
End If
End Sub
Private Sub PicGame_MouseMove(sender As Object, e As MouseEventArgs) Handles picGame.MouseMove
If isInSelectionMode Then
If isSelecting Then
selectionEndPoint = e.Location
selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
picGame.Invalidate()
End If
Else
If isDraggingItem Then
mouseX = e.X
mouseY = e.Y
UpdateHighlight(e.X, e.Y)
picGame.Invalidate()
Else
mouseX = e.X
mouseY = e.Y
UpdateHighlight(e.X, e.Y)
picGame.Invalidate()
If isRightMouseDown Then
If rdoEdge.Checked Then
UpdateHighlight(e.X, e.Y)
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
ElseIf isLeftMouseDown Then
If rdoEdge.Checked Then
UpdateHighlight(e.X, e.Y)
If isMouseInsideGrid Then
SetEdgeStyleAtMousePosition(e.X, e.Y)
End If
Else
AddWallAtMousePosition(e.X, e.Y)
End If
End If
End If
End If
End Sub
Private Sub RemoveEdgeStyleAtMousePosition(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) = False
picGame.Invalidate()
End If
End If
End Sub
Private Sub PicGame_MouseUp(sender As Object, e As MouseEventArgs) Handles picGame.MouseUp
If isInSelectionMode Then
If isSelecting AndAlso e.Button = MouseButtons.Left Then
isSelecting = False
selectionEndPoint = e.Location
selectionRectangle = GetRectangle(selectionStartPoint, selectionEndPoint)
picGame.Invalidate()
If Not selectionRectangle.IsEmpty Then
' Calculate the center point of the selection rectangle
Dim centerX As Integer = selectionRectangle.Left + selectionRectangle.Width \ 2
Dim centerY As Integer = selectionRectangle.Top + selectionRectangle.Height \ 2
Dim menuPosition As New Point(centerX, centerY)
' Show context menu at the center of the selection rectangle
ctmMenu.Show(picGame, menuPosition)
End If
End If
Else
If e.Button = MouseButtons.Left Then
If isDraggingItem Then
isDraggingItem = False
picGame.Capture = False
If isMouseInsideGrid Then
If highlightedWallX AndAlso draggedItemIsWall AndAlso draggedItemIsVerticalWall Then
verticalWallItems(highlightedX)(highlightedY) = draggedItem
verticalWalls(highlightedX)(highlightedY) = False
ElseIf highlightedWallY AndAlso draggedItemIsWall AndAlso Not draggedItemIsVerticalWall Then
horizontalWallItems(highlightedX)(highlightedY) = draggedItem
horizontalWalls(highlightedX)(highlightedY) = False
ElseIf Not highlightedWallX AndAlso Not highlightedWallY AndAlso Not draggedItemIsWall Then
placedItems(highlightedX)(highlightedY) = draggedItem
Else
ReturnItemToOriginalLocation()
End If
Else
ReturnItemToOriginalLocation()
End If
draggedItem = Nothing
If isActionInProgress Then
isActionInProgress = False
redoStack.Clear()
End If
picGame.Invalidate()
Else
isLeftMouseDown = False
If isActionInProgress Then
isActionInProgress = False
redoStack.Clear()
End If
End If
ElseIf e.Button = MouseButtons.Right Then
isRightMouseDown = False
If isActionInProgress Then
isActionInProgress = False
redoStack.Clear()
End If
If rdoEdge.Checked Then
' Remove edge style at the mouse position
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
picGame.Invalidate()
End If
End If
End If
Else
' Existing logic for right-click when not in edge mode
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
End If
End If
End Sub
Private Function IsVerticalWallConnectedToEdge(pt As Point) As Boolean
' Walls at positions x = 0 or x = SQCOUNTX are on the outer edge
' Walls at positions x = 1 or x = SQCOUNTX - 1 are connected to the cells at the edge
Return pt.X <= 1 OrElse pt.X >= SQCOUNTX - 1
End Function
Private Function IsHorizontalWallConnectedToEdge(pt As Point) As Boolean
' Walls at positions y = 0 or y = SQCOUNTY are on the outer edge
' Walls at positions y = 1 or y = SQCOUNTY - 1 are connected to the cells at the edge
Return pt.Y <= 1 OrElse pt.Y >= SQCOUNTY - 1
End Function
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
Private Sub DrawGrid(g As Graphics)
Dim xOrigin As Integer = INDENT
Dim yOrigin As Integer = INDENT
' Draw floor squares
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)
If floorsUseEdgeStyle(x)(y) Then
Using floorBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(floorBrush, floorRect)
End Using
Else
Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
g.FillRectangle(floorBrush, floorRect)
End Using
End If
Next
Next
' Draw wall intersections
For x As Integer = 0 To SQCOUNTX
For y As Integer = 0 To SQCOUNTY
Dim intersectionRect As New Rectangle(xOrigin + x * (SQSizeX + WALL_THICKNESS) - WALL_THICKNESS \ 2,
yOrigin + y * (SQSizeY + WALL_THICKNESS) - WALL_THICKNESS \ 2,
WALL_THICKNESS, WALL_THICKNESS)
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, intersectionRect)
End Using
Next
Next
' Draw vertical walls or floor
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)
If verticalWalls(x)(y) Then
' Draw wall
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
Else
' Draw floor
Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
g.FillRectangle(floorBrush, wallRect)
End Using
End If
Next
Next
' Draw horizontal walls or floor
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)
If horizontalWalls(x)(y) Then
' Draw wall
Using wallBrush As New HatchBrush(wallsHatchStyle, wallsColor1, wallsColor2)
g.FillRectangle(wallBrush, wallRect)
End Using
Else
' Draw floor
Using floorBrush As New HatchBrush(floorsHatchStyle, floorsColor1, floorsColor2)
g.FillRectangle(floorBrush, wallRect)
End Using
End If
Next
Next
' Draw items
DrawItems(g)
DrawWallItems(g)
End Sub
Private Sub DrawItems(g As Graphics)
For x As Integer = 0 To placedItems.Length - 1
If placedItems(x) IsNot Nothing Then
For y As Integer = 0 To placedItems(x).Length - 1
If placedItems(x)(y) IsNot Nothing Then
Dim img As Image = GetImageByKey(placedItems(x)(y).ImageKey)
If img IsNot Nothing Then
ImageAnimator.UpdateFrames(img)
Dim xPos As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim yPos As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + WALL_THICKNESS \ 2
Dim destRect As New Rectangle(xPos, yPos, SQSizeX, SQSizeY)
' Draw the item with preserved transparency
Dim imgAttributes As New Imaging.ImageAttributes()
imgAttributes.SetWrapMode(WrapMode.TileFlipXY)
g.DrawImage(img, destRect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes)
End If
End If
Next
End If
Next
End Sub
Private Sub DrawWallItems(g As Graphics)
' Draw vertical wall items
For x As Integer = 0 To verticalWallItems.Length - 1
If verticalWallItems(x) IsNot Nothing Then
For y As Integer = 0 To verticalWallItems(x).Length - 1
If verticalWallItems(x)(y) IsNot Nothing Then
Dim img As Image = GetImageByKey(verticalWallItems(x)(y).ImageKey)
If img IsNot Nothing Then
ImageAnimator.UpdateFrames(img)
Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
Dim wallWidth As Integer = WALL_THICKNESS
Dim wallHeight As Integer = SQSizeY
' Keep original image width, stretch height
Dim newWidth As Integer = img.Width
Dim newHeight As Integer = wallHeight
' Center horizontally
Dim centeredX As Integer = wallX + (wallWidth - newWidth) \ 2
Dim drawRect As New Rectangle(centeredX, wallY, newWidth, newHeight)
g.DrawImage(img, drawRect)
End If
End If
Next
End If
Next
' Draw horizontal wall items
For x As Integer = 0 To horizontalWallItems.Length - 1
If horizontalWallItems(x) IsNot Nothing Then
For y As Integer = 0 To horizontalWallItems(x).Length - 1
If horizontalWallItems(x)(y) IsNot Nothing Then
Dim img As Image = GetImageByKey(horizontalWallItems(x)(y).ImageKey)
If img IsNot Nothing Then
ImageAnimator.UpdateFrames(img)
Dim wallX As Integer = INDENT + x * (SQSizeX + WALL_THICKNESS) + (WALL_THICKNESS \ 2)
Dim wallY As Integer = INDENT + y * (SQSizeY + WALL_THICKNESS) - (WALL_THICKNESS \ 2)
Dim wallWidth As Integer = SQSizeX
Dim wallHeight As Integer = WALL_THICKNESS
' Stretch width, keep original image height
Dim newWidth As Integer = wallWidth
Dim newHeight As Integer = img.Height
' Center vertically
Dim centeredY As Integer = wallY + (wallHeight - newHeight) \ 2
Dim drawRect As New Rectangle(wallX, centeredY, newWidth, newHeight)
g.DrawImage(img, drawRect)
End If
End If
Next
End If
Next
End Sub
Private Sub DrawHighlight(g As Graphics)
If Not isMouseInsideGrid Then Return
Using highlightPen As New Pen(highlightColor, 2) With {.DashStyle = DashStyle.Dash}
If highlightedWallX Then
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 rect As New Rectangle(x, y, WALL_THICKNESS, SQSizeY)
g.DrawRectangle(highlightPen, rect)
ElseIf highlightedWallY Then
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 rect As New Rectangle(x, y, SQSizeX, WALL_THICKNESS)
g.DrawRectangle(highlightPen, rect)
Else
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 rect As New Rectangle(x, y, SQSizeX, SQSizeY)
g.DrawRectangle(highlightPen, rect)
End If
End Using
End Sub
Private Sub UpdateHighlight(mouseX As Integer, mouseY As Integer)
mouseX -= INDENT
mouseY -= INDENT
isMouseInsideGrid = False
If mouseX >= 0 AndAlso mouseY >= 0 Then
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 AndAlso cellY < SQCOUNTY Then
Dim offsetX As Integer = mouseX Mod cellSizeX
Dim offsetY As Integer = mouseY Mod cellSizeY
highlightedWallX = offsetX < WALL_THICKNESS
highlightedWallY = offsetY < WALL_THICKNESS
highlightedX = cellX
highlightedY = cellY
isMouseInsideGrid = True
End If
End If
End Sub
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 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
' Place walls around the cell
' Left vertical wall at (highlightedX, highlightedY)
If highlightedX >= 0 AndAlso highlightedX < verticalWalls.Length Then
' Only place the wall if there isn't a wall item there
If verticalWallItems(highlightedX)(highlightedY) Is Nothing Then
verticalWalls(highlightedX)(highlightedY) = True
End If
End If
' Right vertical wall at (highlightedX + 1, highlightedY)
If highlightedX + 1 >= 0 AndAlso highlightedX + 1 < verticalWalls.Length Then
If verticalWallItems(highlightedX + 1)(highlightedY) Is Nothing Then
verticalWalls(highlightedX + 1)(highlightedY) = True
End If
End If
' Top horizontal wall at (highlightedX, highlightedY)
If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso highlightedY >= 0 AndAlso highlightedY < horizontalWalls(0).Length Then
If horizontalWallItems(highlightedX)(highlightedY) Is Nothing Then
horizontalWalls(highlightedX)(highlightedY) = True
End If
End If
' Bottom horizontal wall at (highlightedX, highlightedY + 1)
If highlightedX >= 0 AndAlso highlightedX < horizontalWalls.Length AndAlso highlightedY + 1 >= 0 AndAlso highlightedY + 1 < horizontalWalls(0).Length Then
If horizontalWallItems(highlightedX)(highlightedY + 1) Is Nothing Then
horizontalWalls(highlightedX)(highlightedY + 1) = True
End If
End If
picGame.Invalidate()
End If
End If
End Sub
Private Sub SaveStateForUndo()
If isRestoringState Then Return
undoStack.Push(New RoomState(Me))
If undoStack.Count > 50 Then
undoStack = New Stack(Of RoomState)(undoStack.Reverse().Take(50))
End If
End Sub
Private Sub SaveStateForRedo()
If isRestoringState Then Return
redoStack.Push(New RoomState(Me))
End Sub
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 RandomizeWalls()
wallsColor1 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
wallsColor2 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
Dim hatchStyles As Array = [Enum].GetValues(GetType(HatchStyle))
wallsHatchStyle = CType(hatchStyles.GetValue(rand.Next(hatchStyles.Length)), HatchStyle)
' Update the HatchStyle list selection if Walls radio button is checked
UpdateSelections()
End Sub
Private Sub RandomizeFloors()
floorsColor1 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
floorsColor2 = Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256))
Dim hatchStyles As Array = [Enum].GetValues(GetType(HatchStyle))
floorsHatchStyle = CType(hatchStyles.GetValue(rand.Next(hatchStyles.Length)), HatchStyle)
' Update the HatchStyle list selection if Floors radio button is checked
UpdateSelections()
End Sub
Private Sub UpdateSelections()
' Update Hatch Style Selection
If rdoWalls.Checked Then
lstHatchstyles.SelectedItem = wallsHatchStyle.ToString()
ElseIf rdoFloors.Checked Then
lstHatchstyles.SelectedItem = floorsHatchStyle.ToString()
ElseIf rdoEdge.Checked Then
lstHatchstyles.SelectedItem = wallsHatchStyle.ToString()
End If
' Update Music Selection
If Not String.IsNullOrEmpty(SelectedMusic) Then
If cboMusic.Items.Contains(SelectedMusic) Then
cboMusic.SelectedItem = SelectedMusic
Else
MessageBox.Show("The music file '" & SelectedMusic & "' is not available.", "Music Not Found", MessageBoxButtons.OK, MessageBoxIcon.Warning)
cboMusic.SelectedIndex = -1
End If
Else
cboMusic.SelectedIndex = -1 ' No selection
End If
End Sub
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
Private Sub ColorPictureBox_Click(sender As Object, e As EventArgs)
If Not isRestoringState Then SaveStateForUndo()
Dim pb As PictureBox = CType(sender, PictureBox)
Dim selectedColor As Color = pb.BackColor
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
' Update Color1
If rdoFloors.Checked Then
floorsColor1 = selectedColor
ElseIf rdoWalls.Checked Then
wallsColor1 = selectedColor
ElseIf rdoEdge.Checked Then
wallsColor1 = selectedColor ' For edge, use wall's color
End If
ElseIf num >= 51 AndAlso num <= 98 Then
' Update Color2
If rdoFloors.Checked Then
floorsColor2 = selectedColor
ElseIf rdoWalls.Checked Then
wallsColor2 = selectedColor
ElseIf rdoEdge.Checked Then
wallsColor2 = selectedColor ' For edge, use wall's color
End If
End If
End If
End If
picGame.Invalidate()
End Sub
Private Sub PicBlock_MouseDown(sender As Object, e As MouseEventArgs)
Dim pic As PictureBox = CType(sender, PictureBox)
isDraggingItem = True
isActionInProgress = True
If Not isRestoringState Then SaveStateForUndo()
' Create a new PlacedItem with the ImageKey
draggedItem = New PlacedItem() With {.ImageKey = 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
Me.Capture = True
Cursor.Current = Cursors.Hand
picGame.Invalidate()
End Sub
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 GetImageByKey(imageKey As String) As Image
Select Case imageKey
Case "picBlock1"
Return picBlock1.Image
Case "picBaddy1"
Return picBaddy1.Image
Case "picBaddy2"
Return picBaddy2.Image
Case "picLazer1"
Return picLazer1.Image
Case "picLazer2"
Return picLazer2.Image
Case "picFlipper1"
Return picFlipper1.Image
Case "picFlipper2"
Return picFlipper2.Image
Case "picFlipper3"
Return picFlipper3.Image
Case "picFlipper4"
Return picFlipper4.Image
Case "picBaddy3"
Return picBaddy3.Image
Case "picBaddy4"
Return picBaddy4.Image
Case "picCoin"
Return picCoin.Image
Case "picGate"
Return picGate.Image
Case "picKey"
Return picKey.Image
Case "picStart"
Return picStart.Image
Case "picFinish"
Return picFinish.Image
Case "picBlockStopper1"
Return picBlockStopper1.Image
Case "picBall"
Return picBall.Image
Case "picBlockSwitch"
Return picBlockSwitch.Image
Case Else
Return Nothing
End Select
End Function
Private Function IsVerticalItem(imgKey As String) As Boolean
Return imgKey = "picLazer1" OrElse imgKey = "picFlipper1" OrElse imgKey = "picFlipper2"
End Function
Private Function IsHorizontalItem(imgKey As String) As Boolean
Return imgKey = "picLazer2" OrElse imgKey = "picFlipper3" OrElse imgKey = "picFlipper4"
End Function
Private Sub AddWallAtMousePosition(mouseX As Integer, mouseY As Integer)
UpdateHighlight(mouseX, mouseY)
If highlightedWallX Then
If verticalWallItems(highlightedX)(highlightedY) Is Nothing AndAlso
highlightedX > 0 AndAlso highlightedX < SQCOUNTX Then
verticalWalls(highlightedX)(highlightedY) = True
picGame.Invalidate()
End If
ElseIf highlightedWallY Then
If horizontalWallItems(highlightedX)(highlightedY) Is Nothing AndAlso
highlightedY > 0 AndAlso highlightedY < SQCOUNTY Then
horizontalWalls(highlightedX)(highlightedY) = True
picGame.Invalidate()
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 highlightedWallX Then
If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
verticalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) AndAlso Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
verticalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
ElseIf highlightedWallY Then
If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
horizontalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) AndAlso Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
horizontalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
Else
If removeItems AndAlso placedItems(highlightedX)(highlightedY) IsNot Nothing Then
placedItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
End If
End If
End Sub
Private Sub SaveRoomToFile(filePath As String)
Try
Dim roomState As New RoomState(Me)
Dim options As New JsonSerializerOptions With {
.WriteIndented = True,
.IncludeFields = True
}
Dim jsonString As String = JsonSerializer.Serialize(roomState, options)
File.WriteAllText(filePath, jsonString)
Catch ex As Exception
MessageBox.Show("Error saving room: " & ex.Message)
End Try
End Sub
Private Sub LoadRoomFromFile(filePath As String)
Try
If Not File.Exists(filePath) Then Return
Dim options As New JsonSerializerOptions With {
.IncludeFields = True
}
Dim jsonString As String = File.ReadAllText(filePath)
Dim roomState As RoomState = JsonSerializer.Deserialize(Of RoomState)(jsonString, options)
' Ensure music is loaded before restoring state
LoadMusic()
roomState.RestoreState(Me)
' Update selections
UpdateSelections()
picGame.Invalidate()
Catch ex As Exception
MessageBox.Show("Error loading room: " & ex.Message)
End Try
End Sub
Private Sub LoadSettingsFromFile(filePath As String)
Try
If Not File.Exists(filePath) Then Return
Dim lines As String() = File.ReadAllLines(filePath)
Dim section As String = ""
For Each line As String In lines
line = line.Trim()
If String.IsNullOrEmpty(line) OrElse line.StartsWith(";") Then Continue For
If line.StartsWith("[") AndAlso line.EndsWith("]") Then
section = line.Substring(1, line.Length - 2)
Else
Dim parts() As String = line.Split(New Char() {":"c}, 2)
If parts.Length = 2 Then
LoadProperty(parts(0).Trim(), parts(1).Trim(), section)
End If
End If
Next
' Update lstHatchstyles selection after loading settings
UpdateSelections()
Catch ex As Exception
MessageBox.Show("Error loading settings: " & ex.Message)
End Try
End Sub
Private Sub LoadProperty(key As String, value As String, section As String)
Dim isWall As Boolean = (section = "Walls")
Select Case key
Case "HatchStyle"
Dim hatchStyle As HatchStyle
If [Enum].TryParse(value, hatchStyle) Then
If isWall Then wallsHatchStyle = hatchStyle Else floorsHatchStyle = hatchStyle
End If
Case "Color1"
Dim color As Color = ParseColor(value)
If isWall Then wallsColor1 = color Else floorsColor1 = color
Case "Color2"
Dim color As Color = ParseColor(value)
If isWall Then wallsColor2 = color Else floorsColor2 = color
End Select
End Sub
Private Function ParseColor(colorString As String) As Color
Dim parts() As String = colorString.Split(","c)
If parts.Length = 4 Then
Dim a, r, g, b As Integer
If Integer.TryParse(parts(0), a) AndAlso Integer.TryParse(parts(1), r) AndAlso
Integer.TryParse(parts(2), g) AndAlso Integer.TryParse(parts(3), b) Then
Return Color.FromArgb(a, r, g, b)
End If
End If
Return Color.Black
End Function
Private Sub SaveSettingsToFile(filePath As String)
Try
Using sw As New StreamWriter(filePath)
sw.WriteLine("[Walls]")
sw.WriteLine("HatchStyle: " & wallsHatchStyle.ToString())
sw.WriteLine("Color1: " & ColorToString(wallsColor1))
sw.WriteLine("Color2: " & ColorToString(wallsColor2))
sw.WriteLine()
sw.WriteLine("[Floors]")
sw.WriteLine("HatchStyle: " & floorsHatchStyle.ToString())
sw.WriteLine("Color1: " & ColorToString(floorsColor1))
sw.WriteLine("Color2: " & ColorToString(floorsColor2))
End Using
Catch ex As Exception
MessageBox.Show("Error saving settings: " & ex.Message)
End Try
End Sub
Private Function ColorToString(color As Color) As String
Return $"{color.A},{color.R},{color.G},{color.B}"
End Function
Hello! I'd be happy to help you implement these changes.
To achieve the desired behavior, we'll need to modify the code in a few places to ensure that walls connected to an edge cannot be removed unless the edge is removed first. Here are the steps we'll follow:
- Prevent walls connected to edges from being removed when right-click dragging over walls.
- Prevent walls connected to edges from being removed when clearing walls in a selected area.
- Ensure that when an edge is placed, it is impossible to remove the walls around it unless the edge is removed first.
Let's go through each requirement one by one.
1. Prevent Walls Connected to Edges from Being Removed When Right-Click Dragging Over Walls
When rdoWalls is selected and the user right-clicks and drags over walls, we need to check if the wall being removed is connected to an edge (i.e., if there is an edge style applied to any adjacent floor cells). If it is connected to an edge, we should not remove the wall.
Modifications Needed:
In the method RemoveObjectAtMousePosition, where we handle the removal of walls, we'll add a check to determine if the wall is connected to an edge. If it is, we won't remove it.
Implementation Steps:
-
Identify the Code Where Walls are Removed
In your RemoveObjectAtMousePosition method, you handle the removal of walls. Here's the relevant code snippet:
If highlightedWallX Then
If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
' ... (remove wall item)
ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) AndAlso Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
verticalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
ElseIf highlightedWallY Then
If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
' ... (remove wall item)
ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) AndAlso Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
horizontalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
End If
-
Modify RemoveObjectAtMousePosition to Include Edge Checks
We'll add checks to see if the walls are connected to edges by modifying the IsVerticalWallConnectedToEdge and IsHorizontalWallConnectedToEdge methods.
-
Implement Edge Checks
We'll create methods to check if a wall is adjacent to an edge (i.e., if any of the adjacent floor cells have floorsUseEdgeStyle set to True).
For Vertical Walls:
Private Function IsVerticalWallConnectedToEdge(pt As Point) As Boolean
' Vertical walls are between cells (x-1, y) and (x, y)
Dim x As Integer = pt.X
Dim y As Integer = pt.Y
' Check if adjacent cells have edge style applied
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
For Horizontal Walls:
Private Function IsHorizontalWallConnectedToEdge(pt As Point) As Boolean
' Horizontal walls are between cells (x, y-1) and (x, y)
Dim x As Integer = pt.X
Dim y As Integer = pt.Y
' Check if adjacent cells have edge style applied
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
-
Update Removal Conditions
In RemoveObjectAtMousePosition, we'll use these methods to check if the wall is connected to an edge before removing it.
If highlightedWallX Then
If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
verticalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) Then
If Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
verticalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
End If
ElseIf highlightedWallY Then
If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
horizontalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) Then
If Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
horizontalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
End If
End If
This ensures that walls connected to edges are not removed when right-click dragging over walls.
2. Prevent Walls Connected to Edges from Being Removed When Clearing Walls in a Selected Area
When the user selects an area and chooses "Clear Walls," we need to ensure that walls connected to edges are not removed.
Modifications Needed:
In the methods mnuClearWalls_Click and mnuFillWalls_Click (if necessary), we'll modify the code to check if the walls being removed are connected to edges and skip removing them if they are.
Implementation Steps:
-
Modify mnuClearWalls_Click
In your mnuClearWalls_Click method, you loop through the walls in the selected rectangle and remove them. We'll add checks here.
Private Sub mnuClearWalls_Click(sender As Object, e As EventArgs) Handles mnuClearWalls.Click
If Not selectionRectangle.IsEmpty Then
If Not isRestoringState Then SaveStateForUndo()
For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
' Check if the wall is not connected to an edge
If Not IsVerticalWallConnectedToEdge(pt) Then
' Check if there is no edge around it
verticalWalls(pt.X)(pt.Y) = False
verticalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
' Check if the wall is not connected to an edge
If Not IsHorizontalWallConnectedToEdge(pt) Then
horizontalWalls(pt.X)(pt.Y) = False
horizontalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
selectionRectangle = Rectangle.Empty
isInSelectionMode = False
Cursor = Cursors.Default
picGame.Invalidate()
End If
End Sub
This modification ensures that walls connected to edges are not removed when clearing walls in a selected area.
-
Modify mnuFillWalls_Click (Optional)
If you want to prevent walls from being added over edges when filling walls, you might want to modify mnuFillWalls_Click. However, since your requirement is about removing walls, this may not be necessary.
3. Ensure That When an Edge is Placed, It is Impossible to Remove the Walls Around It Unless the Edge is Removed
We need to ensure that any time an edge is placed (floorsUseEdgeStyle(x)(y) = True), it is impossible to remove the walls around it unless the edge is removed first.
Modifications Needed:
Anywhere walls can be removed, we need to ensure that walls connected to edges are not removed unless the edge is removed first. We've already handled this in the previous steps, but we'll double-check the code to ensure no other places need modification.
Implementation Steps:
-
Verify Wall Removal Methods
- Right-Click Drag Removal: Already handled in step 1.
- Selection Clearing: Already handled in step 2.
- Any Other Wall Removal Logic: Review the code to see if there are other places where walls are removed.
-
Check Edge Removal Logic
When an edge is removed, we need to allow the walls around it to be removed again.
For edge removal, the user either sets floorsUseEdgeStyle(x)(y) = False or uses a specific tool to remove edges. We need to ensure that after an edge is removed, the walls around it can be removed.
Since the checks in IsVerticalWallConnectedToEdge and IsHorizontalWallConnectedToEdge rely on the floorsUseEdgeStyle array, once an edge is removed, the walls will no longer be considered connected to an edge and can be removed.
-
Ensure That Walls Cannot be Removed When an Edge is Present
Our previous modifications should suffice to prevent walls connected to edges from being removed unless the edge is removed.
Summary of Code Changes
-
Added Methods to Check if Walls are Connected to Edges
Private Function IsVerticalWallConnectedToEdge(pt As Point) As Boolean
Dim x As Integer = pt.X
Dim y As Integer = pt.Y
' Check adjacent floor cells
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 IsHorizontalWallConnectedToEdge(pt As Point) As Boolean
Dim x As Integer = pt.X
Dim y As Integer = pt.Y
' Check adjacent floor cells
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
-
Modified Wall Removal Logic in RemoveObjectAtMousePosition
If highlightedWallX Then
If removeItems AndAlso verticalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
verticalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso verticalWalls(highlightedX)(highlightedY) Then
If Not IsVerticalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
verticalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
End If
ElseIf highlightedWallY Then
If removeItems AndAlso horizontalWallItems(highlightedX)(highlightedY) IsNot Nothing Then
horizontalWallItems(highlightedX)(highlightedY) = Nothing
picGame.Invalidate()
ElseIf removeWalls AndAlso horizontalWalls(highlightedX)(highlightedY) Then
If Not IsHorizontalWallConnectedToEdge(New Point(highlightedX, highlightedY)) Then
horizontalWalls(highlightedX)(highlightedY) = False
picGame.Invalidate()
End If
End If
End If
-
Modified Wall Clearing Logic in mnuClearWalls_Click
For Each pt As Point In GetVerticalWallsInRectangle(selectionRectangle)
' Check if the wall is not connected to an edge
If Not IsVerticalWallConnectedToEdge(pt) Then
verticalWalls(pt.X)(pt.Y) = False
verticalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
For Each pt As Point In GetHorizontalWallsInRectangle(selectionRectangle)
' Check if the wall is not connected to an edge
If Not IsHorizontalWallConnectedToEdge(pt) Then
horizontalWalls(pt.X)(pt.Y) = False
horizontalWallItems(pt.X)(pt.Y) = Nothing
End If
Next
-
Ensured Consistency Throughout the Code
By adding checks in the methods where walls can be removed, we ensure that walls connected to edges cannot be removed unless the edge is removed first.
Testing the Changes
After implementing these changes, you should test the following scenarios:
-
Right-Click Drag Removal:
- Place an edge on a floor cell.
- Try to remove the walls around it by right-click dragging with
rdoWalls selected.
- The walls should not be removed.
- Remove the edge.
- Try removing the walls again.
- The walls should now be removable.
-
Selection Clearing:
- Place edges on multiple floor cells within a selection.
- Use
btnSelect to select the area.
- Choose "Clear Walls."
- Walls connected to edges should not be removed.
- Remove the edges.
- Try clearing walls again.
- The walls should now be removable.
-
Edge Placement and Wall Removal:
- Place an edge on a floor cell.
- Verify that the walls around it cannot be removed.
- Ensure that walls not connected to edges can still be removed normally.
Conclusion
By implementing these changes, we ensure that walls connected to edges are protected from being removed unless the edge is removed first. This maintains the integrity of edge-styled areas in your design tool.
If you have any questions or need further assistance, feel free to ask!