From 5359b93738fea9e86b6dc8e4c43b0b24e8b8ed81 Mon Sep 17 00:00:00 2001 From: opfromthestart Date: Sat, 17 Apr 2021 08:07:01 -0400 Subject: [PATCH] Bettering the Button Not really TPS is 20 now --- .../client/graphics/gui/Button.java | 31 ++++++++++--- .../progressia/server/ServerThread.java | 2 +- .../progressia/test/LayerTestGUI.java | 3 ++ .../windcorp/progressia/test/LayerTestUI.java | 43 +------------------ 4 files changed, 32 insertions(+), 47 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 ed87fd1..c37d813 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 @@ -2,6 +2,8 @@ package ru.windcorp.progressia.client.graphics.gui; import java.util.function.Supplier; +import com.google.common.eventbus.Subscribe; + import glm.mat._4.Mat4; import glm.vec._2.i.Vec2i; import org.lwjgl.glfw.GLFW; @@ -9,6 +11,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.HoverEvent; import ru.windcorp.progressia.client.graphics.input.bus.InputListener; import ru.windcorp.progressia.client.graphics.input.InputEvent; import ru.windcorp.progressia.client.graphics.input.KeyEvent; @@ -20,19 +23,37 @@ public class Button extends Component { private Vec2i currentSize; private String text; private boolean isDisabled; + private boolean isClicked; public Button(String name, Font font, String text) {//, InputListener onClick, Class onClickClass) { super(name); this.font = font; this.text = text; - setPosition(400, 400); - setSize(107,34); + setPreferredSize(107,34); //super.addListener(onClickClass, onClick); + + addListener(new Object() { + @Subscribe + public void onHoverChanged(HoverEvent e) { + requestReassembly(); + } + }); + + addListener(new Object() { + @Subscribe + public void onLeftClick(KeyEvent e) { + if (e.isLeftMouseButton()) + { + isClicked = e.isPress(); + requestReassembly(); + } + } + }); } public boolean isClicked() { - return super.containsCursor() && InputTracker.isKeyPressed(GLFW.GLFW_MOUSE_BUTTON_LEFT); + return containsCursor() && isClicked; } public void setDisable(boolean isDisabled) @@ -63,11 +84,11 @@ public class Button extends Component { //Inside area if (isHovered()) { - target.fill(getX()+1, getY()+1, getWidth()-2, getHeight()-2, 0xFFC3E4F7); + target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, 0xFFC3E4F7); } else if (!isClicked()) { - target.fill(getX()+1, getY()+1, getWidth()-2, getHeight()-2, Colors.WHITE); + target.fill(getX()+2, getY()+2, getWidth()-4, getHeight()-4, Colors.WHITE); } //text Font tempFont; diff --git a/src/main/java/ru/windcorp/progressia/server/ServerThread.java b/src/main/java/ru/windcorp/progressia/server/ServerThread.java index e4ba615..90f3ce9 100644 --- a/src/main/java/ru/windcorp/progressia/server/ServerThread.java +++ b/src/main/java/ru/windcorp/progressia/server/ServerThread.java @@ -64,7 +64,7 @@ public class ServerThread implements Runnable { public void start() { ticker.start(); - executor.scheduleAtFixedRate(this, 0, 1000, TimeUnit.MILLISECONDS); + executor.scheduleAtFixedRate(this, 0, 1000 / 20, TimeUnit.MILLISECONDS); } @Override diff --git a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java index 7bb1bce..de0bf8c 100755 --- a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java @@ -29,6 +29,7 @@ import ru.windcorp.progressia.client.graphics.font.Font; import ru.windcorp.progressia.client.graphics.gui.Button; import ru.windcorp.progressia.client.graphics.gui.DynamicLabel; import ru.windcorp.progressia.client.graphics.gui.GUILayer; +import ru.windcorp.progressia.client.graphics.gui.Button; import ru.windcorp.progressia.client.graphics.gui.Label; import ru.windcorp.progressia.client.graphics.gui.Panel; import ru.windcorp.progressia.client.graphics.gui.layout.LayoutAlign; @@ -57,6 +58,8 @@ public class LayerTestGUI extends GUILayer { Font font = new Font().withColor(color).deriveOutlined(); TestPlayerControls tpc = TestPlayerControls.getInstance(); + + panel.addChild(new Button("TestButton", new Font().withColor(Colors.BLACK), "I'm in TestGUI")); panel.addChild( new Label( diff --git a/src/main/java/ru/windcorp/progressia/test/LayerTestUI.java b/src/main/java/ru/windcorp/progressia/test/LayerTestUI.java index 686a72e..c93a055 100755 --- a/src/main/java/ru/windcorp/progressia/test/LayerTestUI.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerTestUI.java @@ -18,9 +18,6 @@ package ru.windcorp.progressia.test; -import java.util.Objects; -import java.util.function.Supplier; - import org.lwjgl.glfw.GLFW; import com.google.common.eventbus.Subscribe; @@ -32,34 +29,18 @@ import ru.windcorp.progressia.client.graphics.Colors; import ru.windcorp.progressia.client.graphics.backend.GraphicsInterface; import ru.windcorp.progressia.client.graphics.flat.AssembledFlatLayer; import ru.windcorp.progressia.client.graphics.flat.RenderTarget; -import ru.windcorp.progressia.client.graphics.font.Font; -import ru.windcorp.progressia.client.graphics.gui.Button; -import ru.windcorp.progressia.client.graphics.gui.Label; import ru.windcorp.progressia.client.graphics.input.KeyEvent; import ru.windcorp.progressia.client.graphics.input.bus.Input; import ru.windcorp.progressia.client.graphics.model.LambdaModel; import ru.windcorp.progressia.client.graphics.texture.SimpleTextures; import ru.windcorp.progressia.client.graphics.texture.Texture; import ru.windcorp.progressia.client.graphics.world.Camera; -import ru.windcorp.progressia.client.localization.MutableString; -import ru.windcorp.progressia.client.localization.MutableStringLocalized; public class LayerTestUI extends AssembledFlatLayer { - - private Button testButton; - private Label tl; - + public LayerTestUI() { super("TestUI"); - TestPlayerControls tpc = TestPlayerControls.getInstance(); - - testButton = new Button("TestButton", new Font().withColor(Colors.BLACK), "Hi"); - tl = new Label( - "IsFlyingDisplay", - new Font().withColor(Colors.BLACK), - tmp_dynFormat("LayerTestGUI.IsFlyingDisplay", tpc::isFlying) - ); GraphicsInterface.subscribeToInputEvents(this); } @@ -106,9 +87,7 @@ public class LayerTestUI extends AssembledFlatLayer { ) ); target.popTransform(); - - testButton.requestReassembly(); - + drawCross(target); } @@ -178,23 +157,5 @@ public class LayerTestUI extends AssembledFlatLayer { flag = event.isPress(); invalidate(); } - - private static MutableString tmp_dynFormat(String formatKey, Supplier... suppliers) { - return new MutableStringLocalized(formatKey).apply(s -> { - Object[] args = new Object[suppliers.length]; - for (int i = 0; i < suppliers.length; ++i) { - Supplier supplier = suppliers[i]; - - Object value = supplier != null ? supplier.get() : "null"; - if (!(value instanceof Number)) { - value = Objects.toString(value); - } - - args[i] = value; - } - - return String.format(s, args); - }); - } }