Skip to content

Commit

Permalink
Merge pull request #329 from Dragon-Seeker/1.21.2-FixSlotRenderingIssues
Browse files Browse the repository at this point in the history
[1.21.2] Fix various conflicting rendering elements
  • Loading branch information
gliscowo authored Dec 11, 2024
2 parents 621e4b9 + ae3e0c2 commit 771e70a
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 20 deletions.
9 changes: 7 additions & 2 deletions src/main/java/io/wispforest/owo/compat/rei/ReiUIAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,13 @@ public ReiUIAdapter(Rectangle bounds, BiFunction<Sizing, Sizing, T> rootComponen
this.adapter = OwoUIAdapter.createWithoutScreen(bounds.x, bounds.y, bounds.width, bounds.height, rootComponentMaker);
this.adapter.inspectorZOffset = 900;

if (MinecraftClient.getInstance().currentScreen != null) {
ScreenEvents.remove(MinecraftClient.getInstance().currentScreen).register(screen -> this.adapter.dispose());
var screenWithREI = MinecraftClient.getInstance().currentScreen;

if (screenWithREI != null) {
ScreenEvents.remove(screenWithREI).register(screen -> this.adapter.dispose());
ScreenEvents.afterRender(screenWithREI).register((screen, drawContext, mouseX, mouseY, tickDelta) -> {
this.adapter.drawTooltip(drawContext, mouseX, mouseY, tickDelta);
});
}
}

Expand Down
14 changes: 0 additions & 14 deletions src/main/java/io/wispforest/owo/mixin/ui/HandledScreenMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,6 @@ private void clearSlotScissors(DrawContext context, Slot slot, CallbackInfo ci)
GlStateManager._disableScissorTest();
}

// TODO: [1.21.2-Porting] Figure out f such is still needed and how to handle such
// @Inject(method = "drawSlotHighlight", at = @At(value = "HEAD"))
// private static void enableSlotDepth(DrawContext context, int x, int y, int z, CallbackInfo ci) {
// if (!owo$inOwoScreen) return;
// RenderSystem.enableDepthTest();
// context.getMatrices().translate(0, 0, 300);
// }
//
// @Inject(method = "drawSlotHighlight", at = @At("TAIL"))
// private static void clearSlotDepth(DrawContext context, int x, int y, int z, CallbackInfo ci) {
// if (!owo$inOwoScreen) return;
// context.getMatrices().translate(0, 0, -300);
// }

