button hover effect

This commit is contained in:
2025-04-04 15:34:20 +02:00
parent 48bf48fd56
commit ef2ffcc5e7
7 changed files with 44 additions and 19 deletions
@@ -65,5 +65,5 @@ public class SaturnClient implements ModInitializer {
} else {
LOGGER.error("Failed to authenticate with the server");
}
}
}
}
@@ -245,6 +245,8 @@ public class SaturnUi extends Screen {
}
}
}
widget.tick();
}
tick++;
}
@@ -14,27 +14,34 @@ public class SaturnWidget {
public boolean visible = true;
public boolean focused = false;
public void init() {}
public void init() {
}
public void render(
DrawContext context,
boolean hovering,
int mouseX,
int mouseY
) {}
DrawContext context,
boolean hovering,
int mouseX,
int mouseY) {
}
public void click(int mouseX, int mouseY) {}
public void click(int mouseX, int mouseY) {
}
public void keyPressed(int keyCode, int scanCode, int modifiers) {}
public void keyPressed(int keyCode, int scanCode, int modifiers) {
}
public void charTyped(char typedChar) {}
public void charTyped(char typedChar) {
}
public void tick() {
}
public void mouseScrolled(
int mouseX,
int mouseY,
double horizontalAmount,
double verticalAmount
) {}
int mouseX,
int mouseY,
double horizontalAmount,
double verticalAmount) {
}
public SaturnWidget setX(int x) {
this.x = x;
@@ -28,11 +28,11 @@ public class Textures {
public static final Identifier MOD_BG = Identifier.of("saturnclient", "textures/gui/components/mod_bg.png");
public static Identifier getCloakPreview(String cloak) {
return Identifier.of("saturnclient", "textures/gui/cloak/" + cloak + ".png");
return Identifier.of("saturnclient", "textures/gui/cloak/" + (cloak.isEmpty() ? "none" : cloak) + ".png");
}
public static Identifier getHatPreview(String hat) {
return Identifier.of("saturnclient", "textures/gui/hat/" + hat + ".png");
return Identifier.of("saturnclient", "textures/gui/hat/" + (hat.isEmpty() ? "none" : hat) + ".png");
}
public static Identifier getModIcon(String modId) {
@@ -5,6 +5,7 @@ import java.util.function.Consumer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.math.MathHelper;
import org.saturnclient.saturnclient.SaturnClient;
import org.saturnclient.ui.SaturnUi;
import org.saturnclient.ui.SaturnWidget;
@@ -17,6 +18,8 @@ public class SaturnButton extends SaturnWidget {
public Consumer<SaturnButton> onPress;
public boolean background = true;
private float hoverAlpha;
public SaturnButton(String text, Consumer<SaturnButton> onPress) {
this.text = text;
this.onPress = onPress;
@@ -36,7 +39,7 @@ public class SaturnButton extends SaturnWidget {
int mouseY) {
MinecraftClient minecraftClient = MinecraftClient.getInstance();
int hoverColor = ((int) (alpha * 255) << 24) |
int hoverColor = ((int) (hoverAlpha * 255) << 24) |
(SaturnClient.COLOR.value & 0x00FFFFFF);
if (background) {
@@ -52,6 +55,9 @@ public class SaturnButton extends SaturnWidget {
SaturnClient.getWhite(this.alpha));
if (hovering) {
if (hoverAlpha == 0.0f)
hoverAlpha = 0.1f;
SaturnUi.drawHighResGuiTexture(
context,
Textures.BUTTON_BORDER,
@@ -59,7 +65,9 @@ public class SaturnButton extends SaturnWidget {
0,
this.width,
this.height,
hoverColor);
((int) (hoverAlpha * 255) << 24) | (SaturnClient.COLOR.value & 0x00FFFFFF));
} else {
hoverAlpha = 0.0f;
}
}
@@ -86,4 +94,12 @@ public class SaturnButton extends SaturnWidget {
public void click(int mouseX, int mouseY) {
this.onPress.accept(this);
}
@Override
public void tick() {
if (hoverAlpha > 0.0f && hoverAlpha < 0.9f)
hoverAlpha += 0.06f;
else if (hoverAlpha > 0.9f && hoverAlpha < 1.0f)
hoverAlpha = 1.0f;
}
}