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 { } else {
LOGGER.error("Failed to authenticate with the server"); LOGGER.error("Failed to authenticate with the server");
} }
} }
} }
@@ -245,6 +245,8 @@ public class SaturnUi extends Screen {
} }
} }
} }
widget.tick();
} }
tick++; tick++;
} }
@@ -14,27 +14,34 @@ public class SaturnWidget {
public boolean visible = true; public boolean visible = true;
public boolean focused = false; public boolean focused = false;
public void init() {} public void init() {
}
public void render( public void render(
DrawContext context, DrawContext context,
boolean hovering, boolean hovering,
int mouseX, int mouseX,
int mouseY 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( public void mouseScrolled(
int mouseX, int mouseX,
int mouseY, int mouseY,
double horizontalAmount, double horizontalAmount,
double verticalAmount double verticalAmount) {
) {} }
public SaturnWidget setX(int x) { public SaturnWidget setX(int x) {
this.x = 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 final Identifier MOD_BG = Identifier.of("saturnclient", "textures/gui/components/mod_bg.png");
public static Identifier getCloakPreview(String cloak) { 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) { 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) { public static Identifier getModIcon(String modId) {
@@ -5,6 +5,7 @@ import java.util.function.Consumer;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import org.saturnclient.saturnclient.SaturnClient; import org.saturnclient.saturnclient.SaturnClient;
import org.saturnclient.ui.SaturnUi; import org.saturnclient.ui.SaturnUi;
import org.saturnclient.ui.SaturnWidget; import org.saturnclient.ui.SaturnWidget;
@@ -17,6 +18,8 @@ public class SaturnButton extends SaturnWidget {
public Consumer<SaturnButton> onPress; public Consumer<SaturnButton> onPress;
public boolean background = true; public boolean background = true;
private float hoverAlpha;
public SaturnButton(String text, Consumer<SaturnButton> onPress) { public SaturnButton(String text, Consumer<SaturnButton> onPress) {
this.text = text; this.text = text;
this.onPress = onPress; this.onPress = onPress;
@@ -36,7 +39,7 @@ public class SaturnButton extends SaturnWidget {
int mouseY) { int mouseY) {
MinecraftClient minecraftClient = MinecraftClient.getInstance(); MinecraftClient minecraftClient = MinecraftClient.getInstance();
int hoverColor = ((int) (alpha * 255) << 24) | int hoverColor = ((int) (hoverAlpha * 255) << 24) |
(SaturnClient.COLOR.value & 0x00FFFFFF); (SaturnClient.COLOR.value & 0x00FFFFFF);
if (background) { if (background) {
@@ -52,6 +55,9 @@ public class SaturnButton extends SaturnWidget {
SaturnClient.getWhite(this.alpha)); SaturnClient.getWhite(this.alpha));
if (hovering) { if (hovering) {
if (hoverAlpha == 0.0f)
hoverAlpha = 0.1f;
SaturnUi.drawHighResGuiTexture( SaturnUi.drawHighResGuiTexture(
context, context,
Textures.BUTTON_BORDER, Textures.BUTTON_BORDER,
@@ -59,7 +65,9 @@ public class SaturnButton extends SaturnWidget {
0, 0,
this.width, this.width,
this.height, 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) { public void click(int mouseX, int mouseY) {
this.onPress.accept(this); 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;
}
} }
Binary file not shown.

Before

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB