From 19eb140cedb3f441330a2ad63f2c9b5722642acd Mon Sep 17 00:00:00 2001 From: Magnus Ihse Bursie Date: Sun, 28 Apr 2024 00:06:41 +0200 Subject: [PATCH] feat: Add icon button widgets --- .../gui/widget/IconButtonWidget.java | 38 +++++++++++++++++ .../gui/widget/IconToggleButtonWidget.java | 42 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 src/main/java/se/icus/mag/modsettings/gui/widget/IconButtonWidget.java create mode 100644 src/main/java/se/icus/mag/modsettings/gui/widget/IconToggleButtonWidget.java diff --git a/src/main/java/se/icus/mag/modsettings/gui/widget/IconButtonWidget.java b/src/main/java/se/icus/mag/modsettings/gui/widget/IconButtonWidget.java new file mode 100644 index 0000000..84120ae --- /dev/null +++ b/src/main/java/se/icus/mag/modsettings/gui/widget/IconButtonWidget.java @@ -0,0 +1,38 @@ +package se.icus.mag.modsettings.gui.widget; + +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +public class IconButtonWidget extends ButtonWidget { + private final int textureWidth; + private final int textureHeight; + protected Identifier texture; + + public IconButtonWidget(int x, int y, int width, int height,int textureWidth, int textureHeight, + Identifier texture, ButtonWidget.PressAction onPress) { + this(x, y, width, height, textureWidth, textureHeight, onPress); + this.texture = texture; + } + + protected IconButtonWidget(int x, int y, int width, int height,int textureWidth, int textureHeight, + ButtonWidget.PressAction onPress) { + super(x, y, width, height, Text.empty(), onPress, DEFAULT_NARRATION_SUPPLIER); + this.textureWidth = textureWidth; + this.textureHeight = textureHeight; + } + + @Override + public void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { + super.renderWidget(context, mouseX, mouseY, delta); + int x = this.getX() + this.getWidth() / 2 - this.textureWidth / 2; + int y = this.getY() + this.getHeight() / 2 - this.textureHeight / 2; + context.drawGuiTexture(this.texture, x, y, this.textureWidth, this.textureHeight); + } + + @Override + public void drawMessage(DrawContext context, TextRenderer textRenderer, int color) { + } +} diff --git a/src/main/java/se/icus/mag/modsettings/gui/widget/IconToggleButtonWidget.java b/src/main/java/se/icus/mag/modsettings/gui/widget/IconToggleButtonWidget.java new file mode 100644 index 0000000..d265264 --- /dev/null +++ b/src/main/java/se/icus/mag/modsettings/gui/widget/IconToggleButtonWidget.java @@ -0,0 +1,42 @@ +package se.icus.mag.modsettings.gui.widget; + +import java.util.List; +import net.minecraft.client.gui.tooltip.Tooltip; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.util.Identifier; + +public class IconToggleButtonWidget extends IconButtonWidget { + private final List textures; + private final List tooltips; + private final ToggleAction onChange; + + private int selection; + + public IconToggleButtonWidget(int x, int y, int width, int height, int textureWidth, int textureHeight, + List textures, List tooltips, int selection, ToggleAction onChange) { + super(x, y, width, height, textureWidth, textureHeight, ButtonWidget::onPress); + + this.textures = textures; + this.tooltips = tooltips; + this.onChange = onChange; + this.selection = selection; + updateSelection(); + } + + @Override + public void onPress() { + this.selection = (this.selection + 1) % this.textures.size(); + updateSelection(); + this.onChange.onChange(selection); + } + + private void updateSelection() { + this.texture = this.textures.get(this.selection); + this.setTooltip(this.tooltips.get(this.selection)); + } + + @FunctionalInterface + public interface ToggleAction { + void onChange(int selection); + } +}