diff options
-rw-r--r-- | Notes.md | 5 | ||||
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java | 107 |
2 files changed, 91 insertions, 21 deletions
diff --git a/Notes.md b/Notes.md new file mode 100644 index 0000000..247589b --- /dev/null +++ b/Notes.md @@ -0,0 +1,5 @@ +Changes for `WToggleButton` PR +------------------------------ ++ Added @Environment tag to fix `class_1113` not found error from Sound Instance running on server side. ++ Added default values for images and sizes ++ Added `setOnToggle(Runnable r)`, and supporting handler methods.
\ No newline at end of file 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..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 @@ -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; + + 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.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; } } |