Skip to content

Commit

Permalink
Merge pull request #307 from Dragon-Seeker/1.21-ExclusionZoneAdjustments
Browse files Browse the repository at this point in the history
[1.21] Adjust Exclusion Area code for REI and EMI
  • Loading branch information
gliscowo authored Oct 23, 2024
2 parents 6be8b83 + 2d7812f commit a524f48
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 48 deletions.
26 changes: 5 additions & 21 deletions src/main/java/io/wispforest/owo/compat/emi/OwoEmiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<Component>();
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);
});
}
}
23 changes: 3 additions & 20 deletions src/main/java/io/wispforest/owo/compat/rei/OwoReiPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,7 +27,6 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class OwoReiPlugin implements REIClientPlugin {

Expand Down Expand Up @@ -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<Component>();
rootComponent.collectDescendants(children);
children.remove(rootComponent);

var rectangles = new ArrayList<Rectangle>();
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();
});
}

Expand Down
35 changes: 28 additions & 7 deletions src/main/java/io/wispforest/owo/ui/base/BaseOwoHandledScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<R extends ParentComponent, S extends ScreenHandler> extends HandledScreen<S> implements DisposableScreen {

Expand Down Expand Up @@ -162,6 +165,24 @@ protected <C extends Component> C component(Class<C> 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<Component> componentsForExclusionAreas() {
if (this.children().isEmpty()) return Stream.of();

var rootComponent = uiAdapter.rootComponent;
var children = new ArrayList<Component>();

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) {}

Expand All @@ -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
);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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]
));
}

Expand Down

0 comments on commit a524f48

Please sign in to comment.