diff options
| author | shedaniel <daniel@shedaniel.me> | 2020-03-18 00:35:36 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2020-03-18 00:35:36 +0800 |
| commit | 550fe32e612801daa99493aa0bade083f3330133 (patch) | |
| tree | 7825027dd8933d542a831e2bda6626c63f4cd8db /src | |
| parent | 9e990de7685960391d78ca2cca0ff68bebe1a8cd (diff) | |
| download | RoughlyEnoughItems-550fe32e612801daa99493aa0bade083f3330133.tar.gz RoughlyEnoughItems-550fe32e612801daa99493aa0bade083f3330133.tar.bz2 RoughlyEnoughItems-550fe32e612801daa99493aa0bade083f3330133.zip | |
4.0.14: Better widgets system
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'src')
39 files changed, 1269 insertions, 614 deletions
diff --git a/src/main/java/me/shedaniel/rei/api/widgets/Arrow.java b/src/main/java/me/shedaniel/rei/api/widgets/Arrow.java index 90d840ce2..98a0da428 100644 --- a/src/main/java/me/shedaniel/rei/api/widgets/Arrow.java +++ b/src/main/java/me/shedaniel/rei/api/widgets/Arrow.java @@ -24,12 +24,19 @@ package me.shedaniel.rei.api.widgets; import me.shedaniel.rei.gui.widget.WidgetWithBounds; +import org.jetbrains.annotations.NotNull; public abstract class Arrow extends WidgetWithBounds { + /** + * @return the x coordinate for the top left corner of this widget. + */ public final int getX() { return getBounds().getX(); } + /** + * @return the y coordinate for the top left corner of this widget. + */ public final int getY() { return getBounds().getY(); } @@ -42,16 +49,17 @@ public abstract class Arrow extends WidgetWithBounds { /** * Sets the animation duration in milliseconds. * - * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0 + * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0. */ public abstract void setAnimationDuration(double animationDurationMS); /** * Sets the animation duration in milliseconds. * - * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0 - * @return the arrow itself + * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0. + * @return the arrow itself. */ + @NotNull public final Arrow animationDurationMS(double animationDurationMS) { setAnimationDuration(animationDurationMS); return this; @@ -60,9 +68,10 @@ public abstract class Arrow extends WidgetWithBounds { /** * Sets the animation duration in ticks. * - * @param animationDurationTicks animation duration in ticks, animation is disabled when below or equals to 0 - * @return the arrow itself + * @param animationDurationTicks animation duration in ticks, animation is disabled when below or equals to 0. + * @return the arrow itself. */ + @NotNull public final Arrow animationDurationTicks(double animationDurationTicks) { return animationDurationMS(animationDurationTicks * 50); } @@ -70,8 +79,9 @@ public abstract class Arrow extends WidgetWithBounds { /** * Disables the animation. * - * @return the arrow itself + * @return the arrow itself. */ + @NotNull public final Arrow disableAnimation() { return animationDurationMS(-1); } diff --git a/src/main/java/me/shedaniel/rei/api/widgets/BaseWidget.java b/src/main/java/me/shedaniel/rei/api/widgets/BaseWidget.java new file mode 100644 index 000000000..fe6212473 --- /dev/null +++ b/src/main/java/me/shedaniel/rei/api/widgets/BaseWidget.java @@ -0,0 +1,30 @@ +package me.shedaniel.rei.api.widgets; + +import me.shedaniel.math.Point; +import me.shedaniel.rei.gui.widget.WidgetWithBounds; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.function.BiPredicate; + +public abstract class BaseWidget<T extends BaseWidget<T>> extends WidgetWithBounds { + @Nullable + private BiPredicate<T, Point> containsMousePredicate; + + public final void setContainsMousePredicate(@Nullable BiPredicate<T, Point> predicate) { + this.containsMousePredicate = predicate; + } + + @NotNull + public final T containsMousePredicate(@Nullable BiPredicate<T, Point> predicate) { + setContainsMousePredicate(predicate); + return (T) this; + } + + @Override + public boolean containsMouse(double mouseX, double mouseY) { + if (containsMousePredicate != null) + return containsMousePredicate.test((T) this, new Point(mouseX, mouseY)); + return super.containsMouse(mouseX, mouseY); + } +} diff --git a/src/main/java/me/shedaniel/rei/api/widgets/BurningFire.java b/src/main/java/me/shedaniel/rei/api/widgets/BurningFire.java index 8ea7759e9..fab8f95b9 100644 --- a/src/main/java/me/shedaniel/rei/api/widgets/BurningFire.java +++ b/src/main/java/me/shedaniel/rei/api/widgets/BurningFire.java @@ -24,12 +24,19 @@ package me.shedaniel.rei.api.widgets; import me.shedaniel.rei.gui.widget.WidgetWithBounds; +import org.jetbrains.annotations.NotNull; public abstract class BurningFire extends WidgetWithBounds { + /** + * @return the x coordinate for the top left corner of this widget. + */ public final int getX() { return getBounds().getX(); } + /** + * @return the y coordinate for the top left corner of this widget. + */ public final int getY() { return getBounds().getY(); } @@ -42,16 +49,17 @@ public abstract class BurningFire extends WidgetWithBounds { /** * Sets the animation duration in milliseconds. * - * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0 + * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0. */ public abstract void setAnimationDuration(double animationDurationMS); /** * Sets the animation duration in milliseconds. * - * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0 - * @return the arrow itself + * @param animationDurationMS animation duration in milliseconds, animation is disabled when below or equals to 0. + * @return the arrow itself. */ + @NotNull public final BurningFire animationDurationMS(double animationDurationMS) { setAnimationDuration(animationDurationMS); return this; @@ -60,9 +68,10 @@ public abstract class BurningFire extends WidgetWithBounds { /** * Sets the animation duration in ticks. * - * @param animationDurationTicks animation duration in ticks, animation is disabled when below or equals to 0 - * @return the arrow itself + * @param animationDurationTicks animation duration in ticks, animation is disabled when below or equals to 0. + * @return the arrow itself. */ + @NotNull public final BurningFire animationDurationTicks(double animationDurationTicks) { return animationDurationMS(animationDurationTicks * 50); } @@ -70,8 +79,9 @@ public abstract class BurningFire extends WidgetWithBounds { /** * Disables the animation. * - * @return the arrow itself + * @return the arrow itself. */ + @NotNull public final BurningFire disableAnimation() { return animationDurationMS(-1); } diff --git a/src/main/java/me/shedaniel/rei/api/widgets/Button.java b/src/main/java/me/shedaniel/rei/api/widgets/Button.java new file mode 100644 index 000000000..2bf51e5bc --- /dev/null +++ b/src/main/java/me/shedaniel/rei/api/widgets/Button.java @@ -0,0 +1,160 @@ +package me.shedaniel.rei.api.widgets; + +import me.shedaniel.math.api.Point; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.OptionalInt; +import java.util.function.BiFunction; +import java.util.function.Consumer; +import java.util.function.Function; + +public abstract class Button extends BaseWidget<Button> { + public abstract void setTextColor(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction); + + public final Button textColor(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textColorFunction) { + setTextColor(textColorFunction); + return this; + } + + public abstract int getTextColor(Point mouse); + + public abstract void setTextureId(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction); + + public final Button textureId(@Nullable BiFunction<@NotNull Button, @NotNull Point, @NotNull Integer> textureIdFunction) { + setTextureId(textureIdFunction); + return this; + } + + public abstract int getTextureId(Point mouse); + + public abstract void onClick(); + + public abstract boolean isEnabled(); + + public abstract void setEnabled(boolean enabled); + + public final Button enabled(boolean enabled) { + setEnabled(enabled); + return this; + } + + public abstract OptionalInt getTint(); + + public abstract void setTint(int tint); + + public abstract void removeTint(); + + public final Button tint(@Nullable Integer tint) { + if (tint == null) + removeTint(); + else setTint(tint); + return this; + } + + @NotNull + public abstract String getText(); + + public abstract void setText(@NotNull String text); + + @NotNull + public final Button text(@NotNull String text) { + setText(text); + return this; + } + + @Nullable + public abstract Consumer<Button> getOnClick(); + + public abstract void setOnClick(@Nullable Consumer<Button> onClick); + + @NotNull + public final Button onClick(@Nullable Consumer<Button> onClick) { + setOnClick(onClick); + return this; + } + + @Nullable + public abstract Consumer<Button> getOnRender(); + + public abstract void setOnRender(@Nullable Consumer<Button> onRender); + + @NotNull + public final Button onRender(@Nullable Consumer<Button> onRender) { + setOnRender(onRender); + return this; + } + + /** + * @return whether the button is focusable by pressing tab, ignored if not clickable. + */ + public abstract boolean isFocusable(); + + /** + * Sets whether the button is focusable by pressing tab, ignored if not clickable. + * + * @param focusable whether the button is focusable by pressing tab, ignored if not clickable. + */ + public abstract void setFocusable(boolean focusable); + + /** + * Sets whether the button is focusable by pressing tab, ignored if not clickable. + * + * @param focusable whether the label is focusable by pressing tab, ignored if not clickable. + * @return the button itself. + */ + @NotNull + public final Button focusable(boolean focusable) { + setFocusable(focusable); + return this; + } + + /** + * @return the tooltip from the current tooltip function, null if no tooltip. + */ + @Nullable + public abstract String getTooltip(); + + /** + * Sets the tooltip function used to get the tooltip. + * + * @param tooltip the tooltip function used to get the tooltip. + */ + public abstract void setTooltip(@Nullable Function<@NotNull Button, @Nullable String> tooltip); + + /** + * Sets the tooltip. + * + * @param tooltip the lines of tooltip. + * @return the button itself. + */ + @NotNull + public final Button tooltipLines(@NotNull String... tooltip) { + return tooltipLine(String.join("\n", tooltip)); + } + + /** + * Sets the tooltip. + * + * @param tooltip the line of tooltip. + * @return the button itself. + */ + @NotNull + public final Button tooltipLine(@Nullable String tooltip) { + return tooltipSupplier(label -> tooltip); + } + + /** + * Sets the tooltip function. + * + * @param tooltip the tooltip function used to get the tooltip. + * @return the button itself. + */ + @NotNull + public final Button tooltipSupplier(@Nullable Function<@NotNull Button, @Nullable String> tooltip) { + setTooltip(tooltip); + return this; + } + + public abstract boolean isFocused(); +} diff --git a/src/main/java/me/shedaniel/rei/api/widgets/Label.java b/src/main/java/me/shedaniel/rei/api/widgets/Label.java index 8d4d0247f..e51f504e9 100644 --- a/src/main/java/me/shedaniel/rei/api/widgets/Label.java +++ b/src/main/java/me/shedaniel/rei/api/widgets/Label.java @@ -37,76 +37,179 @@ public abstract class Label extends WidgetWithBounds { public static final int CENTER = 0; public static final int RIGHT_ALIGNED = 1; + /** + * @return whether the label is clickable, ignores if onClick is set. + */ public abstract boolean isClickable(); + /** + * Sets whether the label is clickable, ignores if onClick is set. + * + * @param clickable whether the label is clickable. + */ public abstract void setClickable(boolean clickable); + /** + * Sets the label as clickable, ignores if onClick is set. + * + * @return the label itself. + */ + @NotNull public final Label clickable() { return clickable(true); } + /** + * Sets whether the label is clickable, ignores if onClick is set. + * + * @param clickable whether the label is clickable. + * @return the label itself. + */ + @NotNull public final Label clickable(boolean clickable) { setClickable(clickable); return this; } + /** + * @return the consumer on click, only applicable if the label is clickable, null if not set. + */ @Nullable public abstract Consumer<Label> getOnClick(); + /** + * Sets the on click consumer, only applicable if the label is clickable. + * + * @param onClick the on click consumer, only applicable if the label is clickable. + */ public abstract void setOnClick(@Nullable Consumer<Label> onClick); + /** + * Sets the on click consumer, only applicable if the label is clickable. + * + * @param onClick the on click consumer, only applicable if the label is clickable. + * @return the label itself. + */ + @NotNull public final Label onClick(@Nullable Consumer<Label> onClick) { setOnClick(onClick); return this; } + /** + * @return the consumer before render, null if not set. + */ @Nullable public abstract Consumer<Label> getOnRender(); + /** + * Sets the consumer before render. + * + * @param onRender the consumer before render. + */ public abstract void setOnRender(@Nullable Consumer<Label> onRender); + /** + * Sets the consumer before render. + * + * @param onRender the consumer before render. + * @return the label itself. + */ + @NotNull public final Label onRender(@Nullable Consumer<Label> onRender) { setOnRender(onRender); return this; } + /** + * @return whether the label is focusable by pressing tab, ignored if not clickable. + */ public abstract boolean isFocusable(); + /** + * Sets whether the label is focusable by pressing tab, ignored if not clickable. + * + * @param focusable whether the label is focusable by pressing tab, ignored if not clickable. + */ public abstract void setFocusable(boolean focusable); + /** + * Sets whether the label is focusable by pressing tab, ignored if not clickable. + * + * @param focusable whether the label is focusable by pressing tab, ignored if not clickable. + * @return the label itself. + */ + @NotNull public final Label focusable(boolean focusable) { setFocusable(focusable); return this; } + /** + * @return the tooltip from the current tooltip function, null if no tooltip. + */ @Nullable public abstract String getTooltip(); + /** + * Sets the tooltip function used to get the tooltip. + * + * @param tooltip the tooltip function used to get the tooltip. + */ public abstract void setTooltip(@Nullable Function<Label, @Nullable String> tooltip); + /** + * Sets the tooltip. + * + * @param tooltip the lines of tooltip. + * @return the label itself. + */ + @NotNull public final Label tooltipLines(@NotNull String... tooltip) { return tooltipLine(String.join("\n", tooltip)); } + /** + * Sets the tooltip. + * + * @param tooltip the line of tooltip. + * @return the label itself. + */ + @NotNull public final Label tooltipLine(@Nullable String tooltip) { return tooltipSupplier(label -> tooltip); } + /** + * Sets the tooltip function. + * + * @param tooltip the tooltip function used to get the tooltip. + * @return the label itself. + */ + @NotNull public final Label tooltipSupplier(@Nullable Function<Label, @Nullable String> tooltip) { setTooltip(tooltip); return this; } + /** + * Gets the horizontal alignment of the label, defaulted as centered. + * + * @return {@link Label#LEFT_ALIGNED} if left aligned, {@link Label#CENTER} if centered or {@link Label#RIGHT_ALIGNED} if right aligned}. + */ public abstract int getHorizontalAlignment(); + @NotNull public final Label centered() { return horizontalAlignment(CENTER); } + @NotNull public final Label leftAligned() { return horizontalAlignment(LEFT_ALIGNED); } + @NotNull public final Label rightAligned() { return horizontalAlignment(RIGHT_ALIGNED); } @@ -120,16 +223,19 @@ public abstract class Label extends WidgetWithBounds { public abstract boolean hasShadow(); + @NotNull public final Label noShadow() { return shadow(false); } + @NotNull public final Label shadow() { return shadow(true); } public abstract void setShadow(boolean hasShadow); + @NotNull public final Label shadow(boolean hasShadow) { setShadow(hasShadow); return this; @@ -139,10 +245,12 @@ public abstract class Label extends WidgetWithBounds { public abstract void setColor(int color); + @NotNull public final Label color(int lightModeColor, int darkModeColor) { return color(REIHelper.getInstance().isDarkThemeEnabled() ? darkModeColor : lightModeColor); } + @NotNull public final Label color(int color) { setColor(color); return this; @@ -152,10 +260,12 @@ public abstract class Label extends WidgetWithBounds { public abstract void setHoveredColor(int hoveredColor); + @NotNull public final Label hoveredColor(int lightModeColor, int darkModeColor) { return hoveredColor(REIHelper.getInstance().isDarkThemeEnabled() ? darkModeColor : lightModeColor); } + @NotNull public final Label hoveredColor(int color) { setHoveredColor(color); return this; @@ -174,6 +284,7 @@ public abstract class Label extends WidgetWithBounds { public abstract void setPoint(@NotNull Point point); + @NotNull public final Label point(@NotNull Point point) { setPoint(point); return this; @@ -184,6 +295,7 @@ public abstract class Label extends WidgetWithBounds { public abstract void setText(@NotNull String text); + @NotNull public final Label text(@NotNull String text) { setText(text); return this; diff --git a/src/main/java/me/shedaniel/rei/api/widgets/Panel.java b/src/main/java/me/shedaniel/rei/api/widgets/Panel.java index 441068b62..68d336ad0 100644 --- a/src/main/java/me/shedaniel/rei/api/widgets/Panel.java +++ b/src/main/java/me/shedaniel/rei/api/widgets/Panel.java @@ -34,11 +34,13 @@ public abstract class Panel extends WidgetWithBounds { public abstract void setInnerColor(int innerColor); + @NotNull public final Panel innerColor(int innerColor) { setInnerColor(innerColor); return this; } + @NotNull public final Panel innerColor(int lightColor, int darkColor) { return innerColor(REIHelper.getInstance().isDarkThemeEnabled() ? darkColor : lightColor); } @@ -47,6 +49,7 @@ public abstract class Panel extends WidgetWithBounds { public abstract void setXTextureOffset(int xTextureOffset); + @NotNull public final Panel xTextureOffset(int xTextureOffset) { setXTextureOffset(xTextureOffset); return this; @@ -56,6 +59,7 @@ public abstract class Panel extends WidgetWithBounds { public abstract void setYTextureOffset(int yTextureOffset); + @NotNull public final Panel yTextureOffset(int yTextureOffset) { setYTextureOffset(yTextureOffset); return this; @@ -65,11 +69,13 @@ public abstract class Panel extends WidgetWithBounds { public abstract void setColor(int color); + @NotNull public final Panel color(int color) { setColor(color); return this; } + @NotNull public final Panel color(int lightColor, int darkColor) { return color(REIHelper.getInstance().isDarkThemeEnabled() ? darkColor : lightColor); } @@ -79,6 +85,7 @@ public abstract class Panel extends WidgetWithBounds { public abstract void setRendering(@NotNull Predicate<Panel> rendering); + @NotNull public final Panel rendering(@NotNull Predicate<Panel> rendering) { setRendering(rendering); return this; diff --git a/src/main/java/me/shedaniel/rei/api/widgets/Slot.java b/src/main/java/me/shedaniel/rei/api/widgets/Slot.java index 285dddd22..adbd0c49a 100644 --- a/src/main/java/me/shedaniel/re |
