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..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,15 +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.core.Component; -import io.wispforest.owo.ui.core.ParentComponent; -import io.wispforest.owo.ui.core.Surface; +import io.wispforest.owo.ui.base.BaseOwoHandledScreen; 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) { @@ -36,22 +31,11 @@ public void register(EmiRegistry registry) { }); registry.addGenericExclusionArea((screen, consumer) -> { - if (screen.children().isEmpty() || !(screen instanceof BaseOwoHandledScreenAccessor accessor)) 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; + if (!(screen instanceof BaseOwoHandledScreen owoHandledScreen)) return; - var size = component.fullSize(); - consumer.accept(new Bounds(component.x(), component.y(), size.width(), size.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 b9ccc81a..d239ccf1 100644 --- a/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java +++ b/src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java @@ -6,7 +6,6 @@ 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; @@ -28,7 +27,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.List; public class OwoReiPlugin implements REIClientPlugin { @@ -59,24 +57,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).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 e015f960..11f689d5 100644 --- a/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java +++ b/src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java @@ -15,12 +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.function.BiFunction; +import java.util.stream.Stream; public abstract class BaseOwoHandledScreen extends HandledScreen implements DisposableScreen { @@ -162,6 +165,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) {} @@ -179,12 +200,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 ); } @@ -200,8 +221,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; } @@ -252,7 +273,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] )); }