]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Change keyboard to support multi key presses
authorIrtimaled <irtimaled@gmail.com>
Mon, 23 Sep 2019 04:40:31 +0000 (21:40 -0700)
committerIrtimaled <irtimaled@gmail.com>
Mon, 18 Nov 2019 17:21:09 +0000 (09:21 -0800)
src/main/java/com/irtimaled/bbor/client/ClientProxy.java
src/main/java/com/irtimaled/bbor/client/ClientRenderer.java
src/main/java/com/irtimaled/bbor/client/gui/BoolSettingButton.java
src/main/java/com/irtimaled/bbor/client/gui/SettingsScreen.java
src/main/java/com/irtimaled/bbor/client/keyboard/CustomKeyBinding.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/keyboard/Key.java
src/main/java/com/irtimaled/bbor/client/keyboard/KeyListener.java
src/main/java/com/irtimaled/bbor/config/ConfigManager.java
src/main/java/com/irtimaled/bbor/config/Setting.java
src/main/java/com/irtimaled/bbor/mixin/client/settings/MixinKeyBinding.java

index 42f7f64ebf6c3dbc40f6205c08a752c75a0a3b74..177c53ef5c4b20ed7047ff8db93cd07b8b1800bb 100644 (file)
@@ -2,42 +2,26 @@ package com.irtimaled.bbor.client;
 
 import com.irtimaled.bbor.client.events.*;
 import com.irtimaled.bbor.client.gui.SettingsScreen;
+import com.irtimaled.bbor.client.keyboard.Key;
 import com.irtimaled.bbor.client.keyboard.KeyListener;
 import com.irtimaled.bbor.common.BoundingBoxCache;
 import com.irtimaled.bbor.common.CommonProxy;
 import com.irtimaled.bbor.common.EventBus;
 import com.irtimaled.bbor.common.VillageColorCache;
 import com.irtimaled.bbor.config.ConfigManager;
