]> git.lizzy.rs Git - BoundingBoxOutlineReloaded.git/commitdiff
Change how keyboard events are handled
authorIrtimaled <irtimaled@gmail.com>
Tue, 5 Mar 2019 06:12:26 +0000 (22:12 -0800)
committerIrtimaled <irtimaled@gmail.com>
Sun, 10 Mar 2019 01:43:25 +0000 (17:43 -0800)
In Forge 1.13.2 there's no current keyboard press logic, and in 1.14 there's some weirdness with multiple dispatching of keyboard presses.
Additionally I want long press support to open the config menu.

src/main/java/com/irtimaled/bbor/client/ClientProxy.java
src/main/java/com/irtimaled/bbor/client/events/KeyPressed.java [deleted file]
src/main/java/com/irtimaled/bbor/client/gui/SettingsScreen.java
src/main/java/com/irtimaled/bbor/client/keyboard/Key.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/keyboard/KeyHandler.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/client/keyboard/KeyListener.java [new file with mode: 0644]
src/main/java/com/irtimaled/bbor/mixin/client/MixinMinecraft.java
src/main/java/com/irtimaled/bbor/mixin/client/settings/MixinGameSettings.java
src/main/java/com/irtimaled/bbor/mixin/client/settings/MixinKeyBinding.java

index 4746b882afa196f6d48989e0f54f53ee439e3777..70d95be661a40fae7d0a44d1f8c465e22cf8e6fa 100644 (file)
@@ -1,6 +1,8 @@
 package com.irtimaled.bbor.client;
 
 import com.irtimaled.bbor.client.events.*;
+import com.irtimaled.bbor.client.gui.SettingsScreen;
+import com.irtimaled.bbor.client.keyboard.KeyListener;
 import com.irtimaled.bbor.common.BoundingBoxType;
 import com.irtimaled.bbor.common.CommonProxy;
 import com.irtimaled.bbor.common.EventBus;
@@ -10,7 +12,6 @@ import com.irtimaled.bbor.common.models.BoundingBoxWorldSpawn;
 import com.irtimaled.bbor.config.ConfigManager;
 import com.irtimaled.bbor.config.Setting;
 import net.minecraft.client.Minecraft;
-import net.minecraft.client.settings.KeyBinding;
 import net.minecraft.entity.player.EntityPlayer;
 import net.minecraft.network.NetworkManager;
 import net.minecraft.util.math.BlockPos;
