-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a56c608
commit 2dacdf6
Showing
7 changed files
with
166 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
...c/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorKeybind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.github.notenoughupdates.moulconfig.gui.editors; | ||
|
||
import io.github.notenoughupdates.moulconfig.GuiTextures; | ||
import io.github.notenoughupdates.moulconfig.common.IMinecraft; | ||
import io.github.notenoughupdates.moulconfig.common.RenderContext; | ||
import io.github.notenoughupdates.moulconfig.gui.GuiComponent; | ||
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext; | ||
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent; | ||
import io.github.notenoughupdates.moulconfig.gui.MouseEvent; | ||
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Collections; | ||
|
||
public class GuiOptionEditorKeybind extends ComponentEditor { | ||
private boolean editingKeycode = false; | ||
GuiComponent component; | ||
|
||
public GuiOptionEditorKeybind(ProcessedOption option, int defaultKeyCode) { | ||
super(option); | ||
|
||
component = wrapComponent(new GuiComponent() { | ||
@Override | ||
public int getWidth() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
return 30; | ||
} | ||
|
||
@Override | ||
public void render(@NotNull GuiImmediateContext context) { | ||
int height = getHeight(); | ||
RenderContext renderContext = context.getRenderContext(); | ||
int width = getWidth(); | ||
|
||
renderContext.color(1, 1, 1, 1); | ||
IMinecraft.instance.bindTexture(GuiTextures.BUTTON); | ||
renderContext.drawTexturedRect(width / 6 - 24, height - 7 - 14, 48, 16); | ||
|
||
|
||
String keyName = IMinecraft.instance.getKeyName((int) option.get()); | ||
String text = editingKeycode ? "> " + keyName + " <" : keyName; | ||
renderContext.drawStringCenteredScaledMaxWidth(text, | ||
IMinecraft.instance.getDefaultFontRenderer(), | ||
width / 6, height - 7 - 6, | ||
false, 38, 0xFF303030 | ||
); | ||
|
||
int resetX = width / 6 - 24 + 48 + 3; | ||
int resetY = height - 7 - 14 + 3; | ||
|
||
IMinecraft.instance.bindTexture(GuiTextures.RESET); | ||
renderContext.color(1, 1, 1, 1); | ||
renderContext.drawTexturedRect(resetX, resetY, 10, 11); | ||
int mouseX = context.getMouseX(); | ||
int mouseY = context.getMouseY(); | ||
if (mouseX >= resetX && mouseX < resetX + 10 && | ||
mouseY >= resetY && mouseY < resetY + 11) { | ||
renderContext.scheduleDrawTooltip(Collections.singletonList( | ||
"§cReset to Default" | ||
)); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateContext context) { | ||
if (!(mouseEvent instanceof MouseEvent.Click)) return false; | ||
MouseEvent.Click click = (MouseEvent.Click) mouseEvent; | ||
if (click.getMouseState() && click.getMouseButton() != -1 && editingKeycode) { | ||
editingKeycode = false; | ||
int mouseButton = click.getMouseButton(); | ||
System.out.println(mouseButton); | ||
option.set(mouseButton); | ||
return true; | ||
} | ||
|
||
if (click.getMouseState() && click.getMouseButton() == 0) { | ||
int height = getHeight(); | ||
int width = getHeight(); | ||
int mouseX = context.getMouseX(); | ||
int mouseY = context.getMouseY(); | ||
if (mouseX > width / 6 - 24 && mouseX < width / 6 + 24 && | ||
mouseY > height - 7 - 14 && mouseY < height - 7 + 2) { | ||
editingKeycode = true; | ||
return true; | ||
} | ||
if (mouseX > width / 6 - 24 + 48 + 3 && mouseX < width / 6 - 24 + 48 + 13 && | ||
mouseY > height - 7 - 14 + 3 && mouseY < height - 7 - 14 + 3 + 11) { | ||
option.set(defaultKeyCode); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public boolean keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) { | ||
boolean wasKeyPressedEvent = keyboardEvent instanceof KeyboardEvent.KeyPressed; | ||
if (wasKeyPressedEvent) { | ||
if (editingKeycode) { | ||
KeyboardEvent.KeyPressed keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent; | ||
editingKeycode = false; | ||
int keyCode = -1; | ||
int keycode = keyPressed.getKeycode(); | ||
if (keycode != 256 /* GLFW_KEY_ESCAPE*/ && keycode != 0) { | ||
keyCode = keycode; | ||
} | ||
//if (keyCode > 256) keyCode = 0; | ||
option.set(keyCode); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
return editingKeycode; | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public @NotNull GuiComponent getDelegate() { | ||
return component; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
modern/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/ModernKeybindHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.github.notenoughupdates.moulconfig.platform | ||
|
||
import net.minecraft.client.util.InputUtil | ||
|
||
object ModernKeybindHelper { | ||
fun getKeyName(keyCode: Int): String { | ||
if (keyCode == -1) { | ||
return "NONE" | ||
} else if (keyCode in 0..9) { | ||
return "Button ${keyCode+1}" | ||
} else { | ||
var keyName = InputUtil.fromKeyCode(keyCode, 0).localizedText.string | ||
if (keyName == null) { | ||
keyName = "???" | ||
} | ||
return keyName | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters