diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 23:35:49 +0300 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2020-05-20 23:35:49 +0300 |
commit | 9611a06a3e312855ae38eedf30bfdfecfb3598d8 (patch) | |
tree | 7925df342b7e02dcceaa7f5c2f0aa5dbc6cb3c56 /src/main | |
parent | 5cf70b3c69f663679b45d47800d298ae5eb1a11c (diff) | |
download | LibGui-9611a06a3e312855ae38eedf30bfdfecfb3598d8.tar.gz LibGui-9611a06a3e312855ae38eedf30bfdfecfb3598d8.tar.bz2 LibGui-9611a06a3e312855ae38eedf30bfdfecfb3598d8.zip |
Add focusing support for buttons
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WButton.java | 18 |
1 files changed, 16 insertions, 2 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 1f9525d..987f50b 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 @@ -11,6 +11,7 @@ import net.minecraft.text.Text; import io.github.cottonmc.cotton.gui.client.ScreenDrawing; import io.github.cottonmc.cotton.gui.widget.data.Alignment; +import org.lwjgl.glfw.GLFW; public class WButton extends WWidget { private Text label; @@ -33,13 +34,18 @@ public class WButton extends WWidget { public boolean canResize() { return true; } - + + @Override + public boolean canFocus() { + return true; + } + @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { boolean hovered = (mouseX>=0 && mouseY>=0 && mouseX<getWidth() && mouseY<getHeight()); int state = 1; //1=regular. 2=hovered. 0=disabled. if (!enabled) state = 0; - else if (hovered) state = 2; + else if (hovered || isFocused()) state = 2; float px = 1/256f; float buttonLeft = 0 * px; @@ -83,6 +89,14 @@ 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) { + onClick(0, 0, 0); + } + } + public WButton setOnClick(Runnable r) { this.onClick = r; return this; |