diff options
author | Falkreon <falkreon@gmail.com> | 2019-08-02 14:16:00 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-02 14:16:00 -0500 |
commit | 2e624aa570c675d0d8159422ac8d1ebd94a59442 (patch) | |
tree | 7cf63bdb61473370dd14773ed6d69270d4c8eb62 /src/main/java | |
parent | 64615ae6c66bcfb40b1385e3452a0f4b932ab322 (diff) | |
parent | d9ef4de731834c8c93031279788c2cef853b98d7 (diff) | |
download | LibGui-2e624aa570c675d0d8159422ac8d1ebd94a59442.tar.gz LibGui-2e624aa570c675d0d8159422ac8d1ebd94a59442.tar.bz2 LibGui-2e624aa570c675d0d8159422ac8d1ebd94a59442.zip |
Merge pull request #4 from snowyleopardcc/master
Updates / Changes to WToggleButton
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java | 107 |
1 files changed, 86 insertions, 21 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 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; } } |