package dev.isxander.yacl.gui.controllers; import dev.isxander.yacl.api.ButtonOption; import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.utils.Dimension; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; /** * Simple controller that simply runs the button action on press * and renders a {@link} Text on the right. */ public class ActionController implements Controller { public static final Text DEFAULT_TEXT = Text.translatable("yacl.control.action.execute"); private final ButtonOption option; private final Text text; /** * Constructs an action controller * with the default formatter of {@link ActionController#DEFAULT_TEXT} * * @param option bound option */ public ActionController(ButtonOption option) { this(option, DEFAULT_TEXT); } /** * Constructs an action controller * * @param option bound option * @param text text to display */ public ActionController(ButtonOption option, Text text) { this.option = option; this.text = text; } /** * {@inheritDoc} */ @Override public ButtonOption option() { return option; } /** * {@inheritDoc} */ @Override public Text formatValue() { return text; } /** * {@inheritDoc} */ @Override public ControllerWidget provideWidget(Screen screen, Dimension widgetDimension) { return new ActionControllerElement(this, screen, widgetDimension); } @ApiStatus.Internal public static class ActionControllerElement extends ControllerWidget { public ActionControllerElement(ActionController control, Screen screen, Dimension dim) { super(control, screen, dim); } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { if (isMouseOver(mouseX, mouseY)) { playDownSound(); control.option().action().run(); return true; } return false; } @Override protected int getHoveredControlWidth() { return getUnhoveredControlWidth(); } } }