diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2022-09-06 14:15:50 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2022-09-06 14:15:50 +0300 |
commit | 82950d5b4a58699ae816b5279c27d344d9e98185 (patch) | |
tree | aade7f98b27ffb8a1f1666b505c606425aa91653 | |
parent | 2de3805158ff954b67ab8748ff4637a058599ead (diff) | |
download | LibGui-82950d5b4a58699ae816b5279c27d344d9e98185.tar.gz LibGui-82950d5b4a58699ae816b5279c27d344d9e98185.tar.bz2 LibGui-82950d5b4a58699ae816b5279c27d344d9e98185.zip |
Rework screen input methods
- Now consistent between CottonClientScreen and
CottonInventoryScreen
- Mouse methods always call super and return true
- Keyboard methods call super if there's no focus
- keyPressed calls super and does nothing else for Esc and Tab
(this was the old behaviour in CIS, now also in CCS)
- Fixed a bug where focus was released when clicking on focused
widgets because the mouse coordinates weren't offset correctly
- Fixed CIS never releasing focus
- Fixed focus checking not using isWithinBounds
- Removed pointless null checks
3 files changed, 88 insertions, 85 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java index c82c1e6..4053360 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonClientScreen.java @@ -15,6 +15,7 @@ import io.github.cottonmc.cotton.gui.impl.client.NarrationHelper; import io.github.cottonmc.cotton.gui.widget.WPanel; import io.github.cottonmc.cotton.gui.widget.WWidget; import org.jetbrains.annotations.Nullable; +import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL11; public class CottonClientScreen extends Screen implements CottonScreenImpl { @@ -42,11 +43,11 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { protected WWidget lastResponder = null; private final MouseInputHandler<CottonClientScreen> mouseInputHandler = new MouseInputHandler<>(this); - + public CottonClientScreen(GuiDescription description) { this(ScreenTexts.EMPTY, description); } - + public CottonClientScreen(Text title, GuiDescription description) { super(title); this.description = description; @@ -57,7 +58,7 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { public GuiDescription getDescription() { return description; } - + @Override public void init() { super.init(); @@ -112,10 +113,10 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { } } } - + private void paint(MatrixStack matrices, int mouseX, int mouseY) { renderBackground(matrices); - + if (description!=null) { WPanel root = description.getRootPanel(); if (root!=null) { @@ -132,7 +133,7 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { } } } - + @Override public void render(MatrixStack matrices, int mouseX, int mouseY, float partialTicks) { paint(matrices, mouseX, mouseY); @@ -149,8 +150,7 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { VisualLogger.render(matrices); } - - + @Override public void tick() { super.tick(); @@ -161,60 +161,46 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { } } } - + @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { - if (description.getRootPanel()==null) return super.mouseClicked(mouseX, mouseY, mouseButton); - WWidget focus = description.getFocus(); - if (focus!=null) { - - int wx = focus.getAbsoluteX(); - int wy = focus.getAbsoluteY(); - - if (mouseX>=wx && mouseX<wx+focus.getWidth() && mouseY>=wy && mouseY<wy+focus.getHeight()) { - //Do nothing, focus will get the click soon - } else { - //Invalidate the component first - description.releaseFocus(focus); - } - } - super.mouseClicked(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; + mouseInputHandler.checkFocus(containerX, containerY); if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return true; mouseInputHandler.onMouseDown(containerX, containerY, mouseButton); return true; } - + @Override public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { - if (description.getRootPanel()==null) return super.mouseReleased(mouseX, mouseY, mouseButton); super.mouseReleased(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; mouseInputHandler.onMouseUp(containerX, containerY, mouseButton); - + return true; } - + @Override public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double deltaX, double deltaY) { - if (description.getRootPanel()==null) return super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); - + int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; mouseInputHandler.onMouseDrag(containerX, containerY, mouseButton, deltaX, deltaY); return true; } - + @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); - + super.mouseScrolled(mouseX, mouseY, amount); + int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; mouseInputHandler.onMouseScroll(containerX, containerY, amount); @@ -224,7 +210,7 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { @Override public void mouseMoved(double mouseX, double mouseY) { - if (description.getRootPanel()==null) return; + super.mouseMoved(mouseX, mouseY); int containerX = (int)mouseX-left; int containerY = (int)mouseY-top; @@ -233,30 +219,29 @@ public class CottonClientScreen extends Screen implements CottonScreenImpl { @Override public boolean charTyped(char ch, int keyCode) { - if (description.getFocus()==null) return false; + if (description.getFocus()==null) return super.charTyped(ch, keyCode); description.getFocus().onCharTyped(ch); return true; } - + @Override public boolean keyPressed(int ch, int keyCode, int modifiers) { - if (super.keyPressed(ch, keyCode, modifiers)) return true; - if (description.getFocus()==null) return false; - description.getFocus().onKeyPressed(ch, keyCode, modifiers); - return true; + if (ch == GLFW.GLFW_KEY_ESCAPE || ch == GLFW.GLFW_KEY_TAB) { + // special hardcoded keys, these will never be delivered to widgets + return super.keyPressed(ch, keyCode, modifiers); + } else { + if (description.getFocus() == null) return super.keyPressed(ch, keyCode, modifiers); + description.getFocus().onKeyPressed(ch, keyCode, modifiers); + return true; + } } - + @Override public boolean keyReleased(int ch, int keyCode, int modifiers) { - if (description.getFocus()==null) return false; + if (description.getFocus()==null) return super.keyReleased(ch, keyCode, modifiers); description.getFocus().onKeyReleased(ch, keyCode, modifiers); return true; } - - //@Override - //public Element getFocused() { - //return this; - //} @Override public void renderTextHover(MatrixStack matrices, @Nullable Style textStyle, int x, int y) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java index 8714ad2..74fec98 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/CottonInventoryScreen.java @@ -177,61 +177,35 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl //...yeah, we're going to go ahead and override that. return false; } - - @Override - public boolean charTyped(char ch, int keyCode) { - if (description.getFocus()==null) return false; - description.getFocus().onCharTyped(ch); - return true; - } - - @Override - public boolean keyPressed(int ch, int keyCode, int modifiers) { - if (ch == GLFW.GLFW_KEY_ESCAPE || ch == GLFW.GLFW_KEY_TAB) { - // special hardcoded keys, these will never be delivered to widgets - return super.keyPressed(ch, keyCode, modifiers); - } else { - if (description.getFocus()==null) { - return super.keyPressed(ch, keyCode, modifiers); - } else { - description.getFocus().onKeyPressed(ch, keyCode, modifiers); - return true; - } - } - } - - @Override - public boolean keyReleased(int ch, int keyCode, int modifiers) { - if (description.getFocus()==null) return false; - description.getFocus().onKeyReleased(ch, keyCode, modifiers); - return true; - } - + @Override public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { - boolean result = super.mouseClicked(mouseX, mouseY, mouseButton); + super.mouseClicked(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; - if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result; + mouseInputHandler.checkFocus(containerX, containerY); + if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return true; mouseInputHandler.onMouseDown(containerX, containerY, mouseButton); return true; } - + @Override - public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { //Testing shows that STATE IS ACTUALLY BUTTON + public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) { super.mouseReleased(mouseX, mouseY, mouseButton); + int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; mouseInputHandler.onMouseUp(containerX, containerY, mouseButton); return true; } - + @Override public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double deltaX, double deltaY) { super.mouseDragged(mouseX, mouseY, mouseButton, deltaX, deltaY); - + int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; mouseInputHandler.onMouseDrag(containerX, containerY, mouseButton, deltaX, deltaY); @@ -241,7 +215,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - if (description.getRootPanel()==null) return super.mouseScrolled(mouseX, mouseY, amount); + super.mouseScrolled(mouseX, mouseY, amount); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; @@ -252,7 +226,7 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl @Override public void mouseMoved(double mouseX, double mouseY) { - if (description.getRootPanel()==null) return; + super.mouseMoved(mouseX, mouseY); int containerX = (int)mouseX-x; int containerY = (int)mouseY-y; @@ -260,6 +234,32 @@ public class CottonInventoryScreen<T extends SyncedGuiDescription> extends Handl } @Override + public boolean charTyped(char ch, int keyCode) { + if (description.getFocus()==null) return super.charTyped(ch, keyCode); + description.getFocus().onCharTyped(ch); + return true; + } + + @Override + public boolean keyPressed(int ch, int keyCode, int modifiers) { + if (ch == GLFW.GLFW_KEY_ESCAPE || ch == GLFW.GLFW_KEY_TAB) { + // special hardcoded keys, these will never be delivered to widgets + return super.keyPressed(ch, keyCode, modifiers); + } else { + if (description.getFocus() == null) return super.keyPressed(ch, keyCode, modifiers); + description.getFocus().onKeyPressed(ch, keyCode, modifiers); + return true; + } + } + + @Override + public boolean keyReleased(int ch, int keyCode, int modifiers) { + if (description.getFocus()==null) return super.keyReleased(ch, keyCode, modifiers); + description.getFocus().onKeyReleased(ch, keyCode, modifiers); + return true; + } + + @Override protected void drawBackground(MatrixStack matrices, float partialTicks, int mouseX, int mouseY) {} //This is just an AbstractContainerScreen thing; most Screens don't work this way. private void paint(MatrixStack matrices, int mouseX, int mouseY) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java index 02b8d9a..0814e16 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/client/MouseInputHandler.java @@ -131,4 +131,22 @@ public final class MouseInputHandler<S extends Screen & CottonScreenImpl> { return current; } + + /** + * Checks if the focus is not being hovered and releases it in that case. + * + * @param mouseX the mouse cursor's X coordinate in widget space + * @param mouseY the mouse cursor's Y coordinate in widget space + */ + public void checkFocus(int mouseX, int mouseY) { + WWidget focus = screen.getDescription().getFocus(); + if (focus != null) { + int wx = focus.getAbsoluteX(); + int wy = focus.getAbsoluteY(); + + if (!focus.isWithinBounds(mouseX - wx, mouseY - wy)) { + screen.getDescription().releaseFocus(focus); + } + } + } } |