From 2e5d3296d4c38f153e1d16664912f9f039208ee9 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Mon, 21 Apr 2025 16:13:21 +0200 Subject: [PATCH] weird bug --- .../saturnclient/menus/SaturnMenu.java | 3 +- .../java/org/saturnclient/ui2/Element.java | 10 +++ .../org/saturnclient/ui2/RenderScope.java | 82 ++++--------------- .../org/saturnclient/ui2/SaturnScreen.java | 7 ++ .../org/saturnclient/ui2/elements/Button.java | 25 ++++++ .../org/saturnclient/ui2/elements/Text.java | 2 +- .../org/saturnclient/ui2/resources/Fonts.java | 33 ++++++++ 7 files changed, 96 insertions(+), 66 deletions(-) create mode 100644 src/main/java/org/saturnclient/ui2/elements/Button.java create mode 100644 src/main/java/org/saturnclient/ui2/resources/Fonts.java diff --git a/src/main/java/org/saturnclient/saturnclient/menus/SaturnMenu.java b/src/main/java/org/saturnclient/saturnclient/menus/SaturnMenu.java index fba3ae1..91c7b91 100644 --- a/src/main/java/org/saturnclient/saturnclient/menus/SaturnMenu.java +++ b/src/main/java/org/saturnclient/saturnclient/menus/SaturnMenu.java @@ -1,6 +1,7 @@ package org.saturnclient.saturnclient.menus; import org.saturnclient.ui2.SaturnScreen; +import org.saturnclient.ui2.elements.Button; import org.saturnclient.ui2.elements.Text; public class SaturnMenu extends SaturnScreen { @@ -10,6 +11,6 @@ public class SaturnMenu extends SaturnScreen { @Override public void ui() { - draw(new Text("Saturn Client")); + draw(new Button("Hello, World", false)); } } diff --git a/src/main/java/org/saturnclient/ui2/Element.java b/src/main/java/org/saturnclient/ui2/Element.java index 177c7a5..7592d5e 100644 --- a/src/main/java/org/saturnclient/ui2/Element.java +++ b/src/main/java/org/saturnclient/ui2/Element.java @@ -9,4 +9,14 @@ public class Element { public void render(RenderScope renderScope) { } + + public final void dimensions(int width, int height) { + this.width = width; + this.height = height; + } + + public final void position(int x, int y) { + this.x = x; + this.y = y; + } } diff --git a/src/main/java/org/saturnclient/ui2/RenderScope.java b/src/main/java/org/saturnclient/ui2/RenderScope.java index 69f3013..02c6308 100644 --- a/src/main/java/org/saturnclient/ui2/RenderScope.java +++ b/src/main/java/org/saturnclient/ui2/RenderScope.java @@ -1,6 +1,7 @@ package org.saturnclient.ui2; import org.joml.Matrix4f; +import org.saturnclient.ui2.resources.Fonts; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.TextRenderer; @@ -15,13 +16,23 @@ import net.minecraft.util.math.RotationAxis; public class RenderScope { public MatrixStack matrices; public VertexConsumerProvider.Immediate vertexConsumers; + private int opacity = 255 << 24; public RenderScope(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers) { this.matrices = matrices; this.vertexConsumers = vertexConsumers; } + public void setOpacity(float alpha) { + this.opacity = (int) (alpha * 255) << 24; + } + + public int getColor(int color) { + return opacity | (color & 0x00FFFFFF); + } + public void drawRect(int x, int y, int width, int height, int color) { + color = getColor(color); Matrix4f matrix4f = this.matrices.peek().getPositionMatrix(); int x1 = x; @@ -49,76 +60,19 @@ public class RenderScope { vertexConsumer.vertex(matrix4f, (float) x2, (float) y1, 0).color(color); } - public void drawTexture(int x, int y, int width, int height, int color) { - Matrix4f matrix4f = this.matrices.peek().getPositionMatrix(); + public void drawText(String text, int x, int y, boolean bold, int color) { + color = getColor(color); - int x1 = x; - int x2 = x + width; - int y1 = y; - int y2 = y + height; + Identifier font = bold ? Fonts.PANTON_BOLD : Fonts.PANTON; - int i; - if (x1 < x2) { - i = x1; - x1 = x2; - x2 = i; - } - - if (y1 < y2) { - i = y1; - y1 = y2; - y2 = i; - } - - VertexConsumer vertexConsumer = this.vertexConsumers.getBuffer(RenderLayer.getGui()); - vertexConsumer.vertex(matrix4f, (float) x1, (float) y1, 0).color(color); - vertexConsumer.vertex(matrix4f, (float) x1, (float) y2, 0).color(color); - vertexConsumer.vertex(matrix4f, (float) x2, (float) y2, 0).color(color); - vertexConsumer.vertex(matrix4f, (float) x2, (float) y1, 0).color(color); - } - - public void drawTexture(Identifier sprite, int x, int y, float u, float v, int width, int height, int textureWidth, - int textureHeight, int color) { - this.drawTexture(sprite, x, y, u, v, width, height, width, height, textureWidth, textureHeight, color); - } - - public void drawTexture(Identifier sprite, int x, int y, float u, float v, int width, int height, int textureWidth, - int textureHeight) { - this.drawTexture(sprite, x, y, u, v, width, height, width, height, textureWidth, textureHeight); - } - - public void drawTexture(Identifier sprite, int x, int y, float u, float v, int width, int height, int regionWith, - int regionHeight, int textureWidth, int textureHeight) { - this.drawTexture(sprite, x, y, u, v, width, height, regionWith, regionHeight, textureWidth, textureHeight, -1); - } - - public void drawTexture(Identifier sprite, int x, int y, float u, float v, int width, int height, int regionWidth, - int regionHeight, int textureWidth, int textureHeight, int color) { - this.drawTexturedQuad(sprite, x, x + width, y, y + height, (u + 0.0F) / (float) textureWidth, - (u + (float) regionWidth) / (float) textureWidth, (v + 0.0F) / (float) textureHeight, - (v + (float) regionHeight) / (float) textureHeight, color); - } - - private void drawTexturedQuad(Identifier sprite, int x1, int x2, int y1, int y2, float u1, float u2, float v1, - float v2, int color) { - RenderLayer renderLayer = RenderLayer.getGuiTextured(sprite); - Matrix4f matrix4f = this.matrices.peek().getPositionMatrix(); - VertexConsumer vertexConsumer = this.vertexConsumers.getBuffer(renderLayer); - vertexConsumer.vertex(matrix4f, (float) x1, (float) y1, 0.0F).texture(u1, v1).color(color); - vertexConsumer.vertex(matrix4f, (float) x1, (float) y2, 0.0F).texture(u1, v2).color(color); - 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 drawText(String text, int x, int y, int color) { TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer; - textRenderer.draw(text, (float) x, (float) y, color, false, this.matrices.peek().getPositionMatrix(), + textRenderer.draw(Fonts.setFont(text, font), (float) x, (float) y, color, false, this.matrices.peek().getPositionMatrix(), this.vertexConsumers, TextLayerType.NORMAL, 0, 15728880); } - public void drawRoundedCorner(int width, int height, int radius, int color) { - int w = width * 10; // Width of the box - int h = height * 10; // Height of the box + private void drawRoundedCorner(int width, int height, int radius, int color) { + int w = width * 10; + int h = height * 10; for (int y = 0; y < h; y++) { int startX; diff --git a/src/main/java/org/saturnclient/ui2/SaturnScreen.java b/src/main/java/org/saturnclient/ui2/SaturnScreen.java index e76bb31..5f4fd8d 100644 --- a/src/main/java/org/saturnclient/ui2/SaturnScreen.java +++ b/src/main/java/org/saturnclient/ui2/SaturnScreen.java @@ -5,6 +5,8 @@ import java.util.List; import org.saturnclient.saturnclient.mixin.DrawContextAccessor; +import com.mojang.blaze3d.systems.RenderSystem; + import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; @@ -39,8 +41,13 @@ public class SaturnScreen extends Screen { RenderScope renderScope = new RenderScope(context.getMatrices(), ((DrawContextAccessor) context).getVertexConsumers()); + renderScope.matrices.push(); + for (Element element : elements) { + renderScope.setOpacity(0.5f); element.render(renderScope); } + + renderScope.matrices.pop(); } } diff --git a/src/main/java/org/saturnclient/ui2/elements/Button.java b/src/main/java/org/saturnclient/ui2/elements/Button.java new file mode 100644 index 0000000..a10d961 --- /dev/null +++ b/src/main/java/org/saturnclient/ui2/elements/Button.java @@ -0,0 +1,25 @@ +package org.saturnclient.ui2.elements; + +import org.saturnclient.ui2.Element; +import org.saturnclient.ui2.RenderScope; +import org.saturnclient.ui2.resources.Fonts; + +import net.minecraft.util.Identifier; + +public class Button extends Element { + private String text; + private Identifier font; + + public Button(String text, boolean bold) { + this.text = text; + this.font = Fonts.getFont(bold); + this.dimensions(Fonts.getWidth(text, this.font)+ 25 , Fonts.getHeight() + 10); + this.color = 0xFFFFFFFF; + } + + @Override + public void render(RenderScope renderScope) { + renderScope.drawRoundedRectangle(this.x, this.y, 200, 100, 10, this.color); + // renderScope.drawText(this.text, this.x, this.y, false, this.color); + } +} diff --git a/src/main/java/org/saturnclient/ui2/elements/Text.java b/src/main/java/org/saturnclient/ui2/elements/Text.java index 72f3149..2996d41 100644 --- a/src/main/java/org/saturnclient/ui2/elements/Text.java +++ b/src/main/java/org/saturnclient/ui2/elements/Text.java @@ -13,6 +13,6 @@ public class Text extends Element { @Override public void render(RenderScope renderScope) { - renderScope.drawText(this.text, this.x, this.y, this.color); + renderScope.drawText(this.text, this.x, this.y, false, this.color); } } diff --git a/src/main/java/org/saturnclient/ui2/resources/Fonts.java b/src/main/java/org/saturnclient/ui2/resources/Fonts.java new file mode 100644 index 0000000..a61c156 --- /dev/null +++ b/src/main/java/org/saturnclient/ui2/resources/Fonts.java @@ -0,0 +1,33 @@ +package org.saturnclient.ui2.resources; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +public class Fonts { + public static final Identifier PANTON = Identifier.of("saturnclient", "panton"); + public static final Identifier PANTON_BOLD = Identifier.of("saturnclient", "panton_bold"); + + public static Identifier getFont(boolean bold) { + if (bold) { + return PANTON_BOLD; + } else { + return PANTON; + } + } + + public static Text setFont(String text, Identifier font) { + return Text.literal(text).setStyle( + Style.EMPTY.withFont(font)); + } + + public static int getWidth(String text, Identifier font) { + return MinecraftClient.getInstance().textRenderer.getWidth(Text.literal(text).setStyle( + Style.EMPTY.withFont(font))); + } + + public static int getHeight() { + return MinecraftClient.getInstance().textRenderer.fontHeight; + } +}