From 9dbf431a55a6ce1e6e515a07a26fbe38e93d65fd Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Fri, 6 Mar 2026 00:53:11 +0100 Subject: [PATCH] Element and animations --- .../org/saturnclient/config/manager/Key.java | 97 +++++++++++++++++++ .../java/org/saturnclient/ui/Element.java | 0 .../org/saturnclient/ui/ElementContext.java | 0 .../main/java/org/saturnclient/ui/Utils.java | 63 ++++++++++++ .../org/saturnclient/ui/anim/Animation.java | 37 +++++++ .../java/org/saturnclient/ui/anim/Curve.java | 22 +++++ .../java/org/saturnclient/ui/anim/Fade.java | 27 ++++++ .../org/saturnclient/ui/anim/FadeOut.java | 27 ++++++ .../org/saturnclient/ui/anim/SlideFade.java | 39 ++++++++ .../java/org/saturnclient/ui/anim/SlideX.java | 39 ++++++++ .../java/org/saturnclient/ui/anim/SlideY.java | 39 ++++++++ .../main/java/org/saturnclient/ui/Utils.java | 65 ------------- 12 files changed, 390 insertions(+), 65 deletions(-) rename {versions/1.21.4 => common}/src/main/java/org/saturnclient/ui/Element.java (100%) rename {versions/1.21.4 => common}/src/main/java/org/saturnclient/ui/ElementContext.java (100%) create mode 100644 common/src/main/java/org/saturnclient/ui/Utils.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/Animation.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/Curve.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/Fade.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/FadeOut.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/SlideFade.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/SlideX.java create mode 100644 common/src/main/java/org/saturnclient/ui/anim/SlideY.java delete mode 100644 versions/1.21.4/src/main/java/org/saturnclient/ui/Utils.java diff --git a/common/src/main/java/org/saturnclient/config/manager/Key.java b/common/src/main/java/org/saturnclient/config/manager/Key.java index d6e7b81..ae413ef 100644 --- a/common/src/main/java/org/saturnclient/config/manager/Key.java +++ b/common/src/main/java/org/saturnclient/config/manager/Key.java @@ -52,4 +52,101 @@ public class Key { GLFW_KEY_GRAVE_ACCENT = 96, GLFW_KEY_WORLD_1 = 161, GLFW_KEY_WORLD_2 = 162; + + /** Function keys. */ + public static final int GLFW_KEY_ESCAPE = 256, + GLFW_KEY_ENTER = 257, + GLFW_KEY_TAB = 258, + GLFW_KEY_BACKSPACE = 259, + GLFW_KEY_INSERT = 260, + GLFW_KEY_DELETE = 261, + GLFW_KEY_RIGHT = 262, + GLFW_KEY_LEFT = 263, + GLFW_KEY_DOWN = 264, + GLFW_KEY_UP = 265, + GLFW_KEY_PAGE_UP = 266, + GLFW_KEY_PAGE_DOWN = 267, + GLFW_KEY_HOME = 268, + GLFW_KEY_END = 269, + GLFW_KEY_CAPS_LOCK = 280, + GLFW_KEY_SCROLL_LOCK = 281, + GLFW_KEY_NUM_LOCK = 282, + GLFW_KEY_PRINT_SCREEN = 283, + GLFW_KEY_PAUSE = 284, + GLFW_KEY_F1 = 290, + GLFW_KEY_F2 = 291, + GLFW_KEY_F3 = 292, + GLFW_KEY_F4 = 293, + GLFW_KEY_F5 = 294, + GLFW_KEY_F6 = 295, + GLFW_KEY_F7 = 296, + GLFW_KEY_F8 = 297, + GLFW_KEY_F9 = 298, + GLFW_KEY_F10 = 299, + GLFW_KEY_F11 = 300, + GLFW_KEY_F12 = 301, + GLFW_KEY_F13 = 302, + GLFW_KEY_F14 = 303, + GLFW_KEY_F15 = 304, + GLFW_KEY_F16 = 305, + GLFW_KEY_F17 = 306, + GLFW_KEY_F18 = 307, + GLFW_KEY_F19 = 308, + GLFW_KEY_F20 = 309, + GLFW_KEY_F21 = 310, + GLFW_KEY_F22 = 311, + GLFW_KEY_F23 = 312, + GLFW_KEY_F24 = 313, + GLFW_KEY_F25 = 314, + GLFW_KEY_KP_0 = 320, + GLFW_KEY_KP_1 = 321, + GLFW_KEY_KP_2 = 322, + GLFW_KEY_KP_3 = 323, + GLFW_KEY_KP_4 = 324, + GLFW_KEY_KP_5 = 325, + GLFW_KEY_KP_6 = 326, + GLFW_KEY_KP_7 = 327, + GLFW_KEY_KP_8 = 328, + GLFW_KEY_KP_9 = 329, + GLFW_KEY_KP_DECIMAL = 330, + GLFW_KEY_KP_DIVIDE = 331, + GLFW_KEY_KP_MULTIPLY = 332, + GLFW_KEY_KP_SUBTRACT = 333, + GLFW_KEY_KP_ADD = 334, + GLFW_KEY_KP_ENTER = 335, + GLFW_KEY_KP_EQUAL = 336, + GLFW_KEY_LEFT_SHIFT = 340, + GLFW_KEY_LEFT_CONTROL = 341, + GLFW_KEY_LEFT_ALT = 342, + GLFW_KEY_LEFT_SUPER = 343, + GLFW_KEY_RIGHT_SHIFT = 344, + GLFW_KEY_RIGHT_CONTROL = 345, + GLFW_KEY_RIGHT_ALT = 346, + GLFW_KEY_RIGHT_SUPER = 347, + GLFW_KEY_MENU = 348, + GLFW_KEY_LAST = GLFW_KEY_MENU; + + /** If this bit is set one or more Shift keys were held down. */ + public static final int GLFW_MOD_SHIFT = 0x1; + + /** If this bit is set one or more Control keys were held down. */ + public static final int GLFW_MOD_CONTROL = 0x2; + + /** If this bit is set one or more Alt keys were held down. */ + public static final int GLFW_MOD_ALT = 0x4; + + /** If this bit is set one or more Super keys were held down. */ + public static final int GLFW_MOD_SUPER = 0x8; + + /** + * If this bit is set the Caps Lock key is enabled and the + * {@link #GLFW_LOCK_KEY_MODS LOCK_KEY_MODS} input mode is set. + */ + public static final int GLFW_MOD_CAPS_LOCK = 0x10; + + /** + * If this bit is set the Num Lock key is enabled and the + * {@link #GLFW_LOCK_KEY_MODS LOCK_KEY_MODS} input mode is set. + */ + public static final int GLFW_MOD_NUM_LOCK = 0x20; } diff --git a/versions/1.21.4/src/main/java/org/saturnclient/ui/Element.java b/common/src/main/java/org/saturnclient/ui/Element.java similarity index 100% rename from versions/1.21.4/src/main/java/org/saturnclient/ui/Element.java rename to common/src/main/java/org/saturnclient/ui/Element.java diff --git a/versions/1.21.4/src/main/java/org/saturnclient/ui/ElementContext.java b/common/src/main/java/org/saturnclient/ui/ElementContext.java similarity index 100% rename from versions/1.21.4/src/main/java/org/saturnclient/ui/ElementContext.java rename to common/src/main/java/org/saturnclient/ui/ElementContext.java diff --git a/common/src/main/java/org/saturnclient/ui/Utils.java b/common/src/main/java/org/saturnclient/ui/Utils.java new file mode 100644 index 0000000..9f66b5e --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/Utils.java @@ -0,0 +1,63 @@ +package org.saturnclient.ui; + +import java.util.List; + +import org.saturnclient.config.manager.Key; + +public class Utils { + public static boolean isHovering(int mouseX, int mouseY, int elementWidth, int elementHeight, float elementScale) { + return mouseX >= 0 && + mouseX <= (elementWidth * elementScale) && + mouseY >= 0 && + mouseY <= (elementHeight * elementScale); + } + + public static char getCharFromKey(int keyCode, int modifiers) { + boolean shift = (modifiers & Key.GLFW_MOD_SHIFT) != 0; + + // A-Z + if (keyCode >= Key.GLFW_KEY_A && keyCode <= Key.GLFW_KEY_Z) { + return (char) (shift + ? 'A' + (keyCode - Key.GLFW_KEY_A) + : 'a' + (keyCode - Key.GLFW_KEY_A)); + } + + // 0-9 + if (keyCode >= Key.GLFW_KEY_0 && keyCode <= Key.GLFW_KEY_9) { + return shift + ? ")!@#$%^&*(".charAt(keyCode - Key.GLFW_KEY_0) + : (char) ('0' + (keyCode - Key.GLFW_KEY_0)); + } + + // Special characters + return switch (keyCode) { + case Key.GLFW_KEY_SPACE -> ' '; + case Key.GLFW_KEY_PERIOD -> shift ? '>' : '.'; + case Key.GLFW_KEY_COMMA -> shift ? '<' : ','; + case Key.GLFW_KEY_MINUS -> shift ? '_' : '-'; + case Key.GLFW_KEY_EQUAL -> shift ? '+' : '='; + case Key.GLFW_KEY_SEMICOLON -> shift ? ':' : ';'; + case Key.GLFW_KEY_APOSTROPHE -> shift ? '"' : '\''; + case Key.GLFW_KEY_SLASH -> shift ? '?' : '/'; + case Key.GLFW_KEY_BACKSLASH -> shift ? '|' : '\\'; + case Key.GLFW_KEY_LEFT_BRACKET -> shift ? '{' : '['; + case Key.GLFW_KEY_RIGHT_BRACKET -> shift ? '}' : ']'; + case Key.GLFW_KEY_GRAVE_ACCENT -> shift ? '~' : '`'; + default -> '\0'; + }; + } + + public static T getOrNull(List list, int index) { + return (index >= 0 && index < list.size()) ? list.get(index) : null; + } + + // public static void notify(NotificationKind kind, String title, String toast) + // { + // if (SaturnClient.client.currentScreen instanceof SaturnScreen) { + // ((SaturnScreen) SaturnClient.client.currentScreen) + // .draw(new Notification(SaturnClient.client.currentScreen.width, + // SaturnClient.client.currentScreen.height, kind, + // title, toast)); + // } + // } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/Animation.java b/common/src/main/java/org/saturnclient/ui/anim/Animation.java new file mode 100644 index 0000000..55982ac --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/Animation.java @@ -0,0 +1,37 @@ +package org.saturnclient.ui.anim; + +import java.util.function.Function; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public abstract class Animation { + public int duration; + public int delay = 0; + public Function curve = Curve::easeInOutCubic; + + Animation(AnimationConfig config) { + this.duration = config.duration.value; + switch (config.curve.value) { + case 0: + this.curve = Curve::easeInOutCubic; + break; + case 1: + this.curve = Curve::easeOutCubic; + break; + case 2: + this.curve = Curve::easeInOutBack; + break; + default: + break; + } + } + + Animation(int duration) { + this.duration = duration; + } + + public abstract void tick(double progress, Element element); + + public abstract void init(Element element); +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/Curve.java b/common/src/main/java/org/saturnclient/ui/anim/Curve.java new file mode 100644 index 0000000..e19d503 --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/Curve.java @@ -0,0 +1,22 @@ +package org.saturnclient.ui.anim; + +public class Curve { + public static double easeOutCubic(double x) { + return 1 - Math.pow(1 - x, 3); + } + + public static double easeInOutCubic(double x) { + return (x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2); + } + + public static double easeInOutBack(double x) { + double c1 = 2.5; + double c2 = c1 * 1.525; + + if (x < 0.5) { + return (Math.pow(2 * x, 2) * ((c2 + 1) * 2 * x - c2)) / 2; + } else { + return (Math.pow(2 * x - 2, 2) * ((c2 + 1) * (2 * x - 2) + c2) + 2) / 2; + } + } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/Fade.java b/common/src/main/java/org/saturnclient/ui/anim/Fade.java new file mode 100644 index 0000000..9a5ecef --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/Fade.java @@ -0,0 +1,27 @@ +package org.saturnclient.ui.anim; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public class Fade extends Animation { + float targetOpacity; + + public Fade(int duration) { + super(duration); + } + + public Fade(AnimationConfig config) { + super(config); + } + + @Override + public void tick(double progress, Element element) { + element.opacity = Math.max(0.1f, targetOpacity * (float) progress); + } + + @Override + public void init(Element element) { + targetOpacity = element.opacity; + element.opacity = 0.1f; + } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/FadeOut.java b/common/src/main/java/org/saturnclient/ui/anim/FadeOut.java new file mode 100644 index 0000000..4172883 --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/FadeOut.java @@ -0,0 +1,27 @@ +package org.saturnclient.ui.anim; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public class FadeOut extends Animation { + float targetOpacity; + + public FadeOut(int duration) { + super(duration); + } + + public FadeOut(AnimationConfig config) { + super(config); + } + + @Override + public void tick(double progress, Element element) { + element.opacity = targetOpacity - (float) progress; + } + + @Override + public void init(Element element) { + targetOpacity = element.opacity; + // element.opacity = 1.0f; + } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/SlideFade.java b/common/src/main/java/org/saturnclient/ui/anim/SlideFade.java new file mode 100644 index 0000000..1318287 --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/SlideFade.java @@ -0,0 +1,39 @@ +package org.saturnclient.ui.anim; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public class SlideFade extends Animation { + public Fade fade; + public SlideY slideY; + + public SlideFade(int duration, int offset) { + super(duration); + this.fade = new Fade(duration); + this.slideY = new SlideY(duration, offset); + } + + public SlideFade(AnimationConfig config, int offset) { + super(config); + this.fade = new Fade(duration); + this.slideY = new SlideY(duration, offset); + } + + @Override + public void tick(double progress, Element element) { + fade.delay = this.delay; + fade.duration = this.duration; + + slideY.delay = this.delay; + slideY.duration = this.duration; + + fade.tick(progress, element); + slideY.tick(progress, element); + } + + @Override + public void init(Element element) { + fade.init(element); + slideY.init(element); + } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/SlideX.java b/common/src/main/java/org/saturnclient/ui/anim/SlideX.java new file mode 100644 index 0000000..82792ba --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/SlideX.java @@ -0,0 +1,39 @@ +package org.saturnclient.ui.anim; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public class SlideX extends Animation { + int offset = 20; + double startX; + + public SlideX(AnimationConfig config) { + super(config); + } + + public SlideX(AnimationConfig config, int offset) { + super(config); + this.offset = offset; + } + + public SlideX(int duration) { + super(duration); + } + + public SlideX(int duration, int offset) { + super(duration); + this.offset = offset; + } + + @Override + public void init(Element element) { + startX = element.x - offset; + element.x = (int) startX; + } + + @Override + public void tick(double progress, Element element) { + double currentX = startX + offset * progress; + element.x = (int) currentX; + } +} diff --git a/common/src/main/java/org/saturnclient/ui/anim/SlideY.java b/common/src/main/java/org/saturnclient/ui/anim/SlideY.java new file mode 100644 index 0000000..0c0aa8a --- /dev/null +++ b/common/src/main/java/org/saturnclient/ui/anim/SlideY.java @@ -0,0 +1,39 @@ +package org.saturnclient.ui.anim; + +import org.saturnclient.config.AnimationConfig; +import org.saturnclient.ui.Element; + +public class SlideY extends Animation { + int offset = 20; + double startY; + + public SlideY(AnimationConfig config) { + super(config); + } + + public SlideY(AnimationConfig config, int offset) { + super(config); + this.offset = offset; + } + + public SlideY(int duration) { + super(duration); + } + + public SlideY(int duration, int offset) { + super(duration); + this.offset = offset; + } + + @Override + public void init(Element element) { + startY = element.y - offset; + element.y = (int) startY; + } + + @Override + public void tick(double progress, Element element) { + double currentY = startY + offset * progress; + element.y = (int) currentY; + } +} diff --git a/versions/1.21.4/src/main/java/org/saturnclient/ui/Utils.java b/versions/1.21.4/src/main/java/org/saturnclient/ui/Utils.java deleted file mode 100644 index e85ce9e..0000000 --- a/versions/1.21.4/src/main/java/org/saturnclient/ui/Utils.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.saturnclient.ui; - -import java.util.List; - -import org.lwjgl.glfw.GLFW; -import org.saturnclient.saturnclient.SaturnClient; -import org.saturnclient.ui.elements.Notification; -import org.saturnclient.ui.elements.Notification.NotificationKind; - -public class Utils { - public static boolean isHovering(int mouseX, int mouseY, int elementWidth, int elementHeight, float elementScale) { - return mouseX >= 0 && - mouseX <= (elementWidth * elementScale) && - mouseY >= 0 && - mouseY <= (elementHeight * elementScale); - } - - public static char getCharFromKey(int keyCode, int modifiers) { - boolean shift = (modifiers & GLFW.GLFW_MOD_SHIFT) != 0; - - // A-Z - if (keyCode >= GLFW.GLFW_KEY_A && keyCode <= GLFW.GLFW_KEY_Z) { - return (char) (shift - ? 'A' + (keyCode - GLFW.GLFW_KEY_A) - : 'a' + (keyCode - GLFW.GLFW_KEY_A)); - } - - // 0-9 - if (keyCode >= GLFW.GLFW_KEY_0 && keyCode <= GLFW.GLFW_KEY_9) { - return shift - ? ")!@#$%^&*(".charAt(keyCode - GLFW.GLFW_KEY_0) - : (char) ('0' + (keyCode - GLFW.GLFW_KEY_0)); - } - - // Special characters - return switch (keyCode) { - case GLFW.GLFW_KEY_SPACE -> ' '; - case GLFW.GLFW_KEY_PERIOD -> shift ? '>' : '.'; - case GLFW.GLFW_KEY_COMMA -> shift ? '<' : ','; - case GLFW.GLFW_KEY_MINUS -> shift ? '_' : '-'; - case GLFW.GLFW_KEY_EQUAL -> shift ? '+' : '='; - case GLFW.GLFW_KEY_SEMICOLON -> shift ? ':' : ';'; - case GLFW.GLFW_KEY_APOSTROPHE -> shift ? '"' : '\''; - case GLFW.GLFW_KEY_SLASH -> shift ? '?' : '/'; - case GLFW.GLFW_KEY_BACKSLASH -> shift ? '|' : '\\'; - case GLFW.GLFW_KEY_LEFT_BRACKET -> shift ? '{' : '['; - case GLFW.GLFW_KEY_RIGHT_BRACKET -> shift ? '}' : ']'; - case GLFW.GLFW_KEY_GRAVE_ACCENT -> shift ? '~' : '`'; - default -> '\0'; - }; - } - - public static T getOrNull(List list, int index) { - return (index >= 0 && index < list.size()) ? list.get(index) : null; - } - - public static void notify(NotificationKind kind, String title, String toast) { - if (SaturnClient.client.currentScreen instanceof SaturnScreen) { - ((SaturnScreen) SaturnClient.client.currentScreen) - .draw(new Notification(SaturnClient.client.currentScreen.width, - SaturnClient.client.currentScreen.height, kind, - title, toast)); - } - } -}