-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
src/main/java/se/icus/mag/modsettings/gui/widget/IconButtonWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/se/icus/mag/modsettings/gui/widget/IconToggleButtonWidget.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |