From dd24e4f8e84ed02913196c8ad6093275acc92219 Mon Sep 17 00:00:00 2001 From: xander Date: Fri, 2 Sep 2022 11:16:48 +0100 Subject: button options now consume YACLScreen --- .../java/dev/isxander/yacl/api/ButtonOption.java | 14 +++++----- .../java/dev/isxander/yacl/api/Controller.java | 4 +-- .../yacl/gui/controllers/ActionController.java | 12 +++++---- .../yacl/gui/controllers/BooleanController.java | 5 ++-- .../yacl/gui/controllers/ControllerWidget.java | 6 ++--- .../yacl/gui/controllers/EnumController.java | 5 ++-- .../yacl/gui/controllers/TickBoxController.java | 7 +++-- .../gui/controllers/slider/ISliderController.java | 4 +-- .../slider/SliderControllerElement.java | 3 ++- .../dev/isxander/yacl/impl/ButtonOptionImpl.java | 30 ++++++++++++---------- 10 files changed, 49 insertions(+), 41 deletions(-) (limited to 'src/main') diff --git a/src/main/java/dev/isxander/yacl/api/ButtonOption.java b/src/main/java/dev/isxander/yacl/api/ButtonOption.java index 346bfa4..f3d1e6b 100644 --- a/src/main/java/dev/isxander/yacl/api/ButtonOption.java +++ b/src/main/java/dev/isxander/yacl/api/ButtonOption.java @@ -1,5 +1,6 @@ package dev.isxander.yacl.api; +import dev.isxander.yacl.gui.YACLScreen; import dev.isxander.yacl.impl.ButtonOptionImpl; import net.minecraft.text.MutableText; import net.minecraft.text.Text; @@ -8,10 +9,11 @@ import org.jetbrains.annotations.NotNull; import java.util.ArrayList; import java.util.List; +import java.util.function.Consumer; import java.util.function.Function; -public interface ButtonOption extends Option { - Runnable action(); +public interface ButtonOption extends Option> { + Consumer action(); static Builder createBuilder() { return new Builder(); @@ -20,8 +22,8 @@ public interface ButtonOption extends Option { class Builder { private Text name; private final List tooltipLines = new ArrayList<>(); - private Function> controlGetter; - private Runnable action; + private Function>> controlGetter; + private Consumer action; private Builder() { @@ -41,14 +43,14 @@ public interface ButtonOption extends Option { return this; } - public Builder action(@NotNull Runnable action) { + public Builder action(@NotNull Consumer action) { Validate.notNull(action, "`action` cannot be null"); this.action = action; return this; } - public Builder controller(@NotNull Function> control) { + public Builder controller(@NotNull Function>> control) { Validate.notNull(control, "`control` cannot be null"); this.controlGetter = control; diff --git a/src/main/java/dev/isxander/yacl/api/Controller.java b/src/main/java/dev/isxander/yacl/api/Controller.java index 2c0a5e1..198e5df 100644 --- a/src/main/java/dev/isxander/yacl/api/Controller.java +++ b/src/main/java/dev/isxander/yacl/api/Controller.java @@ -1,8 +1,8 @@ package dev.isxander.yacl.api; 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.screen.Screen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; @@ -26,5 +26,5 @@ public interface Controller { * @param screen parent screen */ @ApiStatus.Internal - ControllerWidget provideWidget(Screen screen, Dimension widgetDimension); + ControllerWidget provideWidget(YACLScreen screen, Dimension widgetDimension); } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java index b632c5f..92ef3d5 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ActionController.java @@ -3,15 +3,17 @@ package dev.isxander.yacl.gui.controllers; import dev.isxander.yacl.api.ButtonOption; import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.utils.Dimension; -import net.minecraft.client.gui.screen.Screen; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; +import java.util.function.Consumer; + /** * Simple controller that simply runs the button action on press * and renders a {@link} Text on the right. */ -public class ActionController implements Controller { +public class ActionController implements Controller> { public static final Text DEFAULT_TEXT = Text.translatable("yacl.control.action.execute"); private final ButtonOption option; @@ -59,13 +61,13 @@ public class ActionController implements Controller { * {@inheritDoc} */ @Override - public ControllerWidget provideWidget(Screen screen, Dimension widgetDimension) { + public ControllerWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { return new ActionControllerElement(this, screen, widgetDimension); } @ApiStatus.Internal public static class ActionControllerElement extends ControllerWidget { - public ActionControllerElement(ActionController control, Screen screen, Dimension dim) { + public ActionControllerElement(ActionController control, YACLScreen screen, Dimension dim) { super(control, screen, dim); } @@ -73,7 +75,7 @@ public class ActionController implements Controller { public boolean mouseClicked(double mouseX, double mouseY, int button) { if (isMouseOver(mouseX, mouseY)) { playDownSound(); - control.option().action().run(); + control.option().action().accept(screen); return true; } return false; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java index 9effd26..6bf0b80 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/BooleanController.java @@ -3,6 +3,7 @@ package dev.isxander.yacl.gui.controllers; import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; @@ -96,13 +97,13 @@ public class BooleanController implements Controller { * {@inheritDoc} */ @Override - public ControllerWidget provideWidget(Screen screen, Dimension widgetDimension) { + public ControllerWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { return new BooleanControllerElement(this, screen, widgetDimension); } @ApiStatus.Internal public static class BooleanControllerElement extends ControllerWidget { - private BooleanControllerElement(BooleanController control, Screen screen, Dimension dim) { + private BooleanControllerElement(BooleanController control, YACLScreen screen, Dimension dim) { super(control, screen, dim); } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java index fe78b2e..77ac9ca 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/ControllerWidget.java @@ -3,8 +3,8 @@ package dev.isxander.yacl.gui.controllers; 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.client.gui.DrawableHelper; -import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.OrderedText; @@ -17,14 +17,14 @@ public abstract class ControllerWidget> extends Abstract protected final List wrappedTooltip; protected Dimension dim; - protected final Screen screen; + protected final YACLScreen screen; protected boolean hovered = false; protected float hoveredTicks = 0; private int prevMouseX, prevMouseY; - public ControllerWidget(T control, Screen screen, Dimension dim) { + public ControllerWidget(T control, YACLScreen screen, Dimension dim) { this.control = control; this.dim = dim; this.screen = screen; diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java index d88401c..f4c4006 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/EnumController.java @@ -4,6 +4,7 @@ import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.NameableEnum; import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; @@ -71,7 +72,7 @@ public class EnumController> implements Controller { * {@inheritDoc} */ @Override - public ControllerWidget> provideWidget(Screen screen, Dimension widgetDimension) { + public ControllerWidget> provideWidget(YACLScreen screen, Dimension widgetDimension) { return new EnumControllerElement<>(this, screen, widgetDimension, enumClass.getEnumConstants()); } @@ -79,7 +80,7 @@ public class EnumController> implements Controller { public static class EnumControllerElement> extends ControllerWidget> { private final T[] values; - public EnumControllerElement(EnumController control, Screen screen, Dimension dim, T[] values) { + public EnumControllerElement(EnumController control, YACLScreen screen, Dimension dim, T[] values) { super(control, screen, dim); this.values = values; } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java index 00df45b..1d78c78 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/TickBoxController.java @@ -3,14 +3,13 @@ package dev.isxander.yacl.gui.controllers; import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.Option; import dev.isxander.yacl.api.utils.Dimension; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.client.gui.DrawableHelper; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; -import java.util.function.Function; - /** * This controller renders a tickbox */ @@ -46,13 +45,13 @@ public class TickBoxController implements Controller { * {@inheritDoc} */ @Override - public ControllerWidget provideWidget(Screen screen, Dimension widgetDimension) { + public ControllerWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { return new TickBoxControllerElement(this, screen, widgetDimension); } @ApiStatus.Internal public static class TickBoxControllerElement extends ControllerWidget { - private TickBoxControllerElement(TickBoxController control, Screen screen, Dimension dim) { + private TickBoxControllerElement(TickBoxController control, YACLScreen screen, Dimension dim) { super(control, screen, dim); } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java index 0f6c2d5..ede3456 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/ISliderController.java @@ -2,8 +2,8 @@ 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.YACLScreen; import dev.isxander.yacl.gui.controllers.ControllerWidget; -import net.minecraft.client.gui.screen.Screen; /** * Simple custom slider implementation that shifts the current value across when shown. @@ -48,7 +48,7 @@ public interface ISliderController extends Controller { * {@inheritDoc} */ @Override - default ControllerWidget provideWidget(Screen screen, Dimension widgetDimension) { + default ControllerWidget provideWidget(YACLScreen screen, Dimension widgetDimension) { return new SliderControllerElement(this, screen, widgetDimension, min(), max(), interval()); } } diff --git a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java index 997ba7d..8af2433 100644 --- a/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java +++ b/src/main/java/dev/isxander/yacl/gui/controllers/slider/SliderControllerElement.java @@ -1,6 +1,7 @@ package dev.isxander.yacl.gui.controllers.slider; 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.DrawableHelper; import net.minecraft.client.gui.screen.Screen; @@ -18,7 +19,7 @@ public class SliderControllerElement extends ControllerWidget option, Screen screen, Dimension dim, double min, double max, double interval) { + public SliderControllerElement(ISliderController option, YACLScreen screen, Dimension dim, double min, double max, double interval) { super(option, screen, dim); this.min = min; this.max = max; diff --git a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java index 4104ff0..748dd0c 100644 --- a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java @@ -3,26 +3,28 @@ package dev.isxander.yacl.impl; import dev.isxander.yacl.api.Binding; import dev.isxander.yacl.api.ButtonOption; import dev.isxander.yacl.api.Controller; +import dev.isxander.yacl.gui.YACLScreen; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.function.Consumer; import java.util.function.Function; @ApiStatus.Internal public class ButtonOptionImpl implements ButtonOption { private final Text name; private final Text tooltip; - private final Runnable action; - private final Controller controller; - private final Binding binding; + private final Consumer action; + private final Controller> controller; + private final Binding> binding; public ButtonOptionImpl( @NotNull Text name, @Nullable Text tooltip, - @NotNull Runnable action, - @NotNull Function> controlGetter + @NotNull Consumer action, + @NotNull Function>> controlGetter ) { this.name = name; this.tooltip = tooltip; @@ -42,17 +44,17 @@ public class ButtonOptionImpl implements ButtonOption { } @Override - public Runnable action() { + public Consumer action() { return action; } @Override - public @NotNull Controller controller() { + public @NotNull Controller> controller() { return controller; } @Override - public @NotNull Binding binding() { + public @NotNull Binding> binding() { return binding; } @@ -62,12 +64,12 @@ public class ButtonOptionImpl implements ButtonOption { } @Override - public @NotNull Runnable pendingValue() { + public @NotNull Consumer pendingValue() { throw new UnsupportedOperationException(); } @Override - public void requestSet(Runnable value) { + public void requestSet(Consumer value) { throw new UnsupportedOperationException(); } @@ -86,19 +88,19 @@ public class ButtonOptionImpl implements ButtonOption { } - private static class EmptyBinderImpl implements Binding { + private static class EmptyBinderImpl implements Binding> { @Override - public void setValue(Runnable value) { + public void setValue(Consumer value) { } @Override - public Runnable getValue() { + public Consumer getValue() { throw new UnsupportedOperationException(); } @Override - public Runnable defaultValue() { + public Consumer defaultValue() { throw new UnsupportedOperationException(); } } -- cgit