diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-21 00:33:38 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-21 00:33:38 +0300 |
commit | eda72ce4fb2696639d6d0a124c7b1035fd08b7c0 (patch) | |
tree | 118753145317d9d295615822562f53708eb4e38d | |
parent | e39e805a35261a27c09cc4ad6f88e19eaf0bb01a (diff) | |
download | LibGui-eda72ce4fb2696639d6d0a124c7b1035fd08b7c0.tar.gz LibGui-eda72ce4fb2696639d6d0a124c7b1035fd08b7c0.tar.bz2 LibGui-eda72ce4fb2696639d6d0a124c7b1035fd08b7c0.zip |
Add toggle button focusing (#57)
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java | 2 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java | 48 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java | 15 | ||||
-rw-r--r-- | src/main/resources/assets/libgui/textures/widget/toggle_focus.png | bin | 0 -> 4005 bytes |
4 files changed, 62 insertions, 3 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java index 987f50b..7946259 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java @@ -92,7 +92,7 @@ public class WButton extends WWidget { @Environment(EnvType.CLIENT) @Override public void onKeyPressed(int ch, int key, int modifiers) { - if (ch == GLFW.GLFW_KEY_SPACE || ch == GLFW.GLFW_KEY_ENTER || ch == GLFW.GLFW_KEY_KP_ENTER) { + if (isActivationKey(ch)) { onClick(0, 0, 0); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 69fef45..ac1f42d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -19,9 +19,11 @@ public class WToggleButton extends WWidget { // Default on/off images protected final static Identifier DEFAULT_OFF_IMAGE = new Identifier("libgui:textures/widget/toggle_off.png"); protected final static Identifier DEFAULT_ON_IMAGE = new Identifier("libgui:textures/widget/toggle_on.png"); + protected final static Identifier DEFAULT_FOCUS_IMAGE = new Identifier("libgui:textures/widget/toggle_focus.png"); protected Identifier onImage; protected Identifier offImage; + protected Identifier focusImage = DEFAULT_FOCUS_IMAGE; @Nullable protected Text label = null; @@ -59,7 +61,10 @@ public class WToggleButton extends WWidget { @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { ScreenDrawing.texturedRect(x, y, 18, 18, isOn ? onImage : offImage, 0xFFFFFFFF); - + if (isFocused()) { + ScreenDrawing.texturedRect(x, y, 18, 18, focusImage, 0xFFFFFFFF); + } + if (label!=null) { ScreenDrawing.drawString(matrices, label, x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); } @@ -70,6 +75,11 @@ public class WToggleButton extends WWidget { return true; } + @Override + public boolean canFocus() { + return true; + } + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { @@ -80,7 +90,14 @@ public class WToggleButton extends WWidget { this.isOn = !this.isOn; onToggle(this.isOn); } - + + @Override + public void onKeyPressed(int ch, int key, int modifiers) { + if (isActivationKey(ch)) { + onClick(0, 0, 0); + } + } + protected void onToggle(boolean on) { if (this.onToggle != null) { this.onToggle.accept(on); @@ -116,4 +133,31 @@ public class WToggleButton extends WWidget { return this; } + + public Identifier getOnImage() { + return onImage; + } + + public WToggleButton setOnImage(Identifier onImage) { + this.onImage = onImage; + return this; + } + + public Identifier getOffImage() { + return offImage; + } + + public WToggleButton setOffImage(Identifier offImage) { + this.offImage = offImage; + return this; + } + + public Identifier getFocusImage() { + return focusImage; + } + + public WToggleButton setFocusImage(Identifier focusImage) { + this.focusImage = focusImage; + return this; + } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java index f06356f..11387e2 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WWidget.java @@ -12,6 +12,7 @@ import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.lwjgl.glfw.GLFW; import javax.annotation.Nullable; @@ -384,4 +385,18 @@ public class WWidget { */ @Environment(EnvType.CLIENT) public void tick() {} + + /** + * Tests if the provided key code is an activation key for widgets. + * + * <p>The activation keys are Enter, keypad Enter, and Space. + * + * @param ch the key code + * @return whether the key is an activation key + * @since 2.0.0 + */ + @Environment(EnvType.CLIENT) + public static boolean isActivationKey(int ch) { + return ch == GLFW.GLFW_KEY_ENTER || ch == GLFW.GLFW_KEY_KP_ENTER || ch == GLFW.GLFW_KEY_SPACE; + } } diff --git a/src/main/resources/assets/libgui/textures/widget/toggle_focus.png b/src/main/resources/assets/libgui/textures/widget/toggle_focus.png Binary files differnew file mode 100644 index 0000000..8350fe6 --- /dev/null +++ b/src/main/resources/assets/libgui/textures/widget/toggle_focus.png |