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
@@ -0,0 +1,200 @@
/*
* Copyright (c) 2026 CleanroomMC contributors
*
* This file is licensed under the CleanroomMC License Version 1.0.
* See the applicable LICENSE file in this directory or a parent directory
* for the full licence terms.
*
* This is visible-source software and is not open-source software.
*/

package com.cleanroommc.client.controls;

import it.unimi.dsi.fastutil.ints.IntArrayList;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.settings.KeyBinding;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public final class GuiControlsHooks {

public static final String TRANSLATE_LAYOUT = "cleanroom.gui.controls.layout";

private static final IntArrayList maskPriority = IntArrayList.of(
KeyModifier.MASK_SHIFT, KeyModifier.MASK_CONTROL, KeyModifier.MASK_CONTROL | KeyModifier.MASK_SHIFT,
KeyModifier.MASK_ALT, KeyModifier.MASK_ALT | KeyModifier.MASK_SHIFT,
KeyModifier.MASK_ALT | KeyModifier.MASK_CONTROL,
KeyModifier.MASK_ALT | KeyModifier.MASK_CONTROL | KeyModifier.MASK_SHIFT
);

private GuiControlsHooks() { }

public enum SortMode implements Translate {

CATEGORY,
NAME,
KEY;

private static final String TRANSLATE_KEY = "cleanroom.gui.controls.sort";
private static final SortMode[] modes = { CATEGORY, NAME, KEY };

private final String modeKey = "cleanroom.gui.controls.mode." + this.name().toLowerCase();

public static int compareKey(KeyBinding bind1, KeyBinding bind2) {
if ((bind1.getKeyModifiersMask() | bind2.getKeyModifiersMask()) == KeyModifier.MASK_NONE ||
bind1.getKeyModifiersMask() == bind2.getKeyModifiersMask()) {
String name1 = bind1.getDisplayName();
String name2 = bind2.getDisplayName();
return name1.equals(name2)
? I18n.format(bind1.getKeyDescription()).compareTo(I18n.format(bind2.getKeyDescription()))
: (name1.length() == name2.length()
? name1.compareTo(name2)
: (name1.length() > name2.length() ? 1 : -1));
} else if (bind1.getKeyModifiersMask() == KeyModifier.MASK_NONE &&
bind2.getKeyModifiersMask() != KeyModifier.MASK_NONE) {
return -1;
} else if (bind1.getKeyModifiersMask() != KeyModifier.MASK_NONE &&
bind2.getKeyModifiersMask() == KeyModifier.MASK_NONE) {
return 1;
} else {
return Integer.compare(
maskPriority.indexOf(bind1.getKeyModifiersMask()),
maskPriority.indexOf(bind2.getKeyModifiersMask())
);
}
}

public boolean byCategory() {
return this == CATEGORY;
}

public boolean byName() {
return this == NAME;
}

public boolean byKey() {
return this == KEY;
}

public SortMode next(GuiButton button) {
return modes[(this.ordinal() + 1) % modes.length].updateButton(button);
}

public String buttonKey() {
return TRANSLATE_KEY;
}

public String modeKey() {
return modeKey;
}

private SortMode updateButton(GuiButton button) {
button.displayString = translateText();
return this;
}

}

public enum FilterMode implements Translate {

NONE,
CONFLICT,
FREE;

private static final String TRANSLATE_KEY = "cleanroom.gui.controls.filter";
private static final FilterMode[] modes = { NONE, CONFLICT, FREE };

private final String modeKey = "cleanroom.gui.controls.mode." + this.name().toLowerCase();

public FilterMode next(GuiButton button) {
return modes[(this.ordinal() + 1) % modes.length].updateButton(button);
}

public boolean allow(KeyBinding key) {
return switch (this) {
case CONFLICT -> {
if (key.getKeyCode() != 0) {
for (KeyBinding keybinding : Minecraft.getMinecraft().gameSettings.keyBindings) {
if (key != keybinding && key.conflicts(keybinding)) {
yield true;
}
}
}
yield false;
}
case FREE -> key.getKeyCode() == 0;
default -> true;
};
}

public String buttonKey() {
return TRANSLATE_KEY;
}

public String modeKey() {
return modeKey;
}

private FilterMode updateButton(GuiButton button) {
button.displayString = translateText();
return this;
}

}

public enum SearchMode implements Translate {

NAME,
CATEGORY,
KEY;

private static final String TRANSLATE_KEY = "cleanroom.gui.controls.search";
private static final SearchMode[] modes = { NAME, CATEGORY, KEY };

private final String modeKey = "cleanroom.gui.controls.mode." + this.name().toLowerCase();

public SearchMode next(GuiButton button) {
return modes[(this.ordinal() + 1) % modes.length].updateButton(button);
}

public String buttonKey() {
return TRANSLATE_KEY;
}

public String modeKey() {
return modeKey;
}

public boolean allow(KeyBinding key, String searchText) {
return switch (this) {
case NAME -> I18n.format(key.getKeyDescription()).toLowerCase().contains(searchText.toLowerCase());
case CATEGORY -> I18n.format(key.getKeyCategory()).toLowerCase().contains(searchText.toLowerCase());
case KEY -> key.getDisplayName().toLowerCase().contains(searchText.toLowerCase());
};
}

private SearchMode updateButton(GuiButton button) {
button.displayString = translateText();
return this;
}

}

interface Translate {

String buttonKey();

String modeKey();

default String translateText() {
return I18n.format(this.buttonKey(), I18n.format(this.modeKey()));
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static char keypad(int key, boolean numLock) {
}

synchronized void key(
int scancode, int key, boolean pressed, boolean repeat, char character, long timestampNs, short mods) {
int scancode, int key, boolean pressed, boolean repeat, char character, long timestampNs, int mods) {
this.pendingCharacter = null;
PendingKey event = new PendingKey(scancode, key, pressed, repeat, timestampNs, mods);
event.character = character;
Expand Down Expand Up @@ -197,11 +197,11 @@ private static final class PendingKey {
private final boolean pressed;
private final boolean repeat;
private final long timestampNs;
private final short mods;
private final int mods;

private char character;

private PendingKey(int scancode, int key, boolean pressed, boolean repeat, long timestampNs, short mods) {
private PendingKey(int scancode, int key, boolean pressed, boolean repeat, long timestampNs, int mods) {
this.scancode = scancode;
this.key = key;
this.pressed = pressed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@ void key(SDL_KeyboardEvent event) {
this.keys[scancode] = event.down();
}
char character = event.down() ? LegacyInputQueues.character(scancode, event.mod(), this.text.active()) : 0;
this.legacy.key(scancode, event.key(), event.down(), event.repeat(), character, event.timestamp(), event.mod());
this.legacy.key(
scancode, event.key(), event.down(), event.repeat(), character, event.timestamp(), event.mod() & 0xFFFF
);
}

void text(SDL_TextInputEvent event) {
Expand Down Expand Up @@ -892,7 +894,7 @@ private void releaseHeldInput(long timestampNs) {
for (int scancode = 0; scancode < this.keys.length; scancode++) {
if (this.keys[scancode]) {
this.keys[scancode] = false;
this.legacy.key(scancode, 0, false, false, (char) 0, timestampNs, (short) 0);
this.legacy.key(scancode, 0, false, false, (char) 0, timestampNs, 0);
}
}
for (int button = 1; button < this.buttons.length; button++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ cleanroom.gui.website=Website
cleanroom.gui.submit_bug=Submit Bug
cleanroom.gui.mod_id=Mod ID: %s

cleanroom.gui.controls.sort=Sort by: %s
cleanroom.gui.controls.filter=Filter: %s
cleanroom.gui.controls.search=Search by: %s
cleanroom.gui.controls.layout=Toggle layout
cleanroom.gui.controls.mode.category=Category
cleanroom.gui.controls.mode.name=Name
cleanroom.gui.controls.mode.key=Key
cleanroom.gui.controls.mode.none=None
cleanroom.gui.controls.mode.conflict=Conflict
cleanroom.gui.controls.mode.free=Free

cleanroom.config.enable=Enable
cleanroom.config.enable.tooltip=Whether to enable Cleanroom's mod list. \nDisabling it stops Cleanroom from redirecting Forge's mod list calls.
cleanroom.config.library_list=Library List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,35 @@

public class KeyBindingMap
{
private static final EnumMap<KeyModifier, IntHashMap<Collection<KeyBinding>>> map = new java.util.EnumMap<KeyModifier, IntHashMap<Collection<KeyBinding>>>(KeyModifier.class);
private static final IntHashMap<Collection<KeyBinding>>[] map = new IntHashMap[8];
static
{
for (KeyModifier modifier : KeyModifier.values())
{
map.put(modifier, new IntHashMap<Collection<KeyBinding>>());
for (int i = 0; i < 8; i++) {
map[i] = new IntHashMap<>();
}
}

@Nullable
@Deprecated // Cleanroom: Use lookupActives
public KeyBinding lookupActive(int keyCode)
{
KeyModifier activeModifier = KeyModifier.getActiveModifier();
if (!activeModifier.matches(keyCode))
{
KeyBinding binding = getBinding(keyCode, activeModifier);
if (binding != null)
{
return binding;
}
}
return getBinding(keyCode, KeyModifier.NONE);
return lookupActives(keyCode).stream().findFirst().orElse(null);
}

public Set<KeyBinding> lookupActives(int keyCode)
{
KeyModifier activeModifier = KeyModifier.getActiveModifier();
if (!activeModifier.matches(keyCode))
int activeModifiersMask = KeyModifier.getActiveModifierMask();
Set<KeyBinding> bindings = getBindings(keyCode, KeyModifier.filterMask(activeModifiersMask, keyCode));
if (!bindings.isEmpty())
{
Set<KeyBinding> bindings = getBindings(keyCode, activeModifier);
if (!bindings.isEmpty())
{
return bindings;
}
}
return getBindings(keyCode, KeyModifier.NONE);
}

@Nullable
private KeyBinding getBinding(int keyCode, KeyModifier keyModifier)
{
Collection<KeyBinding> bindings = map.get(keyModifier).lookup(keyCode);
if (bindings != null)
{
for (KeyBinding binding : bindings)
{
if (binding.isActiveAndMatches(keyCode))
{
return binding;
}
}
return bindings;
}
return null;
return getBindings(keyCode, KeyModifier.MASK_NONE);
}

private Set<KeyBinding> getBindings(int keyCode, KeyModifier keyModifier)
private Set<KeyBinding> getBindings(int keyCode, int mask)
{
Collection<KeyBinding> bindings = map.get(keyModifier).lookup(keyCode);
Collection<KeyBinding> bindings = map[mask].lookup(keyCode);
if (bindings == null)
{
return Collections.emptySet();
Expand All @@ -96,8 +66,8 @@ private Set<KeyBinding> getBindings(int keyCode, KeyModifier keyModifier)

public List<KeyBinding> lookupAll(int keyCode)
{
List<KeyBinding> matchingBindings = new ArrayList<KeyBinding>();
for (IntHashMap<Collection<KeyBinding>> bindingsMap : map.values())
List<KeyBinding> matchingBindings = new ArrayList<>();
for (IntHashMap<Collection<KeyBinding>> bindingsMap : map)
{
Collection<KeyBinding> bindings = bindingsMap.lookup(keyCode);
if (bindings != null)
Expand All @@ -110,22 +80,20 @@ public List<KeyBinding> lookupAll(int keyCode)

public void addKey(int keyCode, KeyBinding keyBinding)
{
KeyModifier keyModifier = keyBinding.getKeyModifier();
IntHashMap<Collection<KeyBinding>> bindingsMap = map.get(keyModifier);
IntHashMap<Collection<KeyBinding>> bindingsMap = map[keyBinding.getKeyModifiersMask()];
Collection<KeyBinding> bindingsForKey = bindingsMap.lookup(keyCode);
if (bindingsForKey == null)
{
bindingsForKey = new ArrayList<KeyBinding>();
bindingsForKey = new ArrayList<>();
bindingsMap.addKey(keyCode, bindingsForKey);
}
bindingsForKey.add(keyBinding);
}

public void removeKey(KeyBinding keyBinding)
{
KeyModifier keyModifier = keyBinding.getKeyModifier();
int keyCode = keyBinding.getKeyCode();
IntHashMap<Collection<KeyBinding>> bindingsMap = map.get(keyModifier);
IntHashMap<Collection<KeyBinding>> bindingsMap = map[keyBinding.getKeyModifiersMask()];
Collection<KeyBinding> bindingsForKey = bindingsMap.lookup(keyCode);
if (bindingsForKey != null)
{
Expand All @@ -139,7 +107,7 @@ public void removeKey(KeyBinding keyBinding)

public void clearMap()
{
for (IntHashMap<Collection<KeyBinding>> bindings : map.values())
for (IntHashMap<Collection<KeyBinding>> bindings : map)
{
bindings.clearMap();
}
Expand Down
Loading
Loading