@@ -22,18 +23,34 @@ import java.net.SocketAddress;
 import static com.irtimaled.bbor.client.Constants.CHUNK_SIZE;
 
 public class ClientProxy extends CommonProxy {
-    public static final String KeyCategory = "Bounding Box Outline Reloaded";
-    public static KeyBinding ActiveHotKey = new KeyBinding("Toggle On/Off", 0x42, KeyCategory);
-    public static KeyBinding OuterBoxOnlyHotKey = new KeyBinding("Toggle Display Outer Box Only", 0x4f, KeyCategory);
+    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);
+    }
+
+    public static void toggleActive() {
+        active = !active;
+        if (active)
+            PlayerData.setActiveY();
+    }
+
+    private static void toggleOuterBoxesOnly() {
+        Setting<Boolean> outerBoxesOnly = ConfigManager.outerBoxesOnly;
+        outerBoxesOnly.set(!outerBoxesOnly.get());
+    }
+
     private ClientRenderer renderer;
 
     @Override
     public void init() {
         super.init();
         EventBus.subscribe(Render.class, e -> render(e.getPartialTicks()));
-        EventBus.subscribe(KeyPressed.class, e -> keyPressed());
         EventBus.subscribe(ConnectedToRemoteServer.class, e -> connectedToServer(e.getNetworkManager()));
         EventBus.subscribe(DisconnectedFromRemoteServer.class, e -> disconnectedFromServer());
         EventBus.subscribe(InitializeClientReceived.class, e -> setWorldData(e.getSeed(), e.getSpawnX(), e.getSpawnZ()));
@@ -41,6 +58,7 @@ public class ClientProxy extends CommonProxy {
         EventBus.subscribe(RemoveBoundingBoxReceived.class, e -> removeBoundingBox(e.getDimensionType(), e.getKey()));
 
         renderer = new ClientRenderer(this::getCache);
+        KeyListener.init();
     }
 
     private void render(float partialTicks) {
@@ -52,21 +70,6 @@ public class ClientProxy extends CommonProxy {
         }
     }
 
-    public static void toggleActive() {
-        active = !active;
-        if (active)
-            PlayerData.setActiveY();
-    }
-
-    private void keyPressed() {
-        if (ActiveHotKey.isPressed()) {
-            toggleActive();
-        } else if (OuterBoxOnlyHotKey.isPressed()) {
-            Setting<Boolean> outerBoxesOnly = ConfigManager.outerBoxesOnly;
-            outerBoxesOnly.set(!outerBoxesOnly.get());
-        }
-    }
-
     private void connectedToServer(NetworkManager networkManager) {
         SocketAddress remoteAddress = networkManager.getRemoteAddress();
         if (remoteAddress instanceof InetSocketAddress) {
diff --git a/src/main/java/com/irtimaled/bbor/client/events/KeyPressed.java b/src/main/java/com/irtimaled/bbor/client/events/KeyPressed.java
deleted file mode 100644 (file)
index dd9480d..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-package com.irtimaled.bbor.client.events;
-
-public class KeyPressed {
-}
index 027668ee48c7fbbba667424f6b772b7558d9a614..7163f038ee12e8ed0aa857c84b396cabccf45a28 100644 (file)
@@ -110,7 +110,7 @@ public class SettingsScreen extends GuiScreen {
     }
 
     protected void initGui() {
-        this.title = "Bounding Box Outline Reloaded";
+        this.title = ClientProxy.Name;
 
         this.controls = new HashSet<>();
         this.addTabs("General", "Structures", "Villages");
diff --git a/src/main/java/com/irtimaled/bbor/client/keyboard/Key.java b/src/main/java/com/irtimaled/bbor/client/keyboard/Key.java
new file mode 100644 (file)
index 0000000..2bdf5c9
--- /dev/null
@@ -0,0 +1,70 @@
+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;
+    private KeyHandler onKeyPress;
+    private KeyHandler onLongKeyPress;
+    private int longPressDuration;
+
+    Key(String description, int keyCode, String category) {
+        super(description, keyCode, category);
+    }
+
+    public Key onKeyPressHandler(KeyHandler onKeyPress) {
+        this.onKeyPress = onKeyPress;
+        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);
+    }
+
+
+    private int pressDuration = 0;
+
+    @Override
+    public boolean isPressed() {
+        return pressDuration == 1;
+    }
+
+    void release() {
+        if (onKeyPress != null && (onLongKeyPress == null || pressDuration < longPressDuration)) {
+            onKeyPress.handle();
+        }
+
+        pressDuration = 0;
+    }
+
+    void repeat() {
+        if (onLongKeyPress == null) return;
+
+        if (pressDuration <= longPressDuration) {
+            pressDuration++;
+        }
+
+        if (pressDuration == longPressDuration) {
+            onLongKeyPress.handle();
+        }
+    }
+
+    void press() {
+        pressDuration++;
+    }
+}
diff --git a/src/main/java/com/irtimaled/bbor/client/keyboard/KeyHandler.java b/src/main/java/com/irtimaled/bbor/client/keyboard/KeyHandler.java
new file mode 100644 (file)
index 0000000..996f66b
--- /dev/null
@@ -0,0 +1,6 @@
+package com.irtimaled.bbor.client.keyboard;
+
+@FunctionalInterface
+public interface KeyHandler {
+    void handle();
+}
diff --git a/src/main/java/com/irtimaled/bbor/client/keyboard/KeyListener.java b/src/main/java/com/irtimaled/bbor/client/keyboard/KeyListener.java
new file mode 100644 (file)
index 0000000..9a83c16
--- /dev/null
@@ -0,0 +1,55 @@
+package com.irtimaled.bbor.client.keyboard;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.settings.KeyBinding;
+import net.minecraft.client.util.InputMappings;
+import org.lwjgl.glfw.GLFW;
+
+import java.util.HashSet;
+import java.util.Set;
+
+public class KeyListener {
+    private static final Minecraft minecraft = Minecraft.getInstance();
+    private static long mainWindowHandle;
+    private static Set<Key> keys = new HashSet<>();
+
+    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);
+        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.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;
+                }
+            }
+        }
+        minecraft.keyboardListener.onKeyEvent(windowHandle, keyCode, scanCode, action, modifiers);
+    }
+
+    public static KeyBinding[] keyBindings() {
+        return keys.stream().toArray(KeyBinding[]::new);
+    }
+}
index 10b3c5a173496a620c79689292fbcf7bdc1345ed..23156af6dd10a5e1ea15c5f1ed85dcdc3bd40dcb 100644 (file)
@@ -1,10 +1,9 @@
 package com.irtimaled.bbor.mixin.client;
 
 import com.irtimaled.bbor.client.ClientProxy;
