From 472af56524624069a2561bf5b5f80f7fbed131e1 Mon Sep 17 00:00:00 2001 From: cvs0 <143862758+cvs0@users.noreply.github.com> Date: Sat, 6 Jul 2024 22:39:05 -0600 Subject: [PATCH] fix: Fixed crash + animated checkbox --- src/main/java/net/aoba/gui/colors/Color.java | 16 ++++++++++ src/main/java/net/aoba/gui/tabs/GoToTab.java | 3 +- .../gui/tabs/components/ButtonComponent.java | 2 -- .../tabs/components/CheckboxComponent.java | 30 +++++++++++-------- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/aoba/gui/colors/Color.java b/src/main/java/net/aoba/gui/colors/Color.java index e713625b..4729b320 100644 --- a/src/main/java/net/aoba/gui/colors/Color.java +++ b/src/main/java/net/aoba/gui/colors/Color.java @@ -60,6 +60,22 @@ public Color(int r, int g, int b, int alpha) { this.alpha = alpha; } + /** + * Interpolates between two colors. + * + * @param color1 The first color. + * @param color2 The second color. + * @param factor The interpolation factor. 0.0 will return color1, 1.0 will return color2. + * @return The interpolated color. + */ + public static Color interpolate(Color color1, Color color2, float factor) { + int r = (int) (color1.r + (color2.r - color1.r) * factor); + int g = (int) (color1.g + (color2.g - color1.g) * factor); + int b = (int) (color1.b + (color2.b - color1.b) * factor); + int alpha = (int) (color1.alpha + (color2.alpha - color1.alpha) * factor); + return new Color(r, g, b, alpha); + } + private void HSVFromRGB(int r, int g, int b) { // Calculate HSV value float rPrime = r / 255.0f; diff --git a/src/main/java/net/aoba/gui/tabs/GoToTab.java b/src/main/java/net/aoba/gui/tabs/GoToTab.java index e1663f96..3b53893a 100644 --- a/src/main/java/net/aoba/gui/tabs/GoToTab.java +++ b/src/main/java/net/aoba/gui/tabs/GoToTab.java @@ -183,8 +183,7 @@ public void OnRender(RenderEvent event) { public void OnUpdate(TickEvent event) { MinecraftClient MC = MinecraftClient.getInstance(); - if (currentNodeIndex < nodes.size() - 1) { - // Check next position + if (nodes != null && currentNodeIndex < nodes.size() - 1) { PathNode next = nodes.get(currentNodeIndex + 1); BlockPos playerPos = MC.player.getBlockPos(); diff --git a/src/main/java/net/aoba/gui/tabs/components/ButtonComponent.java b/src/main/java/net/aoba/gui/tabs/components/ButtonComponent.java index a43f5b65..1f94ca47 100644 --- a/src/main/java/net/aoba/gui/tabs/components/ButtonComponent.java +++ b/src/main/java/net/aoba/gui/tabs/components/ButtonComponent.java @@ -97,10 +97,8 @@ public void setBackgroundColor(Color color) { /** * Draws the button to the screen. - * @param offset The offset (Y location relative to parent) of the Component. * @param drawContext The current draw context of the game. * @param partialTicks The partial ticks used for interpolation. - * @param color The current Color of the UI. */ @Override public void draw(DrawContext drawContext, float partialTicks) { diff --git a/src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java b/src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java index 416116ed..df935fb8 100644 --- a/src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java +++ b/src/main/java/net/aoba/gui/tabs/components/CheckboxComponent.java @@ -35,6 +35,10 @@ public class CheckboxComponent extends Component implements MouseClickListener { private String text; private BooleanSetting checkbox; private Runnable onClick; + private boolean isHovered = false; + private float animationProgress = 0.0f; + private Color hoverBorderColor = new Color(255, 255, 255); + private Color clickAnimationColor = new Color(255, 255, 0); public CheckboxComponent(IGuiElement parent, BooleanSetting checkbox) { super(parent); @@ -48,31 +52,32 @@ public CheckboxComponent(IGuiElement parent, BooleanSetting checkbox) { /** * Draws the checkbox to the screen. - * @param offset The offset (Y location relative to parent) of the Component. * @param drawContext The current draw context of the game. * @param partialTicks The partial ticks used for interpolation. - * @param color The current Color of the UI. */ @Override public void draw(DrawContext drawContext, float partialTicks) { super.draw(drawContext, partialTicks); - + MatrixStack matrixStack = drawContext.getMatrices(); Matrix4f matrix4f = matrixStack.peek().getPositionMatrix(); - - RenderUtils.drawString(drawContext, this.text, actualX + 6, actualY + 8, 0xFFFFFF); - if (this.checkbox.getValue()) { - RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20, - new Color(0, 154, 0, 200)); - } else { - RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20, - new Color(154, 0, 0, 200)); + + // Determine border color based on hover and click state + Color borderColor = isHovered ? hoverBorderColor : new Color(128, 128, 128); + if (animationProgress > 0) { + borderColor = clickAnimationColor; + animationProgress -= partialTicks; // Decrease animation progress } + + // Determine fill color based on checkbox state + Color fillColor = this.checkbox.getValue() ? new Color(0, 154, 0, 200) : new Color(154, 0, 0, 200); + + RenderUtils.drawString(drawContext, this.text, actualX + 6, actualY + 8, 0xFFFFFF); + RenderUtils.drawOutlinedBox(matrix4f, actualX + actualWidth - 24, actualY + 5, 20, 20, borderColor, fillColor); } /** * Handles updating the Checkbox component. - * @param offset The offset (Y position relative to parent) of the Checkbox. */ @Override public void update() { @@ -93,6 +98,7 @@ public void OnMouseClick(MouseClickEvent event) { if(event.button == MouseButton.LEFT && event.action == MouseAction.DOWN) { if (hovered && Aoba.getInstance().hudManager.isClickGuiOpen()) { checkbox.toggle(); + animationProgress = 1.0f; // Reset animation progress on click if(onClick != null) { onClick.run(); }