From 46600fdeeff4e716da16954451ef8aa61f4446e6 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Mon, 16 Sep 2024 17:12:03 -0500 Subject: [PATCH 1/4] [ui] Fix incorrect logic with BaseOwoHandledScreen slot methods --- .../owo/ui/base/BaseOwoHandledScreen.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java index 2eaaf5bb..17f544f0 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java @@ -87,6 +87,25 @@ protected void init() { } } + /** + * Moves the slot with the given index off the screen + * to prevent player intractability when disabled or enabled + * + * @param index The index of the slot to disable + */ + protected void hideSlot(int index) { + hideSlot(this.handler.slots.get(index)); + } + + /** + * Moves the slot off the screen to prevent player + * intractability when disabled or enabled + */ + protected void hideSlot(Slot slot) { + ((SlotAccessor) slot).owo$setX(-300); + ((SlotAccessor) slot).owo$setY(-300); + } + /** * Disable the slot at the given index. Note * that this is hard override and the slot cannot @@ -95,7 +114,7 @@ protected void init() { * @param index The index of the slot to disable */ protected void disableSlot(int index) { - ((OwoSlotExtension) this.handler.slots.get(index)).owo$setDisabledOverride(true); + this.disableSlot(this.handler.slots.get(index)); } /** @@ -115,7 +134,7 @@ protected void disableSlot(Slot slot) { * @param index The index of the slot to enable */ protected void enableSlot(int index) { - ((OwoSlotExtension) this.handler.slots.get(index)).owo$setDisabledOverride(false); + this.enableSlot(this.handler.slots.get(index)); } /** @@ -124,15 +143,23 @@ protected void enableSlot(int index) { * a slot that is disabled through its own will */ protected void enableSlot(Slot slot) { - ((OwoSlotExtension) slot).owo$setDisabledOverride(true); + ((OwoSlotExtension) slot).owo$setDisabledOverride(false); } + /** + * @return whether the given slot is enabled or disabled + * using the {@link OwoSlotExtension} disabling functionality + */ protected boolean isSlotEnabled(int index) { - return ((OwoSlotExtension) this.handler.slots.get(index)).owo$getDisabledOverride(); + return isSlotEnabled(this.handler.slots.get(index)); } + /** + * @return whether the given slot is enabled or disabled + * using the {@link OwoSlotExtension} disabling functionality + */ protected boolean isSlotEnabled(Slot slot) { - return ((OwoSlotExtension) slot).owo$getDisabledOverride(); + return !((OwoSlotExtension) slot).owo$getDisabledOverride(); } /** From 07042ca9d05fcf2f1604c0fe250de4ddfefa04d0 Mon Sep 17 00:00:00 2001 From: Dragon-Seeker Date: Mon, 16 Sep 2024 17:51:31 -0500 Subject: [PATCH 2/4] [ui] Adjust Exclusion Area code for REI and EMI Use instance method on the BaseOwoHandledScreen for custom exclusion areas --- .../owo/compat/emi/OwoEmiPlugin.java | 19 +++---------- .../owo/compat/rei/OwoReiPlugin.java | 23 ++++----------- .../owo/ui/base/BaseOwoHandledScreen.java | 28 +++++++++++++++++++ 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java b/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java index 50a0076c..c81d94e6 100644 --- a/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java +++ b/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java @@ -6,6 +6,7 @@ import io.wispforest.owo.itemgroup.OwoItemGroup; import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor; import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; +import io.wispforest.owo.ui.base.BaseOwoHandledScreen; import io.wispforest.owo.ui.core.Component; import io.wispforest.owo.ui.core.ParentComponent; import io.wispforest.owo.ui.core.Surface; @@ -36,22 +37,10 @@ public void register(EmiRegistry registry) { }); registry.addGenericExclusionArea((screen, consumer) -> { - if (screen.children().isEmpty() || !(screen instanceof BaseOwoHandledScreenAccessor accessor)) return; + if (!(screen instanceof BaseOwoHandledScreen owoHandledScreen)) return; - var adapter = accessor.owo$getUIAdapter(); - if (adapter == null) return; - - var rootComponent = adapter.rootComponent; - var children = new ArrayList(); - rootComponent.collectDescendants(children); - children.remove(rootComponent); - - children.forEach(component -> { - if (component instanceof ParentComponent parent && parent.surface() == Surface.BLANK) return; - - var size = component.fullSize(); - consumer.accept(new Bounds(component.x(), component.y(), size.width(), size.height())); - }); + owoHandledScreen.getExclusionAreas() + .forEach(rect -> consumer.accept(new Bounds(rect.x(), rect.y(), rect.width(), rect.height()))); }); } } diff --git a/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java b/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java index b9ccc81a..c91b006e 100644 --- a/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java +++ b/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java @@ -2,6 +2,7 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; +import dev.emi.emi.api.widget.Bounds; import io.wispforest.owo.itemgroup.OwoItemGroup; import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor; import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; @@ -22,6 +23,7 @@ import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; +import net.minecraft.screen.ScreenHandler; import net.minecraft.util.math.RotationAxis; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -59,24 +61,9 @@ public void registerExclusionZones(ExclusionZones zones) { }); zones.register(BaseOwoHandledScreen.class, screen -> { - if (screen.children().isEmpty()) return List.of(); - - var adapter = ((BaseOwoHandledScreenAccessor) screen).owo$getUIAdapter(); - if (adapter == null) return List.of(); - - var rootComponent = adapter.rootComponent; - var children = new ArrayList(); - rootComponent.collectDescendants(children); - children.remove(rootComponent); - - var rectangles = new ArrayList(); - children.forEach(component -> { - if (component instanceof ParentComponent parent && parent.surface() == Surface.BLANK) return; - - var size = component.fullSize(); - rectangles.add(new Rectangle(component.x(), component.y(), size.width(), size.height())); - }); - return rectangles; + return ((BaseOwoHandledScreen) screen).getExclusionAreas().stream() + .map(rect -> new Rectangle(rect.x(), rect.y(), rect.width(), rect.height())) + .toList(); }); } diff --git a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java index 2eaaf5bb..ff223282 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java @@ -1,7 +1,9 @@ package io.wispforest.owo.ui.base; +import dev.emi.emi.api.widget.Bounds; import io.wispforest.owo.Owo; import io.wispforest.owo.mixin.ui.SlotAccessor; +import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; import io.wispforest.owo.ui.core.*; import io.wispforest.owo.ui.inject.GreedyInputComponent; import io.wispforest.owo.ui.util.DisposableScreen; @@ -20,6 +22,8 @@ import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL11; +import java.util.ArrayList; +import java.util.List; import java.util.function.BiFunction; public abstract class BaseOwoHandledScreen extends HandledScreen implements DisposableScreen { @@ -42,6 +46,30 @@ protected BaseOwoHandledScreen(S handler, PlayerInventory inventory, Text title) super(handler, inventory, title); } + /** + * Called by REI and EMI plugin to tell there respected API + * that the given returned list of {@link PositionedRectangle} + * are meant to be excluded from being rendered within + */ + public List getExclusionAreas() { + if (this.children().isEmpty()) return List.of(); + + var rootComponent = uiAdapter.rootComponent; + var children = new ArrayList(); + + rootComponent.collectDescendants(children); + children.remove(rootComponent); + + return children.stream() + .filter(component -> !(component instanceof ParentComponent parent) || parent.surface() != Surface.BLANK) + .map(component -> { + var size = component.fullSize(); + + return PositionedRectangle.of(component.x(), component.y(), size.width(), size.height()); + }) + .toList(); + } + /** * Initialize the UI adapter for this screen. Usually * the body of this method will simply consist of a call From 2f860a6315cd93feb63cd12e9348f927d5e228fc Mon Sep 17 00:00:00 2001 From: glisco Date: Wed, 23 Oct 2024 22:00:47 +0200 Subject: [PATCH 3/4] [ui] remove hideSlot methods --- .../owo/ui/base/BaseOwoHandledScreen.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java index 17f544f0..e015f960 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java @@ -87,25 +87,6 @@ protected void init() { } } - /** - * Moves the slot with the given index off the screen - * to prevent player intractability when disabled or enabled - * - * @param index The index of the slot to disable - */ - protected void hideSlot(int index) { - hideSlot(this.handler.slots.get(index)); - } - - /** - * Moves the slot off the screen to prevent player - * intractability when disabled or enabled - */ - protected void hideSlot(Slot slot) { - ((SlotAccessor) slot).owo$setX(-300); - ((SlotAccessor) slot).owo$setY(-300); - } - /** * Disable the slot at the given index. Note * that this is hard override and the slot cannot From 2d7812fe99d09cd0b1f61429f5e2bf30e5a9513d Mon Sep 17 00:00:00 2001 From: glisco Date: Wed, 23 Oct 2024 22:33:17 +0200 Subject: [PATCH 4/4] [ui] rename BaseOwoHandledScreen#getExclusionAreas to componentsForExclusionAreas() and improve implementation --- .../owo/compat/emi/OwoEmiPlugin.java | 11 +--- .../owo/compat/rei/OwoReiPlugin.java | 6 +- .../owo/ui/base/BaseOwoHandledScreen.java | 61 ++++++++----------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java b/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java index c81d94e6..16bc7ae5 100644 --- a/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java +++ b/src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java @@ -5,16 +5,10 @@ import dev.emi.emi.api.widget.Bounds; import io.wispforest.owo.itemgroup.OwoItemGroup; import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor; -import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; import io.wispforest.owo.ui.base.BaseOwoHandledScreen; -import io.wispforest.owo.ui.core.Component; -import io.wispforest.owo.ui.core.ParentComponent; -import io.wispforest.owo.ui.core.Surface; import io.wispforest.owo.util.pond.OwoCreativeInventoryScreenExtensions; import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; -import java.util.ArrayList; - public class OwoEmiPlugin implements EmiPlugin { @Override public void register(EmiRegistry registry) { @@ -39,8 +33,9 @@ public void register(EmiRegistry registry) { registry.addGenericExclusionArea((screen, consumer) -> { if (!(screen instanceof BaseOwoHandledScreen owoHandledScreen)) return; - owoHandledScreen.getExclusionAreas() - .forEach(rect -> consumer.accept(new Bounds(rect.x(), rect.y(), rect.width(), rect.height()))); + owoHandledScreen.componentsForExclusionAreas() + .map(component -> new Bounds(component.x(), component.y(), component.width(), component.height())) + .forEach(consumer); }); } } diff --git a/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java b/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java index c91b006e..d239ccf1 100644 --- a/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java +++ b/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java @@ -2,12 +2,10 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.systems.RenderSystem; -import dev.emi.emi.api.widget.Bounds; import io.wispforest.owo.itemgroup.OwoItemGroup; import io.wispforest.owo.mixin.itemgroup.CreativeInventoryScreenAccessor; import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; import io.wispforest.owo.ui.base.BaseOwoHandledScreen; -import io.wispforest.owo.ui.core.Component; import io.wispforest.owo.ui.core.OwoUIDrawContext; import io.wispforest.owo.ui.core.ParentComponent; import io.wispforest.owo.ui.core.Surface; @@ -23,14 +21,12 @@ import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; -import net.minecraft.screen.ScreenHandler; import net.minecraft.util.math.RotationAxis; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; import java.util.Collections; -import java.util.List; public class OwoReiPlugin implements REIClientPlugin { @@ -61,7 +57,7 @@ public void registerExclusionZones(ExclusionZones zones) { }); zones.register(BaseOwoHandledScreen.class, screen -> { - return ((BaseOwoHandledScreen) screen).getExclusionAreas().stream() + return ((BaseOwoHandledScreen) screen).componentsForExclusionAreas() .map(rect -> new Rectangle(rect.x(), rect.y(), rect.width(), rect.height())) .toList(); }); diff --git a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java index ff223282..ec16b2ae 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java @@ -1,9 +1,7 @@ package io.wispforest.owo.ui.base; -import dev.emi.emi.api.widget.Bounds; import io.wispforest.owo.Owo; import io.wispforest.owo.mixin.ui.SlotAccessor; -import io.wispforest.owo.mixin.ui.access.BaseOwoHandledScreenAccessor; import io.wispforest.owo.ui.core.*; import io.wispforest.owo.ui.inject.GreedyInputComponent; import io.wispforest.owo.ui.util.DisposableScreen; @@ -17,14 +15,15 @@ import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.slot.Slot; import net.minecraft.text.Text; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; import org.lwjgl.opengl.GL11; import java.util.ArrayList; -import java.util.List; import java.util.function.BiFunction; +import java.util.stream.Stream; public abstract class BaseOwoHandledScreen extends HandledScreen implements DisposableScreen { @@ -46,30 +45,6 @@ protected BaseOwoHandledScreen(S handler, PlayerInventory inventory, Text title) super(handler, inventory, title); } - /** - * Called by REI and EMI plugin to tell there respected API - * that the given returned list of {@link PositionedRectangle} - * are meant to be excluded from being rendered within - */ - public List getExclusionAreas() { - if (this.children().isEmpty()) return List.of(); - - var rootComponent = uiAdapter.rootComponent; - var children = new ArrayList(); - - rootComponent.collectDescendants(children); - children.remove(rootComponent); - - return children.stream() - .filter(component -> !(component instanceof ParentComponent parent) || parent.surface() != Surface.BLANK) - .map(component -> { - var size = component.fullSize(); - - return PositionedRectangle.of(component.x(), component.y(), size.width(), size.height()); - }) - .toList(); - } - /** * Initialize the UI adapter for this screen. Usually * the body of this method will simply consist of a call @@ -182,6 +157,24 @@ protected C component(Class expectedClass, String id) { return this.uiAdapter.rootComponent.childById(expectedClass, id); } + /** + * Compute a stream of all components for which to + * generate exclusion areas in a recipe viewer overlay. + * Called by the REI and EMI plugins + */ + @ApiStatus.OverrideOnly + public Stream componentsForExclusionAreas() { + if (this.children().isEmpty()) return Stream.of(); + + var rootComponent = uiAdapter.rootComponent; + var children = new ArrayList(); + + rootComponent.collectDescendants(children); + children.remove(rootComponent); + + return children.stream().filter(component -> !(component instanceof ParentComponent parent) || parent.surface() != Surface.BLANK); + } + @Override public void renderBackground(DrawContext context, int mouseX, int mouseY, float delta) {} @@ -199,12 +192,12 @@ public void render(DrawContext vanillaContext, int mouseX, int mouseY, float del if (!slot.isEnabled()) continue; context.drawText(Text.literal("H:" + i), - this.x + slot.x + 15, this.y + slot.y + 9, .5f, 0x0096FF, - OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT + this.x + slot.x + 15, this.y + slot.y + 9, .5f, 0x0096FF, + OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT ); context.drawText(Text.literal("I:" + slot.getIndex()), - this.x + slot.x + 15, this.y + slot.y + 15, .5f, 0x5800FF, - OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT + this.x + slot.x + 15, this.y + slot.y + 15, .5f, 0x5800FF, + OwoUIDrawContext.TextAnchor.BOTTOM_RIGHT ); } @@ -220,8 +213,8 @@ public void render(DrawContext vanillaContext, int mouseX, int mouseY, float del @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if ((modifiers & GLFW.GLFW_MOD_CONTROL) == 0 - && this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent - && inputComponent.onKeyPress(keyCode, scanCode, modifiers)) { + && this.uiAdapter.rootComponent.focusHandler().focused() instanceof GreedyInputComponent inputComponent + && inputComponent.onKeyPress(keyCode, scanCode, modifiers)) { return true; } @@ -272,7 +265,7 @@ public void draw(OwoUIDrawContext context, int mouseX, int mouseY, float partial GL11.glGetIntegerv(GL11.GL_SCISSOR_BOX, scissor); ((OwoSlotExtension) this.slot).owo$setScissorArea(PositionedRectangle.of( - scissor[0], scissor[1], scissor[2], scissor[3] + scissor[0], scissor[1], scissor[2], scissor[3] )); }