From 0d7733f5f79eff3bc3c8e553f70220781ebc60f5 Mon Sep 17 00:00:00 2001 From: sharlottes Date: Sat, 23 Apr 2022 14:02:33 +0900 Subject: [PATCH] fix brush offset --- src/unitinfo/ui/windows/MapEditorDisplay.java | 74 +++++++++---------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/src/unitinfo/ui/windows/MapEditorDisplay.java b/src/unitinfo/ui/windows/MapEditorDisplay.java index f874685..bccd286 100644 --- a/src/unitinfo/ui/windows/MapEditorDisplay.java +++ b/src/unitinfo/ui/windows/MapEditorDisplay.java @@ -3,7 +3,12 @@ package unitinfo.ui.windows; import arc.Events; import arc.graphics.g2d.Draw; import arc.graphics.g2d.Lines; +import arc.math.geom.Bresenham2; import arc.math.geom.Geometry; +import arc.math.geom.Point2; +import arc.scene.Element; +import arc.scene.event.InputEvent; +import arc.scene.event.InputListener; import mindustry.editor.MapEditor; import mindustry.game.EventType; import mindustry.graphics.Layer; @@ -39,8 +44,9 @@ public class MapEditorDisplay extends Window implements Updatable { final Vec2[][] brushPolygons = new Vec2[MapEditor.brushSizes.length][0]; float heat; float brushSize = -1; - boolean hold = false; - int pastX, pastY; + + boolean drawing; + int lastx, lasty; public static Team drawTeam = Team.sharded; public static Block drawBlock = Blocks.router; @@ -71,8 +77,9 @@ public class MapEditorDisplay extends Window implements Updatable { Draw.reset(); } + Tile tile = world.tileWorld(Core.input.mouseWorldX(), Core.input.mouseWorldY()); + if(tile == null || tool == null || brushSize < 1) return; - if(tool==null) return; int index = 0; for(int i = 0; i < MapEditor.brushSizes.length; i++){ if(brushSize == MapEditor.brushSizes[i]){ @@ -83,31 +90,24 @@ public class MapEditorDisplay extends Window implements Updatable { Lines.stroke(Scl.scl(2f), Pal.accent); if((!drawBlock.isMultiblock() || tool == EditorTool.eraser) && tool != EditorTool.fill){ - if(tool == EditorTool.line && hold){ - Vec2 v = Core.input.mouseWorld(); - Lines.poly(brushPolygons[index], pastX, pastY, scaling); - float vx = Mathf.floor(v.x/8)*8-4, vy = Mathf.floor(v.y/8)*8-4; - Lines.poly(brushPolygons[index], vx, vy, scaling); + if(tool == EditorTool.line && drawing){ + Lines.poly(brushPolygons[index], lastx, lasty, scaling); + Lines.poly(brushPolygons[index], tile.x*8, tile.y*8, scaling); } - if((tool.edit || (tool == EditorTool.line && !hold)) && (!mobile || hold)){ - //pencil square outline - Vec2 v = Core.input.mouseWorld(); - float vx = Mathf.floor(v.x/8)*8-4, vy = Mathf.floor(v.y/8)*8-4; + if((tool.edit || (tool == EditorTool.line && !drawing)) && (!mobile || drawing)){ if(tool == EditorTool.pencil && tool.mode == 1){ - Lines.square(vx, vy, scaling * (brushSize + 0.5f)); + Lines.square(tile.x*8, tile.y*8, scaling * (brushSize + 0.5f)); }else{ - Lines.poly(brushPolygons[index], vx, vy, scaling); + Lines.poly(brushPolygons[index], tile.x*8-4, tile.y*8-4, scaling); } } }else{ - if((tool.edit || tool == EditorTool.line) && (!mobile || hold)){ - Vec2 v = Core.input.mouseWorld(); - float vx = Mathf.floor(v.x/8)*8-4, vy = Mathf.floor(v.y/8)*8-4; + if((tool.edit || tool == EditorTool.line) && (!mobile || drawing)){ float offset = (drawBlock.size % 2 == 0 ? scaling / 2f : 0f); Lines.square( - vx + scaling / 2f + offset, - vy + scaling / 2f + offset, + tile.x*8 + scaling / 2f + offset, + tile.y*8 + scaling / 2f + offset, scaling * drawBlock.size / 2f); } } @@ -137,29 +137,21 @@ public class MapEditorDisplay extends Window implements Updatable { heat = 0f; resetPane(); } - if(tool != null && drawBlock != null && !hasMouse()) { - if(Core.input.isTouched()) { - if(!(!mobile&&Core.input.keyDown(KeyCode.mouseLeft))) return; - if(tool== EditorTool.line) { - if(!hold) { - pastX = Mathf.round(Core.input.mouseWorldX() / 8); - pastY = Mathf.round(Core.input.mouseWorldY() / 8); - } - hold = true; - } - else { - pastX = Mathf.round(Core.input.mouseWorldX() / 8); - pastY = Mathf.round(Core.input.mouseWorldY() / 8); - } - tool.touched(pastX, pastY); - } - else if(tool== EditorTool.line) { - if(hold&&pastX>=0&&pastY>=0) tool.touchedLine(pastX, pastY, Mathf.round(Core.input.mouseWorldX() / 8), Mathf.round(Core.input.mouseWorldY() / 8)); - hold = false; - pastX = -1; - pastY = -1; - } + Tile tile = world.tileWorld(Core.input.mouseWorldX(), Core.input.mouseWorldY()); + if(tile == null || tool == null || brushSize < 1 || drawBlock == null || hasMouse()) return; + if(Core.input.isTouched()) { + if((tool == EditorTool.line && drawing) || (!mobile && !Core.input.keyDown(KeyCode.mouseLeft))) return; + drawing = true; + lastx = tile.x; + lasty = tile.y; + tool.touched(lastx, lasty); + } + else { + if(tool == EditorTool.line && drawing) tool.touchedLine(lastx, lasty, tile.x, tile.y); + drawing = false; + lastx = -1; + lasty = -1; } }