From 8e8737afcd4dc37db0ecf1356f9db4a70750ec6a Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 12 May 2025 11:19:26 -0500 Subject: [PATCH] added a toggle --- .../ui2/components/inputs/IntInput.java | 53 +++++++++++++++++++ .../ui2/components/inputs/Toggle.java | 49 +++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/org/saturnclient/ui2/components/inputs/IntInput.java create mode 100644 src/main/java/org/saturnclient/ui2/components/inputs/Toggle.java diff --git a/src/main/java/org/saturnclient/ui2/components/inputs/IntInput.java b/src/main/java/org/saturnclient/ui2/components/inputs/IntInput.java new file mode 100644 index 0000000..da2ba10 --- /dev/null +++ b/src/main/java/org/saturnclient/ui2/components/inputs/IntInput.java @@ -0,0 +1,53 @@ +package org.saturnclient.ui2.components.inputs; + +import org.saturnclient.saturnclient.config.ConfigManager; +import org.saturnclient.saturnclient.config.Property; +import org.saturnclient.ui2.resources.Fonts; + +public class IntInput extends Input { + public Property prop; + + public IntInput(Property prop) { + this.width = 120; + this.height = Fonts.getHeight(); + this.prop = prop; + this.text = String.valueOf(prop.value); + } + + @Override + public void charTyped(char chr) { + if ((Character.isDigit(chr) || chr == '.') && text.length() < 10) { + if (text.equals("0") && cursorPosition == 1) { + text = "" + chr; + } else { + text = text.substring(0, cursorPosition) + + chr + + text.substring(cursorPosition); + + prop.value = text.isEmpty() ? 0 : Integer.parseInt(text); + ConfigManager.save(); + + cursorPosition++; + } + } + } + + @Override + public void backspace() { + if (text.length() == 0) { + text = "0"; + cursorPosition = 1; + } + prop.value = Integer.parseInt(text); + ConfigManager.save(); + } + + @Override + public void checkReset() { + if (prop.isReset) { + this.text = String.valueOf(prop.value); + this.cursorPosition = 0; + prop.isReset = false; + } + } +} diff --git a/src/main/java/org/saturnclient/ui2/components/inputs/Toggle.java b/src/main/java/org/saturnclient/ui2/components/inputs/Toggle.java new file mode 100644 index 0000000..be58056 --- /dev/null +++ b/src/main/java/org/saturnclient/ui2/components/inputs/Toggle.java @@ -0,0 +1,49 @@ +package org.saturnclient.ui2.components.inputs; + +import org.saturnclient.saturnclient.config.Property; +import org.saturnclient.saturnclient.config.ThemeManager; +import org.saturnclient.ui2.Element; +import org.saturnclient.ui2.ElementContext; +import org.saturnclient.ui2.RenderScope; + +public class Toggle extends Element { + private static ThemeManager theme = new ThemeManager("Toggle", "hovering", "enabled"); + private static Property bgColor = theme.property("bg-color", new Property(0xFF808080)); + private static Property fgColor = theme.property("fg-color", new Property(0xFF909090)); + private static Property cornerRadius = theme.property("corner-radius", new Property(30)); + + static { + theme.propertyStateDefault("enabled", "bg-color", 0xaf8B5CF6); + theme.propertyStateDefault("enabled", "fg-color", 0xff8B5CF6); + } + + private Property prop; + + public Toggle(Property prop) { + this.prop = prop; + this.width = 60; + this.height = 30; + } + + @Override + public void render(RenderScope renderScope, ElementContext ctx) { + if (prop.value) { + theme.setState("enabled"); + } + + if (ctx.isHovering()) { + theme.applyState("hovering"); + } + + renderScope.drawRoundedRectangle(0, 0, width, height, cornerRadius.value, bgColor.value); + + renderScope.drawRoundedRectangle(prop.value ? 30 : 0, 0, 30, 30, 30, fgColor.value); + + theme.setState(null); + } + + @Override + public void click(int mouseX, int mouseY) { + prop.value = !prop.value; + } +}