weird bug

This commit is contained in:
2025-04-21 16:13:21 +02:00
parent 50e5a453a4
commit 2e5d3296d4
7 changed files with 96 additions and 66 deletions
@@ -1,6 +1,7 @@
package org.saturnclient.saturnclient.menus; package org.saturnclient.saturnclient.menus;
import org.saturnclient.ui2.SaturnScreen; import org.saturnclient.ui2.SaturnScreen;
import org.saturnclient.ui2.elements.Button;
import org.saturnclient.ui2.elements.Text; import org.saturnclient.ui2.elements.Text;
public class SaturnMenu extends SaturnScreen { public class SaturnMenu extends SaturnScreen {
@@ -10,6 +11,6 @@ public class SaturnMenu extends SaturnScreen {
@Override @Override
public void ui() { public void ui() {
draw(new Text("Saturn Client")); draw(new Button("Hello, World", false));
} }
} }
@@ -9,4 +9,14 @@ public class Element {
public void render(RenderScope renderScope) { 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;
}
} }
@@ -1,6 +1,7 @@
package org.saturnclient.ui2; package org.saturnclient.ui2;
import org.joml.Matrix4f; import org.joml.Matrix4f;
import org.saturnclient.ui2.resources.Fonts;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer; import net.minecraft.client.font.TextRenderer;
@@ -15,13 +16,23 @@ import net.minecraft.util.math.RotationAxis;
public class RenderScope { public class RenderScope {
public MatrixStack matrices; public MatrixStack matrices;
public VertexConsumerProvider.Immediate vertexConsumers; public VertexConsumerProvider.Immediate vertexConsumers;
private int opacity = 255 << 24;
public RenderScope(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers) { public RenderScope(MatrixStack matrices, VertexConsumerProvider.Immediate vertexConsumers) {
this.matrices = matrices; this.matrices = matrices;
this.vertexConsumers = vertexConsumers; 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) { public void drawRect(int x, int y, int width, int height, int color) {
color = getColor(color);
Matrix4f matrix4f = this.matrices.peek().getPositionMatrix(); Matrix4f matrix4f = this.matrices.peek().getPositionMatrix();
int x1 = x; int x1 = x;
@@ -49,76 +60,19 @@ public class RenderScope {
vertexConsumer.vertex(matrix4f, (float) x2, (float) y1, 0).color(color); vertexConsumer.vertex(matrix4f, (float) x2, (float) y1, 0).color(color);
} }
public void drawTexture(int x, int y, int width, int height, int color) { public void drawText(String text, int x, int y, boolean bold, int color) {
Matrix4f matrix4f = this.matrices.peek().getPositionMatrix(); color = getColor(color);
int x1 = x; Identifier font = bold ? Fonts.PANTON_BOLD : Fonts.PANTON;
int x2 = x + width;
int y1 = y;
int y2 = y + height;
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 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); this.vertexConsumers, TextLayerType.NORMAL, 0, 15728880);
} }
public void drawRoundedCorner(int width, int height, int radius, int color) { private void drawRoundedCorner(int width, int height, int radius, int color) {
int w = width * 10; // Width of the box int w = width * 10;
int h = height * 10; // Height of the box int h = height * 10;
for (int y = 0; y < h; y++) { for (int y = 0; y < h; y++) {
int startX; int startX;
@@ -5,6 +5,8 @@ import java.util.List;
import org.saturnclient.saturnclient.mixin.DrawContextAccessor; import org.saturnclient.saturnclient.mixin.DrawContextAccessor;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text; import net.minecraft.text.Text;
@@ -39,8 +41,13 @@ public class SaturnScreen extends Screen {
RenderScope renderScope = new RenderScope(context.getMatrices(), RenderScope renderScope = new RenderScope(context.getMatrices(),
((DrawContextAccessor) context).getVertexConsumers()); ((DrawContextAccessor) context).getVertexConsumers());
renderScope.matrices.push();
for (Element element : elements) { for (Element element : elements) {
renderScope.setOpacity(0.5f);
element.render(renderScope); element.render(renderScope);
} }
renderScope.matrices.pop();
} }
} }
@@ -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);
}
}
@@ -13,6 +13,6 @@ public class Text extends Element {
@Override @Override
public void render(RenderScope renderScope) { 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);
} }
} }
@@ -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;
}
}