Skip to content

Commit

Permalink
UScreen: Fix background being drawn twice on 1.20.2+
Browse files Browse the repository at this point in the history
The contract for UScreen has always been that the user needs to explicitly call
`onDrawBackground` from their `onDrawScreen` override if they want their screen
to have a background; that's how it used to work in Minecraft prior to 1.20.2.

As of 1.20.2, `Screen.render` (called by `onDrawScreen`) now also draws the
background in addition to vanilla elements, as such the background is drawn
twice; and if a custom screen wishes to not have any background, it's draw
anyway.

This commit fixes that by suppressing the vanilla background method call when it
happens within our `super.render` call. That way users retain control over the
background, and in a way that's consistent across versions.

Linear: EM-2544
GitHub: #69
  • Loading branch information
Johni0702 committed May 3, 2024
1 parent d0ab622 commit 7b11dc2
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/gg/essential/universal/UScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class UScreen(
private var guiScaleToRestore = -1
private var restoringGuiScale = false
private val screenToRestore: GuiScreen? = if (restoreCurrentGuiOnClose) currentScreen else null
private var suppressBackground = false

//#if MC>=12000
//$$ private var drawContexts = mutableListOf<DrawContext>()
Expand Down Expand Up @@ -154,6 +155,7 @@ abstract class UScreen(
//$$ lastBackgroundMouseX = mouseX
//$$ lastBackgroundMouseY = mouseY
//$$ lastBackgroundDelta = delta
//$$ if (suppressBackground) return
//#else
//$$ final override fun renderBackground(context: DrawContext) {
//#endif
Expand Down Expand Up @@ -262,6 +264,7 @@ abstract class UScreen(
}

open fun onDrawScreen(matrixStack: UMatrixStack, mouseX: Int, mouseY: Int, partialTicks: Float) {
suppressBackground = true
//#if MC>=12000
//$$ withDrawContext(matrixStack) { drawContext ->
//$$ super.render(drawContext, mouseX, mouseY, partialTicks)
Expand All @@ -277,6 +280,7 @@ abstract class UScreen(
//#endif
}
//#endif
suppressBackground = false
}

@Deprecated(
Expand Down

0 comments on commit 7b11dc2

Please sign in to comment.