image button
This commit is contained in:
@@ -13,7 +13,8 @@ public class MainMenu extends SaturnUi {
|
||||
@Override
|
||||
protected void init() {
|
||||
draw(
|
||||
new SaturnButton("Cloaks", (__) -> {
|
||||
new SaturnButton("Settings", () -> {
|
||||
client.setScreen(new SettingsMenu());
|
||||
}).setX(10).setY(10).setWidth(100).setHeight(20).setAnimation(SaturnAnimation.FADE_SLIDE.distance(15)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.saturnclient.saturnclient.menus;
|
||||
|
||||
import org.saturnclient.saturnclient.widgets.SaturnButton;
|
||||
import org.saturnclient.saturnclient.widgets.SaturnImageButton;
|
||||
import org.saturnclient.ui.SaturnUi;
|
||||
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Identifier;
|
||||
|
||||
public class SettingsMenu extends SaturnUi {
|
||||
public boolean isOpen = false;
|
||||
public static Identifier searchTexture = Identifier.of("saturnclient", "textures/gui/components/search.png");
|
||||
public static Identifier closeTexture = Identifier.of("saturnclient", "textures/gui/components/close.png");
|
||||
public SaturnImageButton searchButton;
|
||||
|
||||
public SettingsMenu() {
|
||||
super(Text.literal("Settings"));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
searchButton = (SaturnImageButton) new SaturnImageButton(searchTexture, 10,
|
||||
10,
|
||||
() -> {
|
||||
isOpen = !isOpen;
|
||||
if (isOpen) {
|
||||
searchButton.sprite = closeTexture;
|
||||
draw(new SaturnButton("ABC", () -> {
|
||||
}).setWidth(30).setHeight(30).setX(70).setY(70));
|
||||
} else {
|
||||
searchButton.sprite = searchTexture;
|
||||
widgets.remove(widgets.size() - 1);
|
||||
}
|
||||
}).setWidth(30).setHeight(30).setX(30).setY(30);
|
||||
|
||||
draw(searchButton);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.saturnclient.saturnclient.widgets;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
import org.saturnclient.ui.SaturnWidget;
|
||||
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
@@ -12,19 +11,19 @@ import net.minecraft.util.math.ColorHelper;
|
||||
import net.minecraft.util.math.MathHelper;
|
||||
|
||||
public class SaturnButton extends SaturnWidget {
|
||||
public static final ButtonTextures TEXTURES = new ButtonTextures(
|
||||
Identifier.ofVanilla("widget/saturn_button"), Identifier.ofVanilla("widget/saturn_button_disabled"),
|
||||
Identifier.ofVanilla("widget/saturn_button_highlighted"));
|
||||
|
||||
public boolean active = true;
|
||||
public String text;
|
||||
public Consumer<SaturnButton> onPress;
|
||||
public Runnable onPress;
|
||||
|
||||
public SaturnButton(String text, Consumer<SaturnButton> onPress) {
|
||||
public SaturnButton(String text, Runnable 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"));
|
||||
|
||||
@Override
|
||||
public void render(DrawContext context, boolean hovering) {
|
||||
MinecraftClient minecraftClient = MinecraftClient.getInstance();
|
||||
@@ -41,6 +40,6 @@ public class SaturnButton extends SaturnWidget {
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
this.onPress.accept(this);
|
||||
this.onPress.run();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.saturnclient.saturnclient.widgets;
|
||||
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.render.RenderLayer;
|
||||
import net.minecraft.util.Identifier;
|
||||
import net.minecraft.util.math.ColorHelper;
|
||||
|
||||
public class SaturnImageButton extends SaturnButton {
|
||||
public Identifier sprite;
|
||||
public int imageWidth;
|
||||
public int imageHeight;
|
||||
|
||||
public SaturnImageButton(Identifier sprite, int imageWidth, int imageHeight, Runnable onPress) {
|
||||
super("", onPress);
|
||||
this.sprite = sprite;
|
||||
this.imageWidth = imageWidth;
|
||||
this.imageHeight = imageHeight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(DrawContext context, boolean hovering) {
|
||||
context.drawGuiTexture(RenderLayer::getGuiTextured, SaturnButton.TEXTURES.get(this.active, hovering), this.x,
|
||||
this.y, this.width, this.height, ColorHelper.getWhite(this.alpha));
|
||||
|
||||
context.drawTexture(RenderLayer::getGuiTextured, sprite, x + (width - imageWidth) / 2,
|
||||
y + (height - imageHeight) / 2, 0, 0, imageWidth, imageHeight, imageWidth,
|
||||
imageHeight, ColorHelper.getWhite(alpha));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void click() {
|
||||
this.onPress.run();
|
||||
}
|
||||
}
|
||||
@@ -15,12 +15,18 @@ public class SaturnUi extends Screen {
|
||||
}
|
||||
|
||||
public void draw(SaturnWidget widget) {
|
||||
widgets.add(widget);
|
||||
synchronized (widgets) {
|
||||
widgets.add(widget);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
|
||||
for (SaturnWidget widget : widgets) {
|
||||
for (SaturnWidget widget : new ArrayList<>(widgets)) {
|
||||
if (!widget.visible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (widget.animation != null) {
|
||||
widget.animation.apply(widget);
|
||||
}
|
||||
@@ -34,7 +40,11 @@ public class SaturnUi extends Screen {
|
||||
@Override
|
||||
public boolean mouseClicked(double mouseX, double mouseY, int button) {
|
||||
if (button == 0) {
|
||||
for (SaturnWidget widget : widgets) {
|
||||
for (SaturnWidget widget : new ArrayList<>(widgets)) {
|
||||
if (!widget.visible) {
|
||||
continue;
|
||||
}
|
||||
|
||||
boolean isMouseInside = widget.x < mouseX && widget.x + widget.width > mouseX
|
||||
&& widget.y < mouseY && widget.y + widget.height > mouseY;
|
||||
if (isMouseInside) {
|
||||
|
||||
@@ -9,6 +9,7 @@ public class SaturnWidget {
|
||||
public int height;
|
||||
public float alpha = 1.0f;
|
||||
public SaturnAnimation animation = null;
|
||||
public boolean visible = true;
|
||||
|
||||
public void render(DrawContext context, boolean hovering) {
|
||||
}
|
||||
@@ -59,4 +60,9 @@ public class SaturnWidget {
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public SaturnWidget setVisible(boolean visible) {
|
||||
this.visible = visible;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 322 B |
Binary file not shown.
|
After Width: | Height: | Size: 523 B |
Reference in New Issue
Block a user