beautiful animations

This commit is contained in:
2025-03-10 01:00:08 +01:00
parent 7f7553d44a
commit 299c8333f9
10 changed files with 193 additions and 224 deletions
@@ -1,27 +0,0 @@
package org.saturnclient.saturnclient.menus;
import org.saturnclient.saturnclient.cloaks.Cloaks;
import org.saturnclient.saturnclient.widgets.SaturnButton;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
public class CloakSelector extends Screen {
public CloakSelector() {
super(Text.literal("Cloaks"));
}
@Override
protected void init() {
int y = ((this.height - (35 * (Cloaks.availableCloaks.size()))) / 2);
for (String cloak : Cloaks.availableCloaks) {
addDrawableChild(SaturnButton.builder(Text.literal(cloak), (__)->{
String uuid = client.player.getUuidAsString().replace("-", "");
Cloaks.setCloak(uuid, cloak);
}, (this.width - 50) / 2, y, 50, 30));
y += 35;
}
}
}
@@ -1,51 +1,19 @@
package org.saturnclient.saturnclient.menus;
import org.saturnclient.saturnclient.widgets.SaturnButton;
import org.saturnclient.saturnclient.widgets.SaturnText;
import org.saturnclient.saturnclient.widgets.SaturnTexture;
import net.minecraft.client.gui.screen.Screen;
import org.saturnclient.ui.SaturnAnimation;
import org.saturnclient.ui.SaturnUi;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
public class MainMenu extends Screen {
public class MainMenu extends SaturnUi {
public MainMenu() {
super(Text.literal("Saturn Client"));
}
@Override
protected void init() {
super.init();
int saturnClientSize = 40;
int saturnClientX = (this.width - saturnClientSize) / 2;
int saturnClientY = ((this.height - 105) - saturnClientSize) / 2;
addDrawable(new SaturnText("Saturn Client")
.position((this.width - this.textRenderer.getWidth("Saturn Client")) / 2,
saturnClientY + saturnClientSize + 1)
.setAlpha(0)
.onRender(t -> {
if (t.alpha < 1.0f) {
t.alpha += 0.1f;
}
}));
addDrawable(SaturnTexture.builder(Identifier.of("saturnclient", "icon.png"))
.dimensions(saturnClientX, saturnClientY + 20, saturnClientSize, saturnClientSize).onRender(t -> {
if (t.y > saturnClientY) {
t.y -= 1;
}
}));
int buttonWidth = 50;
int buttonHeight = 30;
SaturnButton cloakButton = SaturnButton.builder(Text.literal("Cloaks"), (_button) -> {
if (client.player != null) {
client.setScreen(new CloakSelector());
}
}, (this.width - buttonWidth) / 2, (this.height - buttonHeight) / 2, buttonWidth, buttonHeight);
addDrawableChild(cloakButton);
draw(
new SaturnButton("Cloaks", (__) -> {
}).setX(10).setY(10).setWidth(100).setHeight(20).setAnimation(SaturnAnimation.FADE_SLIDE.distance(15)));
}
}
@@ -24,6 +24,11 @@ public abstract class NameTagMixin<S extends EntityRenderState> {
@Shadow
public EntityRenderDispatcher dispatcher;
/**
* @author HexLeo
* @reason Adds the Saturn Client icon to the player's name if they are online
* with Saturn Client
*/
@Overwrite
public void renderLabelIfPresent(S state, Text text_o, MatrixStack matrices,
VertexConsumerProvider vertexConsumers, int light) {
@@ -13,6 +13,11 @@ public abstract class TabListMixin {
@Shadow
private Text displayName;
/**
* @author HexLeo
* @reason Adds the Saturn Client icon to the player's name if they are online
* with Saturn Client
*/
@Overwrite
@Nullable
public Text getDisplayName() {
@@ -1,38 +1,46 @@
package org.saturnclient.saturnclient.widgets;
import java.util.function.Consumer;
import org.saturnclient.ui.SaturnWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ButtonTextures;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper;
import net.minecraft.util.math.MathHelper;
public class SaturnButton extends ButtonWidget {
public class SaturnButton extends SaturnWidget {
public boolean active = true;
public String text;
public Consumer<SaturnButton> onPress;
public SaturnButton(String text, Consumer<SaturnButton> onPress) {
this.text = text;
this.onPress = onPress;
}
private static final ButtonTextures TEXTURES = new ButtonTextures(
Identifier.ofVanilla("widget/saturn_button"), Identifier.ofVanilla("widget/saturn_button_disabled"),
Identifier.ofVanilla("widget/saturn_button_highlighted"));
protected SaturnButton(int x, int y, int width, int height, Text message, PressAction onPress,
NarrationSupplier narrationSupplier) {
super(x, y, width, height, message, onPress, narrationSupplier);
}
@Override
public void render(DrawContext context, boolean hovering) {
MinecraftClient minecraftClient = MinecraftClient.getInstance();
public static SaturnButton builder(Text message, PressAction onPress, int x, int y, int width, int height) {
return new SaturnButton(x, y, width, height, message, onPress, null);
context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(this.active, hovering), this.x,
this.y, this.width, this.height, ColorHelper.getWhite(this.alpha));
int i = this.active ? 16777215 : 10526880;
context.drawText(minecraftClient.textRenderer, this.text,
this.x + ((this.width - minecraftClient.textRenderer.getWidth(this.text)) / 2),
this.y + ((this.height - 7) / 2),
i | MathHelper.ceil(this.alpha * 255.0F) << 24, false);
}
@Override
public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
MinecraftClient minecraftClient = MinecraftClient.getInstance();
context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(this.active, this.isSelected()), this.getX(),
this.getY(), this.getWidth(), this.getHeight(), ColorHelper.getWhite(this.alpha));
int i = this.active ? 16777215 : 10526880;
this.drawMessage(context, minecraftClient.textRenderer, i | MathHelper.ceil(this.alpha * 255.0F) << 24);
public void click() {
this.onPress.accept(this);
}
}
@@ -1,78 +1,19 @@
package org.saturnclient.saturnclient.widgets;
import java.util.function.Consumer;
import org.saturnclient.ui.SaturnWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
import net.minecraft.util.math.ColorHelper;
/**
* Represents a drawable text element in the Saturn Client UI.
* Uses the builder pattern for flexible text creation and configuration.
*/
public class SaturnText implements Drawable {
public class SaturnText extends SaturnWidget {
private String text;
private Consumer<SaturnText> onRender;
public int x;
public int y;
public float alpha;
/**
* Creates a new text element with the specified string.
*
* @param text The text to display
*/
public SaturnText(String text) {
this.text = text;
this.x = 0;
this.y = 0;
this.alpha = 1.0f;
this.onRender = __ -> {
};
}
public SaturnText onRender(Consumer<SaturnText> onRender) {
this.onRender = onRender;
return this;
}
public SaturnText setAlpha(float alpha) {
this.alpha = alpha;
return this;
}
public SaturnText setText(String text) {
this.text = text;
return this;
}
/**
* Creates a new builder instance for the text.
*
* @param text The text content
* @return A new SaturnText instance
*/
public static SaturnText builder(String text) {
return new SaturnText(text);
}
/**
* Sets the position of the text.
*
* @param x The x coordinate
* @param y The y coordinate
* @return This instance for method chaining
*/
public SaturnText position(int x, int y) {
this.x = x;
this.y = y;
return this;
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
onRender.accept(this);
public void render(DrawContext context, boolean hovering) {
context.drawText(MinecraftClient.getInstance().textRenderer, text, x, y, ColorHelper.getWhite(alpha), false);
}
}
@@ -1,95 +1,20 @@
package org.saturnclient.saturnclient.widgets;
import java.util.function.Consumer;
import org.saturnclient.ui.SaturnWidget;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper;
/**
* Represents a drawable texture in the Saturn Client UI.
* Uses the builder pattern for flexible texture creation and configuration.
*/
public class SaturnTexture implements Drawable {
// Predefined textures
public static final Identifier BOX_1_1 = Identifier.of("saturnclient", "textures/gui/box_1_1.png");
public class SaturnTexture extends SaturnWidget {
private final Identifier sprite;
private Consumer<SaturnTexture> onRender;
public int x;
public int y;
public int width;
public int height;
public float alpha;
/**
* Creates a new texture with the specified sprite.
*
* @param sprite The identifier for the texture sprite
*/
private SaturnTexture(Identifier sprite) {
public SaturnTexture(Identifier sprite) {
this.sprite = sprite;
this.x = 0;
this.y = 0;
this.width = 0;
this.height = 0;
this.alpha = 1.0f;
this.onRender = __ -> {
};
}
public SaturnTexture onRender(Consumer<SaturnTexture> onRender) {
this.onRender = onRender;
return this;
}
public SaturnTexture setAlpha(float alpha) {
this.alpha = alpha;
return this;
}
/**
* Creates a new builder instance for the texture.
*
* @param sprite The identifier for the texture sprite
* @return A new SaturnClientTexture instance
*/
public static SaturnTexture builder(Identifier sprite) {
return new SaturnTexture(sprite);
}
/**
* Sets the dimensions and position of the texture.
*
* @param x The x coordinate
* @param y The y coordinate
* @param width The width of the texture
* @param height The height of the texture
* @return This instance for method chaining
*/
public SaturnTexture dimensions(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
return this;
}
/**
* Sets the focused state of the texture.
*
* @param focused The focused state
* @return This instance for method chaining
*/
public SaturnTexture setFocused(boolean focused) {
return this;
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
onRender.accept(this);
public void render(DrawContext context, boolean hovering) {
context.drawTexture(RenderLayer::getGuiTextured, sprite, x, y, 0, 0, width, height, width, height,
ColorHelper.getWhite(alpha));
}
@@ -0,0 +1,34 @@
package org.saturnclient.ui;
public enum SaturnAnimation {
FADE,
SLIDE,
FADE_SLIDE;
public int distance = 0;
public int widgetY = 0;
public void apply(SaturnWidget widget) {
switch (this) {
case FADE:
widget.alpha = Math.min(widget.alpha + 0.05f, 1.0f);
break;
case SLIDE:
if (widgetY < widget.y) {
widget.y -= 1;
}
break;
case FADE_SLIDE:
widget.alpha = Math.min(widget.alpha + 0.05f, 1.0f);
if (widgetY < widget.y) {
widget.y -= 1;
}
break;
}
}
public SaturnAnimation distance(int distance) {
this.distance = distance;
return this;
}
}
@@ -0,0 +1,48 @@
package org.saturnclient.ui;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.Text;
public class SaturnUi extends Screen {
public List<SaturnWidget> widgets = new ArrayList<>();
public SaturnUi(Text title) {
super(title);
}
public void draw(SaturnWidget widget) {
widgets.add(widget);
}
@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
for (SaturnWidget widget : widgets) {
if (widget.animation != null) {
widget.animation.apply(widget);
}
boolean isMouseInside = widget.x < mouseX && widget.x + widget.width > mouseX
&& widget.y < mouseY && widget.y + widget.height > mouseY;
widget.render(context, isMouseInside);
}
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (button == 0) {
for (SaturnWidget widget : widgets) {
boolean isMouseInside = widget.x < mouseX && widget.x + widget.width > mouseX
&& widget.y < mouseY && widget.y + widget.height > mouseY;
if (isMouseInside) {
widget.click();
}
}
}
return false;
}
}
@@ -0,0 +1,62 @@
package org.saturnclient.ui;
import net.minecraft.client.gui.DrawContext;
public class SaturnWidget {
public int x;
public int y;
public int width;
public int height;
public float alpha = 1.0f;
public SaturnAnimation animation = null;
public void render(DrawContext context, boolean hovering) {
}
public void click() {
}
public SaturnWidget setX(int x) {
this.x = x;
return this;
}
public SaturnWidget setY(int y) {
this.y = y;
return this;
}
public SaturnWidget setWidth(int width) {
this.width = width;
return this;
}
public SaturnWidget setHeight(int height) {
this.height = height;
return this;
}
public SaturnWidget setAlpha(float alpha) {
this.alpha = alpha;
return this;
}
public SaturnWidget setAnimation(SaturnAnimation animation) {
this.animation = animation;
switch (animation) {
case FADE:
this.alpha = 0.0f;
break;
case SLIDE:
this.animation.widgetY = y;
y += animation.distance;
break;
case FADE_SLIDE:
this.animation.widgetY = y;
y += animation.distance;
this.alpha = 0.0f;
break;
}
return this;
}
}