From 781410ea049752480b5cd29f97389cee553009c0 Mon Sep 17 00:00:00 2001 From: Joshua Fan Date: Thu, 25 Feb 2021 06:43:40 -0700 Subject: [PATCH] Double-tap to mine, tap anywhere to cancel (#4469) * Double-tap to mine, tap anywhere to cancel * Make comment consistent * Remove desktop left-click mining cancel, prioritize mobile unit control over mining * Mobile: double-tap doesn't configure blocks if unit was double-tapped; control unit detected in first tap of double-tap * Add 'double-tap to mine' setting (default off) * Desktop: cancel mining when mined tile is clicked * Comment typo * Prevent redundant condition check * Cleanup Co-authored-by: Anuken --- core/assets/bundles/bundle.properties | 1 + core/src/mindustry/input/DesktopInput.java | 12 ++++++++-- core/src/mindustry/input/InputHandler.java | 17 +++++++++++++ core/src/mindustry/input/MobileInput.java | 24 ++++++++++++------- .../ui/dialogs/SettingsMenuDialog.java | 2 ++ 5 files changed, 45 insertions(+), 11 deletions(-) diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 40a3e10fb4..c76e21001d 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -800,6 +800,7 @@ setting.logichints.name = Logic Hints setting.flow.name = Display Resource Flow Rate setting.backgroundpause.name = Pause In Background setting.buildautopause.name = Auto-Pause Building +setting.doubletapmine.name = Double-Tap to Mine setting.modcrashdisable.name = Disable Mods On Startup Crash setting.animatedwater.name = Animated Surfaces setting.animatedshields.name = Animated Shields diff --git a/core/src/mindustry/input/DesktopInput.java b/core/src/mindustry/input/DesktopInput.java index 3de5997ec4..4b7143b22f 100644 --- a/core/src/mindustry/input/DesktopInput.java +++ b/core/src/mindustry/input/DesktopInput.java @@ -44,6 +44,10 @@ public class DesktopInput extends InputHandler{ public boolean deleting = false, shouldShoot = false, panning = false; /** Mouse pan speed. */ public float panScale = 0.005f, panSpeed = 4.5f, panBoostSpeed = 11f; + /** Delta time between consecutive clicks. */ + public long selectMillis = 0; + /** Previously selected tile. */ + public Tile prevSelected; boolean showHint(){ return ui.hudfrag.shown && Core.settings.getBool("hints") && selectRequests.isEmpty() && @@ -487,15 +491,19 @@ public class DesktopInput extends InputHandler{ sreq = req; }else if(req != null && req.breaking){ deleting = true; + }else if(Core.settings.getBool("doubletapmine") && selected == prevSelected && Time.timeSinceMillis(selectMillis) < 500){ + tryBeginMine(selected); }else if(selected != null){ //only begin shooting if there's no cursor event - if(!tryTapPlayer(Core.input.mouseWorld().x, Core.input.mouseWorld().y) && !tileTapped(selected.build) && !player.unit().activelyBuilding() && !droppingItem && - !tryBeginMine(selected) && player.unit().mineTile == null && !Core.scene.hasKeyboard()){ + if(!tryTapPlayer(Core.input.mouseWorld().x, Core.input.mouseWorld().y) && !tileTapped(selected.build) && !player.unit().activelyBuilding() && !droppingItem + && (Core.settings.getBool("doubletapmine") ? !tryStopMine(selected) : !tryBeginMine(selected)) && !Core.scene.hasKeyboard()){ player.shooting = shouldShoot; } }else if(!Core.scene.hasKeyboard()){ //if it's out of bounds, shooting is just fine player.shooting = shouldShoot; } + selectMillis = Time.millis(); + prevSelected = selected; }else if(Core.input.keyTap(Binding.deselect) && isPlacing()){ block = null; mode = none; diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index c8ab2b4f21..fe082d5468 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -955,6 +955,23 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ return false; } + /** Tries to stop mining, returns true if mining was stopped. */ + boolean tryStopMine(){ + if(player.unit().mining()){ + player.unit().mineTile = null; + return true; + } + return false; + } + + boolean tryStopMine(Tile tile){ + if(player.unit().mineTile == tile){ + player.unit().mineTile = null; + return true; + } + return false; + } + boolean canMine(Tile tile){ return !Core.scene.hasMouse() && tile.drop() != null diff --git a/core/src/mindustry/input/MobileInput.java b/core/src/mindustry/input/MobileInput.java index c05d709fa9..1624beedef 100644 --- a/core/src/mindustry/input/MobileInput.java +++ b/core/src/mindustry/input/MobileInput.java @@ -71,6 +71,8 @@ public class MobileInput extends InputHandler implements GestureListener{ public Teamc target; /** Payload target being moved to. Can be a position (for dropping), or a unit/block. */ public Position payloadTarget; + /** Unit last tapped, or null if last tap was not on a unit. */ + public Unit unitTapped; //region utility methods @@ -599,11 +601,7 @@ public class MobileInput extends InputHandler implements GestureListener{ //add to selection queue if it's a valid BREAK position selectRequests.add(new BuildPlan(linked.x, linked.y)); }else{ - if(!canTapPlayer(worldx, worldy) && !tileTapped(linked.build)){ - tryBeginMine(cursor); - } - - //control units. + //control units if(count == 2){ //reset payload target payloadTarget = null; @@ -611,12 +609,20 @@ public class MobileInput extends InputHandler implements GestureListener{ if(!player.dead() && Mathf.within(worldx, worldy, player.unit().x, player.unit().y, player.unit().hitSize * 0.6f + 8f) && player.unit().type.commandLimit > 0){ Call.unitCommand(player); }else{ - //control a unit/block - Unit on = selectedUnit(); - if(on != null){ - Call.unitControl(player, on); + //control a unit/block detected on first tap of double-tap + if(unitTapped != null){ + Call.unitControl(player, unitTapped); + }else if(!tryBeginMine(cursor)){ + tileTapped(linked.build); } } + return false; + } + + unitTapped = selectedUnit(); + //prevent mining if placing/breaking blocks + if(!tryStopMine() && !canTapPlayer(worldx, worldy) && !tileTapped(linked.build) && mode == none && !Core.settings.getBool("doubletapmine")){ + tryBeginMine(cursor); } } diff --git a/core/src/mindustry/ui/dialogs/SettingsMenuDialog.java b/core/src/mindustry/ui/dialogs/SettingsMenuDialog.java index 60acd1637b..5290b6c5aa 100644 --- a/core/src/mindustry/ui/dialogs/SettingsMenuDialog.java +++ b/core/src/mindustry/ui/dialogs/SettingsMenuDialog.java @@ -327,6 +327,8 @@ public class SettingsMenuDialog extends SettingsDialog{ game.checkPref("buildautopause", false); } + game.checkPref("doubletapmine", false); + if(!ios){ game.checkPref("modcrashdisable", true); }