From 92264d510b0e4f66719c970b2dbf21b495cfded1 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 6 Nov 2021 13:34:57 +0800 Subject: Fix #647 --- .../rei/api/client/config/ConfigObject.java | 5 + .../client/config/entry/EntryStackProvider.java | 120 +++++++++++++++++++++ .../rei/api/client/gui/SimpleDisplayRenderer.java | 1 - .../rei/api/common/util/CollectionUtils.java | 4 + 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/config/entry/EntryStackProvider.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java index 20165ffc1..040704236 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.api.client.config; import me.shedaniel.clothconfig2.api.ModifierKeyCode; +import me.shedaniel.rei.api.client.config.entry.EntryStackProvider; import me.shedaniel.rei.api.client.favorites.FavoriteEntry; import me.shedaniel.rei.api.client.gui.config.*; import me.shedaniel.rei.api.common.entry.EntryStack; @@ -142,8 +143,12 @@ public interface ConfigObject { @ApiStatus.Experimental List getFavoriteEntries(); + @ApiStatus.ScheduledForRemoval + @Deprecated List> getFilteredStacks(); + List> getFilteredStackProviders(); + @ApiStatus.Experimental boolean shouldAsyncSearch(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/config/entry/EntryStackProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/config/entry/EntryStackProvider.java new file mode 100644 index 000000000..3f784b798 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/config/entry/EntryStackProvider.java @@ -0,0 +1,120 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.config.entry; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.util.EntryStacks; +import net.minecraft.nbt.CompoundTag; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Objects; + +@ApiStatus.Experimental +@ApiStatus.NonExtendable +public interface EntryStackProvider { + EntryStack provide(); + + CompoundTag save(); + + boolean isValid(); + + static EntryStackProvider defer(CompoundTag tag) { + return new EntryStackProvider() { + private EntryStack stack; + + @Override + public EntryStack provide() { + if (stack == null) { + try { + stack = (EntryStack) EntryStack.read(tag); + } catch (Exception e) { + e.printStackTrace(); + return (EntryStack) EntryStack.empty(); + } + + stack = stack.normalize(); + } + + return stack; + } + + @Override + public CompoundTag save() { + return tag.copy(); + } + + @Override + public boolean isValid() { + return !provide().isEmpty(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof EntryStackProvider)) return false; + EntryStackProvider that = (EntryStackProvider) o; + return Objects.equals(provide(), that.provide()); + } + + @Override + public int hashCode() { + return Long.hashCode(EntryStacks.hashExact(provide())); + } + }; + } + + static EntryStackProvider ofStack(EntryStack stack) { + stack = stack.normalize(); + EntryStack finalStack = stack; + return new EntryStackProvider() { + @Override + public EntryStack provide() { + return finalStack; + } + + @Override + public CompoundTag save() { + return finalStack.save(); + } + + @Override + public boolean isValid() { + return !finalStack.isEmpty(); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof EntryStackProvider)) return false; + EntryStackProvider that = (EntryStackProvider) o; + return Objects.equals(provide(), that.provide()); + } + + @Override + public int hashCode() { + return Long.hashCode(EntryStacks.hashExact(provide())); + } + }; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java index 246a32979..ee8ecdbc6 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/SimpleDisplayRenderer.java @@ -45,7 +45,6 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index 09511ac6f..4288ba4e5 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -162,6 +162,10 @@ public class CollectionUtils { return list.parallelStream().map(function).collect(Collectors.toCollection(supplier)); } + public static > C filterAndMapParallel(Collection list, Predicate filter, Function function, Supplier supplier) { + return list.parallelStream().filter(filter).map(function).collect(Collectors.toCollection(supplier)); + } + public static List map(T[] list, Function function) { List l = new ArrayList<>(list.length + 1); for (T t : list) { -- cgit From d1e91e2e2ffa317a52e659f2ca2b76800108f427 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 6 Nov 2021 19:14:30 +0800 Subject: Fix JEI slot overlay, normalize dragging stacks and fix #651 --- api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index 4288ba4e5..aa4ac5037 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -315,7 +315,7 @@ public class CollectionUtils { @Override public T get(int index) { if (index < 0 || index >= realSize) - throw new IndexOutOfBoundsException(String.format("Index %s out of bounds for length %s", index, realSize)); + return null; return list.get(cursor + index); } -- cgit From 7879ac1cc61876bfa0578e34a96239830530a00b Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 7 Nov 2021 18:01:38 +0800 Subject: Modularize region rendering --- .../me/shedaniel/rei/api/client/REIRuntime.java | 8 ++- .../rei/api/client/entry/region/RegionEntry.java | 61 ++++++++++++++++++++++ .../rei/api/client/favorites/FavoriteEntry.java | 26 ++++++++- 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/region/RegionEntry.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java b/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java index 354c1ce19..adc1cb968 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java @@ -54,10 +54,14 @@ public interface REIRuntime extends Reloadable { void toggleOverlayVisible(); default Optional getOverlay() { - return getOverlay(false); + return getOverlay(false, false); } - Optional getOverlay(boolean reset); + default Optional getOverlay(boolean reset) { + return getOverlay(reset, true); + } + + Optional getOverlay(boolean reset, boolean init); @Nullable AbstractContainerScreen getPreviousContainerScreen(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/region/RegionEntry.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/region/RegionEntry.java new file mode 100644 index 000000000..c36a9444b --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/region/RegionEntry.java @@ -0,0 +1,61 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.entry.region; + +import me.shedaniel.rei.api.client.favorites.FavoriteEntry; +import me.shedaniel.rei.api.client.favorites.FavoriteMenuEntry; +import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Collection; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Supplier; + +@ApiStatus.Experimental +@ApiStatus.Internal +public interface RegionEntry> { + EntryStack toStack(); + + T copy(); + + default FavoriteEntry asFavorite() { + FavoriteEntry entry = FavoriteEntry.fromEntryStack(copy().toStack().normalize()); + return entry.isInvalid() ? null : entry; + } + + default boolean isEntryInvalid() { + return false; + } + + default Optional>> getMenuEntries() { + return Optional.empty(); + } + + UUID getUuid(); + + default boolean doAction(int button) { + return false; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntry.java b/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntry.java index 856cea841..493f2ddd3 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntry.java @@ -25,13 +25,16 @@ package me.shedaniel.rei.api.client.favorites; import com.mojang.serialization.DataResult; import com.mojang.serialization.Lifecycle; +import me.shedaniel.rei.api.client.entry.region.RegionEntry; import me.shedaniel.rei.api.client.gui.Renderer; +import me.shedaniel.rei.api.client.util.ClientEntryStacks; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.impl.ClientInternals; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Collection; @@ -41,7 +44,7 @@ import java.util.UUID; import java.util.function.Supplier; @Environment(EnvType.CLIENT) -public abstract class FavoriteEntry { +public abstract class FavoriteEntry implements RegionEntry { public static final String TYPE_KEY = "type"; private final UUID uuid = UUID.randomUUID(); @@ -71,15 +74,23 @@ public abstract class FavoriteEntry { return delegateResult(() -> FavoriteEntryType.registry().get(FavoriteEntryType.ENTRY_STACK).fromArgsResult(stack), null); } + @ApiStatus.ScheduledForRemoval + @Deprecated public static boolean isEntryInvalid(@Nullable FavoriteEntry entry) { return entry == null || entry.isInvalid(); } + @Override + public boolean isEntryInvalid() { + return isInvalid(); + } + public CompoundTag save(CompoundTag tag) { tag.putString(TYPE_KEY, getType().toString()); return Objects.requireNonNull(Objects.requireNonNull(FavoriteEntryType.registry().get(getType())).save(this, tag)); } + @Override public UUID getUuid() { return uuid; } @@ -88,14 +99,17 @@ public abstract class FavoriteEntry { public abstract Renderer getRenderer(boolean showcase); + @Override public abstract boolean doAction(int button); + @Override public Optional>> getMenuEntries() { return Optional.empty(); } public abstract long hashIgnoreAmount(); + @Override public abstract FavoriteEntry copy(); public abstract ResourceLocation getType(); @@ -127,4 +141,14 @@ public abstract class FavoriteEntry { public FavoriteEntry getUnwrapped() { return this; } + + @Override + public EntryStack toStack() { + return ClientEntryStacks.of(getRenderer(false)); + } + + @Override + public FavoriteEntry asFavorite() { + return copy(); + } } -- cgit From ba981379e97f53ffdea4db83666bd0e65c693f66 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sun, 7 Nov 2021 21:07:54 +0800 Subject: Introduce System Favorites --- .../api/client/favorites/FavoriteEntryType.java | 5 ++- .../favorites/SystemFavoriteEntryProvider.java | 38 ++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/favorites/SystemFavoriteEntryProvider.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntryType.java b/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntryType.java index 7c60253b9..a317787fc 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntryType.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/favorites/FavoriteEntryType.java @@ -76,7 +76,8 @@ public interface FavoriteEntryType { interface Registry extends Reloadable { void register(ResourceLocation id, FavoriteEntryType type); - @Nullable FavoriteEntryType get(ResourceLocation id); + @Nullable + FavoriteEntryType get(ResourceLocation id); @Nullable ResourceLocation getId(FavoriteEntryType type); @@ -84,6 +85,8 @@ public interface FavoriteEntryType { Section getOrCrateSection(Component text); Iterable
sections(); + + void registerSystemFavorites(SystemFavoriteEntryProvider provider); } @ApiStatus.NonExtendable diff --git a/api/src/main/java/me/shedaniel/rei/api/client/favorites/SystemFavoriteEntryProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/favorites/SystemFavoriteEntryProvider.java new file mode 100644 index 000000000..c5249d616 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/favorites/SystemFavoriteEntryProvider.java @@ -0,0 +1,38 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.favorites; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.List; + +@FunctionalInterface +@ApiStatus.Experimental +public interface SystemFavoriteEntryProvider { + List provide(); + + default long updateInterval() { + return 250; + } +} -- cgit From aba4c079befd4bb32f732b65c746a22559644d35 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Mon, 8 Nov 2021 23:15:08 +0800 Subject: Big Visual and Functional Changes - Fix #503 - Shift Click to select page, normal click to go back to page 1 - Allow non-consuming draggable visitors - Add scale down animation when dragging a stack to the main item list to dismiss it - Make Cheat Mode not active in Display Screens - Add colors to cosmetic transfer handler errors - Implement JEI animations, fix #501 - Allow favorites dragged stacks to go back to where they are if they are ignored, instead of being at the end of the favorites - Implement favorites & entry list column and row limits - Implement display page height limit - Updated localizations --- .../me/shedaniel/rei/api/client/REIRuntime.java | 12 +++++- .../rei/api/client/config/ConfigObject.java | 18 ++++++++- .../rei/api/client/gui/drag/DraggableStack.java | 9 ++++- .../api/client/gui/drag/DraggableStackVisitor.java | 23 +++++++++-- .../gui/drag/DraggableStackVisitorWidget.java | 25 ++++++++++-- .../api/client/gui/drag/DraggedAcceptorResult.java | 44 ++++++++++++++++++++++ .../rei/api/client/gui/drag/DraggingContext.java | 11 ++++++ .../client/registry/transfer/TransferHandler.java | 19 ++++++++-- 8 files changed, 145 insertions(+), 16 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggedAcceptorResult.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java b/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java index adc1cb968..1af65d4ad 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/REIRuntime.java @@ -24,18 +24,22 @@ package me.shedaniel.rei.api.client; import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.gui.config.SearchFieldLocation; import me.shedaniel.rei.api.client.gui.widgets.TextField; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.client.overlay.ScreenOverlay; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.registry.screen.ScreenRegistry; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.registry.Reloadable; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Optional; @@ -80,7 +84,13 @@ public interface REIRuntime extends Reloadable { SearchFieldLocation getContextualSearchFieldLocation(); - Rectangle calculateEntryListArea(); + @ApiStatus.ScheduledForRemoval + @Deprecated + default Rectangle calculateEntryListArea() { + return calculateEntryListArea(ScreenRegistry.getInstance().getOverlayBounds(ConfigObject.getInstance().getDisplayPanelLocation(), Minecraft.getInstance().screen)); + } + + Rectangle calculateEntryListArea(Rectangle bounds); Rectangle calculateFavoritesListArea(); } diff --git a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java index 040704236..8d35049c2 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java @@ -89,6 +89,8 @@ public interface ConfigObject { int getMaxRecipePerPage(); + int getMaxRecipesPageHeight(); + boolean doesDisableRecipeBook(); boolean doesFixTabCloseContainer(); @@ -163,10 +165,22 @@ public interface ConfigObject { boolean isInventoryHighlightingAllowed(); @ApiStatus.Experimental - double getHorizontalEntriesBoundaries(); + double getHorizontalEntriesBoundariesPercentage(); + + @ApiStatus.Experimental + double getVerticalEntriesBoundariesPercentage(); + + @ApiStatus.Experimental + double getHorizontalEntriesBoundariesColumns(); + + @ApiStatus.Experimental + double getVerticalEntriesBoundariesRows(); + + @ApiStatus.Experimental + double getFavoritesHorizontalEntriesBoundariesPercentage(); @ApiStatus.Experimental - double getVerticalEntriesBoundaries(); + double getFavoritesHorizontalEntriesBoundariesColumns(); @ApiStatus.Experimental SyntaxHighlightingMode getSyntaxHighlightingMode(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStack.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStack.java index 6dfaa9097..b5ccc97f2 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStack.java @@ -26,13 +26,20 @@ package me.shedaniel.rei.api.client.gui.drag; import com.mojang.blaze3d.vertex.PoseStack; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.ApiStatus; public interface DraggableStack { EntryStack getStack(); void drag(); - void release(boolean accepted); + @Deprecated + @ApiStatus.ScheduledForRemoval + default void release(boolean consumed) {} + + default void release(DraggedAcceptorResult result) { + release(result != DraggedAcceptorResult.PASS); + } default void render(PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { getStack().render(matrices, bounds, mouseX, mouseY, delta); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitor.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitor.java index ae973be16..d893eccd4 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitor.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitor.java @@ -29,6 +29,7 @@ import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import org.jetbrains.annotations.ApiStatus; +import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; import java.util.stream.Stream; @@ -51,14 +52,14 @@ public interface DraggableStackVisitor extends Comparable context, DraggableStack stack) { + public DraggedAcceptorResult acceptDraggedStackWithResult(DraggingContext context, DraggableStack stack) { for (DraggableStackVisitor visitor : visitors.get()) { if (visitor.isHandingScreen(context.getScreen())) { - boolean visited = visitor.acceptDraggedStack(context, stack); - if (visited) return true; + DraggedAcceptorResult result = Objects.requireNonNull(visitor.acceptDraggedStackWithResult(context, stack)); + if (result != DraggedAcceptorResult.PASS) return result; } } - return false; + return DraggedAcceptorResult.PASS; } @Override @@ -84,6 +85,8 @@ public interface DraggableStackVisitor extends Comparable context, DraggableStack stack) { Optional acceptor = visitDraggedStack(context, stack); if (acceptor.isPresent()) { @@ -94,6 +97,18 @@ public interface DraggableStackVisitor extends Comparable context, DraggableStack stack) { + return acceptDraggedStack(context, stack) ? DraggedAcceptorResult.CONSUMED : DraggedAcceptorResult.PASS; + } + /** * Returns the accepting bounds for the dragging stack, this should only be called once on drag. * The bounds are used to overlay to indicate to the users that the area is accepting entries. diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitorWidget.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitorWidget.java index 7bb813d4d..4234b2b6b 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitorWidget.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitorWidget.java @@ -39,9 +39,12 @@ public interface DraggableStackVisitorWidget { static DraggableStackVisitorWidget from(Function, Iterable> providers) { return new DraggableStackVisitorWidget() { @Override - public boolean acceptDraggedStack(DraggingContext context, DraggableStack stack) { + public DraggedAcceptorResult acceptDraggedStackWithResult(DraggingContext context, DraggableStack stack) { return StreamSupport.stream(providers.apply(context).spliterator(), false) - .anyMatch(visitor -> visitor.acceptDraggedStack(context, stack)); + .map(visitor -> visitor.acceptDraggedStackWithResult(context, stack)) + .filter(result -> result != DraggedAcceptorResult.PASS) + .findFirst() + .orElse(DraggedAcceptorResult.PASS); } @Override @@ -66,6 +69,8 @@ public interface DraggableStackVisitorWidget { * @param stack the stack being dragged * @return whether the stack is accepted by the widget */ + @ApiStatus.ScheduledForRemoval + @Deprecated default boolean acceptDraggedStack(DraggingContext context, DraggableStack stack) { Optional acceptor = visitDraggedStack(context, stack); if (acceptor.isPresent()) { @@ -76,6 +81,18 @@ public interface DraggableStackVisitorWidget { } } + /** + * Accepts a dragged stack, implementations of this function should check if the {@code context} is within + * boundaries of the widget. + * + * @param context the context of the current dragged stack on the overlay + * @param stack the stack being dragged + * @return the result of the visitor + */ + default DraggedAcceptorResult acceptDraggedStackWithResult(DraggingContext context, DraggableStack stack) { + return acceptDraggedStack(context, stack) ? DraggedAcceptorResult.CONSUMED : DraggedAcceptorResult.PASS; + } + /** * Returns the accepting bounds for the dragging stack, this should only be called once on drag. * The bounds are used to overlay to indicate to the users that the widget is accepting entries. @@ -95,8 +112,8 @@ public interface DraggableStackVisitorWidget { static DraggableStackVisitor toVisitor(DraggableStackVisitorWidget widget, double priority) { return new DraggableStackVisitor<>() { @Override - public boolean acceptDraggedStack(DraggingContext context, DraggableStack stack) { - return widget.acceptDraggedStack(context, stack); + public DraggedAcceptorResult acceptDraggedStackWithResult(DraggingContext context, DraggableStack stack) { + return widget.acceptDraggedStackWithResult(context, stack); } @Override diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggedAcceptorResult.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggedAcceptorResult.java new file mode 100644 index 000000000..05731ef0c --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggedAcceptorResult.java @@ -0,0 +1,44 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.drag; + +/** + * The result of the visitor, this is used to determine if the visitor consumed the stack or not. + */ +public enum DraggedAcceptorResult { + /** + * The visitor consumed the stack, the stack will not be returned to the original stack. + */ + CONSUMED, + /** + * The visitor did not consume the stack, the stack will be returned to the original stack, + * other visitors will not be called. + */ + ACCEPTED, + /** + * The visitor did not consume the stack, the stack will be passed to the next visitor. + */ + PASS, + ; +} \ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggingContext.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggingContext.java index 626d9aa29..20e2c1fe2 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggingContext.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggingContext.java @@ -24,6 +24,7 @@ package me.shedaniel.rei.api.client.gui.drag; import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.REIRuntime; import net.minecraft.client.gui.screens.Screen; import org.jetbrains.annotations.Nullable; @@ -82,6 +83,16 @@ public interface DraggingContext { */ void renderBackToPosition(DraggableStack stack, Point initialPosition, Supplier position); + /** + * Renders the draggable stack back to the bounds {@code bounds}. + * This may be used to animate an unaccepted draggable stack returning to its initial position. + * + * @param stack the stack to use for render + * @param initialPosition the initial bounds of the stack + * @param bounds the boundary supplier of the destination + */ + void renderBackToPosition(DraggableStack stack, Rectangle initialPosition, Supplier bounds); + default DraggingContext cast() { return (DraggingContext) this; } diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandler.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandler.java index 3c3ea6462..9ec3a3a15 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandler.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/transfer/TransferHandler.java @@ -69,7 +69,7 @@ public interface TransferHandler extends Comparable { * Creates a successful result, no further handlers will be called. */ static Result createSuccessful() { - return new ResultImpl(); + return new ResultImpl().color(0x00000000); } /** @@ -77,7 +77,7 @@ public interface TransferHandler extends Comparable { * This will also mark the handler as not applicable. */ static Result createNotApplicable() { - return new ResultImpl(false); + return new ResultImpl(false).color(0x00000000); } /** @@ -86,7 +86,7 @@ public interface TransferHandler extends Comparable { * @param error The error itself */ static Result createFailed(Component error) { - return new ResultImpl(error, new IntArrayList(), 1744764928); + return new ResultImpl(error, new IntArrayList(), 0x67ff0000); } /** @@ -107,7 +107,7 @@ public interface TransferHandler extends Comparable { * @param redSlots A list of slots to be marked as red. Will be passed to {@link TransferDisplayCategory}. */ static Result createFailed(Component error, IntList redSlots) { - return new ResultImpl(error, redSlots, 1744764928); + return new ResultImpl(error, redSlots, 0x67ff0000); } /** @@ -138,6 +138,11 @@ public interface TransferHandler extends Comparable { * @return the color in which the button should be displayed in. */ int getColor(); + + /** + * Sets the color in which the button should be displayed in. + */ + Result color(int color); /** * @return whether this handler has successfully handled the transfer. @@ -243,6 +248,12 @@ public interface TransferHandler extends Comparable { return color; } + @Override + public TransferHandler.Result color(int color) { + this.color = color; + return this; + } + @Override public boolean isSuccessful() { return successful; -- cgit From 57f59e7da8ae83f1ad952e410601409eecf2e1c4 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Thu, 11 Nov 2021 01:01:30 +0800 Subject: Add Dispose Here region, introduce ValueAnimator --- .../client/favorites/CompoundFavoriteRenderer.java | 7 +- .../client/gui/animator/ConstantValueProvider.java | 53 + .../gui/animator/ConventionValueAnimator.java | 67 + .../gui/animator/DoubleValueAnimatorImpl.java | 109 ++ .../gui/animator/MappingProgressValueAnimator.java | 68 + .../client/gui/animator/MappingValueAnimator.java | 62 + .../api/client/gui/animator/NumberAnimator.java | 116 ++ .../client/gui/animator/NumberAnimatorWrapped.java | 80 ++ .../client/gui/animator/ProgressValueAnimator.java | 46 + .../client/gui/animator/RecordValueAnimator.java | 155 ++ .../gui/animator/RecordValueAnimatorArgs.java | 1487 ++++++++++++++++++++ .../rei/api/client/gui/animator/ValueAnimator.java | 192 +++ .../animator/ValueAnimatorAsNumberAnimator.java | 70 + .../rei/api/client/gui/animator/ValueProvider.java | 73 + .../rei/api/client/gui/drag/DraggingContext.java | 6 + .../api/common/category/CategoryIdentifier.java | 5 + .../me/shedaniel/rei/api/common/util/Animator.java | 6 + 17 files changed, 2599 insertions(+), 3 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ProgressValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/RecordValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/RecordValueAnimatorArgs.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ValueAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ValueAnimatorAsNumberAnimator.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ValueProvider.java (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java index e9ce917dd..2ccba2166 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/favorites/CompoundFavoriteRenderer.java @@ -29,7 +29,8 @@ import me.shedaniel.clothconfig2.api.ScissorsHandler; import me.shedaniel.math.Rectangle; import me.shedaniel.rei.api.client.gui.AbstractRenderer; import me.shedaniel.rei.api.client.gui.Renderer; -import me.shedaniel.rei.api.common.util.Animator; +import me.shedaniel.rei.api.client.gui.animator.NumberAnimator; +import me.shedaniel.rei.api.client.gui.animator.ValueAnimator; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.Util; @@ -40,7 +41,7 @@ import java.util.function.IntSupplier; @Environment(EnvType.CLIENT) public class CompoundFavoriteRenderer extends AbstractRenderer { - protected Animator offset = new Animator(0); + protected NumberAnimator offset = ValueAnimator.ofDouble(); protected Rectangle scissorArea = new Rectangle(); protected long nextSwitch = -1; protected IntFunction renderers; @@ -114,7 +115,7 @@ public class CompoundFavoriteRenderer extends AbstractRenderer { } if (Util.getMillis() - nextSwitch > 1000) { nextSwitch = Util.getMillis(); - offset.setTo(((int) offset.target() + 1) % count, 500); + offset.setTo((offset.target().intValue() + 1) % count, 500); } } else { offset.setTo(supplier.getAsInt() % count, 500); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java new file mode 100644 index 000000000..f213702b4 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConstantValueProvider.java @@ -0,0 +1,53 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +final class ConstantValueProvider implements ValueProvider { + private final T value; + + public ConstantValueProvider(T value) { + this.value = value; + } + + @Override + public T value() { + return value; + } + + @Override + public T target() { + return value; + } + + @Override + public void completeImmediately() { + } + + @Override + public void update(double delta) { + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java new file mode 100644 index 000000000..41f4e8263 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/ConventionValueAnimator.java @@ -0,0 +1,67 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.Objects; +import java.util.function.Supplier; + +@ApiStatus.Internal +final class ConventionValueAnimator implements ValueAnimator { + private final ValueAnimator parent; + private final Supplier convention; + private final long duration; + + ConventionValueAnimator(ValueAnimator parent, Supplier convention, long duration) { + this.parent = parent; + this.convention = convention; + this.duration = duration; + setAs(convention.get()); + } + + @Override + public ValueAnimator setTo(T value, long duration) { + return parent.setTo(value, duration); + } + + @Override + public T target() { + return convention.get(); + } + + @Override + public T value() { + return parent.value(); + } + + @Override + public void update(double delta) { + parent.update(delta); + T target = target(); + if (!Objects.equals(parent.target(), target)) { + setTo(target, duration); + } + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java new file mode 100644 index 000000000..d3db45a40 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/DoubleValueAnimatorImpl.java @@ -0,0 +1,109 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import me.shedaniel.clothconfig2.impl.EasingMethod; +import net.minecraft.Util; +import org.jetbrains.annotations.ApiStatus; + +@ApiStatus.Internal +final class DoubleValueAnimatorImpl extends NumberAnimator { + private double amount; + private double target; + private long start; + private long duration; + + DoubleValueAnimatorImpl() { + } + + DoubleValueAnimatorImpl(double amount) { + setAs(amount); + } + + @Override + public NumberAnimator setToNumber(Number value, long duration) { + double doubleValue = value.doubleValue(); + if (target != doubleValue) { + this.set(doubleValue, duration); + } + + return this; + } + + private void set(double value, long duration) { + this.target = value; + this.start = Util.getMillis(); + + if (duration > 0) { + this.duration = duration; + } else { + this.duration = 0; + this.amount = this.target; + } + } + + @Override + public void update(double delta) { + if (duration != 0) { + if (amount < target) { + this.amount = Math.min(ease(amount, target + (target - amount), Math.min(((double) Util.getMillis() - start) / duration * delta * 3.0D, 1.0D), EasingMethod.EasingMethodImpl.LINEAR), target); + } else if (amount > target) { + this.amount = Math.max(ease(amount, target - (amount - target), Math.min(((double) Util.getMillis() - start) / duration * delta * 3.0D, 1.0D), EasingMethod.EasingMethodImpl.LINEAR), target); + } + } + } + + private static double ease(double start, double end, double amount, EasingMethod easingMethod) { + return start + (end - start) * easingMethod.apply(amount); + } + + @Override + public int intValue() { + return (int) amount; + } + + @Override + public long longValue() { + return (long) amount; + } + + @Override + public float floatValue() { + return (float) amount; + } + + @Override + public double doubleValue() { + return amount; + } + + public Double target() { + return target; + } + + @Override + public Double value() { + return amount; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java new file mode 100644 index 000000000..6de268b2a --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingProgressValueAnimator.java @@ -0,0 +1,68 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class MappingProgressValueAnimator implements ProgressValueAnimator { + private final ValueAnimator parent; + private final Function converter; + private final Function backwardsConverter; + + MappingProgressValueAnimator(ValueAnimator parent, Function converter, Function backwardsConverter) { + this.parent = parent; + this.converter = converter; + this.backwardsConverter = backwardsConverter; + } + + @Override + public ProgressValueAnimator setTo(R value, long duration) { + parent.setTo(backwardsConverter.apply(value), duration); + return this; + } + + @Override + public R target() { + return converter.apply(parent.target()); + } + + @Override + public R value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.update(delta); + } + + + @Override + public double progress() { + return parent.value() / 100; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java new file mode 100644 index 000000000..947807972 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/MappingValueAnimator.java @@ -0,0 +1,62 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class MappingValueAnimator implements ValueAnimator { + private final ValueAnimator parent; + private final Function converter; + private final Function backwardsConverter; + + MappingValueAnimator(ValueAnimator parent, Function converter, Function backwardsConverter) { + this.parent = parent; + this.converter = converter; + this.backwardsConverter = backwardsConverter; + } + + @Override + public ValueAnimator setTo(R value, long duration) { + parent.setTo(backwardsConverter.apply(value), duration); + return this; + } + + @Override + public R target() { + return converter.apply(parent.target()); + } + + @Override + public R value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.update(delta); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java new file mode 100644 index 000000000..bf12081ed --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimator.java @@ -0,0 +1,116 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Supplier; + +@ApiStatus.Experimental +public abstract class NumberAnimator extends Number implements ValueAnimator { + public NumberAnimator asDouble() { + return new NumberAnimatorWrapped<>(this, Number::doubleValue); + } + + public NumberAnimator asFloat() { + return new NumberAnimatorWrapped<>(this, Number::floatValue); + } + + public NumberAnimator asInt() { + return new NumberAnimatorWrapped<>(this, d -> (int) Math.round(d.doubleValue())); + } + + public NumberAnimator asLong() { + return new NumberAnimatorWrapped<>(this, d -> Math.round(d.doubleValue())); + } + + @Override + public NumberAnimator setAs(T value) { + ValueAnimator.super.setAs(value); + return this; + } + + public NumberAnimator setAs(int value) { + setAsNumber(value); + return this; + } + + public NumberAnimator setAs(long value) { + setAsNumber(value); + return this; + } + + public NumberAnimator setAs(float value) { + setAsNumber(value); + return this; + } + + public NumberAnimator setAs(double value) { + setAsNumber(value); + return this; + } + + @Override + public NumberAnimator setTo(T value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator setTo(int value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator setTo(long value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator setTo(float value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator setTo(double value, long duration) { + setToNumber(value, duration); + return this; + } + + public NumberAnimator setAsNumber(Number value) { + return setToNumber(value, -1); + } + + public abstract NumberAnimator setToNumber(Number value, long duration); + + @Override + public NumberAnimator withConvention(Supplier convention, long duration) { + ValueAnimator parentConvention = ValueAnimator.super.withConvention(convention, duration); + return new ValueAnimatorAsNumberAnimator(parentConvention) { + @Override + public NumberAnimator setToNumber(Number value, long duration) { + return NumberAnimator.this.setToNumber(value, duration); + } + }; + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java new file mode 100644 index 000000000..0214fb637 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/animator/NumberAnimatorWrapped.java @@ -0,0 +1,80 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.client.gui.animator; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.function.Function; + +@ApiStatus.Internal +final class NumberAnimatorWrapped extends NumberAnimator { + private final NumberAnimator parent; + private final Function converter; + + NumberAnimatorWrapped(NumberAnimator parent, Function converter) { + this.parent = parent; + this.converter = converter; + } + + @Override + public NumberAnimator setToNumber(Number value, long duration) { + this.parent.setToNumber(value, duration); + return this; + } + + @Override + public T target() { + return converter.apply(parent.target()); + } + + @Override + public T value() { + return converter.apply(parent.value()); + } + + @Override + public void update(double delta) { + parent.