diff options
author | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2019-12-15 18:22:50 +0200 |
---|---|---|
committer | Juuxel <6596629+Juuxel@users.noreply.github.com> | 2019-12-15 18:22:50 +0200 |
commit | 0ddb69f5d3061645c0b45441f1ef5e02782321e5 (patch) | |
tree | 51fdc4e5de20054c0e67c8be4542ff9e978f982c /src | |
parent | 0f73ce47bbd3843e07bf71fbaf52b3b7c6bd4c3b (diff) | |
download | LibGui-0ddb69f5d3061645c0b45441f1ef5e02782321e5.tar.gz LibGui-0ddb69f5d3061645c0b45441f1ef5e02782321e5.tar.bz2 LibGui-0ddb69f5d3061645c0b45441f1ef5e02782321e5.zip |
Rework WToggleButton
- Cleaned up the class
- Placed fields/constructors/methods in a sensible order
- Removed unnecessary whitespace
(Why was there a blank line inside of if statements?)
- Deprecated the (width, height) constructors. The sizing wasn't implemented.
- Added accessors for labels.
- Made onToggle a Consumer<Boolean>, so the toggle value can be accessed in the handler.
Diffstat (limited to 'src')
-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; } } |