diff options
author | isXander <xandersmith2008@gmail.com> | 2022-11-27 18:17:36 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-11-27 18:17:36 +0000 |
commit | d163b9128d760e53e34fd6c08dbf782fa3d50c51 (patch) | |
tree | b9ea79bb99bf52ea7de9d059cc05900270ff88d4 /src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java | |
parent | c26d3a89caae20f1a91898202d91de8dc90da5ad (diff) | |
download | YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.gz YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.tar.bz2 YetAnotherConfigLib-d163b9128d760e53e34fd6c08dbf782fa3d50c51.zip |
split sourcesets
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java')
-rw-r--r-- | src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java b/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java new file mode 100644 index 0000000..340983d --- /dev/null +++ b/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java @@ -0,0 +1,120 @@ +package dev.isxander.yacl.gui.controllers; + +import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.api.Option; +import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.AbstractWidget; +import dev.isxander.yacl.gui.YACLScreen; +import net.minecraft.client.gui.DrawableHelper; +import net.minecraft.client.util.math.MatrixStack; +import net.minecraft.text.Text; +import org.lwjgl.glfw.GLFW; + +/** + * This controller renders a tickbox + */ +public class TickBoxController implements Controller<Boolean> { + private final Option<Boolean> option; + + /** + * Constructs a tickbox controller + * + * @param option bound option + */ + public TickBoxController(Option<Boolean> option) { + this.option = option; + } + + /** + * {@inheritDoc} + */ + @Override + public Option<Boolean> option() { + return option; + } + + /** + * {@inheritDoc} + */ + @Override + public Text formatValue() { + return Text.empty(); + } + + /** + * {@inheritDoc} + */ + @Override + public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { + return new TickBoxControllerElement(this, screen, widgetDimension); + } + + public static class TickBoxControllerElement extends ControllerWidget<TickBoxController> { + public TickBoxControllerElement(TickBoxController control, YACLScreen screen, Dimension<Integer> dim) { + super(control, screen, dim); + } + + @Override + protected void drawHoveredControl(MatrixStack matrices, int mouseX, int mouseY, float delta) { + int outlineSize = 10; + int outlineX1 = getDimension().xLimit() - getXPadding() - outlineSize; + int outlineY1 = getDimension().centerY() - outlineSize / 2; + int outlineX2 = getDimension().xLimit() - getXPadding(); + int outlineY2 = getDimension().centerY() + outlineSize / 2; + + int color = getValueColor(); + int shadowColor = multiplyColor(color, 0.25f); + + drawOutline(matrices, outlineX1 + 1, outlineY1 + 1, outlineX2 + 1, outlineY2 + 1, 1, shadowColor); + drawOutline(matrices, outlineX1, outlineY1, outlineX2, outlineY2, 1, color); + if (control.option().pendingValue()) { + DrawableHelper.fill(matrices, outlineX1 + 3, outlineY1 + 3, outlineX2 - 1, outlineY2 - 1, shadowColor); + DrawableHelper.fill(matrices, outlineX1 + 2, outlineY1 + 2, outlineX2 - 2, outlineY2 - 2, color); + } + } + + @Override + protected void drawValueText(MatrixStack matrices, int mouseX, int mouseY, float delta) { + if (!isHovered()) + drawHoveredControl(matrices, mouseX, mouseY, delta); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (!isMouseOver(mouseX, mouseY) || !isAvailable()) + return false; + + toggleSetting(); + return true; + } + + @Override + protected int getHoveredControlWidth() { + return 10; + } + + @Override + protected int getUnhoveredControlWidth() { + return 10; + } + + public void toggleSetting() { + control.option().requestSet(!control.option().pendingValue()); + playDownSound(); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + if (!focused) { + return false; + } + + if (keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_SPACE || keyCode == GLFW.GLFW_KEY_KP_ENTER) { + toggleSetting(); + return true; + } + + return false; + } + } +} |