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 { private final Option option; /** * Constructs a tickbox controller * * @param option bound option */ public TickBoxController(Option option) { this.option = option; } /** * {@inheritDoc} */ @Override public Option option() { return option; } /** * {@inheritDoc} */ @Override public Text formatValue() { return Text.empty(); } /** * {@inheritDoc} */ @Override public AbstractWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { return new TickBoxControllerElement(this, screen, widgetDimension); } public static class TickBoxControllerElement extends ControllerWidget { public TickBoxControllerElement(TickBoxController control, YACLScreen screen, Dimension 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; } } }