From ff02427aa32f0bd37bc61d48441daebdb845a1e7 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 26 Apr 2025 14:55:13 +0200 Subject: [PATCH] Rounded borders --- .../org/saturnclient/ui2/RenderScope.java | 95 +++++++++++++++---- .../org/saturnclient/ui2/elements/Button.java | 18 +++- 2 files changed, 91 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/saturnclient/ui2/RenderScope.java b/src/main/java/org/saturnclient/ui2/RenderScope.java index 7e82d7b..d24cb74 100644 --- a/src/main/java/org/saturnclient/ui2/RenderScope.java +++ b/src/main/java/org/saturnclient/ui2/RenderScope.java @@ -137,6 +137,81 @@ public class RenderScope { this.matrices.pop(); } + public void drawRoundedBorderCornerTopLeft(int s, int radius, int color) { + for (double angle = Math.PI / 2; angle <= Math.PI; angle += Math.PI / 180) { + int x = (int) Math.round(radius * Math.cos(angle)); + int y = (int) Math.round(radius * Math.sin(angle)); + + int snappedX = radius + x; + int snappedY = radius - y; + + drawRect(snappedX, snappedY, s, s, color); + } + } + + + public void drawRoundedBorderCornerTopRight(int s, int radius, int color) { + for (double angle = Math.PI; angle >= Math.PI / 2; angle -= Math.PI / 180) { + int x = (int) Math.round(radius * Math.cos(angle)); + int y = (int) Math.round(radius * Math.sin(angle)); + + int snappedX = radius - x; + int snappedY = radius - y; + + drawRect(snappedX, snappedY, s, s, color); + } + } + + public void drawRoundedBorderCornerBottomLeft(int s, int radius, int color) { + for (double angle = Math.PI; angle <= Math.PI * 1.5; angle += Math.PI / 180) { + int x = (int) Math.round(radius * Math.cos(angle)); + int y = (int) Math.round(radius * Math.sin(angle)); + + int snappedX = radius + x; + int snappedY = radius - y; + + drawRect(snappedX, snappedY, s, s, color); + } + } + + public void drawRoundedBorderCornerBottomRight(int s, int radius, int color) { + for (double angle = Math.PI * 1.5; angle <= Math.PI * 2; angle += Math.PI / 180) { + int x = (int) Math.round(radius * Math.cos(angle)); + int y = (int) Math.round(radius * Math.sin(angle)); + + int snappedX = radius + x; + int snappedY = radius - y; + + drawRect(snappedX, snappedY, s, s, color); + } + } + + public void drawRoundedBorder(int width, int height, int size, int radius, int color) { + radius = Math.min(radius, Math.min(width, height)) * 10; + size *= 10; + width *= 20; + height *= 20; + + matrices.push(); + matrices.scale(0.05f, 0.05f, 1.0f); + drawRoundedBorderCornerTopLeft(size, radius, color); + + drawRect(radius, 0, width - (radius * 2), size, color); + drawRect(0, radius, size, height - (radius * 2), color); + + drawRect(radius, height - size, width - (radius * 2), size, color); + + drawRect(width - size, radius, size, height - (radius * 2), color); + + matrices.translate(width - (radius * 2) - size, 0, 0); + drawRoundedBorderCornerTopRight(size, radius, color); + matrices.translate(0, height - (radius * 2) - size, 0); + drawRoundedBorderCornerBottomRight(size, radius, color); + matrices.translate(-(width - (radius * 2) - size), 0, 0); + drawRoundedBorderCornerBottomLeft(size, radius, color); + matrices.pop(); + } + public int getScaledWindowWidth() { return SaturnClient.client.getWindow().getScaledWidth(); } @@ -172,24 +247,4 @@ public class RenderScope { vertexConsumer.vertex(matrix4f, (float)x2, (float)y2, 0.0F).texture(u2, v2).color(color); vertexConsumer.vertex(matrix4f, (float)x2, (float)y1, 0.0F).texture(u2, v1).color(color); } - - public void drawRoundedBorderCorner(int width, int height, int radius, int color) { - int s = 3; - - // Step through the arc by angle - for (double angle = Math.PI; angle >= Math.PI / 2; angle -= Math.PI / 180) { - int x = (int) Math.round(radius * Math.cos(angle)); - int y = (int) Math.round(radius * Math.sin(angle)); - - // Snap position to s-sized grid to avoid overlapping - int snappedX = (radius - x) / s * s; - int snappedY = (radius - y) / s * s; - - drawRect(snappedX, snappedY, s, s, color); - } - } - - public void drawRoundedBorder(int x, int y, int width, int height, int borderSize, int radius, int color) { - - } } diff --git a/src/main/java/org/saturnclient/ui2/elements/Button.java b/src/main/java/org/saturnclient/ui2/elements/Button.java index 21106ad..bbb2c25 100644 --- a/src/main/java/org/saturnclient/ui2/elements/Button.java +++ b/src/main/java/org/saturnclient/ui2/elements/Button.java @@ -8,6 +8,7 @@ import org.saturnclient.ui2.RenderScope; import org.saturnclient.ui2.resources.Fonts; import net.minecraft.util.Identifier; +import net.minecraft.util.math.RotationAxis; public class Button extends Element { private static ThemeManager theme = new ThemeManager("Button", "hovering"); @@ -32,8 +33,21 @@ public class Button extends Element { @Override public void render(RenderScope renderScope, RenderContext ctx) { - renderScope.drawRoundedBorderCorner(50, 50, 30, fgColor.value); - renderScope.drawRect(30, 0, width, 3, -1); + int w = width; + int h = width; + int r = 30; + int s = 1; + + renderScope.drawRect(0, 0, w, h, bgColor.value); + + renderScope.drawRoundedBorder(w, h, s, r, fgColor.value); + + // renderScope.drawRoundedSide(w - (r / 2 + s - 1), 0, w, h, s, r, fgColor.value); // right + + // renderScope.matrices.push(); + // renderScope.matrices.multiply(RotationAxis.NEGATIVE_Z.rotationDegrees(180)); + + // renderScope.matrices.pop(); // if (ctx.isHovering()) { // theme.setState("hovering");