-import com.irtimaled.bbor.config.Setting;
 
 public class ClientProxy extends CommonProxy {
-    public static final String Name = "Bounding Box Outline Reloaded";
-    public static boolean active;
-
     static {
-        KeyListener.register("Toggle Active", 0x42, Name)
-                .onKeyPressHandler(ClientProxy::toggleActive)
-                .onLongKeyPressHandler(60, SettingsScreen::show);
-        KeyListener.register("Toggle Outer Box Only", 0x4f, Name)
-                .onKeyPressHandler(ClientProxy::toggleOuterBoxesOnly);
+        Key mainKey = KeyListener.register("Toggle Active", "key.keyboard.b")
+                .onKeyPressHandler(ClientRenderer::toggleActive);
+        mainKey.register("key.keyboard.g")
+                .onKeyPressHandler(SettingsScreen::show);
+        mainKey.register("key.keyboard.o")
+                .onKeyPressHandler(() -> ConfigManager.Toggle(ConfigManager.outerBoxesOnly));
     }
 
     private boolean ready;
 
-    public static void toggleActive() {
-        active = !active;
-        if (!active) return;
-
-        PlayerCoords.setActiveY();
-    }
-
-    private static void toggleOuterBoxesOnly() {
-        if(!active) return;
-
-        Setting<Boolean> outerBoxesOnly = ConfigManager.outerBoxesOnly;
-        outerBoxesOnly.set(!outerBoxesOnly.get());
-    }
-
     private ClientRenderer renderer;
 
     @Override
@@ -56,13 +40,13 @@ public class ClientProxy extends CommonProxy {
     }
 
     private void render(Render event) {
-        if (!active || !ready) return;
+        if (!ready) return;
 
-        renderer.render(event.getDimensionId(), ConfigManager.outerBoxesOnly.get());
+        renderer.render(event.getDimensionId());
     }
 
     private void disconnectedFromServer() {
-        active = false;
+        ClientRenderer.deactivate();
         if (ConfigManager.keepCacheBetweenSessions.get()) return;
         ready = false;
         VillageColorCache.clear();
index 2ea7512073fb189017258a8de46a4d0cd790e4c7..7cca95d4feb0a6fdb4c27f9e4c4a3850448f660a 100644 (file)
@@ -16,6 +16,23 @@ public class ClientRenderer {
     private static final int CHUNK_SIZE = 16;
     private static final Map<Class<? extends AbstractBoundingBox>, AbstractRenderer> boundingBoxRendererMap = new HashMap<>();
 
+    private static boolean active;
+
+    public static boolean getActive() {
+        return active;
+    }
+
+    public static void toggleActive() {
+        active = !active;
+        if (!active) return;
+
+        PlayerCoords.setActiveY();
+    }
+
+    static void deactivate() {
+        active = false;
+    }
+
     private final GetCache getCache;
     private long seed;
     private Set<AbstractBoundingBox> spawnChunkBoundingBoxes = new HashSet<>();
@@ -39,7 +56,9 @@ public class ClientRenderer {
         return boundingBox.intersectsBounds(minX, minZ, maxX, maxZ);
     }
 
-    public void render(int dimensionId, Boolean outerBoxesOnly) {
+    public void render(int dimensionId) {
+        if(!active) return;
+
         Map<AbstractBoundingBox, Set<AbstractBoundingBox>> boundingBoxes = getBoundingBoxes(dimensionId);
 
         GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
@@ -50,6 +69,8 @@ public class ClientRenderer {
         if (ConfigManager.alwaysVisible.get()) {
             GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
         }
+
+        Boolean outerBoxesOnly = ConfigManager.outerBoxesOnly.get();
         for (Map.Entry<AbstractBoundingBox, Set<AbstractBoundingBox>> entry : boundingBoxes.entrySet()) {
             AbstractBoundingBox key = entry.getKey();
             if (!key.shouldRender()) continue;
index 363e67bcdb8970ac1b3b3dc6632ba9ace016e3e2..f6ca98c06ef0c11128745021c5e873d86f01c33f 100644 (file)
@@ -1,5 +1,6 @@
 package com.irtimaled.bbor.client.gui;
 
+import com.irtimaled.bbor.config.ConfigManager;
 import com.irtimaled.bbor.config.Setting;
 
 public class BoolSettingButton extends AbstractButton {
@@ -17,6 +18,6 @@ public class BoolSettingButton extends AbstractButton {
 
     @Override
     public void onPressed() {
-        setting.set(!setting.get());
+        ConfigManager.Toggle(setting);
     }
 }
index 1ea491db607703ea59d2e9a8a06ddb1199d0578e..1d751fb5d09cba887ee719b9870b84b85dad70e2 100644 (file)
@@ -1,6 +1,7 @@
 package com.irtimaled.bbor.client.gui;
 
 import com.irtimaled.bbor.client.ClientProxy;
+import com.irtimaled.bbor.client.ClientRenderer;
 import com.irtimaled.bbor.client.renderers.Renderer;
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.TypeHelper;
@@ -110,7 +111,7 @@ public class SettingsScreen extends GuiScreen {
 
     @Override
     protected void initGui() {
-        this.title = ClientProxy.Name;
+        this.title = "Bounding Box Outline Reloaded";
 
         this.controls = new HashSet<>();
         this.addTabs("General", "Structures", "Villages");
@@ -119,12 +120,12 @@ public class SettingsScreen extends GuiScreen {
                 (id, x, y, width) -> new AbstractButton(id, x, y, width, "Active", this.mc.world != null) {
                     @Override
                     public void onPressed() {
-                        ClientProxy.toggleActive();
+                        ClientRenderer.toggleActive();
                     }
 
                     @Override
                     protected int getState() {
-                        return enabled ? ClientProxy.active ? 2 : 1 : 0;
+                        return enabled ? ClientRenderer.getActive() ? 2 : 1 : 0;
                     }
                 },
                 (id, x, y, width) -> new BoolSettingButton(id, x, y, width, "Outer Box Only", ConfigManager.outerBoxesOnly),
diff --git a/src/main/java/com/irtimaled/bbor/client/keyboard/CustomKeyBinding.java b/src/main/java/com/irtimaled/bbor/client/keyboard/CustomKeyBinding.java
new file mode 100644 (file)
index 0000000..1bf7c7e
--- /dev/null
@@ -0,0 +1,24 @@
+package com.irtimaled.bbor.client.keyboard;
+
+import net.minecraft.client.settings.KeyBinding;
+import net.minecraft.client.util.InputMappings;
+
+class CustomKeyBinding extends KeyBinding {
+    private final Key key;
+
+    CustomKeyBinding(String description, int keyCode) {
+        super(description, keyCode, KeyListener.Category);
+        this.key = new Key(keyCode);
+    }
+
+    @Override
+    public void bind(InputMappings.Input input) {
+        super.bind(input);
+        int keyCode = input.getKeyCode();
+        key.updateKeyCode(keyCode);
+    }
+
+    public Key getKey() {
+        return key;
+    }
+}
\ No newline at end of file
index 06179e1f012ce3641201a828e07098d197d55e8f..4192d6bb3ed5c6e3287974f000912e0081f12e0e 100644 (file)
@@ -1,16 +1,18 @@
 package com.irtimaled.bbor.client.keyboard;
 
-import net.minecraft.client.settings.KeyBinding;
 import net.minecraft.client.util.InputMappings;
 
-public class Key extends KeyBinding {
-    private InputMappings.Input input;
+import java.util.HashSet;
+import java.util.Set;
+
+public class Key {
+    private int keyCode;
     private KeyHandler onKeyPress;
-    private KeyHandler onLongKeyPress;
-    private int longPressDuration;
+    private Set<Key> subKeys = new HashSet<>();
+    private boolean triggeredSincePress;
 
-    Key(String description, int keyCode, String category) {
-        super(description, keyCode, category);
+    Key(int keyCode) {
+        this.keyCode = keyCode;
     }
 
     public Key onKeyPressHandler(KeyHandler onKeyPress) {
@@ -18,52 +20,67 @@ public class Key extends KeyBinding {
         return this;
     }
 
-    public Key onLongKeyPressHandler(int duration, KeyHandler onLongKeyPress) {
-        this.longPressDuration = duration;
-        this.onLongKeyPress = onLongKeyPress;
-        return this;
-    }
-
-    InputMappings.Input getInput() {
-        if (input == null)
-            return getDefault();
-        return input;
-    }
-
-    @Override
-    public void bind(InputMappings.Input input) {
-        this.input = input;
-        super.bind(input);
+    void updateKeyCode(int keyCode) {
+        this.keyCode = keyCode;
     }
 
     private int pressDuration = 0;
 
-    @Override
-    public boolean isPressed() {
-        return pressDuration == 1;
+    private void runHandler(KeyHandler onKeyPress) {
+        triggeredSincePress = true;
+        onKeyPress.handle();
     }
 
-    void release() {
-        if (onKeyPress != null && (onLongKeyPress == null || pressDuration < longPressDuration)) {
-            onKeyPress.handle();
+    private void press() {
+        for (Key subKey : subKeys) {
+            subKey.triggeredSincePress = false;
         }
-
-        pressDuration = 0;
+        triggeredSincePress = false;
+        pressDuration++;
     }
 
-    void repeat() {
-        if (onLongKeyPress == null) return;
-
-        if (pressDuration <= longPressDuration) {
-            pressDuration++;
+    private void release() {
+        try {
+            for (Key subKey : subKeys) {
+                if (subKey.pressDuration > 0) {
+                    subKey.release();
+                    return;
+                }
+                if (subKey.triggeredSincePress) {
+                    return;
+                }
+            }
+            if (onKeyPress != null && pressDuration > 0) {
+                runHandler(onKeyPress);
+            }
+        } finally {
+            pressDuration = 0;
         }
+    }
 
-        if (pressDuration == longPressDuration) {
-            onLongKeyPress.handle();
+    boolean handleKeyEvent(int keyCode, boolean isPressed) {
+        if (this.keyCode == keyCode) {
+            if (isPressed) {
+                press();
+            } else {
+                release();
+            }
+            return true;
+        } else if (this.pressDuration > 0) {
+            for (Key subKey : subKeys) {
+                if (subKey.handleKeyEvent(keyCode, isPressed)) {
+                    return true;
+                }
+            }
         }
+
+        return false;
     }
 
-    void press() {
-        pressDuration++;
+    public Key register(String keyName) {
+        InputMappings.Input input = InputMappings.getInputByName(keyName);
+        Key key = new Key(input.getKeyCode());
+        subKeys.add(key);
+        return key;
     }
 }
index a680d7c74736a87c4fbd00e8e0455463a115bcb9..e758719f90b4a17ef2d7317031668b62be6e5742 100644 (file)
@@ -12,44 +12,45 @@ public class KeyListener {
     private static final Minecraft minecraft = Minecraft.getInstance();
     private static long mainWindowHandle;
     private static Set<Key> keys = new HashSet<>();
+    private static Set<CustomKeyBinding> keyBindings = new HashSet<>();
+    public static final String Category = "Bounding Box Outline Reloaded";
 
     public static void init() {
         mainWindowHandle = minecraft.mainWindow.getHandle();
         GLFW.glfwSetKeyCallback(mainWindowHandle, KeyListener::onKeyEvent);
     }
 
-    public static Key register(String description, int keyCode, String category) {
-        Key key = new Key(description, keyCode, category);
+    public static Key register(String description, String keyName) {
+        InputMappings.Input input = InputMappings.getInputByName(keyName);
+        CustomKeyBinding keyBinding = new CustomKeyBinding(description, input.getKeyCode());
+        keyBindings.add(keyBinding);
+
+        Key key = keyBinding.getKey();
         keys.add(key);
         return key;
     }
 
     private static void onKeyEvent(long windowHandle, int keyCode, int scanCode, int action, int modifiers) {
-        if (windowHandle == mainWindowHandle && minecraft.currentScreen == null && keyCode != -1 && !InputMappings.isKeyDown(292)) {
-            InputMappings.Input input = InputMappings.getInputByCode(keyCode, scanCode);
-            for (Key key : keys) {
-                if (key.getInput() == input) {
-                    switch (action) {
-                        case GLFW.GLFW_PRESS:
-                            key.press();
-                            break;
-                        case GLFW.GLFW_REPEAT:
-                            key.repeat();
-                            break;
-                        case GLFW.GLFW_RELEASE:
-                            key.release();
-                            return;
-                    }
-                    if (minecraft.currentScreen != null)
-                        key.release();
-                    return;
-                }
+        boolean isPressed = action > 0;
+        if (windowHandle == mainWindowHandle &&
+                minecraft.currentScreen == null &&
+                keyCode != -1 &&
+                !InputMappings.isKeyDown(292) &&
+                handleKeyEvent(keyCode, isPressed))
+            return;
+        minecraft.keyboardListener.onKeyEvent(windowHandle, keyCode, scanCode, action, modifiers);
+    }
+
+    private static boolean handleKeyEvent(int keyCode, boolean isPressed) {
+        for (Key key : keys) {
+            if (key.handleKeyEvent(keyCode, isPressed)) {
+                return true;
             }
         }
-        minecraft.keyboardListener.onKeyEvent(windowHandle, keyCode, scanCode, action, modifiers);
+        return false;
     }
 
     public static KeyBinding[] keyBindings() {
-        return keys.stream().toArray(KeyBinding[]::new);
+        return keyBindings.toArray(new KeyBinding[0]);
     }
 }
index 73fbbe93436d3149a756c635cfcaf83a8298081d..e8716992e7799509cf869acdd69fa1917c3ffb2c 100644 (file)
@@ -109,4 +109,8 @@ public class ConfigManager {
         settings.add(setting);
         return setting;
     }
+
+    public static void Toggle(Setting<Boolean> setting) {
+        setting.set(!setting.get());
+    }
 }
index b1ee7d473d2cffccab587ecd7acab936d3cde0db..e1dabf3ccf400bf716073793e9e990c3ad78d71f 100644 (file)
@@ -3,7 +3,7 @@ package com.irtimaled.bbor.config;
 public class Setting<T> extends AbstractSetting {
     private T value;
 
-    public Setting(char type, T value) {
+    Setting(char type, T value) {
         super(type);
         this.value = value;
     }
index 168ca9ae8efd4cd7b5293c3c1cac36241a5e1744..f03d0f500525c2309c3885d4d3e3aa0ee2094e9f 100644 (file)
@@ -1,6 +1,6 @@
 package com.irtimaled.bbor.mixin.client.settings;
 
-import com.irtimaled.bbor.client.ClientProxy;
+import com.irtimaled.bbor.client.keyboard.KeyListener;
 import net.minecraft.client.settings.KeyBinding;
 import org.spongepowered.asm.mixin.Final;
 import org.spongepowered.asm.mixin.Mixin;
@@ -15,6 +15,6 @@ public class MixinKeyBinding {
     private static Map<String, Integer> CATEGORY_ORDER;
 
     static {
-        CATEGORY_ORDER.put(ClientProxy.Name, 0);
+        CATEGORY_ORDER.put(KeyListener.Category, 0);
     }
 }