From c9ebe035fe33108e8b6dc0ff4726eebdc9bbeb2f Mon Sep 17 00:00:00 2001 From: zxtej <83355748+zxtej@users.noreply.github.com> Date: Sun, 29 Dec 2024 09:38:53 +0000 Subject: [PATCH] Codechange: Use tap/release instead of evaluating at every tick Also fixed a crash where selected could be null if mouse is dragged outside of world bounds --- core/src/mindustry/input/DesktopInput.java | 30 +++++++------------ .../ui/fragments/BlockConfigFragment.java | 8 ++--- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/core/src/mindustry/input/DesktopInput.java b/core/src/mindustry/input/DesktopInput.java index 2ff8e237e8..7c0f7e74f8 100644 --- a/core/src/mindustry/input/DesktopInput.java +++ b/core/src/mindustry/input/DesktopInput.java @@ -74,8 +74,6 @@ public class DesktopInput extends InputHandler{ /** Previously selected tile. */ public Tile prevSelected; private long lastShiftZ; - /** Whether the current selected building being dragged from*/ - private boolean configDragging = false; @Override public void buildUI(Group group){ @@ -190,7 +188,7 @@ public void drawTop(){ } } - if (configDragging && config.getSelected() != null) { + if (config.dragging) { Vec2 mouseCoords = input.mouseWorld(); Building selected = config.getSelected(); Draw.color(Pal.accent); @@ -800,22 +798,6 @@ void pollInput(){ int cursorY = tileY(Core.input.mouseY()); int rawCursorX = World.toTile(Core.input.mouseWorld().x), rawCursorY = World.toTile(Core.input.mouseWorld().y); - // Handle drag to config behaviour - if (config.selectedCanDrag) { - if (input.keyDown(Binding.select)) { - if (selected.build == config.getSelected()) configDragging = true; - } else if (configDragging) { - Building hovered = world.build(cursorX, cursorY); - if (hovered != config.getSelected()) { - if (hovered != null) { - config.getSelected().onConfigureBuildTapped(hovered); - } - config.hideConfig(); - } - configDragging = false; - } - } - //automatically pause building if the current build queue is empty if(Core.settings.getBool("buildautopause") && isBuilding && !isBuildingIgnoreNetworking()){ isBuilding = false; @@ -931,6 +913,16 @@ else if (Core.input.ctrl()) { selectUnitsRect(); } + // Handle drag to config behaviour + if(config.dragging && Core.input.keyRelease(Binding.select)){ + config.dragging = false; + Building hovered = selected == null ? null : selected.build; + if(hovered != config.getSelected()){ + if(hovered != null) config.getSelected().onConfigureBuildTapped(hovered); + config.hideConfig(); + } + } + if(Core.input.keyTap(Binding.select) && !Core.scene.hasMouse()){ tappedOne = false; BuildPlan plan = getPlan(cursorX, cursorY); diff --git a/core/src/mindustry/ui/fragments/BlockConfigFragment.java b/core/src/mindustry/ui/fragments/BlockConfigFragment.java index 651c53c1ff..bfaca5ad95 100644 --- a/core/src/mindustry/ui/fragments/BlockConfigFragment.java +++ b/core/src/mindustry/ui/fragments/BlockConfigFragment.java @@ -24,7 +24,7 @@ public class BlockConfigFragment{ ); Table table = new Table(); Building selected; - public boolean selectedCanDrag = false; + public boolean dragging = false; public void build(Group parent){ table.visible = false; @@ -36,7 +36,7 @@ public void build(Group parent){ public void forceHide(){ table.visible = false; selected = null; - selectedCanDrag = false; + dragging = false; } public boolean isShown(){ @@ -51,7 +51,7 @@ public void showConfig(Building tile){ if(selected != null) selected.onConfigureClosed(); if(tile.configTapped()){ selected = tile; - selectedCanDrag = validBuilds.contains(selected.getClass()); + dragging = validBuilds.contains(selected.getClass()); table.visible = true; table.clear(); table.background(null); // clear the background as some blocks set custom ones @@ -85,7 +85,7 @@ public boolean hasConfigMouse(){ public void hideConfig(){ if(selected != null) selected.onConfigureClosed(); selected = null; - selectedCanDrag = false; + dragging = false; table.actions(Actions.scaleTo(0f, 1f, 0.06f, Interp.pow3Out), Actions.visible(false)); } }