| 1 | package net.minecraft.client.settings;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Sets;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Set;
|
| 7 | import net.minecraft.client.resources.I18n;
|
| 8 | import net.minecraft.util.IntHashMap;
|
| 9 |
|
| 10 | public class KeyBinding implements Comparable<KeyBinding>
|
| 11 | {
|
| 12 | private static final List<KeyBinding> keybindArray = Lists.<KeyBinding>newArrayList();
|
| 13 | private static final IntHashMap<KeyBinding> hash = new IntHashMap();
|
| 14 | private static final Set<String> keybindSet = Sets.<String>newHashSet();
|
| 15 | private final String keyDescription;
|
| 16 | private final int keyCodeDefault;
|
| 17 | private final String keyCategory;
|
| 18 | private int keyCode;
|
| 19 |
|
| 20 | /** Is the key held down? */
|
| 21 | private boolean pressed;
|
| 22 | private int pressTime;
|
| 23 |
|
| 24 | public static void onTick(int keyCode)
|
| 25 | {
|
| 26 | if (keyCode != 0)
|
| 27 | {
|
| 28 | KeyBinding keybinding = (KeyBinding)hash.lookup(keyCode);
|
| 29 |
|
| 30 | if (keybinding != null)
|
| 31 | {
|
| 32 | ++keybinding.pressTime;
|
| 33 | }
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public static void setKeyBindState(int keyCode, boolean pressed)
|
| 38 | {
|
| 39 | if (keyCode != 0)
|
| 40 | {
|
| 41 | KeyBinding keybinding = (KeyBinding)hash.lookup(keyCode);
|
| 42 |
|
| 43 | if (keybinding != null)
|
| 44 | {
|
| 45 | keybinding.pressed = pressed;
|
| 46 | }
|
| 47 | }
|
| 48 | }
|
| 49 |
|
| 50 | public static void unPressAllKeys()
|
| 51 | {
|
| 52 | for (KeyBinding keybinding : keybindArray)
|
| 53 | {
|
| 54 | keybinding.unpressKey();
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public static void resetKeyBindingArrayAndHash()
|
| 59 | {
|
| 60 | hash.clearMap();
|
| 61 |
|
| 62 | for (KeyBinding keybinding : keybindArray)
|
| 63 | {
|
| 64 | hash.addKey(keybinding.keyCode, keybinding);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | public static Set<String> getKeybinds()
|
| 69 | {
|
| 70 | return keybindSet;
|
| 71 | }
|
| 72 |
|
| 73 | public KeyBinding(String description, int keyCode, String category)
|
| 74 | {
|
| 75 | this.keyDescription = description;
|
| 76 | this.keyCode = keyCode;
|
| 77 | this.keyCodeDefault = keyCode;
|
| 78 | this.keyCategory = category;
|
| 79 | keybindArray.add(this);
|
| 80 | hash.addKey(keyCode, this);
|
| 81 | keybindSet.add(category);
|
| 82 | }
|
| 83 |
|
| 84 | /**
|
| 85 | * Returns true if the key is pressed (used for continuous querying). Should be used in tickers.
|
| 86 | */
|
| 87 | public boolean isKeyDown()
|
| 88 | {
|
| 89 | return this.pressed;
|
| 90 | }
|
| 91 |
|
| 92 | public String getKeyCategory()
|
| 93 | {
|
| 94 | return this.keyCategory;
|
| 95 | }
|
| 96 |
|
| 97 | /**
|
| 98 | * Returns true on the initial key press. For continuous querying use {@link isKeyDown()}. Should be used in key
|
| 99 | * events.
|
| 100 | */
|
| 101 | public boolean isPressed()
|
| 102 | {
|
| 103 | if (this.pressTime == 0)
|
| 104 | {
|
| 105 | return false;
|
| 106 | }
|
| 107 | else
|
| 108 | {
|
| 109 | --this.pressTime;
|
| 110 | return true;
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | private void unpressKey()
|
| 115 | {
|
| 116 | this.pressTime = 0;
|
| 117 | this.pressed = false;
|
| 118 | }
|
| 119 |
|
| 120 | public String getKeyDescription()
|
| 121 | {
|
| 122 | return this.keyDescription;
|
| 123 | }
|
| 124 |
|
| 125 | public int getKeyCodeDefault()
|
| 126 | {
|
| 127 | return this.keyCodeDefault;
|
| 128 | }
|
| 129 |
|
| 130 | public int getKeyCode()
|
| 131 | {
|
| 132 | return this.keyCode;
|
| 133 | }
|
| 134 |
|
| 135 | public void setKeyCode(int keyCode)
|
| 136 | {
|
| 137 | this.keyCode = keyCode;
|
| 138 | }
|
| 139 |
|
| 140 | public int compareTo(KeyBinding p_compareTo_1_)
|
| 141 | {
|
| 142 | int i = I18n.format(this.keyCategory, new Object[0]).compareTo(I18n.format(p_compareTo_1_.keyCategory, new Object[0]));
|
| 143 |
|
| 144 | if (i == 0)
|
| 145 | {
|
| 146 | i = I18n.format(this.keyDescription, new Object[0]).compareTo(I18n.format(p_compareTo_1_.keyDescription, new Object[0]));
|
| 147 | }
|
| 148 |
|
| 149 | return i;
|
| 150 | }
|
| 151 | }
|
| 152 |
|