Skip to content

Commit a29e154

Browse files
Aeltumnlucyydotp
andauthored
Update to Minecraft 1.21.8 (#31)
* update: Minecraft 1.21.8 * Unfocused dialogs can no longer be hovered * Due to internal changes unfocused dialogs now only show the background and text as transparent, all other elements are rendered normally * Fix focused component not properly updating * Update Gui render injection point --------- Co-authored-by: Lucy Poulton <lucy.poulton@noxcrew.com>
1 parent ac4a85f commit a29e154

File tree

19 files changed

+197
-158
lines changed

19 files changed

+197
-158
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.noxcrew.sheeplib;
2+
3+
/**
4+
* An extension to {@link net.minecraft.client.gui.components.AbstractWidget} to allow for hovering to be disabled.
5+
*/
6+
public interface AbstractWidgetExt {
7+
/**
8+
* Returns whether this widget can be hovered over.
9+
*/
10+
boolean sheeplib$isHoverable();
11+
12+
/**
13+
* Sets whether this widget can be hovered over.
14+
*/
15+
void sheeplib$setHoverable(boolean hoverable);
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.noxcrew.sheeplib;
2+
3+
/**
4+
* An extension to {@link net.minecraft.client.gui.GuiGraphics} to allow overriding text opacity.
5+
*/
6+
public interface GuiGraphicsExt {
7+
/**
8+
* Returns the current override to text opacity.
9+
*/
10+
Float sheeplib$getTextOpacityOverride();
11+
12+
/**
13+
* Sets an override to apply to the text opacity of all submitted text elements.
14+
*/
15+
void sheeplib$setTextOpacityOverride(Float override);
16+
}

api/src/main/java/com/noxcrew/sheeplib/StringWidgetExt.java

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.noxcrew.sheeplib.mixin;
2+
3+
4+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
5+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
6+
import com.noxcrew.sheeplib.AbstractWidgetExt;
7+
import net.minecraft.client.gui.GuiGraphics;
8+
import net.minecraft.client.gui.components.AbstractWidget;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Unique;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
13+
/**
14+
* Disables hover detection on widgets in unfocussed widgets.
15+
*/
16+
@Mixin(AbstractWidget.class)
17+
public class AbstractWidgetMixin implements AbstractWidgetExt {
18+
19+
@Unique
20+
private boolean sheeplib$hoverable = true;
21+
22+
@Override
23+
public boolean sheeplib$isHoverable() {
24+
return sheeplib$hoverable;
25+
}
26+
27+
@Override
28+
public void sheeplib$setHoverable(boolean hoverable) {
29+
this.sheeplib$hoverable = hoverable;
30+
}
31+
32+
/**
33+
* Wraps requests if an element is being hovered to always be false if not hoverable.
34+
*/
35+
@WrapOperation(
36+
method = "render",
37+
at = @At(
38+
value = "INVOKE",
39+
target = "Lnet/minecraft/client/gui/GuiGraphics;containsPointInScissor(II)Z"
40+
)
41+
)
42+
public boolean render(GuiGraphics instance, int i, int j, Operation<Boolean> original) {
43+
return sheeplib$hoverable ? original.call(instance, i, j) : false;
44+
}
45+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.noxcrew.sheeplib.mixin;
2+
3+
4+
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
5+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
6+
import com.noxcrew.sheeplib.GuiGraphicsExt;
7+
import net.minecraft.client.gui.Font;
8+
import net.minecraft.client.gui.GuiGraphics;
9+
import net.minecraft.util.ARGB;
10+
import net.minecraft.util.FormattedCharSequence;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.Unique;
13+
14+
/**
15+
* Modifies string drawing to have reduced opacity if requested.
16+
*/
17+
@Mixin(GuiGraphics.class)
18+
public class GuiGraphicsMixin implements GuiGraphicsExt {
19+
20+
@Unique
21+
private Float sheeplib$textOpacityOverride = null;
22+
23+
@Override
24+
public Float sheeplib$getTextOpacityOverride() {
25+
return sheeplib$textOpacityOverride;
26+
}
27+
28+
@Override
29+
public void sheeplib$setTextOpacityOverride(Float override) {
30+
this.sheeplib$textOpacityOverride = override;
31+
}
32+
33+
/**
34+
* Wraps requests if an element is being hovered to always be false if not hoverable.
35+
*/
36+
@WrapMethod(method = "drawString(Lnet/minecraft/client/gui/Font;Lnet/minecraft/util/FormattedCharSequence;IIIZ)V")
37+
public void drawString(Font font, FormattedCharSequence formattedCharSequence, int i, int j, int k, boolean bl, Operation<Void> original) {
38+
if (sheeplib$textOpacityOverride == null) {
39+
original.call(font, formattedCharSequence, i, j, k, bl);
40+
} else {
41+
var originalAlpha = ARGB.alphaFloat(k);
42+
var newAlpha = originalAlpha * sheeplib$textOpacityOverride;
43+
var newColor = ARGB.color(ARGB.as8BitChannel(newAlpha), k);
44+
original.call(font, formattedCharSequence, i, j, newColor, bl);
45+
}
46+
}
47+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.noxcrew.sheeplib.mixin;
22

3-
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
43
import com.noxcrew.sheeplib.DialogContainer;
4+
import net.minecraft.client.DeltaTracker;
55
import net.minecraft.client.gui.Gui;
6-
import net.minecraft.client.gui.LayeredDraw;
6+
import net.minecraft.client.gui.GuiGraphics;
77
import org.spongepowered.asm.mixin.Mixin;
88
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
911

1012
/**
1113
* Adds the {@link DialogContainer} as a render layer.
1214
*/
1315
@Mixin(Gui.class)
1416
public class GuiMixin {
15-
@ModifyExpressionValue(
16-
method="<init>",
17+
@Inject(
18+
method = "render",
1719
at = @At(
1820
value = "INVOKE",
19-
target = "Lnet/minecraft/client/gui/LayeredDraw;add(Lnet/minecraft/client/gui/LayeredDraw$Layer;)Lnet/minecraft/client/gui/LayeredDraw;",
20-
ordinal = 11
21-
)
22-
)
23-
public LayeredDraw addRenderLayer(LayeredDraw original) {
24-
return original.add(DialogContainer.INSTANCE);
21+
target = "Lnet/minecraft/client/gui/Gui;renderTabList(Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/DeltaTracker;)V"
22+
))
23+
public void render(GuiGraphics graphics, DeltaTracker deltaTracker, CallbackInfo ci) {
24+
DialogContainer.INSTANCE.render(graphics, deltaTracker);
2525
}
26-
2726
}

api/src/main/java/com/noxcrew/sheeplib/mixin/StringWidgetMixin.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

api/src/main/kotlin/com/noxcrew/sheeplib/CompoundWidget.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,15 @@ public abstract class CompoundWidget(x: Int, y: Int, width: Int, height: Int) :
8787
// --
8888
// Mouse
8989
// --
90-
public override fun mouseClicked(d: Double, e: Double, i: Int): Boolean = getChildAt(d, e)
91-
.getOrNull()
92-
?.takeIf { it.mouseClicked(d, e, i) }
93-
?.also { it.isFocused = true } != null
90+
public override fun mouseClicked(d: Double, e: Double, i: Int): Boolean {
91+
if (getChildAt(d, e)
92+
.getOrNull()
93+
?.takeIf { it.mouseClicked(d, e, i) }
94+
?.also { setFocused(it) } != null
95+
) return true
96+
setFocused(null)
97+
return false
98+
}
9499

95100
public override fun mouseReleased(d: Double, e: Double, i: Int): Boolean =
96101
super<ContainerEventHandler>.mouseReleased(d, e, i)

api/src/main/kotlin/com/noxcrew/sheeplib/DialogContainer.kt

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import net.minecraft.ChatFormatting
77
import net.minecraft.client.DeltaTracker
88
import net.minecraft.client.Minecraft
99
import net.minecraft.client.gui.GuiGraphics
10-
import net.minecraft.client.gui.LayeredDraw
1110
import net.minecraft.client.gui.components.Renderable
1211
import net.minecraft.client.gui.components.events.ContainerEventHandler
1312
import net.minecraft.client.gui.components.events.GuiEventListener
@@ -21,7 +20,7 @@ import kotlin.reflect.jvm.jvmName
2120
/**
2221
* The container for all open dialogs.
2322
*/
24-
public object DialogContainer : LayeredDraw.Layer, ContainerEventHandler, NarratableEntry {
23+
public object DialogContainer : ContainerEventHandler, NarratableEntry {
2524

2625
private val minecraft = Minecraft.getInstance()
2726
private val logger = LoggerFactory.getLogger("SheepLib")
@@ -35,23 +34,15 @@ public object DialogContainer : LayeredDraw.Layer, ContainerEventHandler, Narrat
3534
/** Whether the container is currently being dragged. */
3635
private var isDragging: Boolean = false
3736

38-
override fun render(guiGraphics: GuiGraphics, deltaTracker: DeltaTracker) {
37+
/** Renders all opened dialogs. */
38+
public fun render(guiGraphics: GuiGraphics, deltaTracker: DeltaTracker) {
3939
val cursorIsActive = minecraft?.screen is ChatScreen
4040

4141
val childX = if (cursorIsActive) minecraft.mouseHandler.getScaledXPos(minecraft.window) else -1
4242
val childY = if (cursorIsActive) minecraft.mouseHandler.getScaledYPos(minecraft.window) else -1
43-
44-
val children = children.value
45-
46-
// Share the Z space evenly between dialogs..
47-
val zOffsetPerDialog = LayeredDraw.Z_SEPARATION / children.size
48-
49-
guiGraphics.pose().pushPose()
50-
children.forEach {
43+
children.value.forEach {
5144
(it as Renderable).render(guiGraphics, childX.toInt(), childY.toInt(), deltaTracker.gameTimeDeltaTicks)
52-
guiGraphics.pose().translate(0f, 0f, zOffsetPerDialog)
5345
}
54-
guiGraphics.pose().popPose()
5546
}
5647

5748
/** Returns an immutable view of this container's dialog. */
@@ -98,7 +89,6 @@ public object DialogContainer : LayeredDraw.Layer, ContainerEventHandler, Narrat
9889
* @throws IllegalArgumentException if [dialog] is both not null and not in this container
9990
*/
10091
override fun setFocused(dialog: GuiEventListener?) {
101-
10292
if (dialog == null) {
10393
focused?.focused = null
10494
}

api/src/main/kotlin/com/noxcrew/sheeplib/dialog/Dialog.kt

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package com.noxcrew.sheeplib.dialog
22

3-
import com.mojang.blaze3d.systems.RenderSystem
3+
import com.noxcrew.sheeplib.AbstractWidgetExt
44
import com.noxcrew.sheeplib.CompoundWidget
55
import com.noxcrew.sheeplib.DialogContainer
6+
import com.noxcrew.sheeplib.GuiGraphicsExt
67
import com.noxcrew.sheeplib.dialog.title.DialogTitleWidget
78
import com.noxcrew.sheeplib.theme.Theme
89
import com.noxcrew.sheeplib.theme.Themed
910
import net.minecraft.client.Minecraft
1011
import net.minecraft.client.gui.GuiGraphics
1112
import net.minecraft.client.gui.components.AbstractWidget
1213
import net.minecraft.client.gui.layouts.Layout
13-
import net.minecraft.client.renderer.RenderType
14+
import net.minecraft.client.renderer.RenderPipelines
15+
import net.minecraft.util.ARGB
1416
import net.minecraft.util.Mth
1517
import org.jetbrains.annotations.ApiStatus.ScheduledForRemoval
1618
import java.io.Closeable
@@ -176,30 +178,42 @@ public abstract class Dialog(
176178
* [Theme.Colors.border] border.
177179
*/
178180
protected open fun renderBackground(graphics: GuiGraphics) {
181+
val baseColor = if (parent == null) theme.colors.dialogBackgroundAlt else theme.colors.dialogBackground
179182
graphics.fill(
180-
RenderType.gui(),
183+
RenderPipelines.GUI,
181184
x,
182185
y,
183186
x + getWidth(),
184187
y + getHeight(),
185-
if (parent == null) theme.colors.dialogBackgroundAlt else theme.colors.dialogBackground
188+
ARGB.color(if (isPopupFocused()) (255 * POPUP_FOCUSED_OPACITY).toInt() else 255, baseColor)
186189
)
187190
if (theme.dialogBorders) graphics.renderOutline(x, y, getWidth(), getHeight(), theme.colors.border)
188191
}
189192

190193
override fun renderWidget(graphics: GuiGraphics, i: Int, j: Int, f: Float) {
191-
if (isPopupFocused()) {
192-
RenderSystem.setShaderColor(1f, 1f, 1f, POPUP_FOCUSED_OPACITY)
193-
}
194194
renderBackground(graphics)
195-
super.renderWidget(graphics, i, j, f)
196-
RenderSystem.setShaderColor(1f, 1f, 1f, 1f)
197-
popup?.let {
198-
graphics.pose().pushPose()
199-
graphics.pose().translate(0f, 0f, 1f)
200-
it.render(graphics, i, j, f)
201-
graphics.pose().popPose()
195+
196+
// Ensure none of the elements in the child widget can be focused
197+
val focused = isPopupFocused()
198+
for (it in children) {
199+
if (it is AbstractWidgetExt) {
200+
it.`sheeplib$setHoverable`(!focused)
201+
}
202202
}
203+
204+
if (focused) {
205+
// If the pop-up is focused we render the sub-dialog with an opacity override so all text
206+
// is drawn slightly opaque. We cannot draw everything with a reduced opacity but text is
207+
// most common so worth overriding.
208+
val override = (graphics as GuiGraphicsExt).`sheeplib$getTextOpacityOverride`()
209+
(graphics as GuiGraphicsExt).`sheeplib$setTextOpacityOverride`(POPUP_FOCUSED_OPACITY)
210+
super.renderWidget(graphics, i, j, f)
211+
(graphics as GuiGraphicsExt).`sheeplib$setTextOpacityOverride`(override)
212+
} else {
213+
super.renderWidget(graphics, i, j, f)
214+
}
215+
216+
popup?.render(graphics, i, j, f)
203217
}
204218

205219
protected companion object {

0 commit comments

Comments
 (0)