diff options
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui/controllers')
28 files changed, 0 insertions, 3038 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java b/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java deleted file mode 100644 index e57cdd2..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ActionController.java +++ /dev/null @@ -1,120 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.mojang.blaze3d.platform.InputConstants; -import dev.isxander.yacl.api.ButtonOption; -import dev.isxander.yacl.api.Controller; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; -import net.minecraft.network.chat.Component; - -import java.util.function.BiConsumer; - -/** - * Simple controller that simply runs the button action on press - * and renders a {@link} Text on the right. - */ -public class ActionController implements Controller<BiConsumer<YACLScreen, ButtonOption>> { - public static final Component DEFAULT_TEXT = Component.translatable("yacl.control.action.execute"); - - private final ButtonOption option; - private final Component 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, Component text) { - this.option = option; - this.text = text; - - } - - /** - * {@inheritDoc} - */ - @Override - public ButtonOption option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return text; - } - - /** - * {@inheritDoc} - */ - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new ActionControllerElement(this, screen, widgetDimension); - } - - public static class ActionControllerElement extends ControllerWidget<ActionController> { - private final String buttonString; - - public ActionControllerElement(ActionController control, YACLScreen screen, Dimension<Integer> dim) { - super(control, screen, dim); - buttonString = control.formatValue().getString().toLowerCase(); - } - - public void executeAction() { - playDownSound(); - control.option().action().accept(screen, control.option()); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (isMouseOver(mouseX, mouseY) && isAvailable()) { - executeAction(); - return true; - } - return false; - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (!focused) { - return false; - } - - if (keyCode == InputConstants.KEY_RETURN || keyCode == InputConstants.KEY_SPACE || keyCode == InputConstants.KEY_NUMPADENTER) { - executeAction(); - return true; - } - - return false; - } - - @Override - protected int getHoveredControlWidth() { - return getUnhoveredControlWidth(); - } - - @Override - public boolean canReset() { - return false; - } - - @Override - public boolean matchesSearch(String query) { - return super.matchesSearch(query) || buttonString.contains(query); - } - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/BooleanController.java b/src/client/java/dev/isxander/yacl/gui/controllers/BooleanController.java deleted file mode 100644 index d7d17d5..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/BooleanController.java +++ /dev/null @@ -1,157 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.mojang.blaze3d.platform.InputConstants; -import com.mojang.blaze3d.vertex.PoseStack; -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.ChatFormatting; -import net.minecraft.network.chat.CommonComponents; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * This controller renders a simple formatted {@link Component} - */ -public class BooleanController implements Controller<Boolean> { - - public static final Function<Boolean, Component> ON_OFF_FORMATTER = (state) -> - state - ? CommonComponents.OPTION_ON - : CommonComponents.OPTION_OFF; - - public static final Function<Boolean, Component> TRUE_FALSE_FORMATTER = (state) -> - state - ? Component.translatable("yacl.control.boolean.true") - : Component.translatable("yacl.control.boolean.false"); - - public static final Function<Boolean, Component> YES_NO_FORMATTER = (state) -> - state - ? CommonComponents.GUI_YES - : CommonComponents.GUI_NO; - - private final Option<Boolean> option; - private final Function<Boolean, Component> valueFormatter; - private final boolean coloured; - - /** - * Constructs a tickbox controller - * with the default value formatter of {@link BooleanController#ON_OFF_FORMATTER} - * - * @param option bound option - */ - public BooleanController(Option<Boolean> option) { - this(option, ON_OFF_FORMATTER, false); - } - - /** - * Constructs a tickbox controller - * with the default value formatter of {@link BooleanController#ON_OFF_FORMATTER} - * - * @param option bound option - * @param coloured value format is green or red depending on the state - */ - public BooleanController(Option<Boolean> option, boolean coloured) { - this(option, ON_OFF_FORMATTER, coloured); - } - - /** - * Constructs a tickbox controller - * - * @param option bound option - * @param valueFormatter format value into any {@link Component} - * @param coloured value format is green or red depending on the state - */ - public BooleanController(Option<Boolean> option, Function<Boolean, Component> valueFormatter, boolean coloured) { - this.option = option; - this.valueFormatter = valueFormatter; - this.coloured = coloured; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Boolean> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * Value format is green or red depending on the state - */ - public boolean coloured() { - return coloured; - } - - /** - * {@inheritDoc} - */ - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new BooleanControllerElement(this, screen, widgetDimension); - } - - public static class BooleanControllerElement extends ControllerWidget<BooleanController> { - public BooleanControllerElement(BooleanController control, YACLScreen screen, Dimension<Integer> dim) { - super(control, screen, dim); - } - - @Override - protected void drawHoveredControl(PoseStack matrices, int mouseX, int mouseY, float 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 getUnhoveredControlWidth(); - } - - public void toggleSetting() { - control.option().requestSet(!control.option().pendingValue()); - playDownSound(); - } - - @Override - protected Component getValueText() { - if (control.coloured()) { - return super.getValueText().copy().withStyle(control.option().pendingValue() ? ChatFormatting.GREEN : ChatFormatting.RED); - } - - return super.getValueText(); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (!isFocused()) { - return false; - } - - if (keyCode == InputConstants.KEY_RETURN || keyCode == InputConstants.KEY_SPACE || keyCode == InputConstants.KEY_NUMPADENTER) { - toggleSetting(); - return true; - } - - return false; - } - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ColorController.java b/src/client/java/dev/isxander/yacl/gui/controllers/ColorController.java deleted file mode 100644 index 473407b..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ColorController.java +++ /dev/null @@ -1,221 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.google.common.collect.ImmutableList; -import com.mojang.blaze3d.vertex.PoseStack; -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.api.utils.MutableDimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.gui.controllers.string.IStringController; -import dev.isxander.yacl.gui.controllers.string.StringControllerElement; -import net.minecraft.ChatFormatting; -import net.minecraft.client.gui.GuiComponent; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.MutableComponent; - -import java.awt.Color; -import java.util.List; - -/** - * A color controller that uses a hex color field as input. - */ -public class ColorController implements IStringController<Color> { - private final Option<Color> option; - private final boolean allowAlpha; - - /** - * Constructs a color controller with {@link ColorController#allowAlpha()} defaulting to false - * - * @param option bound option - */ - public ColorController(Option<Color> option) { - this(option, false); - } - - /** - * Constructs a color controller - * - * @param option bound option - * @param allowAlpha allows the color input to accept alpha values - */ - public ColorController(Option<Color> option, boolean allowAlpha) { - this.option = option; - this.allowAlpha = allowAlpha; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Color> option() { - return option; - } - - public boolean allowAlpha() { - return allowAlpha; - } - - @Override - public String getString() { - return formatValue().getString(); - } - - @Override - public Component formatValue() { - MutableComponent text = Component.literal("#"); - text.append(Component.literal(toHex(option().pendingValue().getRed())).withStyle(ChatFormatting.RED)); - text.append(Component.literal(toHex(option().pendingValue().getGreen())).withStyle(ChatFormatting.GREEN)); - text.append(Component.literal(toHex(option().pendingValue().getBlue())).withStyle(ChatFormatting.BLUE)); - if (allowAlpha()) text.append(toHex(option().pendingValue().getAlpha())); - return text; - } - - private String toHex(int value) { - String hex = Integer.toString(value, 16).toUpperCase(); - if (hex.length() == 1) - hex = "0" + hex; - return hex; - } - - @Override - public void setFromString(String value) { - if (value.startsWith("#")) - value = value.substring(1); - - int red = Integer.parseInt(value.substring(0, 2), 16); - int green = Integer.parseInt(value.substring(2, 4), 16); - int blue = Integer.parseInt(value.substring(4, 6), 16); - - if (allowAlpha()) { - int alpha = Integer.parseInt(value.substring(6, 8), 16); - option().requestSet(new Color(red, green, blue, alpha)); - } else { - option().requestSet(new Color(red, green, blue)); - } - } - - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new ColorControllerElement(this, screen, widgetDimension); - } - - public static class ColorControllerElement extends StringControllerElement { - private final ColorController colorController; - - protected MutableDimension<Integer> colorPreviewDim; - - private final List<Character> allowedChars; - - public ColorControllerElement(ColorController control, YACLScreen screen, Dimension<Integer> dim) { - super(control, screen, dim, true); - this.colorController = control; - this.allowedChars = ImmutableList.of('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'); - } - - @Override - protected void drawValueText(PoseStack matrices, int mouseX, int mouseY, float delta) { - if (isHovered()) { - colorPreviewDim.move(-inputFieldBounds.width() - 5, 0); - super.drawValueText(matrices, mouseX, mouseY, delta); - } - - GuiComponent.fill(matrices, colorPreviewDim.x(), colorPreviewDim.y(), colorPreviewDim.xLimit(), colorPreviewDim.yLimit(), colorController.option().pendingValue().getRGB()); - drawOutline(matrices, colorPreviewDim.x(), colorPreviewDim.y(), colorPreviewDim.xLimit(), colorPreviewDim.yLimit(), 1, 0xFF000000); - } - - @Override - public void write(String string) { - if (string.startsWith("0x")) string = string.substring(2); - for (char chr : string.toCharArray()) { - if (!allowedChars.contains(Character.toLowerCase(chr))) { - return; - } - } - - if (caretPos == 0) - return; - - String trimmed = string.substring(0, Math.min(inputField.length() - caretPos, string.length())); - - if (modifyInput(builder -> builder.replace(caretPos, caretPos + trimmed.length(), trimmed))) { - caretPos += trimmed.length(); - setSelectionLength(); - updateControl(); - } - } - - @Override - protected void doBackspace() { - if (caretPos > 1) { - if (modifyInput(builder -> builder.setCharAt(caretPos - 1, '0'))) { - caretPos--; - updateControl(); - } - } - } - - @Override - protected void doDelete() { - if (caretPos >= 1) { - if (modifyInput(builder -> builder.setCharAt(caretPos, '0'))) { - updateControl(); - } - } - } - - @Override - protected boolean doCut() { - return false; - } - - @Override - protected boolean doCopy() { - return false; - } - - @Override - protected boolean doSelectAll() { - return false; - } - - protected void setSelectionLength() { - selectionLength = caretPos < inputField.length() && caretPos > 0 ? 1 : 0; - } - - @Override - protected int getDefaultCaretPos() { - return colorController.allowAlpha() ? 3 : 1; - } - - @Override - public void setDimension(Dimension<Integer> dim) { - super.setDimension(dim); - - int previewSize = (dim.height() - getYPadding() * 2) / 2; - colorPreviewDim = Dimension.ofInt(dim.xLimit() - getXPadding() - previewSize, dim.centerY() - previewSize / 2, previewSize, previewSize); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - int prevSelectionLength = selectionLength; - selectionLength = 0; - if (super.keyPressed(keyCode, scanCode, modifiers)) { - caretPos = Math.max(1, caretPos); - setSelectionLength(); - return true; - } else selectionLength = prevSelectionLength; - return false; - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (super.mouseClicked(mouseX, mouseY, button)) { - caretPos = Math.max(1, caretPos); - setSelectionLength(); - return true; - } - return false; - } - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java deleted file mode 100644 index 22c469d..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ /dev/null @@ -1,170 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.mojang.blaze3d.vertex.PoseStack; -import dev.isxander.yacl.api.Controller; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.gui.utils.GuiUtils; -import net.minecraft.ChatFormatting; -import net.minecraft.client.gui.ComponentPath; -import net.minecraft.client.gui.GuiComponent; -import net.minecraft.client.gui.components.MultiLineLabel; -import net.minecraft.client.gui.narration.NarratedElementType; -import net.minecraft.client.gui.narration.NarrationElementOutput; -import net.minecraft.client.gui.navigation.FocusNavigationEvent; -import net.minecraft.network.chat.Component; -import org.jetbrains.annotations.Nullable; - -public abstract class ControllerWidget<T extends Controller<?>> extends AbstractWidget { - protected final T control; - protected MultiLineLabel wrappedTooltip; - protected final YACLScreen screen; - - protected boolean focused = false; - protected boolean hovered = false; - - protected final Component modifiedOptionName; - protected final String optionNameString; - - public ControllerWidget(T control, YACLScreen screen, Dimension<Integer> dim) { - super(dim); - this.control = control; - this.screen = screen; - control.option().addListener((opt, pending) -> updateTooltip()); - updateTooltip(); - this.modifiedOptionName = control.option().name().copy().withStyle(ChatFormatting.ITALIC); - this.optionNameString = control.option().name().getString().toLowerCase(); - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { - hovered = isMouseOver(mouseX, mouseY); - - Component name = control.option().changed() ? modifiedOptionName : control.option().name(); - Component shortenedName = Component.literal(GuiUtils.shortenString(name.getString(), textRenderer, getDimension().width() - getControlWidth() - getXPadding() - 7, "...")).setStyle(name.getStyle()); - - drawButtonRect(matrices, getDimension().x(), getDimension().y(), getDimension().xLimit(), getDimension().yLimit(), isHovered(), isAvailable()); - matrices.pushPose(); - matrices.translate(getDimension().x() + getXPadding(), getTextY(), 0); - textRenderer.drawShadow(matrices, shortenedName, 0, 0, getValueColor()); - matrices.popPose(); - - drawValueText(matrices, mouseX, mouseY, delta); - if (isHovered()) { - drawHoveredControl(matrices, mouseX, mouseY, delta); - } - } - - @Override - public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) { - if (hovered || focused) { - YACLScreen.renderMultilineTooltip(matrices, textRenderer, wrappedTooltip, getDimension().centerX(), getDimension().y() - 5, getDimension().yLimit() + 5, screen.width, screen.height); - } - } - - protected void drawHoveredControl(PoseStack matrices, int mouseX, int mouseY, float delta) { - - } - - protected void drawValueText(PoseStack matrices, int mouseX, int mouseY, float delta) { - Component valueText = getValueText(); - matrices.pushPose(); - matrices.translate(getDimension().xLimit() - textRenderer.width(valueText) - getXPadding(), getTextY(), 0); - textRenderer.drawShadow(matrices, valueText, 0, 0, getValueColor()); - matrices.popPose(); - } - - private void updateTooltip() { - this.wrappedTooltip = MultiLineLabel.create(textRenderer, control.option().tooltip(), screen.width / 3 * 2 - 10); - } - - protected int getControlWidth() { - return isHovered() ? getHoveredControlWidth() : getUnhoveredControlWidth(); - } - - public boolean isHovered() { - return isAvailable() && (hovered || focused); - } - - protected abstract int getHoveredControlWidth(); - - protected int getUnhoveredControlWidth() { - return textRenderer.width(getValueText()); - } - - protected int getXPadding() { - return 5; - } - - protected int getYPadding() { - return 2; - } - - protected Component getValueText() { - return control.formatValue(); - } - - protected boolean isAvailable() { - return control.option().available(); - } - - protected int getValueColor() { - return isAvailable() ? -1 : inactiveColor; - } - - @Override - public boolean canReset() { - return true; - } - - protected void drawOutline(PoseStack matrices, int x1, int y1, int x2, int y2, int width, int color) { - GuiComponent.fill(matrices, x1, y1, x2, y1 + width, color); - GuiComponent.fill(matrices, x2, y1, x2 - width, y2, color); - GuiComponent.fill(matrices, x1, y2, x2, y2 - width, color); - GuiComponent.fill(matrices, x1, y1, x1 + width, y2, color); - } - - protected float getTextY() { - return getDimension().y() + getDimension().height() / 2f - textRenderer.lineHeight / 2f; - } - - @Nullable - @Override - public ComponentPath nextFocusPath(FocusNavigationEvent focusNavigationEvent) { - if (!this.isAvailable()) - return null; - return !this.isFocused() ? ComponentPath.leaf(this) : null; - } - - @Override - public boolean isFocused() { - return focused; - } - - @Override - public void setFocused(boolean focused) { - this.focused = focused; - } - - @Override - public void unfocus() { - this.focused = false; - } - - @Override - public boolean matchesSearch(String query) { - return optionNameString.contains(query.toLowerCase()); - } - - @Override - public NarrationPriority narrationPriority() { - return focused ? NarrationPriority.FOCUSED : isHovered() ? NarrationPriority.HOVERED : NarrationPriority.NONE; - } - - @Override - public void updateNarration(NarrationElementOutput builder) { - builder.add(NarratedElementType.TITLE, control.option().name()); - builder.add(NarratedElementType.HINT, control.option().tooltip()); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/LabelController.java b/src/client/java/dev/isxander/yacl/gui/controllers/LabelController.java deleted file mode 100644 index 1dfae0b..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/LabelController.java +++ /dev/null @@ -1,193 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.mojang.blaze3d.vertex.PoseStack; -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.ComponentPath; -import net.minecraft.client.gui.GuiComponent; -import net.minecraft.client.gui.components.MultiLineLabel; -import net.minecraft.client.gui.narration.NarratedElementType; -import net.minecraft.client.gui.narration.NarrationElementOutput; -import net.minecraft.client.gui.navigation.FocusNavigationEvent; -import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.HoverEvent; -import net.minecraft.network.chat.Style; -import net.minecraft.util.FormattedCharSequence; -import net.minecraft.world.item.ItemStack; -import org.jetbrains.annotations.Nullable; - -import java.util.List; - -/** - * Simply renders some text as a label. - */ -public class LabelController implements Controller<Component> { - private final Option<Component> option; - /** - * Constructs a label controller - * - * @param option bound option - */ - public LabelController(Option<Component> option) { - this.option = option; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Component> option() { - return option; - } - - @Override - public Component formatValue() { - return option().pendingValue(); - } - - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new LabelControllerElement(screen, widgetDimension); - } - - public class LabelControllerElement extends AbstractWidget { - private List<FormattedCharSequence> wrappedText; - protected MultiLineLabel wrappedTooltip; - protected boolean focused; - - protected final YACLScreen screen; - - public LabelControllerElement(YACLScreen screen, Dimension<Integer> dim) { - super(dim); - this.screen = screen; - option().addListener((opt, pending) -> updateTooltip()); - updateTooltip(); - updateText(); - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { - updateText(); - - float y = getDimension().y(); - for (FormattedCharSequence text : wrappedText) { - textRenderer.drawShadow(matrices, text, getDimension().x() + getXPadding(), y + getYPadding(), option().available() ? -1 : 0xFFA0A0A0); - y += textRenderer.lineHeight; - } - - if (isFocused()) { - GuiComponent.fill(matrices, getDimension().x() - 1, getDimension().y() - 1, getDimension().xLimit() + 1, getDimension().y(), -1); - GuiComponent.fill(matrices, getDimension().x() - 1, getDimension().y() - 1, getDimension().x(), getDimension().yLimit() + 1, -1); - GuiComponent.fill(matrices, getDimension().x() - 1, getDimension().yLimit(), getDimension().xLimit() + 1, getDimension().yLimit() + 1, -1); - GuiComponent.fill(matrices, getDimension().xLimit(), getDimension().y() - 1, getDimension().xLimit() + 1, getDimension().yLimit() + 1, -1); - } - } - - @Override - public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) { - if (isMouseOver(mouseX, mouseY)) { - YACLScreen.renderMultilineTooltip(matrices, textRenderer, wrappedTooltip, getDimension().centerX(), getDimension().y() - 5, getDimension().yLimit() + 5, screen.width, screen.height); - - Style style = getStyle(mouseX, mouseY); - if (style != null && style.getHoverEvent() != null) { - HoverEvent hoverEvent = style.getHoverEvent(); - HoverEvent.ItemStackInfo itemStackContent = hoverEvent.getValue(HoverEvent.Action.SHOW_ITEM); - if (itemStackContent != null) { - ItemStack stack = itemStackContent.getItemStack(); - screen.renderTooltip(matrices, screen.getTooltipFromItem(stack), stack.getTooltipImage(), mouseX, mouseY); - } else { - HoverEvent.EntityTooltipInfo entityContent = hoverEvent.getValue(HoverEvent.Action.SHOW_ENTITY); - if (entityContent != null) { - if (this.client.options.advancedItemTooltips) { - screen.renderComponentTooltip(matrices, entityContent.getTooltipLines(), mouseX, mouseY); - } - } else { - Component text = hoverEvent.getValue(HoverEvent.Action.SHOW_TEXT); - if (text != null) { - MultiLineLabel multilineText = MultiLineLabel.create(textRenderer, text, getDimension().width()); - YACLScreen.renderMultilineTooltip(matrices, textRenderer, multilineText, getDimension().centerX(), getDimension().y(), getDimension().yLimit(), screen.width, screen.height); - } - } - } - } - } - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (!isMouseOver(mouseX, mouseY)) - return false; - - Style style = getStyle((int) mouseX, (int) mouseY); - return screen.handleComponentClicked(style); - } - - protected Style getStyle(int mouseX, int mouseY) { - if (!getDimension().isPointInside(mouseX, mouseY)) - return null; - - int x = mouseX - getDimension().x(); - int y = mouseY - getDimension().y() - getYPadding(); - int line = y / textRenderer.lineHeight; - - if (x < 0 || x > getDimension().xLimit()) return null; - if (y < 0 || y > getDimension().yLimit()) return null; - if (line < 0 || line >= wrappedText.size()) return null; - - return textRenderer.getSplitter().componentStyleAtWidth(wrappedText.get(line), x); - } - - private int getXPadding() { - return 4; - } - - private int getYPadding() { - return 3; - } - - private void updateText() { - wrappedText = textRenderer.split(formatValue(), getDimension().width() - getXPadding() * 2); - setDimension(getDimension().withHeight(wrappedText.size() * textRenderer.lineHeight + getYPadding() * 2)); - } - - private void updateTooltip() { - this.wrappedTooltip = MultiLineLabel.create(textRenderer, option().tooltip(), screen.width / 3 * 2 - 10); - } - - @Override - public boolean matchesSearch(String query) { - return formatValue().getString().toLowerCase().contains(query.toLowerCase()); - } - - @Nullable - @Override - public ComponentPath nextFocusPath(FocusNavigationEvent focusNavigationEvent) { - if (!option().available()) - return null; - return !this.isFocused() ? ComponentPath.leaf(this) : null; - } - - @Override - public boolean isFocused() { - return focused; - } - - @Override - public void setFocused(boolean focused) { - this.focused = focused; - } - - @Override - public void updateNarration(NarrationElementOutput builder) { - builder.add(NarratedElementType.TITLE, formatValue()); - } - - @Override - public NarrationPriority narrationPriority() { - return NarrationPriority.FOCUSED; - } - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java b/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java deleted file mode 100644 index 1acccf1..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java +++ /dev/null @@ -1,133 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.google.common.collect.ImmutableList; -import com.mojang.blaze3d.vertex.PoseStack; -import dev.isxander.yacl.api.ListOption; -import dev.isxander.yacl.api.ListOptionEntry; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.*; -import net.minecraft.client.gui.components.events.ContainerEventHandler; -import net.minecraft.client.gui.components.events.GuiEventListener; -import net.minecraft.client.gui.narration.NarrationElementOutput; -import net.minecraft.network.chat.Component; -import org.jetbrains.annotations.Nullable; - -import java.util.List; - -public class ListEntryWidget extends AbstractWidget implements ContainerEventHandler { - private final TooltipButtonWidget removeButton, moveUpButton, moveDownButton; - private final AbstractWidget entryWidget; - - private final ListOption<?> listOption; - private final ListOptionEntry<?> listOptionEntry; - - private final String optionNameString; - - private GuiEventListener focused; - private boolean dragging; - - public ListEntryWidget(YACLScreen screen, ListOptionEntry<?> listOptionEntry, AbstractWidget entryWidget) { - super(entryWidget.getDimension().withHeight(Math.max(entryWidget.getDimension().height(), 20) - ((listOptionEntry.parentGroup().indexOf(listOptionEntry) == listOptionEntry.parentGroup().options().size() - 1) ? 0 : 2))); // -2 to remove the padding - this.listOptionEntry = listOptionEntry; - this.listOption = listOptionEntry.parentGroup(); - this.optionNameString = listOptionEntry.name().getString().toLowerCase(); - this.entryWidget = entryWidget; - - Dimension<Integer> dim = entryWidget.getDimension(); - entryWidget.setDimension(dim.clone().move(20 * 2, 0).expand(-20 * 3, 0)); - - removeButton = new TooltipButtonWidget(screen, dim.xLimit() - 20, dim.y(), 20, 20, Component.literal("\u274c"), Component.translatable("yacl.list.remove"), btn -> { - listOption.removeEntry(listOptionEntry); - updateButtonStates(); - }); - - moveUpButton = new TooltipButtonWidget(screen, dim.x(), dim.y(), 20, 20, Component.literal("\u2191"), Component.translatable("yacl.list.move_up"), btn -> { - int index = listOption.indexOf(listOptionEntry) - 1; - if (index >= 0) { - listOption.removeEntry(listOptionEntry); - listOption.insertEntry(index, listOptionEntry); - updateButtonStates(); - } - }); - - moveDownButton = new TooltipButtonWidget(screen, dim.x() + 20, dim.y(), 20, 20, Component.literal("\u2193"), Component.translatable("yacl.list.move_down"), btn -> { - int index = listOption.indexOf(listOptionEntry) + 1; - if (index < listOption.options().size()) { - listOption.removeEntry(listOptionEntry); - listOption.insertEntry(index, listOptionEntry); - updateButtonStates(); - } - }); - - updateButtonStates(); - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { - updateButtonStates(); // update every render in case option becomes available/unavailable - - removeButton.setY(getDimension().y()); - moveUpButton.setY(getDimension().y()); - moveDownButton.setY(getDimension().y()); - entryWidget.setDimension(entryWidget.getDimension().withY(getDimension().y())); - - removeButton.render(matrices, mouseX, mouseY, delta); - moveUpButton.render(matrices, mouseX, mouseY, delta); - moveDownButton.render(matrices, mouseX, mouseY, delta); - entryWidget.render(matrices, mouseX, mouseY, delta); - } - - @Override - public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) { - removeButton.renderHoveredTooltip(matrices); - moveUpButton.renderHoveredTooltip(matrices); - moveDownButton.renderHoveredTooltip(matrices); - } - - protected void updateButtonStates() { - removeButton.active = listOption.available(); - moveUpButton.active = listOption.indexOf(listOptionEntry) > 0 && listOption.available(); - moveDownButton.active = listOption.indexOf(listOptionEntry) < listOption.options().size() - 1 && listOption.available(); - } - - @Override - public void unfocus() { - entryWidget.unfocus(); - } - - @Override - public void updateNarration(NarrationElementOutput builder) { - entryWidget.updateNarration(builder); - } - - @Override - public boolean matchesSearch(String query) { - return optionNameString.contains(query.toLowerCase()); - } - - @Override - public List<? extends GuiEventListener> children() { - return ImmutableList.of(moveUpButton, moveDownButton, entryWidget, removeButton); - } - - @Override - public boolean isDragging() { - return dragging; - } - - @Override - public void setDragging(boolean dragging) { - this.dragging = dragging; - } - - @Nullable - @Override - public GuiEventListener getFocused() { - return focused; - } - - @Override - public void setFocused(@Nullable GuiEventListener focused) { - this.focused = focused; - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java b/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java deleted file mode 100644 index 3f615db..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/TickBoxController.java +++ /dev/null @@ -1,120 +0,0 @@ -package dev.isxander.yacl.gui.controllers; - -import com.mojang.blaze3d.platform.InputConstants; -import com.mojang.blaze3d.vertex.PoseStack; -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.GuiComponent; -import net.minecraft.network.chat.Component; - -/** - * 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 Component formatValue() { - return Component.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(PoseStack 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()) { - GuiComponent.fill(matrices, outlineX1 + 3, outlineY1 + 3, outlineX2 - 1, outlineY2 - 1, shadowColor); - GuiComponent.fill(matrices, outlineX1 + 2, outlineY1 + 2, outlineX2 - 2, outlineY2 - 2, color); - } - } - - @Override - protected void drawValueText(PoseStack 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 == InputConstants.KEY_RETURN || keyCode == InputConstants.KEY_SPACE || keyCode == InputConstants.KEY_NUMPADENTER) { - toggleSetting(); - return true; - } - - return false; - } - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java deleted file mode 100644 index ff20039..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingControllerElement.java +++ /dev/null @@ -1,60 +0,0 @@ -package dev.isxander.yacl.gui.controllers.cycling; - -import com.mojang.blaze3d.platform.InputConstants; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.gui.controllers.ControllerWidget; -import net.minecraft.client.gui.screens.Screen; - -public class CyclingControllerElement extends ControllerWidget<ICyclingController<?>> { - - public CyclingControllerElement(ICyclingController<?> control, YACLScreen screen, Dimension<Integer> dim) { - super(control, screen, dim); - } - - public void cycleValue(int increment) { - int targetIdx = control.getPendingValue() + increment; - if (targetIdx >= control.getCycleLength()) { - targetIdx -= control.getCycleLength(); - } else if (targetIdx < 0) { - targetIdx += control.getCycleLength(); - } - control.setPendingValue(targetIdx); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (!isMouseOver(mouseX, mouseY) || (button != 0 && button != 1) || !isAvailable()) - return false; - - playDownSound(); - cycleValue(button == 1 || Screen.hasShiftDown() || Screen.hasControlDown() ? -1 : 1); - - return true; - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (!focused) - return false; - - switch (keyCode) { - case InputConstants.KEY_LEFT -> - cycleValue(-1); - case InputConstants.KEY_RIGHT -> - cycleValue(1); - case InputConstants.KEY_RETURN, InputConstants.KEY_SPACE, InputConstants.KEY_NUMPADENTER -> - cycleValue(Screen.hasControlDown() || Screen.hasShiftDown() ? -1 : 1); - default -> { - return false; - } - } - - return true; - } - - @Override - protected int getHoveredControlWidth() { - return getUnhoveredControlWidth(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java deleted file mode 100644 index 34f2cc9..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/CyclingListController.java +++ /dev/null @@ -1,79 +0,0 @@ -package dev.isxander.yacl.gui.controllers.cycling; - -import com.google.common.collect.ImmutableList; -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * A controller where once clicked, cycles through elements - * in the provided list. - */ -public class CyclingListController<T> implements ICyclingController<T> { - private final Option<T> option; - private final Function<T, Component> valueFormatter; - private final ImmutableList<T> values; - - /** - * Constructs a {@link CyclingListController}, with a default - * value formatter of {@link Object#toString()}. - * @param option option of which to bind the controller to - * @param values the values to cycle through - */ - public CyclingListController(Option<T> option, Iterable<T> values) { - this(option, values, value -> Component.literal(value.toString())); - } - - /** - * Constructs a {@link CyclingListController} - * @param option option of which to bind the controller to - * @param values the values to cycle through - * @param valueFormatter function of how to convert each value to a string to display - */ - public CyclingListController(Option<T> option, Iterable<T> values, Function<T, Component> valueFormatter) { - this.option = option; - this.valueFormatter = valueFormatter; - this.values = ImmutableList.copyOf(values); - } - - /** - * {@inheritDoc} - */ - @Override - public Option<T> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(int ordinal) { - option().requestSet(values.get(ordinal)); - } - - /** - * {@inheritDoc} - */ - @Override - public int getPendingValue() { - return values.indexOf(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public int getCycleLength() { - return values.size(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java deleted file mode 100644 index ebd2cb6..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/EnumController.java +++ /dev/null @@ -1,60 +0,0 @@ -package dev.isxander.yacl.gui.controllers.cycling; - -import dev.isxander.yacl.api.NameableEnum; -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; -import net.minecraft.util.OptionEnum; - -import java.util.Arrays; -import java.util.function.Function; - -/** - * Simple controller type that displays the enum on the right. - * <p> - * Cycles forward with left click, cycles backward with right click or when shift is held - * - * @param <T> enum type - */ -public class EnumController<T extends Enum<T>> extends CyclingListController<T> { - public static <T extends Enum<T>> Function<T, Component> getDefaultFormatter() { - return value -> { - if (value instanceof NameableEnum nameableEnum) - return nameableEnum.getDisplayName(); - if (value instanceof OptionEnum translatableOption) - return translatableOption.getCaption(); - return Component.literal(value.toString()); - }; - } - - /** - * Constructs a cycling enum controller with a default value formatter and all values being available. - * The default value formatter first searches if the - * enum is a {@link NameableEnum} or {@link OptionEnum} else, just uses {@link Enum#toString()} - * - * @param option bound option - */ - public EnumController(Option<T> option) { - this(option, getDefaultFormatter()); - } - - /** - * Constructs a cycling enum controller with all values being available. - * - * @param option bound option - * @param valueFormatter format the enum into any {@link Component} - */ - public EnumController(Option<T> option, Function<T, Component> valueFormatter) { - this(option, valueFormatter, option.typeClass().getEnumConstants()); - } - - /** - * Constructs a cycling enum controller. - * - * @param option bound option - * @param valueFormatter format the enum into any {@link Component} - * @param availableValues all enum constants that can be cycled through - */ - public EnumController(Option<T> option, Function<T, Component> valueFormatter, T[] availableValues) { - super(option, Arrays.asList(availableValues), valueFormatter); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java b/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java deleted file mode 100644 index 081b572..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/cycling/ICyclingController.java +++ /dev/null @@ -1,38 +0,0 @@ -package dev.isxander.yacl.gui.controllers.cycling; - -import dev.isxander.yacl.api.Controller; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; - -/** - * This interface simply generifies setting and getting of - * the pending value, using an ordinal so elements can cycle through - * without knowing the content. - */ -public interface ICyclingController<T> extends Controller<T> { - /** - * Sets the pending value to whatever corresponds to the ordinal - * @param ordinal index of element to set - */ - void setPendingValue(int ordinal); - - /** - * Gets the pending ordinal that corresponds to the actual value - * @return ordinal - */ - int getPendingValue(); - - /** - * Allows the element when it should wrap-around back to zeroth ordinal - */ - int getCycleLength(); - - /** - * {@inheritDoc} - */ - @Override - default AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new CyclingControllerElement(this, screen, widgetDimension); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/package-info.java b/src/client/java/dev/isxander/yacl/gui/controllers/package-info.java deleted file mode 100644 index 12ce86b..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/package-info.java +++ /dev/null @@ -1,12 +0,0 @@ -/** - * This package contains all {@link dev.isxander.yacl.api.Controller} implementations - * - * <ul> - * <li>For numbers: {@link dev.isxander.yacl.gui.controllers.slider}</li> - * <li>For booleans: {@link dev.isxander.yacl.gui.controllers.TickBoxController}</li> - * <li>For lists/enums: {@link dev.isxander.yacl.gui.controllers.cycling}</li> - * <li>For strings: {@link dev.isxander.yacl.gui.controllers.string.StringController}</li> - * <li>For {@link dev.isxander.yacl.api.ButtonOption}: {@link dev.isxander.yacl.gui.controllers.ActionController}</li> - * </ul> - */ -package dev.isxander.yacl.gui.controllers; diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java deleted file mode 100644 index 8e044b1..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/DoubleSliderController.java +++ /dev/null @@ -1,114 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; -import org.apache.commons.lang3.Validate; - -import java.util.function.Function; - -/** - * {@link ISliderController} for doubles. - */ -public class DoubleSliderController implements ISliderController<Double> { - /** - * Formats doubles to two decimal places - */ - public static final Function<Double, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,.2f", value).replaceAll("[\u00a0\u202F]", " ")); - - private final Option<Double> option; - - private final double min, max, interval; - - private final Function<Double, Component> valueFormatter; - - /** - * Constructs a {@link ISliderController} for doubles - * using the default value formatter {@link DoubleSliderController#DEFAULT_FORMATTER}. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - */ - public DoubleSliderController(Option<Double> option, double min, double max, double interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - /** - * Constructs a {@link ISliderController} for doubles. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - * @param valueFormatter format the value into any {@link Component} - */ - public DoubleSliderController(Option<Double> option, double min, double max, double interval, Function<Double, Component> valueFormatter) { - Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); - Validate.isTrue(interval > 0, "`interval` must be more than 0"); - Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); - - this.option = option; - this.min = min; - this.max = max; - this.interval = interval; - this.valueFormatter = valueFormatter; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Double> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return max; - } - - /** - * {@inheritDoc} - */ - @Override - public double interval() { - return interval; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet(value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } - -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java deleted file mode 100644 index 25f2206..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/FloatSliderController.java +++ /dev/null @@ -1,114 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; -import org.apache.commons.lang3.Validate; - -import java.util.function.Function; - -/** - * {@link ISliderController} for floats. - */ -public class FloatSliderController implements ISliderController<Float> { - /** - * Formats floats to one decimal place - */ - public static final Function<Float, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,.1f", value).replaceAll("[\u00a0\u202F]", " ")); - - private final Option<Float> option; - - private final float min, max, interval; - - private final Function<Float, Component> valueFormatter; - - /** - * Constructs a {@link ISliderController} for floats - * using the default value formatter {@link FloatSliderController#DEFAULT_FORMATTER}. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - */ - public FloatSliderController(Option<Float> option, float min, float max, float interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - /** - * Constructs a {@link ISliderController} for floats. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - * @param valueFormatter format the value into any {@link Component} - */ - public FloatSliderController(Option<Float> option, float min, float max, float interval, Function<Float, Component> valueFormatter) { - Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); - Validate.isTrue(interval > 0, "`interval` must be more than 0"); - Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); - - this.option = option; - this.min = min; - this.max = max; - this.interval = interval; - this.valueFormatter = valueFormatter; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Float> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return max; - } - - /** - * {@inheritDoc} - */ - @Override - public double interval() { - return interval; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((float) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } - -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java deleted file mode 100644 index aa3c18f..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java +++ /dev/null @@ -1,54 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Controller; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.AbstractWidget; -import dev.isxander.yacl.gui.YACLScreen; - -/** - * Simple custom slider implementation that shifts the current value across when shown. - * <p> - * For simplicity, {@link SliderControllerElement} works in doubles so each - * {@link ISliderController} must cast to double. This is to get around re-writing the element for every type. - */ -public interface ISliderController<T extends Number> extends Controller<T> { - /** - * Gets the minimum value for the slider - */ - double min(); - - /** - * Gets the maximum value for the slider - */ - double max(); - - /** - * Gets the interval (or step size) for the slider. - */ - double interval(); - - /** - * Gets the range of the slider. - */ - default double range() { - return max() - min(); - } - - /** - * Sets the {@link dev.isxander.yacl.api.Option}'s pending value - */ - void setPendingValue(double value); - - /** - * Gets the {@link dev.isxander.yacl.api.Option}'s pending value - */ - double pendingValue(); - - /** - * {@inheritDoc} - */ - @Override - default AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new SliderControllerElement(this, screen, widgetDimension, min(), max(), interval()); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java deleted file mode 100644 index 4a68497..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/IntegerSliderController.java +++ /dev/null @@ -1,111 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; -import org.apache.commons.lang3.Validate; - -import java.util.function.Function; - -/** - * {@link ISliderController} for integers. - */ -public class IntegerSliderController implements ISliderController<Integer> { - public static final Function<Integer, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,d", value).replaceAll("[\u00a0\u202F]", " ")); - - private final Option<Integer> option; - - private final int min, max, interval; - - private final Function<Integer, Component> valueFormatter; - - /** - * Constructs a {@link ISliderController} for integers - * using the default value formatter {@link IntegerSliderController#DEFAULT_FORMATTER}. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - */ - public IntegerSliderController(Option<Integer> option, int min, int max, int interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - /** - * Constructs a {@link ISliderController} for integers. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - * @param valueFormatter format the value into any {@link Component} - */ - public IntegerSliderController(Option<Integer> option, int min, int max, int interval, Function<Integer, Component> valueFormatter) { - Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); - Validate.isTrue(interval > 0, "`interval` must be more than 0"); - Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); - - this.option = option; - this.min = min; - this.max = max; - this.interval = interval; - this.valueFormatter = valueFormatter; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Integer> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return max; - } - - /** - * {@inheritDoc} - */ - @Override - public double interval() { - return interval; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((int) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } - -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java deleted file mode 100644 index 681e7cf..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/LongSliderController.java +++ /dev/null @@ -1,111 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import dev.isxander.yacl.api.Option; -import net.minecraft.network.chat.Component; -import org.apache.commons.lang3.Validate; - -import java.util.function.Function; - -/** - * {@link ISliderController} for longs. - */ -public class LongSliderController implements ISliderController<Long> { - public static final Function<Long, Component> DEFAULT_FORMATTER = value -> Component.literal(String.format("%,d", value).replaceAll("[\u00a0\u202F]", " ")); - - private final Option<Long> option; - - private final long min, max, interval; - - private final Function<Long, Component> valueFormatter; - - /** - * Constructs a {@link ISliderController} for longs - * using the default value formatter {@link LongSliderController#DEFAULT_FORMATTER}. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - */ - public LongSliderController(Option<Long> option, long min, long max, long interval) { - this(option, min, max, interval, DEFAULT_FORMATTER); - } - - /** - * Constructs a {@link ISliderController} for longs. - * - * @param option bound option - * @param min minimum slider value - * @param max maximum slider value - * @param interval step size (or increments) for the slider - * @param valueFormatter format the value into any {@link Component} - */ - public LongSliderController(Option<Long> option, long min, long max, long interval, Function<Long, Component> valueFormatter) { - Validate.isTrue(max > min, "`max` cannot be smaller than `min`"); - Validate.isTrue(interval > 0, "`interval` must be more than 0"); - Validate.notNull(valueFormatter, "`valueFormatter` must not be null"); - - this.option = option; - this.min = min; - this.max = max; - this.interval = interval; - this.valueFormatter = valueFormatter; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<Long> option() { - return option; - } - - /** - * {@inheritDoc} - */ - @Override - public Component formatValue() { - return valueFormatter.apply(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return max; - } - - /** - * {@inheritDoc} - */ - @Override - public double interval() { - return interval; - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((long) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } - -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java deleted file mode 100644 index d00f3d7..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java +++ /dev/null @@ -1,164 +0,0 @@ -package dev.isxander.yacl.gui.controllers.slider; - -import com.mojang.blaze3d.platform.InputConstants; -import com.mojang.blaze3d.vertex.PoseStack; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.gui.controllers.ControllerWidget; -import net.minecraft.client.gui.GuiComponent; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.util.Mth; - -public class SliderControllerElement extends ControllerWidget<ISliderController<?>> { - private final double min, max, interval; - - private float interpolation; - - private Dimension<Integer> sliderBounds; - - private boolean mouseDown = false; - - public SliderControllerElement(ISliderController<?> option, YACLScreen screen, Dimension<Integer> dim, double min, double max, double interval) { - super(option, screen, dim); - this.min = min; - this.max = max; - this.interval = interval; - setDimension(dim); - } - - @Override - public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { - super.render(matrices, mouseX, mouseY, delta); - - calculateInterpolation(); - } - - @Override - protected void drawHoveredControl(PoseStack matrices, int mouseX, int mouseY, float delta) { - // track - GuiComponent.fill(matrices, sliderBounds.x(), sliderBounds.centerY() - 1, sliderBounds.xLimit(), sliderBounds.centerY(), -1); - // track shadow - GuiComponent.fill(matrices, sliderBounds.x() + 1, sliderBounds.centerY(), sliderBounds.xLimit() + 1, sliderBounds.centerY() + 1, 0xFF404040); - - // thumb shadow - GuiComponent.fill(matrices, getThumbX() - getThumbWidth() / 2 + 1, sliderBounds.y() + 1, getThumbX() + getThumbWidth() / 2 + 1, sliderBounds.yLimit() + 1, 0xFF404040); - // thumb - GuiComponent.fill(matrices, getThumbX() - getThumbWidth() / 2, sliderBounds.y(), getThumbX() + getThumbWidth() / 2, sliderBounds.yLimit(), -1); - } - - @Override - protected void drawValueText(PoseStack matrices, int mouseX, int mouseY, float delta) { - matrices.pushPose(); - if (isHovered()) - matrices.translate(-(sliderBounds.width() + 6 + getThumbWidth() / 2f), 0, 0); - super.drawValueText(matrices, mouseX, mouseY, delta); - matrices.popPose(); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (!isAvailable() || button != 0 || !sliderBounds.isPointInside((int) mouseX, (int) mouseY)) - return false; - - mouseDown = true; - - setValueFromMouse(mouseX); - return true; - } - - @Override - public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - if (!isAvailable() || button != 0 || !mouseDown) - return false; - - setValueFromMouse(mouseX); - return true; - } - - public void incrementValue(double amount) { - control.setPendingValue(Mth.clamp(control.pendingValue() + interval * amount, min, max)); - calculateInterpolation(); - } - - @Override - public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - if (!isAvailable() || (!isMouseOver(mouseX, mouseY)) || (!Screen.hasShiftDown() && !Screen.hasControlDown())) - return false; - - incrementValue(amount); - return true; - } - - @Override - public boolean mouseReleased(double mouseX, double mouseY, int button) { - if (isAvailable() && mouseDown) - playDownSound(); - mouseDown = false; - - return super.mouseReleased(mouseX, mouseY, button); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (!focused) - return false; - - switch (keyCode) { - case InputConstants.KEY_LEFT -> incrementValue(-1); - case InputConstants.KEY_RIGHT -> incrementValue(1); - default -> { - return false; - } - } - - return true; - } - - @Override - public boolean isMouseOver(double mouseX, double mouseY) { - return super.isMouseOver(mouseX, mouseY) || mouseDown; - } - - protected void setValueFromMouse(double mouseX) { - double value = (mouseX - sliderBounds.x()) / sliderBounds.width() * control.range(); - control.setPendingValue(roundToInterval(value)); - calculateInterpolation(); - } - - protected double roundToInterval(double value) { - return Mth.clamp(min + (interval * Math.round(value / interval)), min, max); // extremely imprecise, requires clamping - } - - @Override - protected int getHoveredControlWidth() { - return sliderBounds.width() + getUnhoveredControlWidth() + 6 + getThumbWidth() / 2; - } - - protected void calculateInterpolation() { - interpolation = Mth.clamp((float) ((control.pendingValue() - control.min()) * 1 / control.range()), 0f, 1f); - } - - @Override - public void setDimension(Dimension<Integer> dim) { - super.setDimension(dim); - int trackWidth = dim.width() / 3; - if (optionNameString.isEmpty()) - trackWidth = dim.width() / 2; - - sliderBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - getThumbWidth() / 2 - trackWidth, dim.centerY() - 5, trackWidth, 10); - } - - protected int getThumbX() { - return (int) (sliderBounds.x() + sliderBounds.width() * interpolation); - } - - protected int getThumbWidth() { - return 4; - } - - @Override - public void postRender(PoseStack matrices, int mouseX, int mouseY, float delta) { - if (super.isMouseOver(mouseX, mouseY) || focused) - super.postRender(matrices, mouseX, mouseY, delta); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java b/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java deleted file mode 100644 index bff0d57..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/slider/package-info.java +++ /dev/null @@ -1,10 +0,0 @@ -/** - * This package contains implementations of sliders for different number types - * <ul> - * <li>For doubles: {@link dev.isxander.yacl.gui.controllers.slider.DoubleSliderController}</li> - * <li>For floats: {@link dev.isxander.yacl.gui.controllers.slider.FloatSliderController}</li> - * <li>For integers: {@link dev.isxander.yacl.gui.controllers.slider.IntegerSliderController}</li> - * <li>For longs: {@link dev.isxander.yacl.gui.controllers.slider.LongSliderController}</li> - * </ul> - */ -package dev.isxander.yacl.gui.controllers.slider; diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/IStringController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/IStringController.java deleted file mode 100644 index 6a603d2..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/IStringController.java +++ /dev/null @@ -1,44 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string; - -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.network.chat.Component; - -/** - * A controller that can be any type but can input and output a string. - */ -public interface IStringController<T> extends Controller<T> { - /** - * Gets the option's pending value as a string. - * - * @see Option#pendingValue() - */ - String getString(); - - /** - * Sets the option's pending value from a string. - * - * @see Option#requestSet(Object) - */ - void setFromString(String value); - - /** - * {@inheritDoc} - */ - @Override - default Component formatValue() { - return Component.literal(getString()); - } - - default boolean isInputValid(String input) { - return true; - } - - @Override - default AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new StringControllerElement(this, screen, widgetDimension, true); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/StringController.java deleted file mode 100644 index 3a07641..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringController.java +++ /dev/null @@ -1,40 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string; - -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; - -/** - * A custom text field implementation for strings. - */ -public class StringController implements IStringController<String> { - private final Option<String> option; - - /** - * Constructs a string controller - * - * @param option bound option - */ - public StringController(Option<String> option) { - this.option = option; - } - - /** - * {@inheritDoc} - */ - @Override - public Option<String> option() { - return option; - } - - @Override - public String getString() { - return option().pendingValue(); - } - - @Override - public void setFromString(String value) { - option().requestSet(value); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java deleted file mode 100644 index cb2ad4e..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/StringControllerElement.java +++ /dev/null @@ -1,408 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string; - -import com.mojang.blaze3d.platform.InputConstants; -import com.mojang.blaze3d.systems.RenderSystem; -import com.mojang.blaze3d.vertex.PoseStack; -import dev.isxander.yacl.api.utils.Dimension; -import dev.isxander.yacl.gui.YACLScreen; -import dev.isxander.yacl.gui.controllers.ControllerWidget; -import dev.isxander.yacl.gui.utils.GuiUtils; -import net.minecraft.ChatFormatting; -import net.minecraft.client.gui.GuiComponent; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.network.chat.Component; - -import java.util.function.Consumer; - -public class StringControllerElement extends ControllerWidget<IStringController<?>> { - protected final boolean instantApply; - - protected String inputField; - protected Dimension<Integer> inputFieldBounds; - protected boolean inputFieldFocused; - - protected int caretPos; - protected int selectionLength; - - protected int renderOffset; - - protected float ticks; - - private final Component emptyText; - - public StringControllerElement(IStringController<?> control, YACLScreen screen, Dimension<Integer> dim, boolean instantApply) { - super(control, screen, dim); - this.instantApply = instantApply; - inputField = control.getString(); - inputFieldFocused = false; - selectionLength = 0; - emptyText = Component.literal("Click to type...").withStyle(ChatFormatting.GRAY); - control.option().addListener((opt, val) -> inputField = control.getString()); - setDimension(dim); - } - - @Override - protected void drawHoveredControl(PoseStack matrices, int mouseX, int mouseY, float delta) { - - } - - @Override - protected void drawValueText(PoseStack matrices, int mouseX, int mouseY, float delta) { - Component valueText = getValueText(); - if (!isHovered()) valueText = Component.literal(GuiUtils.shortenString(valueText.getString(), textRenderer, getMaxUnwrapLength(), "...")).setStyle(valueText.getStyle()); - - matrices.pushPose(); - int textX = getDimension().xLimit() - textRenderer.width(valueText) + renderOffset - getXPadding(); - matrices.translate(textX, getTextY(), 0); - GuiUtils.enableScissor(inputFieldBounds.x(), inputFieldBounds.y() - 2, inputFieldBounds.width() + 1, inputFieldBounds.height() + 4); - textRenderer.drawShadow(matrices, valueText, 0, 0, getValueColor()); - matrices.popPose(); - - if (isHovered()) { - ticks += delta; - - String text = getValueText().getString(); - - GuiComponent.fill(matrices, inputFieldBounds.x(), inputFieldBounds.yLimit(), inputFieldBounds.xLimit(), inputFieldBounds.yLimit() + 1, -1); - GuiComponent.fill(matrices, inputFieldBounds.x() + 1, inputFieldBounds.yLimit() + 1, inputFieldBounds.xLimit() + 1, inputFieldBounds.yLimit() + 2, 0xFF404040); - - if (inputFieldFocused || focused) { - if (caretPos > text.length()) - caretPos = text.length(); - - int caretX = textX + textRenderer.width(text.substring(0, caretPos)) - 1; - if (text.isEmpty()) - caretX = inputFieldBounds.x() + inputFieldBounds.width() / 2; - - if (ticks % 20 <= 10) { - GuiComponent.fill(matrices, caretX, inputFieldBounds.y(), caretX + 1, inputFieldBounds.yLimit(), -1); - } - - if (selectionLength != 0) { - int selectionX = textX + textRenderer.width(text.substring(0, caretPos + selectionLength)); - GuiComponent.fill(matrices, caretX, inputFieldBounds.y() - 1, selectionX, inputFieldBounds.yLimit(), 0x803030FF); - } - } - } - RenderSystem.disableScissor(); - } - - @Override - public boolean mouseClicked(double mouseX, double mouseY, int button) { - if (isAvailable() && getDimension().isPointInside((int) mouseX, (int) mouseY)) { - inputFieldFocused = true; - - if (!inputFieldBounds.isPointInside((int) mouseX, (int) mouseY)) { - caretPos = getDefaultCaretPos(); - } else { - // gets the appropriate caret position for where you click - int textX = (int) mouseX - (inputFieldBounds.xLimit() - textRenderer.width(getValueText())); - int pos = -1; - int currentWidth = 0; - for (char ch : inputField.toCharArray()) { - pos++; - int charLength = textRenderer.width(String.valueOf(ch)); - if (currentWidth + charLength / 2 > textX) { // if more than halfway past the characters select in front of that char - caretPos = pos; - break; - } else if (pos == inputField.length() - 1) { - // if we have reached the end and no matches, it must be the second half of the char so the last position - caretPos = pos + 1; - } - currentWidth += charLength; - } - - selectionLength = 0; - } - return true; - } else { - inputFieldFocused = false; - } - - return false; - } - - protected int getDefaultCaretPos() { - return inputField.length(); - } - - @Override - public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - if (!inputFieldFocused) - return false; - - switch (keyCode) { - case InputConstants.KEY_ESCAPE, InputConstants.KEY_RETURN -> { - unfocus(); - return true; - } - case InputConstants.KEY_LEFT -> { - if (Screen.hasShiftDown()) { - if (Screen.hasControlDown()) { - int spaceChar = findSpaceIndex(true); - selectionLength += caretPos - spaceChar; - caretPos = spaceChar; - } else if (caretPos > 0) { - caretPos--; - selectionLength += 1; - } - checkRenderOffset(); - } else { - if (caretPos > 0) { - if (selectionLength != 0) - caretPos += Math.min(selectionLength, 0); - else - caretPos--; - } - checkRenderOffset(); - selectionLength = 0; - } - - return true; - } - case InputConstants.KEY_RIGHT -> { - if (Screen.hasShiftDown()) { - if (Screen.hasControlDown()) { - int spaceChar = findSpaceIndex(false); - selectionLength -= spaceChar - caretPos; - caretPos = spaceChar; - } else if (caretPos < inputField.length()) { - caretPos++; - selectionLength -= 1; - } - checkRenderOffset(); - } else { - if (caretPos < inputField.length()) { - if (selectionLength != 0) - caretPos += Math.max(selectionLength, 0); - else - caretPos++; - checkRenderOffset(); - } - selectionLength = 0; - } - - return true; - } - case InputConstants.KEY_BACKSPACE -> { - doBackspace(); - return true; - } - case InputConstants.KEY_DELETE -> { - doDelete(); - return true; - } - } - - if (Screen.isPaste(keyCode)) { - return doPaste(); - } else if (Screen.isCopy(keyCode)) { - return doCopy(); - } else if (Screen.isCut(keyCode)) { - return doCut(); - } else if (Screen.isSelectAll(keyCode)) { - return doSelectAll(); - } - - return false; - } - - protected boolean doPaste() { - this.write(client.keyboardHandler.getClipboard()); - return true; - } - - protected boolean doCopy() { - if (selectionLength != 0) { - client.keyboardHandler.setClipboard(getSelection()); - return true; - } - return false; - } - - protected boolean doCut() { - if (selectionLength != 0) { - client.keyboardHandler.setClipboard(getSelection()); - this.write(""); - return true; - } - return false; - } - - protected boolean doSelectAll() { - caretPos = inputField.length(); - checkRenderOffset(); - selectionLength = -caretPos; - return true; - } - - protected void checkRenderOffset() { - if (textRenderer.width(inputField) < getUnshiftedLength()) { - renderOffset = 0; - return; - } - - int textX = getDimension().xLimit() - textRenderer.width(inputField) - getXPadding(); - int caretX = textX + textRenderer.width(inputField.substring(0, caretPos)) - 1; - - int minX = getDimension().xLimit() - getXPadding() - getUnshiftedLength(); - int maxX = minX + getUnshiftedLength(); - - if (caretX + renderOffset < minX) { - renderOffset = minX - caretX; - } else if (caretX + renderOffset > maxX) { - renderOffset = maxX - caretX; - } - } - - @Override - public boolean charTyped(char chr, int modifiers) { - if (!inputFieldFocused) - return false; - - write(Character.toString(chr)); - - return true; - } - - protected void doBackspace() { - if (selectionLength != 0) { - write(""); - } else if (caretPos > 0) { - if (modifyInput(builder -> builder.deleteCharAt(caretPos - 1))) { - caretPos--; - checkRenderOffset(); - } - } - } - - protected void doDelete() { - if (selectionLength != 0) { - write(""); - } else if (caretPos < inputField.length()) { - modifyInput(builder -> builder.deleteCharAt(caretPos)); - } - } - - public void write(String string) { - if (selectionLength == 0) { - if (modifyInput(builder -> builder.insert(caretPos, string))) { - caretPos += string.length(); - checkRenderOffset(); - } - } else { - int start = getSelectionStart(); - int end = getSelectionEnd(); - - if (modifyInput(builder -> builder.replace(start, end, string))) { - caretPos = start + string.length(); - selectionLength = 0; - checkRenderOffset(); - } - } - } - - public boolean modifyInput(Consumer<StringBuilder> consumer) { - StringBuilder temp = new StringBuilder(inputField); - consumer.accept(temp); - if (!control.isInputValid(temp.toString())) - return false; - inputField = temp.toString(); - if (instantApply) - updateControl(); - return true; - } - - public int getUnshiftedLength() { - if (optionNameString.isEmpty()) - return getDimension().width() - getXPadding() * 2; - return getDimension().width() / 8 * 5; - } - - public int getMaxUnwrapLength() { - if (optionNameString.isEmpty()) - return getDimension().width() - getXPadding() * 2; - return getDimension().width() / 2; - } - - public int getSelectionStart() { - return Math.min(caretPos, caretPos + selectionLength); - } - - public int getSelectionEnd() { - return Math.max(caretPos, caretPos + selectionLength); - } - - protected String getSelection() { - return inputField.substring(getSelectionStart(), getSelectionEnd()); - } - - protected int findSpaceIndex(boolean reverse) { - int i; - int fromIndex = caretPos; - if (reverse) { - if (caretPos > 0) - fromIndex -= 1; - i = this.inputField.lastIndexOf(" ", fromIndex); - - if (i == -1) i = 0; - } else { - if (caretPos < inputField.length()) - fromIndex += 1; - i = this.inputField.indexOf(" ", fromIndex); - - if (i == -1) i = inputField.length(); - } - - return i; - } - - @Override - public void setFocused(boolean focused) { - super.setFocused(focused); - inputFieldFocused = focused; - } - - @Override - public void unfocus() { - super.unfocus(); - inputFieldFocused = false; - renderOffset = 0; - if (!instantApply) updateControl(); - } - - @Override - public void setDimension(Dimension<Integer> dim) { - super.setDimension(dim); - - int width = Math.max(6, Math.min(textRenderer.width(getValueText()), getUnshiftedLength())); - inputFieldBounds = Dimension.ofInt(dim.xLimit() - getXPadding() - width, dim.centerY() - textRenderer.lineHeight / 2, width, textRenderer.lineHeight); - } - - @Override - public boolean isHovered() { - return super.isHovered() || inputFieldFocused; - } - - protected void updateControl() { - control.setFromString(inputField); - } - - @Override - protected int getUnhoveredControlWidth() { - return !isHovered() ? Math.min(getHoveredControlWidth(), getMaxUnwrapLength()) : getHoveredControlWidth(); - } - - @Override - protected int getHoveredControlWidth() { - return Math.min(textRenderer.width(getValueText()), getUnshiftedLength()); - } - - @Override - protected Component getValueText() { - if (!inputFieldFocused && inputField.isEmpty()) - return emptyText; - - return instantApply || !inputFieldFocused ? control.formatValue() : Component.literal(inputField); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/DoubleFieldController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/DoubleFieldController.java deleted file mode 100644 index df28241..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/DoubleFieldController.java +++ /dev/null @@ -1,104 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string.number; - -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.gui.controllers.slider.DoubleSliderController; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * {@inheritDoc} - */ -public class DoubleFieldController extends NumberFieldController<Double> { - private final double min, max; - - /** - * Constructs a double field controller - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - * @param formatter display text, not used whilst editing - */ - public DoubleFieldController(Option<Double> option, double min, double max, Function<Double, Component> formatter) { - super(option, formatter); - this.min = min; - this.max = max; - } - - /** - * Constructs a double field controller. - * Uses {@link DoubleSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - */ - public DoubleFieldController(Option<Double> option, double min, double max) { - this(option, min, max, DoubleSliderController.DEFAULT_FORMATTER); - } - - /** - * Constructs a double field controller. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - * @param formatter display text, not used whilst editing - */ - public DoubleFieldController(Option<Double> option, Function<Double, Component> formatter) { - this(option, -Double.MAX_VALUE, Double.MAX_VALUE, formatter); - } - - /** - * Constructs a double field controller. - * Uses {@link DoubleSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - */ - public DoubleFieldController(Option<Double> option) { - this(option, -Double.MAX_VALUE, Double.MAX_VALUE, DoubleSliderController.DEFAULT_FORMATTER); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return this.min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return this.max; - } - - /** - * {@inheritDoc} - */ - @Override - public String getString() { - return String.valueOf(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet(value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/FloatFieldController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/FloatFieldController.java deleted file mode 100644 index 957100a..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/FloatFieldController.java +++ /dev/null @@ -1,104 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string.number; - -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.gui.controllers.slider.FloatSliderController; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * {@inheritDoc} - */ -public class FloatFieldController extends NumberFieldController<Float> { - private final float min, max; - - /** - * Constructs a double field controller - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - * @param formatter display text, not used whilst editing - */ - public FloatFieldController(Option<Float> option, float min, float max, Function<Float, Component> formatter) { - super(option, formatter); - this.min = min; - this.max = max; - } - - /** - * Constructs a double field controller. - * Uses {@link FloatSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - */ - public FloatFieldController(Option<Float> option, float min, float max) { - this(option, min, max, FloatSliderController.DEFAULT_FORMATTER); - } - - /** - * Constructs a double field controller. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - * @param formatter display text, not used whilst editing - */ - public FloatFieldController(Option<Float> option, Function<Float, Component> formatter) { - this(option, -Float.MAX_VALUE, Float.MAX_VALUE, formatter); - } - - /** - * Constructs a double field controller. - * Uses {@link FloatSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - */ - public FloatFieldController(Option<Float> option) { - this(option, -Float.MAX_VALUE, Float.MAX_VALUE, FloatSliderController.DEFAULT_FORMATTER); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return this.min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return this.max; - } - - /** - * {@inheritDoc} - */ - @Override - public String getString() { - return String.valueOf(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((float) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/IntegerFieldController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/IntegerFieldController.java deleted file mode 100644 index 2d64a3a..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/IntegerFieldController.java +++ /dev/null @@ -1,109 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string.number; - -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.gui.controllers.slider.IntegerSliderController; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * {@inheritDoc} - */ -public class IntegerFieldController extends NumberFieldController<Integer> { - private final int min, max; - - /** - * Constructs a double field controller - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - * @param formatter display text, not used whilst editing - */ - public IntegerFieldController(Option<Integer> option, int min, int max, Function<Integer, Component> formatter) { - super(option, formatter); - this.min = min; - this.max = max; - } - - /** - * Constructs a double field controller. - * Uses {@link IntegerSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - */ - public IntegerFieldController(Option<Integer> option, int min, int max) { - this(option, min, max, IntegerSliderController.DEFAULT_FORMATTER); - } - - /** - * Constructs a double field controller. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - * @param formatter display text, not used whilst editing - */ - public IntegerFieldController(Option<Integer> option, Function<Integer, Component> formatter) { - this(option, -Integer.MAX_VALUE, Integer.MAX_VALUE, formatter); - } - - /** - * Constructs a double field controller. - * Uses {@link IntegerSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - */ - public IntegerFieldController(Option<Integer> option) { - this(option, -Integer.MAX_VALUE, Integer.MAX_VALUE, IntegerSliderController.DEFAULT_FORMATTER); - } - - @Override - public boolean isInputValid(String input) { - return input.matches("\\d+|-|"); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return this.min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return this.max; - } - - /** - * {@inheritDoc} - */ - @Override - public String getString() { - return String.valueOf(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((int) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/LongFieldController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/LongFieldController.java deleted file mode 100644 index a640621..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/LongFieldController.java +++ /dev/null @@ -1,109 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string.number; - -import dev.isxander.yacl.api.Option; -import dev.isxander.yacl.gui.controllers.slider.LongSliderController; -import net.minecraft.network.chat.Component; - -import java.util.function.Function; - -/** - * {@inheritDoc} - */ -public class LongFieldController extends NumberFieldController<Long> { - private final long min, max; - - /** - * Constructs a double field controller - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - * @param formatter display text, not used whilst editing - */ - public LongFieldController(Option<Long> option, long min, long max, Function<Long, Component> formatter) { - super(option, formatter); - this.min = min; - this.max = max; - } - - /** - * Constructs a double field controller. - * Uses {@link LongSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * - * @param option option to bind controller to - * @param min minimum allowed value (clamped on apply) - * @param max maximum allowed value (clamped on apply) - */ - public LongFieldController(Option<Long> option, long min, long max) { - this(option, min, max, LongSliderController.DEFAULT_FORMATTER); - } - - /** - * Constructs a double field controller. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - * @param formatter display text, not used whilst editing - */ - public LongFieldController(Option<Long> option, Function<Long, Component> formatter) { - this(option, -Long.MAX_VALUE, Long.MAX_VALUE, formatter); - } - - /** - * Constructs a double field controller. - * Uses {@link LongSliderController#DEFAULT_FORMATTER} as display text, - * not used whilst editing. - * Does not have a minimum or a maximum range. - * - * @param option option to bind controller to - */ - public LongFieldController(Option<Long> option) { - this(option, -Long.MAX_VALUE, Long.MAX_VALUE, LongSliderController.DEFAULT_FORMATTER); - } - - @Override - public boolean isInputValid(String input) { - return input.matches("\\d+|-|"); - } - - /** - * {@inheritDoc} - */ - @Override - public double min() { - return this.min; - } - - /** - * {@inheritDoc} - */ - @Override - public double max() { - return this.max; - } - - /** - * {@inheritDoc} - */ - @Override - public String getString() { - return String.valueOf(option().pendingValue()); - } - - /** - * {@inheritDoc} - */ - @Override - public void setPendingValue(double value) { - option().requestSet((long) value); - } - - /** - * {@inheritDoc} - */ - @Override - public double pendingValue() { - return option().pendingValue(); - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/NumberFieldController.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/NumberFieldController.java deleted file mode 100644 index 4240849..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/NumberFieldController.java +++ /dev/null @@ -1,69 +0,0 @@ -package dev.isxander.yacl.gui.controllers.string.number; - -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 dev.isxander.yacl.gui.controllers.slider.ISliderController; -import dev.isxander.yacl.gui.controllers.string.IStringController; -import dev.isxander.yacl.gui.controllers.string.StringControllerElement; -import net.minecraft.network.chat.Component; -import net.minecraft.util.Mth; - -import java.text.DecimalFormatSymbols; -import java.util.function.Function; - -/** - * Controller that allows you to enter in numbers using a text field. - * - * @param <T> number type - */ -public abstract class NumberFieldController<T extends Number> implements ISliderController<T>, IStringController<T> { - private final Option<T> option; - private final Function<T, Component> displayFormatter; - - public NumberFieldController(Option<T> option, Function<T, Component> displayFormatter) { - this.option = option; - this.displayFormatter = displayFormatter; - } - - @Override - public Option<T> option() { - return this.option; - } - - @Override - public void setFromString(String value) { - if (value.isEmpty() || value.equals(".") || value.equals("-")) value = "0"; - setPendingValue(Mth.clamp(Double.parseDouble(cleanupNumberString(value)), min(), max())); - } - - @Override - public double pendingValue() { - return option().pendingValue().doubleValue(); - } - - @Override - public boolean isInputValid(String input) { - return input.matches("[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)|[.]||-"); - } - - @Override - public Component formatValue() { - return displayFormatter.apply(option().pendingValue()); - } - - @Override - public AbstractWidget provideWidget(YACLScreen screen, Dimension<Integer> widgetDimension) { - return new StringControllerElement(this, screen, widgetDimension, false); - } - - protected String cleanupNumberString(String number) { - return number.replace(String.valueOf(DecimalFormatSymbols.getInstance().getGroupingSeparator()), ""); - } - - @Override - public double interval() { - return -1; - } -} diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/package-info.java b/src/client/java/dev/isxander/yacl/gui/controllers/string/number/package-info.java deleted file mode 100644 index 86b9314..0000000 --- a/src/client/java/dev/isxander/yacl/gui/controllers/string/number/package-info.java +++ /dev/null @@ -1,10 +0,0 @@ -/** - * This package contains implementations of input fields for different number types - * <ul> - * <li>For doubles: {@link dev.isxander.yacl.gui.controllers.string.number.DoubleFieldController}</li> - * <li>For floats: {@link dev.isxander.yacl.gui.controllers.string.number.FloatFieldController}</li> - * <li>For integers: {@link dev.isxander.yacl.gui.controllers.string.number.IntegerFieldController}</li> - * <li>For longs: {@link dev.isxander.yacl.gui.controllers.string.number.LongFieldController}</li> - * </ul> - */ -package dev.isxander.yacl.gui.controllers.string.number; |