-import com.irtimaled.bbor.client.events.KeyPressed;
-import com.irtimaled.bbor.common.EventBus;
 import com.irtimaled.bbor.config.ConfigManager;
 import net.minecraft.client.Minecraft;
+import net.minecraft.client.main.GameConfiguration;
 import org.spongepowered.asm.mixin.Mixin;
 import org.spongepowered.asm.mixin.injection.At;
 import org.spongepowered.asm.mixin.injection.Inject;
@@ -12,14 +11,16 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
 
 @Mixin(Minecraft.class)
 public class MixinMinecraft {
-    @Inject(method = "init", at = @At("RETURN"))
-    private void init(CallbackInfo ci) {
-        ConfigManager.loadConfig(((Minecraft) (Object) this).gameDir);
-        new ClientProxy().init();
+    private ClientProxy clientProxy;
+
+    @Inject(method = "<init>", at = @At("RETURN"))
+    private void constructor(GameConfiguration configuration, CallbackInfo ci) {
+        ConfigManager.loadConfig(configuration.folderInfo.gameDir);
+        clientProxy = new ClientProxy();
     }
 
-    @Inject(method = "processKeyBinds", at = @At("HEAD"))
-    public void processKeyBinds(CallbackInfo ci) {
-        EventBus.publish(new KeyPressed());
+    @Inject(method = "init", at = @At("RETURN"))
+    private void init(CallbackInfo ci) {
+        clientProxy.init();
     }
 }
index b2916e53a70a891abd2ffb5db50bd414fa9007b7..0f527eb63c468471d9961165e5c7bb6e669aea47 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.GameSettings;
 import net.minecraft.client.Minecraft;
 import net.minecraft.client.settings.KeyBinding;
@@ -24,7 +24,7 @@ public class MixinGameSettings {
     }
 
     private KeyBinding[] getKeysAll() {
-        return ArrayUtils.addAll(keyBindings, ClientProxy.ActiveHotKey, ClientProxy.OuterBoxOnlyHotKey);
+        return ArrayUtils.addAll(keyBindings, KeyListener.keyBindings());
     }
 
     @Inject(method = "<init>(Lnet/minecraft/client/Minecraft;Ljava/io/File;)V", at = @At("RETURN"))
index 3e012b96314c363e906b908562dfbfa8e25c525f..05e827c708d3ac878937a44aa284c0ad71095a9e 100644 (file)
@@ -13,6 +13,6 @@ public class MixinKeyBinding {
     private static Map<String, Integer> CATEGORY_ORDER;
 
     static {
-        CATEGORY_ORDER.put(ClientProxy.KeyCategory, 0);
+        CATEGORY_ORDER.put(ClientProxy.Name, 0);
     }
 }