| 1 | /*
|
| 2 | * This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
|
| 3 | * Copyright (c) Meteor Development.
|
| 4 | */
|
| 5 |
|
| 6 | package meteordevelopment.meteorclient.utils.misc;
|
| 7 |
|
| 8 | import com.mojang.blaze3d.platform.InputConstants;
|
| 9 | import meteordevelopment.meteorclient.utils.misc.input.Input;
|
| 10 | import net.minecraft.client.input.KeyEvent;
|
| 11 | import net.minecraft.client.input.MouseButtonInfo;
|
| 12 | import net.minecraft.nbt.CompoundTag;
|
| 13 | import net.minecraft.nbt.ListTag;
|
| 14 | import net.minecraft.nbt.StringTag;
|
| 15 |
|
| 16 | import java.util.Arrays;
|
| 17 | import java.util.Collection;
|
| 18 | import java.util.EnumSet;
|
| 19 | import java.util.Objects;
|
| 20 | import java.util.Set;
|
| 21 |
|
| 22 | public class Keybind implements ISerializable<Keybind>, ICopyable<Keybind> {
|
| 23 | public enum Modifier {
|
| 24 | SHIFT(InputConstants.MOD_SHIFT),
|
| 25 | CONTROL(InputConstants.MOD_CONTROL),
|
| 26 | ALT(InputConstants.MOD_ALT),
|
| 27 | SUPER(InputConstants.MOD_SUPER),
|
| 28 | CAPS_LOCK(InputConstants.MOD_CAPS_LOCK),
|
| 29 | NUM_LOCK(InputConstants.MOD_NUM_LOCK),
|
| 30 | ;
|
| 31 |
|
| 32 | private static final Modifier[] VALUES = values();
|
| 33 |
|
| 34 | public final int mask;
|
| 35 |
|
| 36 | Modifier(int mask) {
|
| 37 | this.mask = mask;
|
| 38 | }
|
| 39 |
|
| 40 | public boolean isKeyPressed() {
|
| 41 | return switch (this) {
|
| 42 | case CONTROL -> Input.isKeyPressed(InputConstants.KEY_LCONTROL) || Input.isKeyPressed(InputConstants.KEY_RCONTROL);
|
| 43 | case SUPER -> Input.isKeyPressed(InputConstants.KEY_LSUPER) || Input.isKeyPressed(InputConstants.KEY_RSUPER);
|
| 44 | case ALT -> Input.isKeyPressed(InputConstants.KEY_LALT) || Input.isKeyPressed(InputConstants.KEY_RALT);
|
| 45 | case SHIFT -> Input.isKeyPressed(InputConstants.KEY_LSHIFT) || Input.isKeyPressed(InputConstants.KEY_RSHIFT);
|
| 46 | case CAPS_LOCK -> Input.isKeyPressed(InputConstants.KEY_CAPSLOCK);
|
| 47 | case NUM_LOCK -> Input.isKeyPressed(InputConstants.KEY_NUMLOCK);
|
| 48 | };
|
| 49 | }
|
| 50 |
|
| 51 | private static EnumSet<Modifier> fromRawValue(int modifiers) {
|
| 52 | var result = EnumSet.noneOf(Modifier.class);
|
| 53 | for (var mod : VALUES) {
|
| 54 | if ((mod.mask & modifiers) != 0) result.add(mod);
|
| 55 | }
|
| 56 | return result;
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | public InputConstants.Key key;
|
| 61 | private final EnumSet<Modifier> modifiers = EnumSet.noneOf(Modifier.class);
|
| 62 |
|
| 63 | private Keybind(InputConstants.Key key, Collection<Modifier> modifiers) {
|
| 64 | this.key = key;
|
| 65 | this.modifiers.addAll(modifiers);
|
| 66 | }
|
| 67 |
|
| 68 | public static Keybind none() {
|
| 69 | return new Keybind(InputConstants.UNKNOWN, Set.of());
|
| 70 | }
|
| 71 |
|
| 72 | public static Keybind fromKey(InputConstants.Key key) {
|
| 73 | return new Keybind(key, Set.of());
|
| 74 | }
|
| 75 |
|
| 76 | public static Keybind fromKey(int key) {
|
| 77 | return fromKey(keyboard(key));
|
| 78 | }
|
| 79 |
|
| 80 | public static Keybind fromKeys(InputConstants.Key key, Collection<Modifier> modifiers) {
|
| 81 | return new Keybind(key, modifiers);
|
| 82 | }
|
| 83 |
|
| 84 | public static Keybind fromKeys(InputConstants.Key key, Modifier... modifiers) {
|
| 85 | return fromKeys(key, Arrays.asList(modifiers));
|
| 86 | }
|
| 87 |
|
| 88 | public static Keybind fromKeys(int key, int modifiers) {
|
| 89 | return fromKeys(keyboard(key), Modifier.fromRawValue(modifiers));
|
| 90 | }
|
| 91 |
|
| 92 | public static Keybind fromButton(int button) {
|
| 93 | return fromKey(mouse(button));
|
| 94 | }
|
| 95 |
|
| 96 | public boolean isSet() {
|
| 97 | return !key.equals(InputConstants.UNKNOWN);
|
| 98 | }
|
| 99 |
|
| 100 | public boolean isKey() {
|
| 101 | return key.getType() != InputConstants.Type.MOUSE;
|
| 102 | }
|
| 103 |
|
| 104 | public boolean hasMods() {
|
| 105 | return !modifiers.isEmpty();
|
| 106 | }
|
| 107 |
|
| 108 | public int getValue() {
|
| 109 | return key.getValue();
|
| 110 | }
|
| 111 |
|
| 112 | public void set(InputConstants.Key key, Collection<Modifier> modifiers) {
|
| 113 | this.key = key;
|
| 114 | this.modifiers.clear();
|
| 115 | this.modifiers.addAll(modifiers);
|
| 116 | }
|
| 117 |
|
| 118 | public void set(InputConstants.Key key, Modifier... modifiers) {
|
| 119 | set(key, Arrays.asList(modifiers));
|
| 120 | }
|
| 121 |
|
| 122 | public void set(boolean isKey, int value, int modifiers) {
|
| 123 | set(isKey ? keyboard(value) : mouse(value), isKey ? Modifier.fromRawValue(modifiers) : Set.of());
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public Keybind set(Keybind value) {
|
| 128 | set(value.key, value.modifiers);
|
| 129 |
|
| 130 | return this;
|
| 131 | }
|
| 132 |
|
| 133 | public void reset() {
|
| 134 | set(InputConstants.UNKNOWN, Set.of());
|
| 135 | }
|
| 136 |
|
| 137 | public boolean canBindTo(InputConstants.Key key, Collection<Modifier> modifiers) {
|
| 138 | if (key.getType() != InputConstants.Type.MOUSE) {
|
| 139 | if (!modifiers.isEmpty() && isKeyMod(key)) return false;
|
| 140 | return !key.equals(InputConstants.UNKNOWN) && !key.equals(keyboard(InputConstants.KEY_ESCAPE));
|
| 141 | }
|
| 142 |
|
| 143 | return !key.equals(mouse(InputConstants.MOUSE_BUTTON_LEFT)) && !key.equals(mouse(InputConstants.MOUSE_BUTTON_RIGHT));
|
| 144 | }
|
| 145 |
|
| 146 | public boolean canBindTo(boolean isKey, int value, int modifiers) {
|
| 147 | return canBindTo(isKey ? keyboard(value) : mouse(value), isKey ? Modifier.fromRawValue(modifiers) : Set.of());
|
| 148 | }
|
| 149 |
|
| 150 | public boolean matches(InputConstants.Key key, Set<Modifier> modifiers) {
|
| 151 | if (!isSet() || !this.key.equals(key)) return false;
|
| 152 | if (!hasMods()) return modifiers.stream().noneMatch(m -> m == Modifier.SHIFT || m == Modifier.CONTROL || m == Modifier.ALT || m == Modifier.SUPER);
|
| 153 | return this.modifiers.equals(modifiers);
|
| 154 | }
|
| 155 |
|
| 156 | public boolean matches(boolean isKey, int value, int modifiers) {
|
| 157 | return matches(isKey ? keyboard(value) : mouse(value), isKey ? Modifier.fromRawValue(modifiers) : Set.of());
|
| 158 | }
|
| 159 |
|
| 160 | public boolean matches(KeyEvent input) {
|
| 161 | return matches(InputConstants.getKey(input), Modifier.fromRawValue(input.modifiers()));
|
| 162 | }
|
| 163 |
|
| 164 | public boolean matches(MouseButtonInfo input) {
|
| 165 | return matches(mouse(input.button()), Set.of());
|
| 166 | }
|
| 167 |
|
| 168 | public boolean isPressed() {
|
| 169 | if (key.getType() == InputConstants.Type.MOUSE) return Input.isButtonPressed(key.getValue());
|
| 170 |
|
| 171 | return modifiersPressed() && Input.isKeyPressed(key.getValue());
|
| 172 | }
|
| 173 |
|
| 174 | private boolean modifiersPressed() {
|
| 175 | if (!hasMods()) return true;
|
| 176 |
|
| 177 | for (Modifier modifier : modifiers) {
|
| 178 | if (!modifier.isKeyPressed()) return false;
|
| 179 | }
|
| 180 |
|
| 181 | return true;
|
| 182 | }
|
| 183 |
|
| 184 | private boolean isKeyMod(InputConstants.Key key) {
|
| 185 | return key.getValue() >= InputConstants.KEY_LSHIFT && key.getValue() <= InputConstants.KEY_RSUPER;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public Keybind copy() {
|
| 190 | return new Keybind(key, modifiers);
|
| 191 | }
|
| 192 |
|
| 193 | @Override
|
| 194 | public String toString() {
|
| 195 | if (!isSet()) return "None";
|
| 196 | if (key.getType() == InputConstants.Type.MOUSE) return key.getDisplayName().getString();
|
| 197 | if (modifiers.isEmpty()) return key.getDisplayName().getString();
|
| 198 |
|
| 199 | StringBuilder label = new StringBuilder();
|
| 200 | if (modifiers.contains(Modifier.CONTROL)) label.append("Ctrl + ");
|
| 201 | if (modifiers.contains(Modifier.SUPER)) label.append("Cmd + ");
|
| 202 | if (modifiers.contains(Modifier.ALT)) label.append("Alt + ");
|
| 203 | if (modifiers.contains(Modifier.SHIFT)) label.append("Shift + ");
|
| 204 | if (modifiers.contains(Modifier.CAPS_LOCK)) label.append("Caps Lock + ");
|
| 205 | if (modifiers.contains(Modifier.NUM_LOCK)) label.append("Num Lock + ");
|
| 206 | label.append(key.getDisplayName().getString());
|
| 207 |
|
| 208 | return label.toString();
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public boolean equals(Object o) {
|
| 213 | if (this == o) return true;
|
| 214 | if (o == null || getClass() != o.getClass()) return false;
|
| 215 | Keybind keybind = (Keybind) o;
|
| 216 | return key.equals(keybind.key) && modifiers.equals(keybind.modifiers);
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | public int hashCode() {
|
| 221 | return Objects.hash(key, modifiers);
|
| 222 | }
|
| 223 |
|
| 224 | // Serialization
|
| 225 |
|
| 226 | @Override
|
| 227 | public CompoundTag toTag() {
|
| 228 | CompoundTag tag = new CompoundTag();
|
| 229 |
|
| 230 | tag.putString("key", key.getName());
|
| 231 |
|
| 232 | ListTag modifiersTag = new ListTag();
|
| 233 | for (Modifier modifier : modifiers) {
|
| 234 | modifiersTag.add(StringTag.valueOf(modifier.name()));
|
| 235 | }
|
| 236 | tag.put("modifiers", modifiersTag);
|
| 237 |
|
| 238 | return tag;
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public Keybind fromTag(CompoundTag tag) {
|
| 243 | if (tag.contains("key")) {
|
| 244 | // New format: key name + modifier enum names
|
| 245 | try {
|
| 246 | key = InputConstants.getKey(tag.getStringOr("key", ""));
|
| 247 | } catch (IllegalArgumentException e) {
|
| 248 | key = InputConstants.UNKNOWN;
|
| 249 | }
|
| 250 |
|
| 251 | modifiers.clear();
|
| 252 | ListTag modifiersTag = tag.getListOrEmpty("modifiers");
|
| 253 | for (int i = 0; i < modifiersTag.size(); i++) {
|
| 254 | try {
|
| 255 | modifiers.add(Modifier.valueOf(modifiersTag.getStringOr(i, "")));
|
| 256 | } catch (IllegalArgumentException ignored) {}
|
| 257 | }
|
| 258 | } else {
|
| 259 | // Legacy format: raw ints (GLFW)
|
| 260 | boolean isKey = tag.getBooleanOr("isKey", true);
|
| 261 | int value = tag.getIntOr("value", InputConstants.UNKNOWN.getValue());
|
| 262 | int mods = tag.getIntOr("modifiers", 0);
|
| 263 |
|
| 264 | set(isKey ? keyboard(value) : mouse(value), isKey ? Modifier.fromRawValue(mods) : Set.of());
|
| 265 | }
|
| 266 |
|
| 267 | return this;
|
| 268 | }
|
| 269 |
|
| 270 | private static InputConstants.Key keyboard(int key) {
|
| 271 | return InputConstants.Type.KEYSYM.getOrCreate(key);
|
| 272 | }
|
| 273 |
|
| 274 | private static InputConstants.Key mouse(int button) {
|
| 275 | return InputConstants.Type.MOUSE.getOrCreate(button);
|
| 276 | }
|
| 277 |
|
| 278 | }
|
| 279 |
|