button element

This commit is contained in:
2025-04-21 17:37:28 +02:00
parent 404838b70d
commit 08a48c8f92
5 changed files with 25 additions and 7 deletions
@@ -36,7 +36,7 @@ public class SaturnClient implements ModInitializer {
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if (SaturnClientConfig.saturnTitleScreen.value && screen instanceof TitleScreen
&& !(screen instanceof SaturnMenu)) {
client.setScreen(new SaturnMenu());
client.setScreen(new SaturnTitleScreen());
}
});
@@ -2,7 +2,6 @@ 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 {
public SaturnMenu() {
@@ -11,6 +10,6 @@ public class SaturnMenu extends SaturnScreen {
@Override
public void ui() {
draw(new Button("Hello, World", false));
draw(new Button("Settings", false).center(width, height));
}
}
@@ -19,4 +19,18 @@ public class Element {
this.x = x;
this.y = y;
}
public final Element center(int w, int h) {
this.x = (w - width) / 2;
this.y = (h - height) / 2;
return this;
}
public final Element centerOffset(int w, int h, int offsetX, int offsetY) {
this.x = (w - width) / 2 + offsetX;
this.y = (h - height) / 2 + offsetY;
return this;
}
}
@@ -5,8 +5,6 @@ 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;
@@ -45,7 +43,10 @@ public class SaturnScreen extends Screen {
for (Element element : elements) {
renderScope.setOpacity(0.5f);
renderScope.matrices.push();
renderScope.matrices.translate(element.x, element.y, 0);
element.render(renderScope);
renderScope.matrices.pop();
}
renderScope.matrices.pop();
@@ -8,17 +8,21 @@ import net.minecraft.util.Identifier;
public class Button extends Element {
private String text;
private boolean bold;
private Identifier font;
public Button(String text, boolean bold) {
this.text = text;
this.bold = bold;
this.font = Fonts.getFont(bold);
this.dimensions(Fonts.getWidth(text, this.font)+ 25 , Fonts.getHeight() + 10);
this.dimensions(Fonts.getWidth(text, font)+ 25 , Fonts.getHeight() + 10);
this.color = 0xFFFFFFFF;
}
@Override
public void render(RenderScope renderScope) {
renderScope.drawRoundedRectangle(0, 0, width, height, 10, color);
renderScope.drawRoundedRectangle(0, 0, width, height, 10, 0xFF000000);
renderScope.drawText(text, width / 2 - Fonts.getWidth(text, font) / 2, height / 2 - Fonts.getHeight() / 2, bold, color);
}
}