Skip to content

Commit 6414f32

Browse files
committed
1.21.4 port
1 parent 311f3b1 commit 6414f32

File tree

3 files changed

+112
-126
lines changed

3 files changed

+112
-126
lines changed

modern/1.21.4/src/main/kotlin/io/github/notenoughupdates/moulconfig/gui/GuiComponentWrapper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class GuiComponentWrapper(
5151
super.render(drawContext, i, j, f)
5252
val ctx = createContext(drawContext)
5353
context.root.render(ctx)
54-
ctx.renderContext.doDrawTooltip()
54+
ctx.renderContext.renderExtraLayers()
5555
}
5656

5757
override fun charTyped(c: Char, i: Int): Boolean {
Lines changed: 76 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package io.github.notenoughupdates.moulconfig.platform
22

33
import com.mojang.blaze3d.systems.RenderSystem
4-
import io.github.notenoughupdates.moulconfig.common.DynamicTextureReference
5-
import io.github.notenoughupdates.moulconfig.common.IFontRenderer
6-
import io.github.notenoughupdates.moulconfig.common.IItemStack
7-
import io.github.notenoughupdates.moulconfig.common.MyResourceLocation
8-
import io.github.notenoughupdates.moulconfig.common.RenderContext
9-
import io.github.notenoughupdates.moulconfig.common.TextureFilter
4+
import io.github.notenoughupdates.moulconfig.common.*
5+
import io.github.notenoughupdates.moulconfig.internal.FilterAssertionCache
6+
import io.github.notenoughupdates.moulconfig.internal.Warnings
107
import net.minecraft.client.MinecraftClient
118
import net.minecraft.client.gl.ShaderProgramKeys
129
import net.minecraft.client.gui.DrawContext
@@ -15,21 +12,17 @@ import net.minecraft.client.render.RenderLayer
1512
import net.minecraft.client.render.RenderPhase
1613
import net.minecraft.client.render.VertexFormat
1714
import net.minecraft.client.render.VertexFormats
18-
import net.minecraft.client.texture.NativeImageBackedTexture
1915
import net.minecraft.client.util.InputUtil
2016
import net.minecraft.text.Text
21-
import net.minecraft.util.Identifier
2217
import org.joml.Matrix4f
2318
import org.lwjgl.glfw.GLFW
24-
import org.lwjgl.opengl.GL11
25-
import java.awt.image.BufferedImage
26-
import java.util.concurrent.ThreadLocalRandom
19+
import java.util.function.Consumer
2720

2821
class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
2922
companion object {
3023
val RL = RenderLayer.of(
3124
"moulconfig_inverted_rect",
32-
VertexFormats.POSITION,
25+
VertexFormats.POSITION_COLOR,
3326
VertexFormat.DrawMode.QUADS,
3427
RenderLayer.DEFAULT_BUFFER_SIZE,
3528
false,
@@ -58,53 +51,8 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
5851

5952
val mouse = MinecraftClient.getInstance().mouse
6053
val window = MinecraftClient.getInstance().window
61-
override fun disableDepth() {
62-
RenderSystem.disableDepthTest()
63-
}
64-
65-
override fun enableDepth() {
66-
RenderSystem.enableDepthTest()
67-
}
68-
69-
fun NativeImageBackedTexture.setData(img: BufferedImage) {
70-
for (i in (0 until img.width)) {
71-
for (j in (0 until img.height)) {
72-
val argb = img.getRGB(i, j)
73-
image!!.setColorArgb(i, j, argb)
74-
}
75-
}
76-
}
77-
78-
override fun generateDynamicTexture(img: BufferedImage): DynamicTextureReference {
79-
val texture = NativeImageBackedTexture(img.width, img.height, true)
80-
texture.setData(img)
81-
texture.upload()
82-
val id = Identifier.of("moulconfig", "dynamic/${ThreadLocalRandom.current().nextLong()}")
83-
MinecraftClient.getInstance().textureManager.registerTexture(id, texture)
84-
return object : DynamicTextureReference() {
85-
override fun update(bufferedImage: BufferedImage) {
86-
texture.setData(img)
87-
texture.upload()
88-
}
89-
90-
override val identifier: MyResourceLocation
91-
get() = MoulConfigPlatform.fromIdentifier(id)
92-
93-
override fun doDestroy() {
94-
MinecraftClient.getInstance().textureManager.destroyTexture(id)
95-
}
96-
}
97-
}
9854

9955

100-
override fun refreshScissor() {
101-
drawContext.setScissor(drawContext.scissorStack.stack.peekLast())
102-
}
103-
104-
override fun disableScissor() {
105-
drawContext.setScissor(null)
106-
}
107-
10856
override fun pushMatrix() {
10957
drawContext.matrices.push()
11058
}
@@ -113,24 +61,12 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
11361
drawContext.matrices.pop()
11462
}
11563

116-
override fun translate(x: Float, y: Float, z: Float) {
117-
drawContext.matrices.translate(x, y, z)
118-
}
119-
120-
override fun scale(x: Float, y: Float, z: Float) {
121-
drawContext.matrices.scale(x, y, z)
64+
override fun translate(x: Float, y: Float) {
65+
drawContext.matrices.translate(x, y, 0F)
12266
}
12367

124-
var tintR = 1F
125-
var tintG = 1F
126-
var tintB = 1F
127-
var tintA = 1F
128-
129-
override fun color(r: Float, g: Float, b: Float, a: Float) {
130-
tintR = r
131-
tintG = g
132-
tintB = b
133-
tintA = a
68+
override fun scale(x: Float, y: Float) {
69+
drawContext.matrices.scale(x, y, 1F)
13470
}
13571

13672
override fun isMouseButtonDown(mouseButton: Int): Boolean {
@@ -141,8 +77,7 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
14177
return InputUtil.isKeyPressed(window.handle, keyboardKey)
14278
}
14379

144-
145-
override fun drawTriangles(vararg coordinates: Float) {
80+
override fun drawColoredTriangles(color: Int, vararg coordinates: Float) {
14681
require(coordinates.size % 6 == 0)
14782
RenderSystem.enableBlend()
14883
drawContext.draw {
@@ -151,7 +86,7 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
15186

15287
for (i in 0 until (coordinates.size / 2)) {
15388
buf.vertex(matrix, coordinates[i * 2], coordinates[i * 2 + 1], 0.0F)
154-
.color(tintA, tintR, tintG, tintB).next()
89+
.color(color).next()
15590
}
15691
}
15792
RenderSystem.disableBlend()
@@ -164,56 +99,54 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
16499
y: Int,
165100
color: Int,
166101
shadow: Boolean
167-
): Int {
168-
return drawContext.drawText((fontRenderer as ModernFontRenderer).textRenderer, text, x, y, color, shadow)
102+
) {
103+
drawContext.drawText((fontRenderer as ModernFontRenderer).textRenderer, text, x, y, color, shadow)
169104
}
170105

171106
override fun drawColoredRect(left: Float, top: Float, right: Float, bottom: Float, color: Int) {
172107
drawContext.fill(left.toInt(), top.toInt(), right.toInt(), bottom.toInt(), color)
173108
}
174109

175-
override fun invertedRect(left: Float, top: Float, right: Float, bottom: Float) {
110+
override fun invertedRect(left: Float, top: Float, right: Float, bottom: Float, additiveColor: Int) {
176111
drawContext.draw {
177112
val matrix = drawContext.matrices.peek().positionMatrix
178113
val buffer = it.getBuffer(RL)
179-
buffer.vertex(matrix, left, bottom, 0F).next()
180-
buffer.vertex(matrix, right, bottom, 0F).next()
181-
buffer.vertex(matrix, right, top, 0F).next()
182-
buffer.vertex(matrix, left, top, 0F).next()
183-
}
184-
}
185-
186-
override fun setTextureMinMagFilter(textureFilter: TextureFilter) {
187-
// TODO bind texture first
188-
val filter = when (textureFilter) {
189-
TextureFilter.LINEAR -> GL11.GL_LINEAR
190-
TextureFilter.NEAREST -> GL11.GL_NEAREST
114+
buffer.vertex(matrix, left, bottom, 0F).color(additiveColor).next()
115+
buffer.vertex(matrix, right, bottom, 0F).color(additiveColor).next()
116+
buffer.vertex(matrix, right, top, 0F).color(additiveColor).next()
117+
buffer.vertex(matrix, left, top, 0F).color(additiveColor).next()
191118
}
192-
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, filter)
193-
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, filter)
194119
}
195120

196-
override fun drawTexturedRect(
197-
x: Float,
198-
y: Float,
199-
width: Float,
200-
height: Float,
201-
u1: Float,
202-
v1: Float,
203-
u2: Float,
204-
v2: Float
121+
override fun drawTexturedTintedRect(
122+
texture: MyResourceLocation,
123+
x: Float, y: Float,
124+
width: Float, height: Float,
125+
u1: Float, v1: Float, u2: Float, v2: Float,
126+
color: Int, filter: TextureFilter,
205127
) {
128+
FilterAssertionCache.assertTextureFilter(texture, filter)
206129
drawContext.draw {
130+
MinecraftClient.getInstance()
131+
.textureManager
132+
.getTexture(MoulConfigPlatform.fromMyResourceLocation(texture))
133+
.setFilter(
134+
when (filter) {
135+
TextureFilter.LINEAR -> true
136+
TextureFilter.NEAREST -> false
137+
},
138+
false
139+
)
207140
val matrix4f: Matrix4f = drawContext.matrices.peek().positionMatrix
208-
val bufferBuilder = it.getBuffer(RenderLayer.getGuiTextured(MoulConfigPlatform.boundTexture!!))
141+
val bufferBuilder = it.getBuffer(RenderLayer.getGuiTextured(MoulConfigPlatform.fromMyResourceLocation(texture)))
209142
bufferBuilder.vertex(matrix4f, x, y, 0F).texture(u1, v1)
210-
.color(tintA, tintG, tintB, tintA).next()
143+
.color(color).next()
211144
bufferBuilder.vertex(matrix4f, x, y + height, 0f).texture(u1, v2)
212-
.color(tintA, tintG, tintB, tintA).next()
145+
.color(color).next()
213146
bufferBuilder.vertex(matrix4f, x + width, y + height, 0f).texture(u2, v2)
214-
.color(tintA, tintG, tintB, tintA).next()
147+
.color(color).next()
215148
bufferBuilder.vertex(matrix4f, x + width, y, 0F).texture(u2, v1)
216-
.color(tintA, tintG, tintB, tintA).next()
149+
.color(color).next()
217150
}
218151
}
219152

@@ -235,15 +168,14 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
235168
}
236169

237170
override fun drawGradientRect(
238-
zLevel: Int,
239171
left: Int,
240172
top: Int,
241173
right: Int,
242174
bottom: Int,
243175
startColor: Int,
244176
endColor: Int
245177
) {
246-
drawContext.fillGradient(RenderLayer.getGui(), left, top, right, bottom, startColor, endColor, zLevel)
178+
drawContext.fillGradient(RenderLayer.getGui(), left, top, right, bottom, startColor, endColor, 0)
247179
}
248180

249181
override fun pushScissor(left: Int, top: Int, right: Int, bottom: Int) {
@@ -253,6 +185,21 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
253185
refreshScissor()
254186
}
255187

188+
override fun assertNoScissors() {
189+
if (!drawContext.scissorStack.stack.isEmpty()) {
190+
Warnings.warn("Scissors found despite no scissor assertion", 4)
191+
}
192+
}
193+
194+
override fun pushRawScissor(left: Int, top: Int, right: Int, bottom: Int) {
195+
drawContext.scissorStack.stack.addLast(ScreenRect(left, top, right - left, bottom - top))
196+
refreshScissor()
197+
}
198+
199+
fun refreshScissor() {
200+
drawContext.setScissor(drawContext.scissorStack.stack.peekLast())
201+
}
202+
256203
override fun popScissor() {
257204
drawContext.disableScissor()
258205
}
@@ -274,22 +221,29 @@ class ModernRenderContext(val drawContext: DrawContext) : RenderContext {
274221
)
275222
}
276223

277-
var scheduledTooltip: List<String>? = null
224+
override fun drawTooltipNow(x: Int, y: Int, tooltipLines: List<String>) {
225+
drawContext.drawTooltip(
226+
MinecraftClient.getInstance().textRenderer,
227+
tooltipLines.map { Text.literal(it) },
228+
x,
229+
y
230+
)
231+
}
278232

279-
override fun scheduleDrawTooltip(tooltipLines: MutableList<String>) {
280-
scheduledTooltip = tooltipLines
233+
override fun renderExtraLayers() {
234+
// Left blank: [drawOnTop] renders directly.
281235
}
282236

283-
override fun doDrawTooltip() {
284-
if (scheduledTooltip != null) {
285-
drawContext.drawTooltip(
286-
MinecraftClient.getInstance().textRenderer,
287-
scheduledTooltip!!.map { Text.literal(it) },
288-
// TODO: improve this somewhat
289-
(MinecraftClient.getInstance().mouse.x / MinecraftClient.getInstance().window.scaleFactor).toInt(),
290-
(MinecraftClient.getInstance().mouse.y / MinecraftClient.getInstance().window.scaleFactor).toInt(),
291-
)
237+
override fun drawOnTop(layer: Layer, scissorBehaviour: RenderContext.ScissorBehaviour, later: Consumer<RenderContext>) {
238+
pushMatrix()
239+
if (scissorBehaviour == RenderContext.ScissorBehaviour.ESCAPE) {
240+
pushRawScissor(0, 0, minecraft.scaledWidth, minecraft.scaledHeight)
292241
}
242+
drawContext.matrices.translate(0F, 0F, layer.sortIndex.toFloat())
243+
later.accept(this)
244+
if (scissorBehaviour == RenderContext.ScissorBehaviour.ESCAPE) {
245+
popScissor()
246+
}
247+
popMatrix()
293248
}
294-
295249
}

modern/1.21.4/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/MoulConfigPlatform.kt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.notenoughupdates.moulconfig.platform
22

33
import io.github.notenoughupdates.moulconfig.common.ClickType
4+
import io.github.notenoughupdates.moulconfig.common.DynamicTextureReference
45
import io.github.notenoughupdates.moulconfig.common.IFontRenderer
56
import io.github.notenoughupdates.moulconfig.common.IKeyboardConstants
67
import io.github.notenoughupdates.moulconfig.common.IMinecraft
@@ -16,13 +17,16 @@ import net.fabricmc.loader.api.FabricLoader
1617
import net.minecraft.client.MinecraftClient
1718
import net.minecraft.client.gui.DrawContext
1819
import net.minecraft.client.gui.screen.Screen
20+
import net.minecraft.client.texture.NativeImageBackedTexture
1921
import net.minecraft.client.util.InputUtil
2022
import net.minecraft.text.ClickEvent
2123
import net.minecraft.text.Text
2224
import net.minecraft.util.Identifier
2325
import org.apache.logging.log4j.LogManager
2426
import org.lwjgl.glfw.GLFW
27+
import java.awt.image.BufferedImage
2528
import java.io.InputStream
29+
import java.util.concurrent.ThreadLocalRandom
2630

2731

2832
class MoulConfigPlatform : IMinecraft {
@@ -37,7 +41,6 @@ class MoulConfigPlatform : IMinecraft {
3741

3842
lateinit var instance: MoulConfigPlatform
3943
private set
40-
var boundTexture: Identifier? = null
4144
fun fromIdentifier(identifier: Identifier): MyResourceLocation {
4245
return MyResourceLocation(identifier.namespace, identifier.path)
4346
}
@@ -49,9 +52,38 @@ class MoulConfigPlatform : IMinecraft {
4952

5053
override val isDevelopmentEnvironment: Boolean
5154
get() = FabricLoader.getInstance().isDevelopmentEnvironment
55+
fun NativeImageBackedTexture.setData(img: BufferedImage) {
56+
for (i in (0 until img.width)) {
57+
for (j in (0 until img.height)) {
58+
val argb = img.getRGB(i, j)
59+
image!!.setColorArgb(i, j, argb)
60+
}
61+
}
62+
}
63+
64+
override fun generateDynamicTexture(img: BufferedImage): DynamicTextureReference {
65+
val texture = NativeImageBackedTexture(img.width, img.height, true)
66+
texture.setData(img)
67+
texture.upload()
68+
val id = Identifier.of("moulconfig", "dynamic/${ThreadLocalRandom.current().nextLong()}")
69+
MinecraftClient.getInstance().textureManager.registerTexture(id, texture)
70+
return object : DynamicTextureReference() {
71+
override fun update(bufferedImage: BufferedImage) {
72+
texture.setData(img)
73+
texture.upload()
74+
}
75+
76+
override val identifier: MyResourceLocation
77+
get() = MoulConfigPlatform.fromIdentifier(id)
78+
79+
override fun doDestroy() {
80+
MinecraftClient.getInstance().textureManager.destroyTexture(id)
81+
}
82+
}
83+
}
5284

53-
override fun bindTexture(resourceLocation: MyResourceLocation) {
54-
boundTexture = fromMyResourceLocation(resourceLocation)
85+
override fun isGeneratedSentinel(resourceLocation: MyResourceLocation): Boolean {
86+
return resourceLocation.root == "moulconfig" && resourceLocation.path.startsWith("dynamic/")
5587
}
5688

5789
override fun getLogger(label: String): MCLogger {

0 commit comments

Comments
 (0)