diff options
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java | 121 |
1 files changed, 69 insertions, 52 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java index 8935a95..e860a5f 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java @@ -10,98 +10,80 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import javax.annotation.Nullable; +import java.util.function.Consumer; + public class WToggleButton extends WWidget { + /** Default On / Off Images */ + protected final static Identifier DEFAULT_OFF_IMAGE = new Identifier("libgui:textures/widget/toggle_off.png"); + protected final static Identifier DEFAULT_ON_IMAGE = new Identifier("libgui:textures/widget/toggle_on.png"); + protected Identifier onImage; + protected Identifier offImage; + + protected Text label = null; + + protected boolean isOn = false; + @Nullable protected Consumer<Boolean> onToggle = null; + + protected int color = WLabel.DEFAULT_TEXT_COLOR; + protected int darkmodeColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; /** All default values, no text */ public WToggleButton() { - this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE, 18, 8); + this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE); } /** Defaults with text */ public WToggleButton(Text text) { - this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE, 18, 8); + this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE); this.label = text; } - /** Custom images, with default sizes */ + /** Custom images */ public WToggleButton(Identifier onImage, Identifier offImage) { - this.onImage = onImage; this.offImage = offImage; } /** Custom images, with default sizes and a label */ public WToggleButton(Identifier onImage, Identifier offImage, Text label) { - this.onImage = onImage; this.offImage = offImage; this.label = label; } - /** Custom images, with custom sizes */ + /** + * @deprecated Use {@link #WToggleButton(Identifier, Identifier)} instead. + */ + @Deprecated public WToggleButton(Identifier onImage, Identifier offImage, int width, int height) { - - this.onImage = onImage; - this.offImage = offImage; - this.width = width; - this.height = height; + this(onImage, offImage); } - /** Fully customized, custom images, sizes and label */ + /** + * @deprecated Use {@link #WToggleButton(Identifier, Identifier, Text)} instead. + */ + @Deprecated public WToggleButton(Text label, Identifier onImage, Identifier offImage, int width, int height) { - this(onImage, offImage, width, height); - - this.label = label; - } - - public WToggleButton color(int light, int dark) { - - this.color = light; - this.darkmodeColor = dark; - - return this; + this(onImage, offImage, label); } - Text label = null; - - /** Default On / Off Images */ - protected final static Identifier DEFAULT_OFF_IMAGE = new Identifier("libgui:textures/widget/toggle_off.png"); - protected final static Identifier DEFAULT_ON_IMAGE = new Identifier("libgui:textures/widget/toggle_on.png"); - - protected Identifier onImage; - protected Identifier offImage; - - /** Default size values */ - protected int width = 18; - protected int height = 18; - - protected boolean isOn = false; - protected Runnable onToggle; - - protected int color = WLabel.DEFAULT_TEXT_COLOR; - protected int darkmodeColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; - - @Environment(EnvType.CLIENT) @Override public void paintBackground(int x, int y) { - ScreenDrawing.texturedRect(x, y, 18, 18, isOn ? onImage : offImage, 0xFFFFFFFF); if (label!=null) { - ScreenDrawing.drawString(label.asFormattedString(), x + 22, y+6, LibGuiClient.config.darkMode ? darkmodeColor : color); } } @Override public boolean canResize() { - return true; } - @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { @@ -114,19 +96,54 @@ public class WToggleButton extends WWidget { } protected void onToggle(boolean on) { - if (this.onToggle != null) { - - this.onToggle.run(); + this.onToggle.accept(on); } } public boolean getToggle() { return this.isOn; } public void setToggle(boolean on) { this.isOn = on; } - /** Set on toggle handler */ + /** + * Set on toggle handler + * + * @deprecated Use {@link #setOnToggle(Consumer)} + */ + @Deprecated public void setOnToggle(Runnable r) { + this.onToggle = on -> r.run(); + } - this.onToggle = r; + @Nullable + public Consumer<Boolean> getOnToggle() { + return this.onToggle; + } + + public WToggleButton setOnToggle(@Nullable Consumer<Boolean> onToggle) { + this.onToggle = onToggle; + return this; + } + + public Text getLabel() { + return label; + } + + public void setLabel(Text label) { + this.label = label; + } + + /** + * @deprecated Use {@link #setColor} instead. + */ + @Deprecated + public WToggleButton color(int light, int dark) { + return setColor(light, dark); + } + + public WToggleButton setColor(int light, int dark) { + this.color = light; + this.darkmodeColor = dark; + + return this; } } |