From 0e96125db3f91890657d1026feb0879bbf761dc7 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 10 Apr 2021 14:28:46 +0800 Subject: BatchEntryRenderer -> BatchedEntryRenderer Signed-off-by: shedaniel --- .../client/entry/renderer/BatchEntryRenderer.java | 62 --------------- .../entry/renderer/BatchedEntryRenderer.java | 88 ++++++++++++++++++++++ .../api/client/entry/renderer/EntryRenderer.java | 7 ++ 3 files changed, 95 insertions(+), 62 deletions(-) delete mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchEntryRenderer.java create mode 100644 api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchedEntryRenderer.java (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchEntryRenderer.java deleted file mode 100644 index ba7e5d150..000000000 --- a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchEntryRenderer.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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.entry.renderer; - -import com.mojang.blaze3d.vertex.PoseStack; -import me.shedaniel.math.Rectangle; -import me.shedaniel.rei.api.common.entry.EntryStack; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.MultiBufferSource; - -public interface BatchEntryRenderer extends EntryRenderer { - static int getBatchIdFrom(EntryStack entry) { - EntryRenderer renderer = entry.getRenderer(); - if (renderer instanceof BatchEntryRenderer) return ((BatchEntryRenderer) renderer).getBatchId(entry); - return renderer.getClass().hashCode(); - } - - default int getBatchId(EntryStack entry) { - return getClass().hashCode(); - } - - void startBatch(EntryStack entry, PoseStack matrices, float delta); - - void renderBase(EntryStack entry, PoseStack matrices, MultiBufferSource.BufferSource immediate, Rectangle bounds, int mouseX, int mouseY, float delta); - - void renderOverlay(EntryStack entry, PoseStack matrices, MultiBufferSource.BufferSource immediate, Rectangle bounds, int mouseX, int mouseY, float delta); - - void endBatch(EntryStack entry, PoseStack matrices, float delta); - - @Deprecated - @Override - default void render(EntryStack entry, PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { - startBatch(entry, matrices, delta); - MultiBufferSource.BufferSource immediate = Minecraft.getInstance().renderBuffers().bufferSource(); - renderBase(entry, matrices, immediate, bounds, mouseX, mouseY, delta); - immediate.endBatch(); - renderOverlay(entry, matrices, immediate, bounds, mouseX, mouseY, delta); - immediate.endBatch(); - endBatch(entry, matrices, delta); - } -} \ No newline at end of file diff --git a/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchedEntryRenderer.java b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchedEntryRenderer.java new file mode 100644 index 000000000..400ef727c --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/client/entry/renderer/BatchedEntryRenderer.java @@ -0,0 +1,88 @@ +/* + * 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.entry.renderer; + +import com.mojang.blaze3d.vertex.PoseStack; +import me.shedaniel.math.Rectangle; +import me.shedaniel.rei.api.common.entry.EntryStack; +import net.minecraft.client.Minecraft; +import net.minecraft.client.renderer.MultiBufferSource; + +public interface BatchedEntryRenderer extends EntryRenderer { + /** + * Returns a batch identifier, stacks with the same batch identifier will be grouped together + * into a batch. + * + * @param entry the stack + * @param bounds the bounds of the entry + * @return the batch identifier + */ + default int getBatchIdentifier(EntryStack entry, Rectangle bounds) { + return getClass().hashCode(); + } + + /** + * Modifies the {@link PoseStack} passed tp various batch rendering methods. + * + * @param matrices the matrix stack + * @return the modified matrix stack, could be an entirely different stack + */ + default PoseStack batchModifyMatrices(PoseStack matrices) { + return matrices; + } + + /** + * Starts the batch rendering, used to setup states, only called once with every batch. + * + * @param entry the first entry in the batch + * @param matrices the matrix stack + * @param delta the tick delta + */ + void startBatch(EntryStack entry, PoseStack matrices, float delta); + + void renderBase(EntryStack entry, PoseStack matrices, MultiBufferSource.BufferSource immediate, Rectangle bounds, int mouseX, int mouseY, float delta); + + void renderOverlay(EntryStack entry, PoseStack matrices, MultiBufferSource.BufferSource immediate, Rectangle bounds, int mouseX, int mouseY, float delta); + + /** + * Ends the batch rendering, used to setup states, only called once with every batch. + * + * @param entry the first entry in the batch + * @param matrices the matrix stack + * @param delta the tick delta + */ + void endBatch(EntryStack entry, PoseStack matrices, float delta); + + @Override + default void render(EntryStack entry, PoseStack matrices, Rectangle bounds, int mouseX, int mouseY, float delta) { + matrices = batchModifyMatrices(matrices); + startBatch(entry, matrices, delta); + MultiBufferSource.BufferSource immediate = Minecraft.getInstance().renderBuffers().bufferSource(); + renderBase(entry, matrices, immediate, bounds, mouseX, mouseY, delta); + immediate.endBatch(); + renderOverlay(entry, matrices, immediate, bounds, mouseX, mouseY, delta); + immediate.endBatch(); + endBatch(entry, matrices, delta); + } +} \ No newline at end of file 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 9ee60c5f8..e5b78b788 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 @@ -34,6 +34,13 @@ import net.fabricmc.api.Environment; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +/** + * A renderer to render a {@link EntryStack}. + * Use {@link me.shedaniel.rei.api.client.util.ClientEntryStacks#setRenderer} to change the {@link EntryRenderer} for a {@link EntryStack}. + * + * @param the entry type + * @see BatchedEntryRenderer + */ public interface EntryRenderer { static EntryRenderer empty() { return ClientInternals.getEmptyEntryRenderer(); -- cgit From 6d7690b3410e6aa837089987c08598cb9ecbe45a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 10 Apr 2021 14:30:41 +0800 Subject: Change how EntryStack.Settings is exposed Signed-off-by: shedaniel --- .../rei/api/client/util/ClientEntryStacks.java | 28 ++++++++++++++++++ .../shedaniel/rei/api/common/entry/EntryStack.java | 34 +++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-) (limited to 'api/src/main/java/me/shedaniel') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java b/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java index 537a658f2..7409f1150 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/util/ClientEntryStacks.java @@ -23,10 +23,16 @@ package me.shedaniel.rei.api.client.util; +import me.shedaniel.architectury.fluid.FluidStack; +import me.shedaniel.rei.api.client.entry.renderer.EntryRenderer; import me.shedaniel.rei.api.client.entry.type.BuiltinClientEntryTypes; import me.shedaniel.rei.api.client.gui.Renderer; +import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.EntryStack; +import java.util.function.BiFunction; +import java.util.function.Function; + public final class ClientEntryStacks { private ClientEntryStacks() {} @@ -37,4 +43,26 @@ public final class ClientEntryStacks { return EntryStack.of(BuiltinClientEntryTypes.RENDERING, renderer); } + + public static EntryStack setNotRenderer(EntryStack stack) { + return setRenderer(stack, EntryRenderer.empty()); + } + + public static EntryStack setRenderer(EntryStack stack, EntryRenderer renderer) { + return stack.setting(EntryStack.Settings.RENDERER, s -> renderer); + } + + @SuppressWarnings("rawtypes") + public static EntryStack setRenderer(EntryStack stack, Function, EntryRenderer> rendererProvider) { + return stack.setting(EntryStack.Settings.RENDERER, (Function) rendererProvider); + } + + @SuppressWarnings("rawtypes") + public static EntryStack setTooltipProcessor(EntryStack stack, BiFunction, Tooltip, Tooltip> processor) { + return stack.setting(EntryStack.Settings.TOOLTIP_PROCESSOR, (BiFunction) processor); + } + + public static EntryStack setFluidRenderRatio(EntryStack stack, float ratio) { + return stack.setting(EntryStack.Settings.FLUID_RENDER_RATIO, ratio); + } } 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 660605f32..3b0f6b454 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 @@ -23,6 +23,8 @@ package me.shedaniel.rei.api.common.entry; +import me.shedaniel.architectury.utils.Env; +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; @@ -103,7 +105,7 @@ public interface EntryStack extends TextRepresentable, Renderer { @Environment(EnvType.CLIENT) default EntryRenderer getRenderer() { - EntryRenderer renderer = get(Settings.RENDER).apply(this); + EntryRenderer renderer = get(Settings.RENDERER).apply(this); return renderer == null ? EntryRenderer.empty() : renderer.cast(); } @@ -138,39 +140,43 @@ public interface EntryStack extends TextRepresentable, Renderer { R get(Settings settings); + @Environment(EnvType.CLIENT) default EntryStack tooltip(Component... tooltips) { return tooltip(Arrays.asList(tooltips)); } + @Environment(EnvType.CLIENT) default EntryStack tooltip(List tooltips) { return tooltip(stack -> tooltips); } + @Environment(EnvType.CLIENT) default EntryStack tooltip(Function, List> tooltipProvider) { return setting(Settings.TOOLTIP_APPEND_EXTRA, tooltipProvider); } + @Deprecated class Settings { @ApiStatus.Internal private static final List> SETTINGS = new ArrayList<>(); - public static final Supplier TRUE = () -> true; - public static final Supplier FALSE = () -> false; @Environment(EnvType.CLIENT) - public static final Function, EntryRenderer> DEFAULT_RENDERER = stack -> stack.getDefinition().getRenderer(); + public static Settings, EntryRenderer>> RENDERER; @Environment(EnvType.CLIENT) - public static final Function, EntryRenderer> EMPTY_RENDERER = stack -> EntryRenderer.empty(); - public static final BiFunction, Tooltip, Tooltip> DEFAULT_TOOLTIP_PROCESSOR = (stack, tooltip) -> tooltip; + public static Settings, Tooltip, Tooltip>> TOOLTIP_PROCESSOR; @Environment(EnvType.CLIENT) - public static final Settings, EntryRenderer>> RENDER = new Settings<>(DEFAULT_RENDERER); - @Deprecated - public static final Settings, Tooltip, Tooltip>> TOOLTIP_PROCESSOR = new Settings<>(DEFAULT_TOOLTIP_PROCESSOR); - @Deprecated - public static final Settings, List>> TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); + public static Settings, List>> TOOLTIP_APPEND_EXTRA; @Environment(EnvType.CLIENT) - public static final Float DEFAULT_RENDER_RATIO = 1.0F; - @Environment(EnvType.CLIENT) - public static final Settings FLUID_RENDER_RATIO = new Settings<>(DEFAULT_RENDER_RATIO); + public static Settings FLUID_RENDER_RATIO; + + static { + EnvExecutor.runInEnv(Env.CLIENT, () -> () -> { + RENDERER = new Settings<>(stack -> stack.getDefinition().getRenderer()); + TOOLTIP_PROCESSOR = new Settings<>((stack, tooltip) -> tooltip); + TOOLTIP_APPEND_EXTRA = new Settings<>(stack -> Collections.emptyList()); + FLUID_RENDER_RATIO = new Settings<>(1.0F); + }); + } private R defaultValue; private short id; -- cgit