From 8a448c818025945b2f33d0db01c375678f224d30 Mon Sep 17 00:00:00 2001 From: CC Date: Thu, 1 Aug 2019 12:34:10 -0500 Subject: Updates / Changes to WToggleButton --- .../cottonmc/cotton/gui/widget/WToggleButton.java | 107 +++++++++++++++++---- 1 file changed, 86 insertions(+), 21 deletions(-) (limited to 'src/main/java') 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 3ec1c46..a0d4773 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 @@ -11,57 +11,122 @@ import net.minecraft.text.Text; import net.minecraft.util.Identifier; public class WToggleButton extends WWidget { - Text label = null; - Identifier off_image = new Identifier("libgui:widget/toggle_off.png"); - Identifier on_image = new Identifier("libgui:widget/toggle_on.png"); - boolean on = false; - 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); } - + + /** Defaults with text */ public WToggleButton(Text text) { + this(DEFAULT_ON_IMAGE, DEFAULT_OFF_IMAGE, 18, 8); this.label = text; } - + + /** Custom images, with default sizes */ + 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 */ + public WToggleButton(Identifier onImage, Identifier offImage, int width, int height) { + + this.onImage = onImage; + this.offImage = offImage; + this.width = width; + this.height = height; + } + + /** Fully customized, custom images, sizes and label */ + 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; } - + + Text label = null; + + /** Default On / Off Images */ + protected final static Identifier DEFAULT_OFF_IMAGE = new Identifier("libgui:widget/toggle_off.png"); + protected final static Identifier DEFAULT_ON_IMAGE = new Identifier("libgui:widget/toggle_on.png"); + + protected Identifier onImage; + protected Identifier offImage; + + /** Default size values */ + protected int width = 18; + protected int height = 18; + + boolean isOn = false; + 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.rect(on ? on_image : off_image, x, y, 18, 8, 0xFFFFFFFF); + + ScreenDrawing.rect(isOn ? DEFAULT_ON_IMAGE : DEFAULT_OFF_IMAGE, x, y, width, height, 0xFFFFFFFF); if (label!=null) { - ScreenDrawing.drawString(label.asFormattedString(), x+20, y, LibGuiClient.config.darkMode ? darkmodeColor : color); + + ScreenDrawing.drawString(label.asFormattedString(), x + 20, y, LibGuiClient.config.darkMode ? darkmodeColor : color); } } @Override public boolean canResize() { + return true; } - + + + @Environment(EnvType.CLIENT) @Override public void onClick(int x, int y, int button) { super.onClick(x, y, button); MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F)); - this.on = !this.on; - - onToggle(this.on); + + this.isOn = !this.isOn; + onToggle(this.isOn); } - public void onToggle(boolean on) { - + protected void onToggle(boolean on) { + + if (this.onToggle != null) { + + this.onToggle.run(); + } } - public void setToggle(boolean on) { - this.on = on; + public boolean getToggle() { return this.isOn; } + public void setToggle(boolean on) { this.isOn = on; } + + /** Set on toggle handler */ + public void setOnToggle(Runnable r) { + + this.onToggle = r; } } -- cgit From d9ef4de731834c8c93031279788c2cef853b98d7 Mon Sep 17 00:00:00 2001 From: snowyleopardcc Date: Fri, 2 Aug 2019 14:13:39 -0500 Subject: Updated access on variables... --- src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java') 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 a0d4773..badb005 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 @@ -76,8 +76,8 @@ public class WToggleButton extends WWidget { protected int width = 18; protected int height = 18; - boolean isOn = false; - Runnable onToggle; + protected boolean isOn = false; + protected Runnable onToggle; protected int color = WLabel.DEFAULT_TEXT_COLOR; protected int darkmodeColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR; -- cgit