now it draws a line between the old and new mouse positions so drawn paths are continuous

This commit is contained in:
2026-05-14 06:55:31 -05:00
parent 48f889bd44
commit 5b4c246b4f
+49 -12
View File
@@ -83,6 +83,10 @@ func main() {
cellSize := float32(25) cellSize := float32(25)
lastMousePos := rl.NewVector2(-1, -1)
startPos := rl.NewVector2(-1, -1)
endPos := rl.NewVector2(-1, -1)
mapImage := rl.GenImageColor(width, height, rl.NewColor(240, 240, 240, 255)) mapImage := rl.GenImageColor(width, height, rl.NewColor(240, 240, 240, 255))
mapTexture := rl.LoadTextureFromImage(mapImage) mapTexture := rl.LoadTextureFromImage(mapImage)
defer rl.UnloadTexture(mapTexture) defer rl.UnloadTexture(mapTexture)
@@ -130,22 +134,55 @@ func main() {
// Paint logic // Paint logic
if rl.IsMouseButtonDown(rl.MouseLeftButton) { if rl.IsMouseButtonDown(rl.MouseLeftButton) {
worldPos := rl.GetScreenToWorld2D(rl.GetMousePosition(), camera) mousePos := rl.GetMousePosition()
x := int(worldPos.X / cellSize) if int(mousePos.X) < int(canvasWidth) {
y := int(worldPos.Y / cellSize) worldPos := rl.GetScreenToWorld2D(rl.GetMousePosition(), camera)
if x >= 0 && x < width && y >= 0 && y < height { x := int(worldPos.X / cellSize)
switch activeTool { y := int(worldPos.Y / cellSize)
case 0: // Wall if x >= 0 && x < width && y >= 0 && y < height {
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 0, 0, 255)) // mapImage is one pixel per cell; drawing uses cell indices, not raw world coords.
case 1: // Start switch activeTool {
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 255, 0, 255)) case 0: // Wall — need previous cell for line segment
case 2: // End if lastMousePos.X != -1 && lastMousePos.Y != -1 {
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(255, 0, 0, 255)) prevX := int(lastMousePos.X / cellSize)
prevY := int(lastMousePos.Y / cellSize)
rl.ImageDrawLine(mapImage, int32(prevX), int32(prevY), int32(x), int32(y), rl.NewColor(0, 0, 0, 255))
newTex := rl.LoadTextureFromImage(mapImage)
rl.UnloadTexture(mapTexture)
mapTexture = newTex
}
case 1: // Start — must run on first paint frame (lastMousePos may be -1 after release)
if int(startPos.X) != x || int(startPos.Y) != y {
if startPos.X >= 0 && startPos.Y >= 0 {
rl.ImageDrawPixel(mapImage, int32(startPos.X), int32(startPos.Y), rl.NewColor(240, 240, 240, 255))
}
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(0, 255, 0, 255))
startPos = rl.NewVector2(float32(x), float32(y))
newTex := rl.LoadTextureFromImage(mapImage)
rl.UnloadTexture(mapTexture)
mapTexture = newTex
}
case 2: // End
if int(endPos.X) != x || int(endPos.Y) != y {
if endPos.X >= 0 && endPos.Y >= 0 {
rl.ImageDrawPixel(mapImage, int32(endPos.X), int32(endPos.Y), rl.NewColor(240, 240, 240, 255))
}
rl.ImageDrawPixel(mapImage, int32(x), int32(y), rl.NewColor(255, 0, 0, 255))
endPos = rl.NewVector2(float32(x), float32(y))
newTex := rl.LoadTextureFromImage(mapImage)
rl.UnloadTexture(mapTexture)
mapTexture = newTex
}
}
lastMousePos = worldPos
} }
mapTexture = rl.LoadTextureFromImage(mapImage)
} }
} }
if rl.IsMouseButtonReleased(rl.MouseLeftButton) {
lastMousePos = rl.NewVector2(-1, -1)
}
// --- DRAWING --- // --- DRAWING ---
rl.BeginDrawing() rl.BeginDrawing()
rl.ClearBackground(rl.White) rl.ClearBackground(rl.White)