From d63133c7205cc56b4b91e1527487cf1591d5c193 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 23 Sep 2023 12:02:06 -0400 Subject: [PATCH] Bindings per command/stance --- core/assets/bundles/bundle.properties | 22 ++++++++++--- core/src/mindustry/ai/UnitCommand.java | 27 ++++++++++----- core/src/mindustry/ai/UnitStance.java | 19 +++++++---- core/src/mindustry/input/Binding.java | 28 ++++++++-------- .../ui/fragments/PlacementFragment.java | 33 ++++--------------- 5 files changed, 67 insertions(+), 62 deletions(-) diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index bec644f4b5..29fe75771a 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -1173,11 +1173,23 @@ keybind.command_mode.name = Command Mode keybind.command_queue.name = Queue Unit Command keybind.create_control_group.name = Create Control Group keybind.cancel_orders.name = Cancel Orders -keybind.unit_stance_1.name = Unit Stance 1 -keybind.unit_stance_2.name = Unit Stance 2 -keybind.unit_stance_3.name = Unit Stance 3 -keybind.unit_stance_4.name = Unit Stance 4 -keybind.unit_stance_5.name = Unit Stance 5 + +keybind.unit_stance_shoot.name = Unit Stance: Shoot +keybind.unit_stance_hold_fire.name = Unit Stance: Hold Fire +keybind.unit_stance_pursue_target.name = Unit Stance: Pursue Target +keybind.unit_stance_patrol.name = Unit Stance: Patrol +keybind.unit_stance_ram.name = Unit Stance: Ram + +keybind.unit_command_move = Unit Command: Move +keybind.unit_command_repair = Unit Command: Repair +keybind.unit_command_rebuild = Unit Command: Rebuild +keybind.unit_command_assist = Unit Command: Assist +keybind.unit_command_mine = Unit Command: Mine +keybind.unit_command_boost = Unit Command: Boost +keybind.unit_command_load_units = Unit Command: Load Units +keybind.unit_command_load_blocks = Unit Command: Load Blocks +keybind.unit_command_unload_payload = Unit Command: Unload Payload + keybind.rebuild_select.name = Rebuild Region keybind.schematic_select.name = Select Region keybind.schematic_menu.name = Schematic Menu diff --git a/core/src/mindustry/ai/UnitCommand.java b/core/src/mindustry/ai/UnitCommand.java index 1f048c72ec..507dbfa2f0 100644 --- a/core/src/mindustry/ai/UnitCommand.java +++ b/core/src/mindustry/ai/UnitCommand.java @@ -4,9 +4,11 @@ import arc.*; import arc.func.*; import arc.scene.style.*; import arc.struct.*; +import arc.util.*; import mindustry.ai.types.*; import mindustry.entities.units.*; import mindustry.gen.*; +import mindustry.input.*; /** Defines a pattern of behavior that an RTS-controlled unit should follow. Shows up in the command UI. */ public class UnitCommand{ @@ -15,34 +17,34 @@ public class UnitCommand{ public static final UnitCommand - moveCommand = new UnitCommand("move", "right", null){{ + moveCommand = new UnitCommand("move", "right", Binding.unit_command_move, null){{ drawTarget = true; resetTarget = false; }}, - repairCommand = new UnitCommand("repair", "modeSurvival", u -> new RepairAI()), - rebuildCommand = new UnitCommand("rebuild", "hammer", u -> new BuilderAI()), - assistCommand = new UnitCommand("assist", "players", u -> { + repairCommand = new UnitCommand("repair", "modeSurvival", Binding.unit_command_repair, u -> new RepairAI()), + rebuildCommand = new UnitCommand("rebuild", "hammer", Binding.unit_command_rebuild, u -> new BuilderAI()), + assistCommand = new UnitCommand("assist", "players", Binding.unit_command_assist, u -> { var ai = new BuilderAI(); ai.onlyAssist = true; return ai; }), - mineCommand = new UnitCommand("mine", "production", u -> new MinerAI()), - boostCommand = new UnitCommand("boost", "up", u -> new BoostAI()){{ + mineCommand = new UnitCommand("mine", "production", Binding.unit_command_mine, u -> new MinerAI()), + boostCommand = new UnitCommand("boost", "up", Binding.unit_command_boost, u -> new BoostAI()){{ switchToMove = false; drawTarget = true; resetTarget = false; }}, - loadUnitsCommand = new UnitCommand("loadUnits", "download", null){{ + loadUnitsCommand = new UnitCommand("loadUnits", "download", Binding.unit_command_load_units, null){{ switchToMove = false; drawTarget = true; resetTarget = false; }}, - loadBlocksCommand = new UnitCommand("loadBlocks", "down", null){{ + loadBlocksCommand = new UnitCommand("loadBlocks", "down", Binding.unit_command_load_blocks, null){{ switchToMove = false; drawTarget = true; resetTarget = false; }}, - unloadPayloadCommand = new UnitCommand("unloadPayload", "upload", null){{ + unloadPayloadCommand = new UnitCommand("unloadPayload", "upload", Binding.unit_command_unload_payload, null){{ switchToMove = false; drawTarget = true; resetTarget = false; @@ -62,6 +64,8 @@ public class UnitCommand{ public boolean drawTarget = false; /** Whether to reset targets when switching to or from this command. */ public boolean resetTarget = true; + /** Key to press for this command. */ + public @Nullable Binding keybind = null; public UnitCommand(String name, String icon, Func controller){ this.name = name; @@ -72,6 +76,11 @@ public class UnitCommand{ all.add(this); } + public UnitCommand(String name, String icon, Binding keybind, Func controller){ + this(name, icon, controller); + this.keybind = keybind; + } + public String localized(){ return Core.bundle.get("command." + name); } diff --git a/core/src/mindustry/ai/UnitStance.java b/core/src/mindustry/ai/UnitStance.java index 5f955c6762..a76668d307 100644 --- a/core/src/mindustry/ai/UnitStance.java +++ b/core/src/mindustry/ai/UnitStance.java @@ -3,7 +3,9 @@ package mindustry.ai; import arc.*; import arc.scene.style.*; import arc.struct.*; +import arc.util.*; import mindustry.gen.*; +import mindustry.input.*; public class UnitStance{ /** List of all stances by ID. */ @@ -11,12 +13,12 @@ public class UnitStance{ public static final UnitStance - stop = new UnitStance("stop", "cancel"), //not a real stance, cannot be selected, just cancels ordewrs - shoot = new UnitStance("shoot", "commandAttack"), - holdFire = new UnitStance("holdfire", "none"), - pursueTarget = new UnitStance("pursuetarget", "right"), - patrol = new UnitStance("patrol", "refresh"), - ram = new UnitStance("ram", "rightOpen"); + stop = new UnitStance("stop", "cancel", Binding.cancel_orders), //not a real stance, cannot be selected, just cancels ordewrs + shoot = new UnitStance("shoot", "commandAttack", Binding.unit_stance_shoot), + holdFire = new UnitStance("holdfire", "none", Binding.unit_stance_hold_fire), + pursueTarget = new UnitStance("pursuetarget", "right", Binding.unit_stance_pursue_target), + patrol = new UnitStance("patrol", "refresh", Binding.unit_stance_patrol), + ram = new UnitStance("ram", "rightOpen", Binding.unit_stance_ram); /** Unique ID number. */ public final int id; @@ -24,10 +26,13 @@ public class UnitStance{ public final String name; /** Name of UI icon (from Icon class). */ public final String icon; + /** Key to press for this stance. */ + public @Nullable Binding keybind = null; - public UnitStance(String name, String icon){ + public UnitStance(String name, String icon, Binding keybind){ this.name = name; this.icon = icon; + this.keybind = keybind; id = all.size; all.add(this); diff --git a/core/src/mindustry/input/Binding.java b/core/src/mindustry/input/Binding.java index 2a9126dbfc..b05354f9c1 100644 --- a/core/src/mindustry/input/Binding.java +++ b/core/src/mindustry/input/Binding.java @@ -44,21 +44,21 @@ public enum Binding implements KeyBind{ cancel_orders(KeyCode.unset), - unit_stance_1(KeyCode.unset), - unit_stance_2(KeyCode.unset), - unit_stance_3(KeyCode.unset), - unit_stance_4(KeyCode.unset), - unit_stance_5(KeyCode.unset), + unit_stance_shoot(KeyCode.unset), + unit_stance_hold_fire(KeyCode.unset), + unit_stance_pursue_target(KeyCode.unset), + unit_stance_patrol(KeyCode.unset), + unit_stance_ram(KeyCode.unset), - unit_command_1(KeyCode.unset), - unit_command_2(KeyCode.unset), - unit_command_3(KeyCode.unset), - unit_command_4(KeyCode.unset), - unit_command_5(KeyCode.unset), - unit_command_6(KeyCode.unset), - unit_command_7(KeyCode.unset), - unit_command_8(KeyCode.unset), - unit_command_9(KeyCode.unset), + unit_command_move(KeyCode.unset), + unit_command_repair(KeyCode.unset), + unit_command_rebuild(KeyCode.unset), + unit_command_assist(KeyCode.unset), + unit_command_mine(KeyCode.unset), + unit_command_boost(KeyCode.unset), + unit_command_load_units(KeyCode.unset), + unit_command_load_blocks(KeyCode.unset), + unit_command_unload_payload(KeyCode.unset), category_prev(KeyCode.comma, "blocks"), category_next(KeyCode.period), diff --git a/core/src/mindustry/ui/fragments/PlacementFragment.java b/core/src/mindustry/ui/fragments/PlacementFragment.java index b457a9a27a..d359484340 100644 --- a/core/src/mindustry/ui/fragments/PlacementFragment.java +++ b/core/src/mindustry/ui/fragments/PlacementFragment.java @@ -68,27 +68,6 @@ public class PlacementFragment{ Binding.block_select_down }; - Binding[] stanceBindings = { - Binding.cancel_orders, - Binding.unit_stance_1, - Binding.unit_stance_2, - Binding.unit_stance_3, - Binding.unit_stance_4, - Binding.unit_stance_5, - }; - - Binding[] commandBindings = { - Binding.unit_command_1, - Binding.unit_command_2, - Binding.unit_command_3, - Binding.unit_command_4, - Binding.unit_command_5, - Binding.unit_command_6, - Binding.unit_command_7, - Binding.unit_command_8, - Binding.unit_command_9, - }; - public PlacementFragment(){ Events.on(WorldLoadEvent.class, event -> { Core.app.post(() -> { @@ -607,17 +586,17 @@ public class PlacementFragment{ } //not a huge fan of running input logic here, but it's convenient as the stance arrays are all here... - for(int i = 0; i < Math.min(stanceBindings.length, stances.size); i++){ + for(UnitStance stance : stances){ //first stance must always be the stop stance - if(Core.input.keyTap(stanceBindings[i]) && (i != 0 || stances.get(0) == UnitStance.stop)){ - Call.setUnitStance(player, control.input.selectedUnits.mapInt(un -> un.id).toArray(), stances.get(i)); + if(stance.keybind != null && Core.input.keyTap(stance.keybind)){ + Call.setUnitStance(player, control.input.selectedUnits.mapInt(un -> un.id).toArray(), stance); } } - for(int i = 0; i < Math.min(commandBindings.length, commands.size); i++){ + for(UnitCommand command : commands){ //first stance must always be the stop stance - if(Core.input.keyTap(commandBindings[i])){ - Call.setUnitCommand(player, control.input.selectedUnits.mapInt(un -> un.id).toArray(), commands.get(i)); + if(command.keybind != null && Core.input.keyTap(command.keybind)){ + Call.setUnitCommand(player, control.input.selectedUnits.mapInt(un -> un.id).toArray(), command); } } }