Skip to content

Commit 568d10f

Browse files
committed
Cleanup z level hacks for rendering mekanism guis as at some point vanilla switched from a range of [-1,000, 1,000] to [-10,000, 10,000]
1 parent e01b6ad commit 568d10f

File tree

6 files changed

+30
-54
lines changed

6 files changed

+30
-54
lines changed

src/main/java/mekanism/client/gui/GuiMekanism.java

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public abstract class GuiMekanism<CONTAINER extends AbstractContainerMenu> exten
5353
public static final ResourceLocation BASE_BACKGROUND = MekanismUtils.getResource(ResourceType.GUI, "base.png");
5454
public static final ResourceLocation SHADOW = MekanismUtils.getResource(ResourceType.GUI, "shadow.png");
5555
public static final ResourceLocation BLUR = MekanismUtils.getResource(ResourceType.GUI, "blur.png");
56-
private static final int EXTRA_Z_OFFSET = -500;
5756
//TODO: Look into defaulting this to true
5857
protected boolean dynamicSlots;
5958
protected boolean initialFocusSet;
@@ -324,11 +323,10 @@ record PreviousElement(int index, GuiElement element, boolean wasFocus) {
324323

325324
@Override
326325
protected void renderLabels(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY) {
327-
//Note: We intentionally don't push the modelViewStack, see notes further down in this method for more details
328-
//TODO - 1.20: Figure this out as some transforms and hacks may be unnecessary now
329326
PoseStack pose = guiGraphics.pose();
330-
//TODO - 1.20: Re-evaluate?? this always was against the pose but what is it for
331-
pose.translate(0, 0, 300);
327+
pose.pushPose();
328+
//Shift forward as far as tooltips get shifted so that we don't risk intersecting the rendered items
329+
pose.translate(0, 0, 400);
332330
for (GuiEventListener c : children()) {
333331
if (c instanceof GuiElement element) {
334332
element.onDrawBackground(guiGraphics, mouseX, mouseY, MekanismRenderer.getPartialTick());
@@ -363,50 +361,44 @@ protected void renderLabels(@NotNull GuiGraphics guiGraphics, int mouseX, int mo
363361
}
364362
pose.popPose();
365363
}
364+
pose.popPose();
366365
// then render tooltips, translating above max z offset to prevent clashing
366+
// It is IMPORTANT that we do this to ensure any delayed rendering we do the for the tooltip happens above the other things
367+
// and so that we let the translation leak out into the super method so that the carried item renders at the correct z level
368+
pose.translate(0, 0, maxZOffset);
369+
370+
pose.pushPose();
371+
//Note: Because we are doing this from renderLabels instead of as part of a render override,
372+
// we need to unshift back to the position the other methods expect to be called from
373+
pose.translate(-leftPos, -topPos, 0);
367374
GuiElement tooltipElement = getWindowHovering(mouseX, mouseY);
368375
if (tooltipElement == null) {
369376
tooltipElement = (GuiElement) GuiUtils.findChild(children(), mouseX, mouseY, (child, x, y) -> child instanceof GuiElement && child.isMouseOver(x, y));
370377
}
371-
372-
// translate forwards using RenderSystem. this should never have to happen as we do all the necessary translations with MatrixStacks,
373-
// but Minecraft has decided to not fully adopt MatrixStacks for many crucial ContainerScreen render operations. should be re-evaluated
374-
// when mc updates related logic on their end (IMPORTANT)
375-
//TODO - 1.20: Is this necessary used to occur on the model view stack
376-
pose.translate(0, 0, maxZOffset);
377-
378-
// render tooltips
379-
//TODO - 1.20: Can we remove these transforms that are around the tooltip rendering
380-
// No, we maybe could remove from the tooltip element portion but definitely not from the stack rendering/parent renderTooltip method
381-
pose.translate(-leftPos, -topPos, 0);
382378
if (tooltipElement != null) {
383379
tooltipElement.renderToolTip(guiGraphics, mouseX, mouseY);
384380
}
385381
renderTooltip(guiGraphics, mouseX, mouseY);
386-
pose.translate(leftPos, topPos, 0);
387-
388-
// IMPORTANT: additional hacky translation so held items render okay. re-evaluate as discussed above
389-
// Note: It is important that we don't wrap our adjustments to the modelViewStack in so that we can
390-
// have the adjustments to the z-value persist into the vanilla methods
391-
//TODO - 1.20: Is this necessary (used to happen to the model view stack)
392-
pose.translate(0, 0, 200);
382+
pose.popPose();
393383
}
394384

395385
/**
396386
* @implNote Copy of super, but adjusts the z value for tooltip rendering
397387
*/
398388
@Override
399389
public final void renderWithTooltip(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float partialTick) {
390+
//Note: We wrap super with a push and pop, so that when we intentionally don't pop our changes in renderLabels
391+
// then we make sure to clean them up here
392+
PoseStack pose = graphics.pose();
393+
pose.pushPose();
400394
render(graphics, mouseX, mouseY, partialTick);
401395
if (deferredTooltipRendering != null) {
402-
PoseStack pose = graphics.pose();
403-
pose.pushPose();
404-
//Note: extra z offset comes from the offset we do in render
405-
pose.translate(0, 0, EXTRA_Z_OFFSET + maxZOffset);
396+
//Note: render has a pop at the end of it in vanilla, so we have to apply the deferred tooltip rendering again
397+
pose.translate(0, 0, maxZOffset);
406398
graphics.renderTooltip(font, deferredTooltipRendering.tooltip(), deferredTooltipRendering.positioner(), mouseX, mouseY);
407399
clearTooltipForNextRenderPass();
408-
pose.popPose();
409400
}
401+
pose.popPose();
410402
}
411403

412404
protected void drawForegroundText(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY) {
@@ -690,19 +682,6 @@ public Font getFont() {
690682
return font == null ? minecraft.font : font;
691683
}
692684

693-
//TODO - 1.20: Test this and everything else relating to the switch to guigraphics
694-
// My guess is we may be able to remove the model view stack hacks??
695-
@Override
696-
public void render(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
697-
PoseStack pose = guiGraphics.pose();
698-
pose.pushPose();
699-
// shift back a whole lot so we can stack more windows
700-
//TODO - 1.20: Validate this, used to translate the modelViewStack
701-
pose.translate(0, 0, EXTRA_Z_OFFSET);
702-
super.render(guiGraphics, mouseX, mouseY, partialTicks);
703-
pose.popPose();
704-
}
705-
706685
@Override
707686
public boolean currentlyQuickCrafting() {
708687
return isQuickCrafting && !quickCraftSlots.isEmpty();

src/main/java/mekanism/client/gui/element/GuiArrowSelection.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package mekanism.client.gui.element;
22

3+
import com.mojang.blaze3d.vertex.PoseStack;
34
import java.util.function.Supplier;
45
import mekanism.client.gui.GuiUtils;
56
import mekanism.client.gui.IGuiWrapper;
@@ -34,8 +35,13 @@ public void renderToolTip(@NotNull GuiGraphics guiGraphics, int mouseX, int mous
3435
if (component != null) {
3536
int tooltipX = mouseX + 5;
3637
int tooltipY = mouseY - 5;
38+
PoseStack pose = guiGraphics.pose();
39+
pose.pushPose();
40+
//Mirror vanilla's tooltip rendering offset
41+
pose.translate(0, 0, 400);
3742
GuiUtils.renderBackgroundTexture(guiGraphics, GuiInnerScreen.SCREEN, GuiInnerScreen.SCREEN_SIZE, GuiInnerScreen.SCREEN_SIZE, tooltipX - 3, tooltipY - 4, getStringWidth(component) + 6, 16, 256, 256);
3843
drawString(guiGraphics, component, tooltipX, tooltipY, screenTextColor());
44+
pose.popPose();
3945
}
4046
}
4147

src/main/java/mekanism/client/gui/element/GuiDropdown.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package mekanism.client.gui.element;
22

3-
import com.mojang.blaze3d.vertex.PoseStack;
43
import java.util.EnumMap;
54
import java.util.Map;
65
import java.util.function.Consumer;
@@ -72,11 +71,6 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
7271
@Override
7372
public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
7473
super.drawBackground(guiGraphics, mouseX, mouseY, partialTicks);
75-
PoseStack pose = guiGraphics.pose();
76-
pose.pushPose();
77-
//TODO: Figure out why we need a translation of 1 to fix the text intersecting for the dictionary but it works just fine
78-
// for the QIO item viewer
79-
pose.translate(0, 0, 1);
8074
renderBackgroundTexture(guiGraphics, getResource(), GuiInnerScreen.SCREEN_SIZE, GuiInnerScreen.SCREEN_SIZE);
8175

8276
int index = getHoveredIndex(mouseX, mouseY);
@@ -97,7 +91,6 @@ public void drawBackground(@NotNull GuiGraphics guiGraphics, int mouseX, int mou
9791
}
9892
}
9993
}
100-
pose.popPose();
10194
}
10295

10396
@NotNull

src/main/java/mekanism/client/gui/element/GuiElement.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,9 @@ public final void onDrawBackground(@NotNull GuiGraphics guiGraphics, int mouseX,
571571
public final void renderShifted(@NotNull GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
572572
//Copy of super.render, except doesn't update the tooltip for the next render pass, as we handle that via renderTooltip
573573
if (this.visible) {
574-
this.isHovered = mouseX >= this.getX() && mouseY >= this.getY() && mouseX < this.getX() + this.width && mouseY < this.getY() + this.height;
574+
//TODO - 1.21: Do we need to add support for guiGraphics.containsPointInScissor(mouseX, mouseY) to more places where we do adhoc mouse over checks?
575+
this.isHovered = guiGraphics.containsPointInScissor(mouseX, mouseY) && mouseX >= this.getX() && mouseY >= this.getY() &&
576+
mouseX < this.getX() + this.width && mouseY < this.getY() + this.height;
575577
renderWidget(guiGraphics, mouseX, mouseY, partialTicks);
576578
}
577579
}

src/main/java/mekanism/client/gui/element/text/GuiTextField.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public GuiTextField setPasteTransformer(UnaryOperator<String> pasteTransformer)
142142

143143
public GuiTextField setBackground(BackgroundType backgroundType) {
144144
this.backgroundType = backgroundType;
145+
this.textField.setBordered(backgroundType == BackgroundType.DEFAULT);
145146
return this;
146147
}
147148

@@ -325,10 +326,6 @@ public void setTextColorUneditable(int color) {
325326
textField.setTextColorUneditable(color);
326327
}
327328

328-
public void setBordered(boolean bordered) {
329-
textField.setBordered(bordered);
330-
}
331-
332329
public void setEditable(boolean enabled) {
333330
textField.setEditable(enabled);
334331
}

src/main/java/mekanism/client/gui/robit/GuiRobitRepair.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ protected void addGuiElements() {
4242
itemNameField.setCanLoseFocus(false);
4343
itemNameField.setTextColor(-1);
4444
itemNameField.setTextColorUneditable(-1);
45-
itemNameField.setBordered(false);
4645
itemNameField.setBackground(BackgroundType.NONE);
4746
itemNameField.setMaxLength(50);
4847
itemNameField.setResponder(this::onNameChanged);

0 commit comments

Comments
 (0)