Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
package org.polyfrost.oneconfig.api.platform.v1.internal;

import com.mojang.blaze3d.platform.InputConstants;
import org.lwjgl.glfw.GLFW;
import org.polyfrost.oneconfig.api.platform.v1.Keys;

public class KeysImpl implements Keys {
@Override
public String keyName(int key) {
//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
InputConstants.Key input = InputConstants.Type.KEYSYM.getOrCreate(key);
return input == InputConstants.UNKNOWN ? "None" : input.getDisplayName().getString();
}

@Override
public String mouseName(int button) {
return InputConstants.Type.MOUSE.getOrCreate(button).getDisplayName().getString();
}

@Override
public int getKeyA() {
return InputConstants.KEY_A;
}

@Override
public int getKeyC() {
return InputConstants.KEY_C;
}

@Override
public int getKeyD() {
return InputConstants.KEY_D;
}

@Override
public int getKeyE() {
return InputConstants.KEY_E;
}

@Override
public int getKeyH() {
return InputConstants.KEY_H;
}

@Override
public int getKeyL() {
return InputConstants.KEY_L;
}

@Override
public int getKeyR() {
return InputConstants.KEY_R;
}

@Override
public int getKeyV() {
return InputConstants.KEY_V;
}

@Override
public int getKeyX() {
return InputConstants.KEY_X;
}

@Override
public int getKeyDelete() {
return InputConstants.KEY_DELETE;
}

@Override
public int getMouseButtonLeft() {
return InputConstants.MOUSE_BUTTON_LEFT;
}

@Override
public int getKeyLeftShift() {
return InputConstants.KEY_LSHIFT;
Expand Down Expand Up @@ -37,11 +103,21 @@ public int getKeyRightAlt() {

@Override
public int getKeyLeftSuper() {
return GLFW.GLFW_KEY_LEFT_SUPER;
//? if >= 26.3 {
/*return InputConstants.KEY_LGUI;
*///?} elif >= 1.21.10 {
return InputConstants.KEY_LSUPER;
//?} else
//return InputConstants.KEY_LWIN;
}

@Override
public int getKeyRightSuper() {
return GLFW.GLFW_KEY_RIGHT_SUPER;
//? if >= 26.3 {
/*return InputConstants.KEY_RGUI;
*///?} elif >= 1.21.10 {
return InputConstants.KEY_RSUPER;
//?} else
//return InputConstants.KEY_RWIN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//?}
import org.polyfrost.oneconfig.api.event.v1.EventManager;
import org.polyfrost.oneconfig.api.event.v1.events.ScreenOpenEvent;
import org.polyfrost.oneconfig.api.platform.v1.Keys;
import org.polyfrost.oneconfig.api.platform.v1.Platform;
import org.polyfrost.oneconfig.api.ui.v1.keybind.KeyModifiers;
import org.polyfrost.oneconfig.api.ui.v1.keybind.KeybindManager;
import org.polyfrost.oneconfig.api.ui.v1.keybind.MinecraftKeybindBridge;
Expand Down Expand Up @@ -102,6 +104,7 @@ public synchronized void register(OneConfigKeybind bind) {
});
OneConfigKeybind def = bind.getDefaultKeybind();
OneConfigKeybind defSrc = def != null ? def : bind;
//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
InputConstants.Type type = defSrc.isMousePrimary() ? InputConstants.Type.MOUSE : InputConstants.Type.KEYSYM;
KeyMapping mapping = detached(
bind.getName(),
Expand Down Expand Up @@ -235,7 +238,7 @@ private long[] combo(KeyMapping mapping) {
} else {
InputConstants.Key key = ((KeyMappingAccessor) mapping).oneconfig$getKey();
int v = key.getValue();
if (v < 0) return null;
if (key == InputConstants.UNKNOWN) return null;
set.add(key.getType() == InputConstants.Type.MOUSE ? (MOUSE_TAG | v) : (long) v);
set.add(MODS_TAG);
}
Expand Down Expand Up @@ -302,14 +305,13 @@ private static int[] toArray(List<Integer> list) {
return out;
}

private static byte modBit(int glfw) {
return switch (glfw) {
case 340, 344 -> KeyModifiers.SHIFT;
case 341, 345 -> KeyModifiers.CTRL;
case 342, 346 -> KeyModifiers.ALT;
case 343, 347 -> KeyModifiers.META;
default -> KeyModifiers.NONE;
};
private static byte modBit(int key) {
Keys keys = Platform.compatibility().keys();
if (key == keys.getKeyLeftShift() || key == keys.getKeyRightShift()) return KeyModifiers.SHIFT;
if (key == keys.getKeyLeftControl() || key == keys.getKeyRightControl()) return KeyModifiers.CTRL;
if (key == keys.getKeyLeftAlt() || key == keys.getKeyRightAlt()) return KeyModifiers.ALT;
if (key == keys.getKeyLeftSuper() || key == keys.getKeyRightSuper()) return KeyModifiers.META;
return KeyModifiers.NONE;
}

public static String fullComboText(int[] keys, int[] mouse, byte mods) {
Expand All @@ -319,10 +321,11 @@ public static String fullComboText(int[] keys, int[] mouse, byte mods) {
if (KeyModifiers.INSTANCE.has(mods, KeyModifiers.ALT)) parts.add("Alt");
if (KeyModifiers.INSTANCE.has(mods, KeyModifiers.META)) parts.add("Meta");
if (keys != null) {
//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
for (int k : keys) parts.add(InputConstants.Type.KEYSYM.getOrCreate(k).getDisplayName().getString());
}
if (mouse != null) {
for (int b : mouse) parts.add("Mouse " + (b + 1));
for (int b : mouse) parts.add(Platform.compatibility().keys().mouseName(b));
}
return parts.isEmpty() ? "None" : String.join(" + ", parts);
}
Expand Down Expand Up @@ -367,6 +370,7 @@ private void setKey(KeyMapping mapping, InputConstants.Key key) {
}

private void applyKeyTo(KeyMapping mapping, OneConfigKeybind bind) {
//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
setKey(mapping, (bind.isMousePrimary() ? InputConstants.Type.MOUSE : InputConstants.Type.KEYSYM).getOrCreate(bind.getBoundCode()));
}

Expand All @@ -382,6 +386,11 @@ public void reconcile() {

if (actualValue == bind.getBoundCode() && actualMouse == bind.isMousePrimary()) continue;

if (actual == InputConstants.UNKNOWN) {
pending.add(() -> KeybindManager.rebindFromMinecraft(bind, null, null, KeyModifiers.NONE));
continue;
}

OneConfigKeybind def = bind.getDefaultKeybind();
if (def != null && actualValue == def.getBoundCode() && actualMouse == def.isMousePrimary()) {
pending.add(() -> KeybindManager.rebindFromMinecraft(bind, def.getKeyCodes(), def.getMouseBtns(), def.getMods()));
Expand Down Expand Up @@ -411,12 +420,13 @@ public static Component comboLabel(OneConfigKeybind bind, Component base) {
List<String> extra = new ArrayList<>();
if (keys != null) {
for (int i = keysPrimary ? 1 : 0; i < keys.length; i++) {
//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
extra.add(InputConstants.Type.KEYSYM.getOrCreate(keys[i]).getDisplayName().getString());
}
}
if (mouse != null) {
for (int i = keysPrimary ? 0 : 1; i < mouse.length; i++) {
extra.add("Mouse " + (mouse[i] + 1));
extra.add(Platform.compatibility().keys().mouseName(mouse[i]));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.polyfrost.oneconfig.api.ui.v1.keybind.internal;

import com.mojang.blaze3d.platform.InputConstants;
import org.jetbrains.annotations.Nullable;

public final class MinecraftKeybindCodec implements KeybindCodec {
//? if >= 26.3
/*private static final int MAX_KEY = Integer.MAX_VALUE; // SDL keycodes are sparse and unicode/scancode-masked*/
//? if < 26.3
private static final int MAX_KEY = 348; // GLFW_KEY_LAST

private static final int MIN_MOUSE = InputConstants.MOUSE_BUTTON_LEFT;
private static final int MAX_MOUSE = InputConstants.MOUSE_BUTTON_LEFT + 7; // vanilla names eight mouse buttons

@Override
public @Nullable String keyName(int code) {
if (code < 0 || code > MAX_KEY) return null;

//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
InputConstants.Key key = InputConstants.Type.KEYSYM.getOrCreate(code);
if (key == InputConstants.UNKNOWN) return null;

// Ignore fallback names
if (("key.keyboard." + code).equals(key.getName())) return null;

//? if < 26.3 {
// GLFW to SDL aliases
if ("key.keyboard.keypad.decimal".equals(key.getName())) return "key.keyboard.keypad.period";
if ("key.keyboard.menu".equals(key.getName())) return "key.keyboard.application";
//?}

return key.getName();
}

@Override
public @Nullable Integer keyCode(String name) {
//? if < 26.3 {
// SDL to GLFW aliases
if ("key.keyboard.keypad.period".equals(name)) name = "key.keyboard.keypad.decimal";
if ("key.keyboard.application".equals(name)) name = "key.keyboard.menu";
//?}

InputConstants.Key key = lookup(name);
if (key == null) return null;

if (("key.keyboard." + key.getValue()).equals(name)) return null;

//~ if < 26.3 'Type.KEYBOARD' -> 'Type.KEYSYM'
return key.getType() == InputConstants.Type.KEYSYM && key != InputConstants.UNKNOWN ? key.getValue() : null;
}

@Override
public @Nullable String mouseName(int button) {
if (button < MIN_MOUSE || button > MAX_MOUSE) return null;
return InputConstants.Type.MOUSE.getOrCreate(button).getName();
}

@Override
public @Nullable Integer mouseButton(String name) {
InputConstants.Key key = lookup(name);
if (key == null || key.getType() != InputConstants.Type.MOUSE) return null;

int value = key.getValue();
return value >= MIN_MOUSE && value <= MAX_MOUSE ? value : null;
}

private static @Nullable InputConstants.Key lookup(String name) {
int dot = name.lastIndexOf('.');
if (dot >= 0) {
try {
int suffix = Integer.parseInt(name.substring(dot + 1));
if (name.startsWith("key.mouse.")) {
if (suffix - 1 < MIN_MOUSE || suffix - 1 > MAX_MOUSE) return null;
} else if (suffix < 0 || suffix > MAX_KEY) {
return null;
}
} catch (NumberFormatException ignored) {
}
}

try {
return InputConstants.getKey(name);
} catch (RuntimeException ignored) {
return null;
}
}

@Override
public @Nullable String legacyKeyName(int code) {
if (code >= 48 && code <= 57) return "key.keyboard." + (char) code;
if (code >= 65 && code <= 90) return "key.keyboard." + (char) (code + 32);
if (code >= 290 && code <= 314) return "key.keyboard.f" + (code - 289);
if (code >= 320 && code <= 329) return "key.keyboard.keypad." + (code - 320);
return switch (code) {
case 32 -> "key.keyboard.space";
case 39 -> "key.keyboard.apostrophe";
case 44 -> "key.keyboard.comma";
case 45 -> "key.keyboard.minus";
case 46 -> "key.keyboard.period";
case 47 -> "key.keyboard.slash";
case 59 -> "key.keyboard.semicolon";
case 61 -> "key.keyboard.equal";
case 91 -> "key.keyboard.left.bracket";
case 92 -> "key.keyboard.backslash";
case 93 -> "key.keyboard.right.bracket";
case 96 -> "key.keyboard.grave.accent";
case 161 -> "key.keyboard.world.1";
case 162 -> "key.keyboard.world.2";
case 256 -> "key.keyboard.escape";
case 257 -> "key.keyboard.enter";
case 258 -> "key.keyboard.tab";
case 259 -> "key.keyboard.backspace";
case 260 -> "key.keyboard.insert";
case 261 -> "key.keyboard.delete";
case 262 -> "key.keyboard.right";
case 263 -> "key.keyboard.left";
case 264 -> "key.keyboard.down";
case 265 -> "key.keyboard.up";
case 266 -> "key.keyboard.page.up";
case 267 -> "key.keyboard.page.down";
case 268 -> "key.keyboard.home";
case 269 -> "key.keyboard.end";
case 280 -> "key.keyboard.caps.lock";
case 281 -> "key.keyboard.scroll.lock";
case 282 -> "key.keyboard.num.lock";
case 283 -> "key.keyboard.print.screen";
case 284 -> "key.keyboard.pause";
case 330 -> "key.keyboard.keypad.period";
case 331 -> "key.keyboard.keypad.divide";
case 332 -> "key.keyboard.keypad.multiply";
case 333 -> "key.keyboard.keypad.subtract";
case 334 -> "key.keyboard.keypad.add";
case 335 -> "key.keyboard.keypad.enter";
case 336 -> "key.keyboard.keypad.equal";
case 340 -> "key.keyboard.left.shift";
case 341 -> "key.keyboard.left.control";
case 342 -> "key.keyboard.left.alt";
case 343 -> "key.keyboard.left.win";
case 344 -> "key.keyboard.right.shift";
case 345 -> "key.keyboard.right.control";
case 346 -> "key.keyboard.right.alt";
case 347 -> "key.keyboard.right.win";
case 348 -> "key.keyboard.application";
default -> null;
};
}

@Override
public @Nullable String legacyMouseName(int button) {
return switch (button) {
case 0 -> "key.mouse.left";
case 1 -> "key.mouse.right";
case 2 -> "key.mouse.middle";
case 3, 4, 5, 6, 7 -> "key.mouse." + (button + 1);
default -> null;
};
}
}
Loading
Loading