diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-04-06 15:47:20 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-04-06 15:47:20 +0800 |
| commit | 966bbd7fe43237f9b1aa6cfd94cf5e34a9bcfddf (patch) | |
| tree | 7c19b609f1d61f0afde3947742056dbb8b17a396 | |
| parent | 5b0c5838978fc74f783c6dcff2983af4fb97d912 (diff) | |
| download | RoughlyEnoughItems-966bbd7fe43237f9b1aa6cfd94cf5e34a9bcfddf.tar.gz RoughlyEnoughItems-966bbd7fe43237f9b1aa6cfd94cf5e34a9bcfddf.tar.bz2 RoughlyEnoughItems-966bbd7fe43237f9b1aa6cfd94cf5e34a9bcfddf.zip | |
Expose draggable stacks
Signed-off-by: shedaniel <daniel@shedaniel.me>
52 files changed, 703 insertions, 211 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/REIOverlay.java b/api/src/main/java/me/shedaniel/rei/api/client/REIOverlay.java index b585c2642..5ef067f6a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/REIOverlay.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/REIOverlay.java @@ -33,7 +33,7 @@ public abstract class REIOverlay extends WidgetWithBounds { public abstract void queueReloadOverlay(); - public abstract DraggingContext getDraggingContext(); + public abstract DraggingContext<?> getDraggingContext(); public abstract boolean isNotInExclusionZones(double mouseX, double mouseY); } 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 c4b2205ae..917a88b75 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 @@ -23,6 +23,7 @@ package me.shedaniel.rei.api.client.gui; +import com.google.common.base.Predicates; import com.mojang.blaze3d.vertex.PoseStack; import it.unimi.dsi.fastutil.longs.LongOpenHashSet; import it.unimi.dsi.fastutil.longs.LongSet; @@ -46,39 +47,42 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Comparator; import java.util.List; -import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; public class SimpleDisplayRenderer extends DisplayRenderer implements WidgetHolder { - private static final Comparator<EntryStack<?>> ENTRY_COMPARATOR = Comparator.comparingLong(EntryStacks::hashExact); - private static final ResourceLocation CHEST_GUI_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/recipecontainer.png"); - private List<Slot> inputWidgets; - private List<Slot> outputWidgets; - private List<GuiEventListener> widgets; + protected static final Comparator<EntryStack<?>> ENTRY_COMPARATOR = Comparator.comparingLong(EntryStacks::hashExact); + protected static final ResourceLocation CHEST_GUI_TEXTURE = new ResourceLocation("roughlyenoughitems", "textures/gui/recipecontainer.png"); + protected List<Slot> inputWidgets; + protected List<Slot> outputWidgets; + protected List<GuiEventListener> widgets; @ApiStatus.Internal - private SimpleDisplayRenderer(List<EntryIngredient> input, List<EntryIngredient> output) { - this.inputWidgets = simplify(input).stream().filter(stacks -> !stacks.isEmpty()).map(stacks -> Widgets.createSlot(new Point(0, 0)).entries(stacks).disableBackground().disableHighlight().disableTooltips()).collect(Collectors.toList()); - this.outputWidgets = CollectionUtils.map(simplify(output), outputStacks -> - Widgets.createSlot(new Point(0, 0)).entries(CollectionUtils.filterToList(outputStacks, stack -> !stack.isEmpty())).disableBackground().disableHighlight().disableTooltips()); + protected SimpleDisplayRenderer(List<EntryIngredient> input, List<EntryIngredient> output) { + this.inputWidgets = CollectionUtils.map(simplify(input), this::createSlot); + this.outputWidgets = CollectionUtils.map(simplify(output), this::createSlot); this.widgets = Stream.concat(inputWidgets.stream(), outputWidgets.stream()).collect(Collectors.toList()); } + private Slot createSlot(EntryIngredient ingredient) { + return Widgets.createSlot(new Point(0, 0)) + .entries(CollectionUtils.filterToList(ingredient, stack -> !stack.isEmpty())) + .disableBackground() + .disableHighlight() + .disableTooltips(); + } + private static List<EntryIngredient> simplify(List<EntryIngredient> original) { List<EntryIngredient> out = new ArrayList<>(); for (EntryIngredient ingredient : original) { - if (out.stream().noneMatch(s -> equalsList(ingredient, s))) { - out.add(ingredient); + EntryIngredient filter = ingredient.filter(Predicates.not(EntryStack::isEmpty)); + if (!filter.isEmpty() && out.stream().noneMatch(s -> equalsList(filter, s))) { + out.add(filter); } } return out; } - public static DisplayRenderer from(Supplier<List<EntryIngredient>> input, Supplier<List<EntryIngredient>> output) { - return from(input.get(), output.get()); - } - public static DisplayRenderer from(List<EntryIngredient> input, List<EntryIngredient> output) { return new SimpleDisplayRenderer(input, output); } @@ -148,7 +152,7 @@ public class SimpleDisplayRenderer extends DisplayRenderer implements WidgetHold } public int getItemsHeight() { - return Mth.ceil(((float) inputWidgets.size()) / (getItemsPerLine() - 2)); + return Math.max(Mth.ceil(((float) inputWidgets.size()) / (getItemsPerLine() - 2)), outputWidgets.size()); } public int getItemsPerLine() { diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/config/DisplayScreenType.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/config/DisplayScreenType.java index 7500f5375..8e2fcc726 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/config/DisplayScreenType.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/config/DisplayScreenType.java @@ -26,11 +26,9 @@ package me.shedaniel.rei.api.client.gui.config; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.resources.language.I18n; -import org.jetbrains.annotations.ApiStatus; import java.util.Locale; -@ApiStatus.Internal @Environment(EnvType.CLIENT) public enum DisplayScreenType { UNSET, diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProvider.java index 4fc3e892c..fe5ccffe2 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProvider.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProvider.java @@ -23,22 +23,61 @@ package me.shedaniel.rei.api.client.gui.drag; +import net.minecraft.client.gui.screens.Screen; import org.jetbrains.annotations.Nullable; import java.util.function.Supplier; -@FunctionalInterface -public interface DraggableStackProvider { - static DraggableStackProvider from(Supplier<Iterable<DraggableStackProvider>> providers) { - return (context, mouseX, mouseY) -> { - for (DraggableStackProvider provider : providers.get()) { - DraggableStack stack = provider.getHoveredStack(context, mouseX, mouseY); - if (stack != null) return stack; +/** + * A provider for supplying {@link DraggableStack} to the screen. + */ +public interface DraggableStackProvider<T extends Screen> extends Comparable<DraggableStackProvider<T>> { + static <T extends Screen> DraggableStackProvider<T> from(Supplier<Iterable<DraggableStackProvider<T>>> providers) { + return new DraggableStackProvider<T>() { + @Override + public <R extends Screen> boolean isHandingScreen(R screen) { + for (DraggableStackProvider<T> provider : providers.get()) { + if (provider.isHandingScreen(screen)) { + return true; + } + } + return false; + } + + @Override + @Nullable + public DraggableStack getHoveredStack(DraggingContext<T> context, double mouseX, double mouseY) { + for (DraggableStackProvider<T> provider : providers.get()) { + if (provider.isHandingScreen(context.getScreen())) { + DraggableStack stack = provider.getHoveredStack(context, mouseX, mouseY); + if (stack != null) return stack; + } + } + return null; } - return null; }; } @Nullable - DraggableStack getHoveredStack(DraggingContext context, double mouseX, double mouseY); + DraggableStack getHoveredStack(DraggingContext<T> context, double mouseX, double mouseY); + + <R extends Screen> boolean isHandingScreen(R screen); + + default DraggingContext<T> getContext() { + return DraggingContext.getInstance().cast(); + } + + /** + * Gets the priority of the handler, the higher the priority, the earlier this is called. + * + * @return the priority + */ + default double getPriority() { + return 0f; + } + + @Override + default int compareTo(DraggableStackProvider<T> o) { + return Double.compare(getPriority(), o.getPriority()); + } }
\ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProviderWidget.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProviderWidget.java new file mode 100644 index 000000000..8efb79311 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackProviderWidget.java @@ -0,0 +1,73 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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; + +import net.minecraft.client.gui.screens.Screen; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Function; + +/** + * An interface to be implemented on {@link me.shedaniel.rei.api.client.gui.widgets.Widget} to provide + * {@link DraggableStack}. + */ +@FunctionalInterface +public interface DraggableStackProviderWidget { + static DraggableStackProviderWidget from(Function<DraggingContext<Screen>, Iterable<DraggableStackProviderWidget>> providers) { + return (context, mouseX, mouseY) -> { + for (DraggableStackProviderWidget provider : providers.apply(context)) { + DraggableStack stack = provider.getHoveredStack(context, mouseX, mouseY); + if (stack != null) return stack; + } + return null; + }; + } + + @Nullable + DraggableStack getHoveredStack(DraggingContext<Screen> context, double mouseX, double mouseY); + + static DraggableStackProvider<Screen> toProvider(DraggableStackProviderWidget widget) { + return toProvider(widget, 0D); + } + + static DraggableStackProvider<Screen> toProvider(DraggableStackProviderWidget widget, double priority) { + return new DraggableStackProvider<Screen>() { + @Override + @Nullable + public DraggableStack getHoveredStack(DraggingContext<Screen> context, double mouseX, double mouseY) { + return widget.getHoveredStack(context, mouseX, mouseY); + } + + @Override + public <R extends Screen> boolean isHandingScreen(R screen) { + return true; + } + + @Override + public double getPriority() { + return priority; + } + }; + } +} 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 8ebdf9c57..6959d912a 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 @@ -23,23 +23,63 @@ package me.shedaniel.rei.api.client.gui.drag; +import net.minecraft.client.gui.screens.Screen; + import java.util.Optional; import java.util.function.Supplier; -@FunctionalInterface -public interface DraggableStackVisitor { - static DraggableStackVisitor from(Supplier<Iterable<DraggableStackVisitor>> visitors) { - return (stack) -> { - for (DraggableStackVisitor visitor : visitors.get()) { - Optional<Acceptor> acceptor = visitor.visitDraggedStack(stack); - if (acceptor.isPresent()) return acceptor; +/** + * A visitor for accepting {@link DraggableStack} to the screen. + */ +public interface DraggableStackVisitor<T extends Screen> extends Comparable<DraggableStackVisitor<T>> { + static <T extends Screen> DraggableStackVisitor<T> from(Supplier<Iterable<DraggableStackVisitor<T>>> visitors) { + return new DraggableStackVisitor<T>() { + @Override + public <R extends Screen> boolean isHandingScreen(R screen) { + for (DraggableStackVisitor<T> visitor : visitors.get()) { + if (visitor.isHandingScreen(screen)) { + return true; + } + } + return false; + } + + @Override + public Optional<Acceptor> visitDraggedStack(DraggingContext<T> context, DraggableStack stack) { + for (DraggableStackVisitor<T> visitor : visitors.get()) { + if (visitor.isHandingScreen(context.getScreen())) { + Optional<Acceptor> acceptor = visitor.visitDraggedStack(context, stack); + if (acceptor.isPresent()) return acceptor; + } + } + return Optional.empty(); } - return Optional.empty(); }; } - Optional<Acceptor> visitDraggedStack(DraggableStack stack); + Optional<Acceptor> visitDraggedStack(DraggingContext<T> context, DraggableStack stack); + + <R extends Screen> boolean isHandingScreen(R screen); + + default DraggingContext<T> getContext() { + return DraggingContext.getInstance().cast(); + } + + /** + * Gets the priority of the handler, the higher the priority, the earlier this is called. + * + * @return the priority + */ + default double getPriority() { + return 0.0; + } + + @Override + default int compareTo(DraggableStackVisitor<T> o) { + return Double.compare(getPriority(), o.getPriority()); + } + @FunctionalInterface interface Acceptor { void accept(DraggableStack stack); } 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 new file mode 100644 index 000000000..0af21ddc5 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/drag/DraggableStackVisitorWidget.java @@ -0,0 +1,71 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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; + +import net.minecraft.client.gui.screens.Screen; + +import java.util.Optional; +import java.util.function.Function; + +/** + * An interface to be implemented on {@link me.shedaniel.rei.api.client.gui.widgets.Widget} to accept + * incoming {@link DraggableStack}. + */ +@FunctionalInterface +public in |
