From f8c36104ad9154e76e4459c7f58def7fcd5d9f8a Mon Sep 17 00:00:00 2001 From: Christian Date: Tue, 14 Aug 2012 22:06:49 -0400 Subject: [PATCH] KeyBinding handler change: a handler now takes an array of keys and calls back with the activated key. Sorry for the inconvenience but this improves the usability of this API a lot --- .../modloader/ModLoaderKeyBindingHandler.java | 10 ++-- .../client/registry/KeyBindingRegistry.java | 56 ++++++++++++------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/client/cpw/mods/fml/client/modloader/ModLoaderKeyBindingHandler.java b/client/cpw/mods/fml/client/modloader/ModLoaderKeyBindingHandler.java index 9017043288..b78b9e1392 100644 --- a/client/cpw/mods/fml/client/modloader/ModLoaderKeyBindingHandler.java +++ b/client/cpw/mods/fml/client/modloader/ModLoaderKeyBindingHandler.java @@ -43,17 +43,17 @@ public class ModLoaderKeyBindingHandler extends KeyBindingRegistry.KeyHandler */ public ModLoaderKeyBindingHandler(KeyBinding keyBinding, boolean allowRepeat, ModLoaderModContainer modContainer) { - super(keyBinding, allowRepeat); + super(new KeyBinding[] { keyBinding }, new boolean[] { allowRepeat }); this.modContainer=modContainer; } public void onRenderEndTick() { - ((net.minecraft.src.BaseMod)modContainer.getMod()).keyboardEvent(keyBinding); + ((net.minecraft.src.BaseMod)modContainer.getMod()).keyboardEvent(keyBindings[0]); } @Override - public void keyDown(EnumSet type, boolean end, boolean repeats) + public void keyDown(EnumSet type, KeyBinding kb, boolean end, boolean repeats) { if (!end) { @@ -72,7 +72,7 @@ public class ModLoaderKeyBindingHandler extends KeyBindingRegistry.KeyHandler } @Override - public void keyUp(EnumSet type, boolean end) + public void keyUp(EnumSet type, KeyBinding kb, boolean end) { if (!end) { @@ -99,6 +99,6 @@ public class ModLoaderKeyBindingHandler extends KeyBindingRegistry.KeyHandler @Override public String getLabel() { - return modContainer.getModId() +" KB "+keyBinding.field_74512_d; + return modContainer.getModId() +" KB "+keyBindings[0].field_74512_d; } } diff --git a/client/cpw/mods/fml/client/registry/KeyBindingRegistry.java b/client/cpw/mods/fml/client/registry/KeyBindingRegistry.java index 81ee564505..c824046d45 100644 --- a/client/cpw/mods/fml/client/registry/KeyBindingRegistry.java +++ b/client/cpw/mods/fml/client/registry/KeyBindingRegistry.java @@ -40,19 +40,25 @@ public class KeyBindingRegistry */ public static abstract class KeyHandler implements ITickHandler { - protected KeyBinding keyBinding; - protected boolean keyDown; - protected boolean repeating; + protected KeyBinding[] keyBindings; + protected boolean[] keyDown; + protected boolean[] repeatings; - public KeyHandler(KeyBinding keyBinding, boolean repeating) + /** + * Pass an array of keybindings and a repeat flag for each one + * + * @param keyBindings + * @param repeatings + */ + public KeyHandler(KeyBinding[] keyBindings, boolean[] repeatings) { - this.keyBinding = keyBinding; - this.repeating = repeating; + this.keyBindings = keyBindings; + this.repeatings = repeatings; } - public KeyBinding getKeyBinding() + public KeyBinding[] getKeyBindings() { - return this.keyBinding; + return this.keyBindings; } /** @@ -75,19 +81,24 @@ public class KeyBindingRegistry private void keyTick(EnumSet type, boolean tickEnd) { - int keyCode = keyBinding.field_74512_d; - boolean state = (keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode)); - if (state != keyDown || (state && repeating)) + for (int i = 0; i < keyBindings.length; i++) { - if (state) + KeyBinding keyBinding = keyBindings[i]; + int keyCode = keyBinding.field_74512_d; + boolean state = (keyCode < 0 ? Mouse.isButtonDown(keyCode + 100) : Keyboard.isKeyDown(keyCode)); + if (state != keyDown[i] || (state && repeatings[i])) { - keyDown(type, tickEnd, state!=keyDown); + if (state) + { + keyDown(type, keyBinding, tickEnd, state!=keyDown[i]); + } + else + { + keyUp(type, keyBinding, tickEnd); + } + keyDown[i] = state; } - else - { - keyUp(type, tickEnd); - } - keyDown = state; + } } @@ -101,7 +112,7 @@ public class KeyBindingRegistry * @param tickEnd was it an end or start tick which fired the key * @param isRepeat is it a repeat key event */ - public abstract void keyDown(EnumSet types, boolean tickEnd, boolean isRepeat); + public abstract void keyDown(EnumSet types, KeyBinding kb, boolean tickEnd, boolean isRepeat); /** * Fired once when the key changes state from down to up * @@ -110,7 +121,7 @@ public class KeyBindingRegistry * @param types the type(s) of tick that fired when this key was first down * @param tickEnd was it an end or start tick which fired the key */ - public abstract void keyUp(EnumSet types, boolean tickEnd); + public abstract void keyUp(EnumSet types, KeyBinding kb, boolean tickEnd); /** @@ -138,7 +149,10 @@ public class KeyBindingRegistry ArrayList harvestedBindings = Lists.newArrayList(); for (KeyHandler key : keyHandlers) { - harvestedBindings.add(key.keyBinding); + for (KeyBinding kb : key.keyBindings) + { + harvestedBindings.add(kb); + } } KeyBinding[] modKeyBindings = harvestedBindings.toArray(new KeyBinding[harvestedBindings.size()]); KeyBinding[] allKeys = new KeyBinding[settings.field_74324_K.length + modKeyBindings.length];