Skip to content

Commit

Permalink
feat: Add icon button widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
magicus committed Apr 27, 2024
1 parent 7106c41 commit 19eb140
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
}
}
Original file line number Diff line number Diff line change
@@ -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<Identifier> textures;
private final List<Tooltip> tooltips;
private final ToggleAction onChange;

private int selection;

public IconToggleButtonWidget(int x, int y, int width, int height, int textureWidth, int textureHeight,
List<Identifier> textures, List<Tooltip> 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);
}
}

0 comments on commit 19eb140

Please sign in to comment.