Skip to content

Commit 2dacdf6

Browse files
committed
Keybind support
1 parent a56c608 commit 2dacdf6

File tree

7 files changed

+166
-2
lines changed

7 files changed

+166
-2
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IMinecraft.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ interface IMinecraft {
3737

3838
fun sendClickableChatMessage(message: String, action: String, type: ClickType)
3939

40+
fun getKeyName(keyCode: Int): String
41+
4042
/**
4143
* This is a method to provide a render context. Note that constructing this context directly will potentially give
4244
* you an incorrect render state, leading to visual glitches. Depending on your platform, this might also require
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package io.github.notenoughupdates.moulconfig.gui.editors;
2+
3+
import io.github.notenoughupdates.moulconfig.GuiTextures;
4+
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
5+
import io.github.notenoughupdates.moulconfig.common.RenderContext;
6+
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
7+
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext;
8+
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent;
9+
import io.github.notenoughupdates.moulconfig.gui.MouseEvent;
10+
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
11+
import org.jetbrains.annotations.NotNull;
12+
13+
import java.util.Collections;
14+
15+
public class GuiOptionEditorKeybind extends ComponentEditor {
16+
private boolean editingKeycode = false;
17+
GuiComponent component;
18+
19+
public GuiOptionEditorKeybind(ProcessedOption option, int defaultKeyCode) {
20+
super(option);
21+
22+
component = wrapComponent(new GuiComponent() {
23+
@Override
24+
public int getWidth() {
25+
return 0;
26+
}
27+
28+
@Override
29+
public int getHeight() {
30+
return 30;
31+
}
32+
33+
@Override
34+
public void render(@NotNull GuiImmediateContext context) {
35+
int height = getHeight();
36+
RenderContext renderContext = context.getRenderContext();
37+
int width = getWidth();
38+
39+
renderContext.color(1, 1, 1, 1);
40+
IMinecraft.instance.bindTexture(GuiTextures.BUTTON);
41+
renderContext.drawTexturedRect(width / 6 - 24, height - 7 - 14, 48, 16);
42+
43+
44+
String keyName = IMinecraft.instance.getKeyName((int) option.get());
45+
String text = editingKeycode ? "> " + keyName + " <" : keyName;
46+
renderContext.drawStringCenteredScaledMaxWidth(text,
47+
IMinecraft.instance.getDefaultFontRenderer(),
48+
width / 6, height - 7 - 6,
49+
false, 38, 0xFF303030
50+
);
51+
52+
int resetX = width / 6 - 24 + 48 + 3;
53+
int resetY = height - 7 - 14 + 3;
54+
55+
IMinecraft.instance.bindTexture(GuiTextures.RESET);
56+
renderContext.color(1, 1, 1, 1);
57+
renderContext.drawTexturedRect(resetX, resetY, 10, 11);
58+
int mouseX = context.getMouseX();
59+
int mouseY = context.getMouseY();
60+
if (mouseX >= resetX && mouseX < resetX + 10 &&
61+
mouseY >= resetY && mouseY < resetY + 11) {
62+
renderContext.scheduleDrawTooltip(Collections.singletonList(
63+
"§cReset to Default"
64+
));
65+
}
66+
}
67+
68+
@Override
69+
public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateContext context) {
70+
if (!(mouseEvent instanceof MouseEvent.Click)) return false;
71+
MouseEvent.Click click = (MouseEvent.Click) mouseEvent;
72+
if (click.getMouseState() && click.getMouseButton() != -1 && editingKeycode) {
73+
editingKeycode = false;
74+
int mouseButton = click.getMouseButton();
75+
System.out.println(mouseButton);
76+
option.set(mouseButton);
77+
return true;
78+
}
79+
80+
if (click.getMouseState() && click.getMouseButton() == 0) {
81+
int height = getHeight();
82+
int width = getHeight();
83+
int mouseX = context.getMouseX();
84+
int mouseY = context.getMouseY();
85+
if (mouseX > width / 6 - 24 && mouseX < width / 6 + 24 &&
86+
mouseY > height - 7 - 14 && mouseY < height - 7 + 2) {
87+
editingKeycode = true;
88+
return true;
89+
}
90+
if (mouseX > width / 6 - 24 + 48 + 3 && mouseX < width / 6 - 24 + 48 + 13 &&
91+
mouseY > height - 7 - 14 + 3 && mouseY < height - 7 - 14 + 3 + 11) {
92+
option.set(defaultKeyCode);
93+
return true;
94+
}
95+
}
96+
97+
return false;
98+
}
99+
100+
@Override
101+
public boolean keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) {
102+
boolean wasKeyPressedEvent = keyboardEvent instanceof KeyboardEvent.KeyPressed;
103+
if (wasKeyPressedEvent) {
104+
if (editingKeycode) {
105+
KeyboardEvent.KeyPressed keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent;
106+
editingKeycode = false;
107+
int keyCode = -1;
108+
int keycode = keyPressed.getKeycode();
109+
if (keycode != 256 /* GLFW_KEY_ESCAPE*/ && keycode != 0) {
110+
keyCode = keycode;
111+
}
112+
//if (keyCode > 256) keyCode = 0;
113+
option.set(keyCode);
114+
return true;
115+
} else {
116+
return false;
117+
}
118+
}
119+
120+
return editingKeycode;
121+
}
122+
});
123+
}
124+
125+
@Override
126+
public @NotNull GuiComponent getDelegate() {
127+
return component;
128+
}
129+
}