@ModifyVariable(method = "mouseClicked", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/SimpleOption;getValue()Ljava/lang/Object;", ordinal = 0), ordinal = 3)
private int doNoThrow(int slotId, @Local() Slot slot) {
return (((Object) this instanceof BaseOwoHandledScreen<?, ?>) && slot != null) ? slot.id : slotId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import io.wispforest.owo.ui.util.DisposableScreen;
import io.wispforest.owo.ui.util.UIErrorToast;
import io.wispforest.owo.util.pond.OwoSlotExtension;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;
import net.minecraft.text.Text;
Expand Down Expand Up @@ -82,6 +84,10 @@ protected void init() {
this.build(this.uiAdapter.rootComponent);

this.uiAdapter.inflateAndMount();

ScreenEvents.afterRender(this).register((screen, drawContext, mouseX, mouseY, tickDelta) -> {
if (this.uiAdapter != null) this.uiAdapter.drawTooltip(drawContext, mouseX, mouseY, tickDelta);
});
} catch (Exception error) {
Owo.LOGGER.warn("Could not initialize owo screen", error);
UIErrorToast.report(error);
Expand Down Expand Up @@ -261,6 +267,63 @@ public void dispose() {
@Override
protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {}

@Override
protected void drawSlotHighlightBack(DrawContext context) {
context.push().translate(0, 0, this.getLayerZOffset(HandledScreenLayer.SLOT));
super.drawSlotHighlightBack(context);
}

@Override
protected void drawSlotHighlightFront(DrawContext context) {
super.drawSlotHighlightFront(context);
context.pop();
}

@Override
protected void drawItem(DrawContext context, ItemStack stack, int x, int y, @Nullable String amountText) {
context.push().translate(0, 0, this.getLayerZOffset(HandledScreenLayer.CURSOR_ITEM));
super.drawItem(context, stack, x, y, amountText);
context.pop();
}

@Override
protected void drawMouseoverTooltip(DrawContext context, int x, int y) {
context.push().translate(0, 0, this.getLayerZOffset(HandledScreenLayer.ITEM_TOOLTIP));
super.drawMouseoverTooltip(context, x, y);
context.pop();
}

/**
* Return the z-offset to apply to rendering the given {@code layer}
*/
protected int getLayerZOffset(HandledScreenLayer layer) {
return layer == HandledScreenLayer.CURSOR_ITEM ? -6900 : 300;
}

/**
* Different layers of handled screen rendering, the z-offset
* of which can be adjusted in an owo screen using {@link #getLayerZOffset(HandledScreenLayer)}
*/
protected enum HandledScreenLayer {
/**
* The items in all slots, along with the highlight
* of the hovered slot
*/
SLOT,

/**
* The item currently held by the cursor. More specifically, any item
* rendered through the {@link #drawItem(DrawContext, ItemStack, int, int, String)} method
*/
CURSOR_ITEM,

/**
* The tooltip of an item in a slot. More specifically, any tooltip
* rendered through {@link #drawMouseoverTooltip(DrawContext, int, int)}
*/
ITEM_TOOLTIP
}

public class SlotComponent extends BaseComponent {

protected final Slot slot;
Expand Down Expand Up @@ -325,4 +388,4 @@ public void updateY(int y) {
((SlotAccessor) this.slot).owo$setY(y - BaseOwoHandledScreen.this.y);
}
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/io/wispforest/owo/ui/base/BaseOwoScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.wispforest.owo.ui.inject.GreedyInputComponent;
import io.wispforest.owo.ui.util.DisposableScreen;
import io.wispforest.owo.ui.util.UIErrorToast;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.screen.Screen;
Expand Down Expand Up @@ -91,6 +92,10 @@ protected void init() {
this.build(this.uiAdapter.rootComponent);

this.uiAdapter.inflateAndMount();

ScreenEvents.afterRender(this).register((screen, drawContext, mouseX, mouseY, tickDelta) -> {
if (this.uiAdapter != null) this.uiAdapter.drawTooltip(drawContext, mouseX, mouseY, tickDelta);
});
} catch (Exception error) {
Owo.LOGGER.warn("Could not initialize owo screen", error);
UIErrorToast.report(error);
Expand Down
24 changes: 22 additions & 2 deletions src/main/java/io/wispforest/owo/ui/core/OwoUIAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
* even if you choose to not use {@link io.wispforest.owo.ui.base.BaseOwoScreen}
* you can always simply add it as a widget and get most of the functionality
* working out of the box
* <p>
* To draw the UI tree managed by this adapter, call {@link OwoUIAdapter#render(DrawContext, int, int, float)}.
* Note that this does not draw the current tooltip of the UI - this must be done separately
* by invoking {@link #drawTooltip(DrawContext, int, int, float)}. If in a scenario with multiple adapters
* or other sources rendering UI elements to the screen, it is generally desirable to delay tooltip
* drawing until after all UI is drawn to avoid layering issues.
*
* @see io.wispforest.owo.ui.base.BaseOwoScreen
*/
Expand Down Expand Up @@ -180,8 +186,6 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic
GlStateManager._disableScissorTest();
RenderSystem.disableDepthTest();

this.rootComponent.drawTooltip(owoContext, mouseX, mouseY, partialTicks, delta);

final var hovered = this.rootComponent.childAt(mouseX, mouseY);
if (!disposed && hovered != null) {
this.cursorAdapter.applyStyle(hovered.cursorStyle());
Expand All @@ -200,6 +204,22 @@ public void render(DrawContext context, int mouseX, int mouseY, float partialTic
}
}

/**
* Draw the current tooltip of the UI managed by this adapter. This method
* must not be called without a previous, corresponding call to {@link #render(DrawContext, int, int, float)}
*
*
* @since 0.12.19
*/
public void drawTooltip(DrawContext context, int mouseX, int mouseY, float partialTicks) {
if (!(context instanceof OwoUIDrawContext)) context = OwoUIDrawContext.of(context);
var owoContext = (OwoUIDrawContext) context;

final var delta = MinecraftClient.getInstance().getRenderTickCounter().getLastFrameDuration();

this.rootComponent.drawTooltip(owoContext, mouseX, mouseY, partialTicks, delta);
}

@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return this.rootComponent.isInBoundingBox(mouseX, mouseY);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/wispforest/owo/ui/layers/Layers.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ public static <S extends Screen, R extends ParentComponent> Layer<S, R> add(BiFu
for (var instance : getInstances(screen)) {
instance.adapter.render(context, mouseX, mouseY, tickDelta);
}

for (var instance : getInstances(screen)) {
instance.adapter.drawTooltip(context, mouseX, mouseY, tickDelta);
}
});

ScreenMouseEvents.allowMouseClick(screeen).register((screen, mouseX, mouseY, button) -> {
Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/owo.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ transitive-accessible class net/minecraft/item/ItemGroup$EntriesImpl
transitive-accessible class net/minecraft/client/gui/DrawContext$ScissorStack
transitive-extendable method net/minecraft/client/gui/widget/CheckboxWidget <init> (IIILnet/minecraft/text/Text;Lnet/minecraft/client/font/TextRenderer;ZLnet/minecraft/client/gui/widget/CheckboxWidget$Callback;)V

accessible class net/minecraft/registry/RegistryOps$CachedRegistryInfoGetter
accessible class net/minecraft/registry/RegistryOps$CachedRegistryInfoGetter

extendable method net/minecraft/client/gui/screen/ingame/HandledScreen drawSlotHighlightBack (Lnet/minecraft/client/gui/DrawContext;)V
extendable method net/minecraft/client/gui/screen/ingame/HandledScreen drawSlotHighlightFront (Lnet/minecraft/client/gui/DrawContext;)V
extendable method net/minecraft/client/gui/screen/ingame/HandledScreen drawItem (Lnet/minecraft/client/gui/DrawContext;Lnet/minecraft/item/ItemStack;IILjava/lang/String;)V

0 comments on commit 771e70a

Please sign in to comment.