diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-10-22 15:15:08 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-10-23 19:38:57 +0800 |
| commit | b17c43db7fa123eaffb671c59f337e8d9d1dece0 (patch) | |
| tree | a76592f19c1cc82508ea1ac2069a663d60c784e6 /api/src/main/java/me/shedaniel | |
| parent | 34fdd4fd90e84595cf29cd9db494bdfa073f17c7 (diff) | |
| download | RoughlyEnoughItems-b17c43db7fa123eaffb671c59f337e8d9d1dece0.tar.gz RoughlyEnoughItems-b17c43db7fa123eaffb671c59f337e8d9d1dece0.tar.bz2 RoughlyEnoughItems-b17c43db7fa123eaffb671c59f337e8d9d1dece0.zip | |
Fix #645
Diffstat (limited to 'api/src/main/java/me/shedaniel')
6 files changed, 191 insertions, 2 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java index 3943a76eb..43819351a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRenderer.java @@ -40,9 +40,10 @@ import org.jetbrains.annotations.Nullable; * * @param <T> the entry type * @see BatchedEntryRenderer + * @see EntryRendererRegistry */ @Environment(EnvType.CLIENT) -public interface EntryRenderer<T> { +public interface EntryRenderer<T> extends EntryRendererProvider<T> { static <T> EntryRenderer<T> empty() { return ClientInternals.getEmptyEntryRenderer(); } @@ -58,4 +59,9 @@ public interface EntryRenderer<T> { default <O> EntryRenderer<O> cast() { return (EntryRenderer<O>) this; } + + @Override + default EntryRenderer<T> provide(EntryStack<T> entry, EntryRenderer<T> last) { + return this; + } }
\ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java new file mode 100644 index 000000000..659df5cff --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererProvider.java @@ -0,0 +1,54 @@ +/* + * 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.renderer; + +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; + +/** + * A functional interface to provide {@link EntryRenderer} for {@link EntryStack}. + * + * @param <T> the entry type + * @see EntryRenderer + */ +@FunctionalInterface +@Environment(EnvType.CLIENT) +public interface EntryRendererProvider<T> { + static <T> EntryRendererProvider<T> empty() { + return EntryRenderer.empty(); + } + + /** + * Returns a new {@link EntryRenderer} for a specific {@link EntryStack}, + * a previous {@link EntryRenderer} will be provided, this may be an empty renderer. + * {@code null} is not an accepted value, return the previous renderer if this provider + * does not modify the renderer. + * + * @param entry the entry stack to render for, do not store or cache this + * @param last the previous entry renderer + * @return the new entry renderer, {@code null} is not accepted here + */ + EntryRenderer<T> provide(EntryStack<T> entry, EntryRenderer<T> last); +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.java new file mode 100644 index 000000000..95b8981b3 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/EntryRendererRegistry.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.entry.renderer; + +import me.shedaniel.math.Point; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.type.EntryType; +import me.shedaniel.rei.api.common.plugins.PluginManager; +import me.shedaniel.rei.api.common.registry.Reloadable; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +/** + * Registry to transform {@link EntryRenderer} for stacks at a global level. + * For specific stacks, you can use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setRenderer} + */ +@ApiStatus.Experimental +public interface EntryRendererRegistry extends Reloadable<REIClientPlugin> { + static EntryRendererRegistry getInstance() { + return PluginManager.getClientInstance().get(EntryRendererRegistry.class); + } + + <T> void register(EntryType<T> type, EntryRendererProvider<T> provider); + + default <T> void transformTooltip(EntryType<T> type, TooltipTransformer<T> transformer) { + register(type, (entry, last) -> { + return new ForwardingEntryRenderer<T>(last) { + @Override + @Nullable + public Tooltip getTooltip(EntryStack<T> entry, Point mouse) { + return transformer.transform(entry, mouse, super.getTooltip(entry, mouse)); + } + }; + }); + } + + <T> EntryRenderer<T> get(EntryStack<T> stack); + + @FunctionalInterface + interface TooltipTransformer<T> { + @Nullable + Tooltip transform(EntryStack<T> entry, Point mouse, @Nullable Tooltip tooltip); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java new file mode 100644 index 000000000..2c56d5176 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/ForwardingEntryRenderer.java @@ -0,0 +1,50 @@ +/* + * 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.renderer; + +import com.mojang.blaze3d.vertex.PoseStack; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; +import me.shedaniel.rei.api.common.entry.EntryStack; +import org.jetbrains.annotations.Nullable; + +public abstract class ForwardingEntryRenderer<T> implements EntryRenderer<T> { + protected EntryRenderer<T> next; + + public ForwardingEntryRenderer(EntryRenderer<T> next) { + this.next = next; + } + + @Override + public void render(EntryStack<T> entry, PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { + this.next.render(entry, matrices, bounds, mouseX, mouseY, delta); + } + + @Override + @Nullable + public Tooltip getTooltip(EntryStack<T> entry, Point mouse) { + return this.next.getTooltip(entry, mouse); + } +} diff --git a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java index e65ccc4e1..a6be0beeb 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/plugins/REIClientPlugin.java @@ -23,6 +23,7 @@ package me.shedaniel.rei.api.client.plugins; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.favorites.FavoriteEntryType; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; @@ -39,6 +40,16 @@ import org.jetbrains.annotations.ApiStatus; @Environment(EnvType.CLIENT) public interface REIClientPlugin extends REIPlugin<REIClientPlugin> { /** + * Registers new entry renderers + * + * @param registry the entry renderer registry + */ + @ApiStatus.OverrideOnly + @ApiStatus.Experimental + default void registerEntryRenderers(EntryRendererRegistry registry) { + } + + /** * Registers new categories * * @param registry the category registry diff --git a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java index d92d12d6f..0abe5a43a 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/entry/EntryStack.java @@ -28,6 +28,7 @@ import me.shedaniel.architectury.utils.EnvExecutor; import me.shedaniel.math.Point; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; +import me.shedaniel.rei.api.client.entry.renderer.EntryRendererRegistry; import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.type.EntryDefinition; @@ -182,7 +183,7 @@ public interface EntryStack<T> extends TextRepresentable, Renderer { static { EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { - RENDERER = new Settings<>(stack -> stack.getDefinition().getRenderer()); + RENDERER = new Settings<>(stack -> EntryRendererRegistry.getInstance().get(stack)); TOOLTIP_PROCESSOR = new Settings<>((stack, tooltip) -> tooltip); TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); FLUID_RENDER_RATIO = new Settings<>(1.0F); |