common/src/main/java/io/github/notenoughupdates/moulconfig/processor/BuiltinMoulConfigGuis.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public static void addProcessors(MoulConfigProcessor<?> processor) {
4343
processedOption,
4444
configEditorDropdown.values()
4545
));
46-
46+
processor.registerConfigEditor(ConfigEditorKeybind.class, (processedOption, keybind) ->
47+
new GuiOptionEditorKeybind(processedOption, keybind.defaultKey()));
4748
processor.registerConfigEditor(ConfigEditorSlider.class, (processedOption, configEditorSlider) ->
4849
new GuiOptionEditorSlider(processedOption, configEditorSlider.minValue(), configEditorSlider.maxValue(), configEditorSlider.minStep()));
4950
processor.registerConfigEditor(ConfigEditorInfoText.class, (processedOption, configEditorInfoText) ->

legacy/src/main/java/io/github/notenoughupdates/moulconfig/internal/ForgeMinecraft.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class ForgeMinecraft : IMinecraft {
130130
)
131131
}
132132

133+
override fun getKeyName(keyCode: Int): String {
134+
return KeybindHelper.getKeyName(keyCode)
135+
}
136+
133137
override fun isMouseButtonDown(mouseButton: Int): Boolean {
134138
return Mouse.isButtonDown(mouseButton)
135139
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.notenoughupdates.moulconfig.platform
2+
3+
import net.minecraft.client.util.InputUtil
4+
5+
object ModernKeybindHelper {
6+
fun getKeyName(keyCode: Int): String {
7+
if (keyCode == -1) {
8+
return "NONE"
9+
} else if (keyCode in 0..9) {
10+
return "Button ${keyCode+1}"
11+
} else {
12+
var keyName = InputUtil.fromKeyCode(keyCode, 0).localizedText.string
13+
if (keyName == null) {
14+
keyName = "???"
15+
}
16+
return keyName
17+
}
18+
}
19+
}

modern/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/ModernMinecraft.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ class ModernMinecraft : IMinecraft {
162162
})
163163
}
164164

165+
override fun getKeyName(keyCode: Int): String {
166+
return ModernKeybindHelper.getKeyName(keyCode)
167+
}
168+
165169
override fun provideTopLevelRenderContext(): RenderContext {
166170
return ModernRenderContext(
167171
DrawContext(

modern/src/main/kotlin/io/github/notenoughupdates/moulconfig/test/TestCategoryA.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package io.github.notenoughupdates.moulconfig.test
33
import com.google.gson.annotations.Expose
44
import io.github.notenoughupdates.moulconfig.annotations.*
55
import io.github.notenoughupdates.moulconfig.observer.Property
6+
import org.lwjgl.glfw.GLFW
67
import java.util.*
7-
import kotlin.collections.ArrayList
88

99
class TestCategoryA {
1010
@ConfigOption(name = "Test Option", desc = "Test toggle")
@@ -90,4 +90,9 @@ class TestCategoryA {
9090
return str
9191
}
9292
}
93+
94+
@Expose
95+
@ConfigOption(name = "Keybind", desc = "The Number One")
96+
@ConfigEditorKeybind(defaultKey = GLFW.GLFW_KEY_1)
97+
var slot1: Int = GLFW.GLFW_KEY_1
9398
}

0 commit comments

Comments
 (0)