From b9b450cb3c024952a2748fbcfb83726d061964de Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 25 Jun 2022 22:36:21 +0800 Subject: Use a method for DelegateWidget and add no-op widget --- .../rei/api/client/gui/widgets/DelegateWidget.java | 35 ++++++++++++---------- .../rei/api/client/gui/widgets/Widgets.java | 4 +++ .../client/registry/display/DisplayCategory.java | 28 ++++++++--------- .../client/registry/display/DisplayRegistry.java | 14 ++++----- .../me/shedaniel/rei/impl/ClientInternals.java | 2 ++ 5 files changed, 47 insertions(+), 36 deletions(-) (limited to 'api/src') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/DelegateWidget.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/DelegateWidget.java index 19c73516e..67ca753f4 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/DelegateWidget.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/DelegateWidget.java @@ -41,9 +41,13 @@ public class DelegateWidget extends WidgetWithBounds { this.children = Collections.singletonList(widget); } + public Widget delegate() { + return widget; + } + @Override public void render(PoseStack poseStack, int i, int j, float f) { - widget.render(poseStack, i, j, f); + delegate().render(poseStack, i, j, f); } @Override @@ -53,37 +57,38 @@ public class DelegateWidget extends WidgetWithBounds { @Override public Rectangle getBounds() { - return widget instanceof WidgetWithBounds ? ((WidgetWithBounds) widget).getBounds() : EMPTY; + return delegate() instanceof WidgetWithBounds withBounds ? withBounds.getBounds() : EMPTY; } @Override public void setZ(int z) { - widget.setZ(z); + delegate().setZ(z); } @Override public int getZ() { - return widget.getZ(); + return delegate().getZ(); } @Nullable @Override public GuiEventListener getFocused() { - return widget; + return delegate(); } @Override public void setFocused(@Nullable GuiEventListener guiEventListener) { - if (guiEventListener == widget) { - super.setFocused(widget); + Widget delegate = delegate(); + if (guiEventListener == delegate) { + super.setFocused(delegate); } else { - widget.setFocused(guiEventListener); + delegate.setFocused(guiEventListener); } } @Override public boolean containsMouse(double mouseX, double mouseY) { - return widget.containsMouse(mouseX, mouseY); + return delegate().containsMouse(mouseX, mouseY); } @Override @@ -93,32 +98,32 @@ public class DelegateWidget extends WidgetWithBounds { @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { - return widget.mouseScrolled(mouseX, mouseY, amount); + return delegate().mouseScrolled(mouseX, mouseY, amount); } @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { - return widget.keyPressed(keyCode, scanCode, modifiers); + return delegate().keyPressed(keyCode, scanCode, modifiers); } @Override public boolean keyReleased(int keyCode, int scanCode, int modifiers) { - return widget.keyReleased(keyCode, scanCode, modifiers); + return delegate().keyReleased(keyCode, scanCode, modifiers); } @Override public boolean charTyped(char character, int modifiers) { - return widget.charTyped(character, modifiers); + return delegate().charTyped(character, modifiers); } @Override public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { - return widget.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); + return delegate().mouseDragged(mouseX, mouseY, button, deltaX, deltaY); } @Override public boolean mouseReleased(double mouseX, double mouseY, int button) { this.setDragging(false); - return widget.mouseReleased(mouseX, mouseY, button); + return delegate().mouseReleased(mouseX, mouseY, button); } } diff --git a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widgets.java b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widgets.java index 191d276d2..df535cfa3 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widgets.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/gui/widgets/Widgets.java @@ -303,6 +303,10 @@ public final class Widgets { return ClientInternals.getWidgetsProvider().concatWidgets(widgets); } + public static Widget noOp() { + return ClientInternals.getWidgetsProvider().noOp(); + } + public static Iterable walk(Iterable listeners, Predicate predicate) { return () -> new AbstractIterator() { Stack> stack; diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java index 0e376aa9f..c01d1a9f3 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java @@ -46,13 +46,13 @@ import java.util.List; @Environment(EnvType.CLIENT) public interface DisplayCategory extends DisplayCategoryView, Identifiable { /** - * Returns the renderer of the icon displayed in the category tab. - *

- * A simple implementation is the {@link me.shedaniel.rei.api.common.entry.EntryStack}. + * Returns the identifier of this {@link DisplayCategory}. + * This identifier must be the same one used to register the category + * in {@link me.shedaniel.rei.api.client.registry.category.CategoryRegistry}. * - * @return the renderer of the icon + * @return the identifier of this category */ - Renderer getIcon(); + CategoryIdentifier getCategoryIdentifier(); /** * Returns the category title for the category. @@ -61,6 +61,15 @@ public interface DisplayCategory extends DisplayCategoryView< */ Component getTitle(); + /** + * Returns the renderer of the icon displayed in the category tab. + *

+ * A simple implementation is the {@link me.shedaniel.rei.api.common.entry.EntryStack}. + * + * @return the renderer of the icon + */ + Renderer getIcon(); + /** * {@inheritDoc} */ @@ -116,15 +125,6 @@ public interface DisplayCategory extends DisplayCategoryView< return -1; } - /** - * Returns the identifier of this {@link DisplayCategory}. - * This identifier must be the same one used to register the category - * in {@link me.shedaniel.rei.api.client.registry.category.CategoryRegistry}. - * - * @return the identifier of this category - */ - CategoryIdentifier getCategoryIdentifier(); - /** * Returns the display merger for this category. *

diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java index 0af581d55..464cbafbb 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java @@ -219,7 +219,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of object * @param the type of display */ - default , D extends Display> void registerRecipeFiller(Class typeClass, RecipeType recipeType, Function filler) { + default , D extends Display> void registerRecipeFiller(Class typeClass, RecipeType recipeType, Function filler) { registerRecipeFiller(typeClass, type -> Objects.equals(recipeType, type), filler); } @@ -234,7 +234,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of object * @param the type of display */ - default , D extends Display> void registerRecipeFiller(Class typeClass, Predicate> recipeType, Function filler) { + default , D extends Display> void registerRecipeFiller(Class typeClass, Predicate> recipeType, Function filler) { registerRecipeFiller(typeClass, recipeType, Predicates.alwaysTrue(), filler); } @@ -249,7 +249,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of object * @param the type of display */ - default , D extends Display> void registerRecipeFiller(Class typeClass, Predicate> recipeType, Predicate predicate, Function filler) { + default , D extends Display> void registerRecipeFiller(Class typeClass, Predicate> recipeType, Predicate predicate, Function filler) { registerFiller(typeClass, recipe -> recipeType.test((RecipeType) recipe.getType()) && ((Predicate) predicate).test(recipe), filler); } @@ -264,7 +264,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of object * @param the type of display */ - default void registerFiller(Class typeClass, Function filler) { + default void registerFiller(Class typeClass, Function filler) { registerFiller(typeClass, Predicates.alwaysTrue(), filler); } @@ -280,7 +280,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of object * @param the type of display */ - void registerFiller(Class typeClass, Predicate predicate, Function filler); + void registerFiller(Class typeClass, Predicate predicate, Function filler); /** * Registers a display filler, to be filled during {@link #tryFillDisplay(Object)}. @@ -295,7 +295,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param the type of display */ @ApiStatus.Experimental - void registerFiller(Class typeClass, BiPredicate predicate, Function filler); + void registerFiller(Class typeClass, BiPredicate predicate, Function filler); /** * Registers a display filler, to be filled during {@link #tryFillDisplay(Object)}. @@ -307,7 +307,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @param filler the filler, taking an object and returning a {@code D} * @param the type of display */ - void registerFiller(Predicate predicate, Function filler); + void registerFiller(Predicate predicate, Function filler); /** * Tries to fill displays from {@code T}. diff --git a/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java index 382f4873c..cf6e13be6 100644 --- a/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java +++ b/api/src/main/java/me/shedaniel/rei/impl/ClientInternals.java @@ -201,5 +201,7 @@ public final class ClientInternals { Widget createShapelessIcon(Point point); Widget concatWidgets(List widgets); + + Widget noOp(); } } -- cgit