From 6e1b0e3f6944a720288e7b605cc4de8e4cfcd61f Mon Sep 17 00:00:00 2001 From: opfromthestart Date: Sat, 17 Apr 2021 09:12:09 -0400 Subject: [PATCH] Buttons work Clicking and disabling now works --- .../client/graphics/gui/Button.java | 60 +++++++++++-------- .../progressia/test/LayerTestGUI.java | 6 +- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/gui/Button.java b/src/main/java/ru/windcorp/progressia/client/graphics/gui/Button.java index 3f8ea5e..1fdbdb0 100644 --- a/src/main/java/ru/windcorp/progressia/client/graphics/gui/Button.java +++ b/src/main/java/ru/windcorp/progressia/client/graphics/gui/Button.java @@ -12,6 +12,7 @@ import ru.windcorp.progressia.client.graphics.backend.InputTracker; import ru.windcorp.progressia.client.graphics.flat.RenderTarget; import ru.windcorp.progressia.client.graphics.font.Font; import ru.windcorp.progressia.client.graphics.Colors; +import ru.windcorp.progressia.client.graphics.gui.event.FocusEvent; import ru.windcorp.progressia.client.graphics.gui.event.HoverEvent; import ru.windcorp.progressia.client.graphics.input.bus.InputListener; import ru.windcorp.progressia.client.graphics.input.InputEvent; @@ -40,19 +41,20 @@ public class Button extends Component { } }); - Button inButton = this; - addListener(new Object() { @Subscribe - public void onLeftClick(KeyEvent e) { - if (e.isLeftMouseButton()) - { - isClicked = e.isPress(); - onClick.accept(inButton); - requestReassembly(); - } + public void onFocusChanged(FocusEvent e) { + requestReassembly(); } }); + + Button inButton = this; + + addListener((Class) KeyEvent.class, (InputListener) e -> {isClicked = e.isPress(); + if (!inButton.isDisabled()) + onClick.accept(inButton); + requestReassembly(); + return true;}); } public boolean isClicked() @@ -70,43 +72,49 @@ public class Button extends Component { return isDisabled; } + public void setText(String newText) + { + text = newText; + requestReassembly(); + } + @Override protected void assembleSelf(RenderTarget target) { //Border - if (isClicked() || isHovered() || isFocused()) - { - target.fill(getX(), getY(), getWidth(), getHeight(), 0xFF37A2E6); - } - else if (!isDisabled()) - { - target.fill(getX(), getY(), getWidth(), getHeight(), 0xFFCBCBD0); - } - else + if (isDisabled()) { target.fill(getX(), getY(), getWidth(), getHeight(), 0xFFE5E5E5); } + else if (isClicked() || isHovered() || isFocused()) + { + target.fill(getX(), getY(), getWidth(), getHeight(), 0xFF37A2E6); + } + else + { + target.fill(getX(), getY(), getWidth(), getHeight(), 0xFFCBCBD0); + } //Inside area - if (isHovered()) + if (!isClicked() && isHovered() && !isDisabled()) { target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, 0xFFC3E4F7); } - else if (!isClicked()) + else if (!isClicked() || isDisabled()) { target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, Colors.WHITE); } //text Font tempFont; - if (isClicked()) + if (isDisabled()) + { + tempFont = font.withColor(Colors.GRAY_A); + } + else if (isClicked()) { tempFont = font.withColor(Colors.WHITE); } - else if (!isDisabled()) - { - tempFont = font.withColor(Colors.BLACK); - } else { - tempFont = font.withColor(Colors.GRAY_A); + tempFont = font.withColor(Colors.BLACK); } target.pushTransform(new Mat4().identity().translate(getX(), getY(), -1000).scale(2)); target.addCustomRenderer(tempFont.assemble( (CharSequence) this.text, this.getWidth())); diff --git a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java index 27a046e..0cf5a8e 100755 --- a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java @@ -59,7 +59,11 @@ public class LayerTestGUI extends GUILayer { TestPlayerControls tpc = TestPlayerControls.getInstance(); - panel.addChild(new Button("TestButton", new Font().withColor(Colors.BLACK), "I'm in TestGUI", b -> {})); + Button disableButton = new Button("TestButton", new Font().withColor(Colors.BLACK), "I'm in TestGUI", b -> {b.setDisable(!b.isDisabled());}); + + panel.addChild(disableButton); + + panel.addChild(new Button("TestButton", new Font().withColor(Colors.BLACK), "I enable the above button", b -> {disableButton.setDisable(false);})); panel.addChild( new Label(