Skip to content

Commit e768dce

Browse files
committed
Rect impl, Scissor utils, refactor
1 parent b268015 commit e768dce

File tree

8 files changed

+127
-25
lines changed

8 files changed

+127
-25
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package com.lambda.event.events
22

3+
import com.lambda.Lambda.mc
34
import com.lambda.event.Event
45
import com.lambda.event.callback.Cancellable
56
import com.lambda.event.callback.ICancellable
7+
import com.lambda.util.math.Vec2d
68

79
abstract class RenderEvent : Event {
810
class World : RenderEvent()
911

1012
abstract class GUI(val scaleFactor: Double) : RenderEvent() {
1113
class Scaled(scaleFactor: Double) : GUI(scaleFactor)
1214
class Fixed : GUI(1.0)
15+
16+
val screenSize = Vec2d(mc.window.framebufferWidth, mc.window.framebufferHeight) / scaleFactor
1317
}
1418
class UpdateTarget : RenderEvent(), ICancellable by Cancellable()
1519
}

common/src/main/kotlin/com/lambda/graphics/RenderMain.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
package com.lambda.graphics
22

33
import com.lambda.Lambda.mc
4-
import com.lambda.event.EventFlow
54
import com.lambda.event.EventFlow.post
65
import com.lambda.event.events.RenderEvent
76
import com.lambda.graphics.gl.GlStateUtils.setupGL
87
import com.lambda.graphics.gl.Matrices
98
import com.lambda.graphics.gl.Matrices.resetMatrix
109
import com.lambda.graphics.gl.Matrices.translate
1110
import com.lambda.module.modules.client.HUD
12-
import com.lambda.util.math.Vec2d
13-
import net.minecraft.client.util.math.MatrixStack
1411
import org.joml.Matrix4f
1512

1613
object RenderMain {
1714
val projectionMatrix = Matrix4f()
1815
val modelViewMatrix: Matrix4f get() = Matrices.stack.peek().positionMatrix
1916

20-
var screenSize = Vec2d.ONE
21-
2217
@JvmStatic
2318
fun render2D() {
2419
resetMatrix()
@@ -39,7 +34,6 @@ object RenderMain {
3934

4035
val scaledWidth = width / factor
4136
val scaledHeight = height / factor
42-
screenSize = Vec2d(scaledWidth, scaledHeight)
4337

4438
projectionMatrix.setOrtho(0f, scaledWidth.toFloat(), scaledHeight.toFloat(), 0f, 1000f, 21000f)
4539
}

common/src/main/kotlin/com/lambda/graphics/buffer/vao/VAO.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ package com.lambda.graphics.buffer.vao
22

33
import com.lambda.graphics.buffer.vao.vertex.VertexAttrib
44
import com.lambda.graphics.buffer.vao.vertex.VertexMode
5-
import com.lambda.graphics.gl.MemoryUtils.address
6-
import com.lambda.graphics.gl.MemoryUtils.byteBuffer
7-
import com.lambda.graphics.gl.MemoryUtils.capacity
8-
import com.lambda.graphics.gl.MemoryUtils.color
9-
import com.lambda.graphics.gl.MemoryUtils.copy
10-
import com.lambda.graphics.gl.MemoryUtils.int
11-
import com.lambda.graphics.gl.MemoryUtils.vec2
12-
import com.lambda.graphics.gl.MemoryUtils.vec3
5+
import com.lambda.graphics.gl.Memory.address
6+
import com.lambda.graphics.gl.Memory.byteBuffer
7+
import com.lambda.graphics.gl.Memory.capacity
8+
import com.lambda.graphics.gl.Memory.color
9+
import com.lambda.graphics.gl.Memory.copy
10+
import com.lambda.graphics.gl.Memory.int
11+
import com.lambda.graphics.gl.Memory.vec2
12+
import com.lambda.graphics.gl.Memory.vec3
1313
import com.lambda.graphics.gl.VaoUtils
1414
import com.lambda.graphics.gl.VaoUtils.bindIndexBuffer
1515
import com.lambda.graphics.gl.VaoUtils.bindVertexArray

common/src/main/kotlin/com/lambda/graphics/gl/MemoryUtils.kt renamed to common/src/main/kotlin/com/lambda/graphics/gl/Memory.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import java.awt.Color
77
import java.nio.Buffer
88
import java.nio.ByteBuffer
99

10-
object MemoryUtils {
10+
object Memory {
1111
private val vec2Size = VertexAttrib.Vec2.size
1212
private val vec3Size = VertexAttrib.Vec3.size
1313
private val colorSize = VertexAttrib.Color.size
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.lambda.graphics.gl
2+
3+
import com.lambda.Lambda.mc
4+
import com.lambda.module.modules.client.HUD
5+
import com.lambda.util.math.MathUtils.ceilToInt
6+
import com.lambda.util.math.MathUtils.floorToInt
7+
import com.lambda.util.math.Rect
8+
import com.lambda.util.math.Vec2d
9+
import com.mojang.blaze3d.systems.RenderSystem.disableScissor
10+
import com.mojang.blaze3d.systems.RenderSystem.enableScissor
11+
import kotlin.math.max
12+
import kotlin.math.min
13+
14+
object Scissor {
15+
private var stack = ArrayDeque<Entry>()
16+
17+
fun scissor(rect: Rect, block: () -> Unit) = scissor(rect.leftTop, rect.rightBottom, block)
18+
19+
fun scissor(pos1: Vec2d, pos2: Vec2d, block: () -> Unit) {
20+
stack.lastOrNull()?.let {
21+
registerScissor(
22+
// clamp corners so children scissor box can't overlap parent
23+
Vec2d(max(pos1.x, it.pos1.x), max(pos1.y, it.pos1.y)),
24+
Vec2d(min(pos2.x, it.pos2.x), min(pos2.y, it.pos2.y)),
25+
block
26+
)
27+
} ?: registerScissor(pos1, pos2, block)
28+
}
29+
30+
private fun registerScissor(pos1: Vec2d, pos2: Vec2d, block: () -> Unit) {
31+
scissor(Entry(pos1, pos2))
32+
33+
block()
34+
35+
stack.removeLast()
36+
stack.lastOrNull().apply(::scissor)
37+
}
38+
39+
private fun scissor(entry: Entry?) {
40+
if (entry == null) {
41+
disableScissor()
42+
return
43+
}
44+
45+
stack.add(entry)
46+
47+
val pos1 = entry.pos1 * HUD.scale
48+
val pos2 = entry.pos2 * HUD.scale
49+
50+
val width = max(pos2.x - pos1.x, 0.0)
51+
val height = max(pos2.y - pos1.y, 0.0)
52+
53+
val y = mc.window.framebufferHeight - pos1.y - height
54+
55+
enableScissor(
56+
pos1.x.floorToInt(),
57+
y.floorToInt(),
58+
width.ceilToInt(),
59+
height.ceilToInt()
60+
)
61+
}
62+
63+
private data class Entry(val pos1: Vec2d, val pos2: Vec2d)
64+
}

common/src/main/kotlin/com/lambda/graphics/renderer/gui/rect/RectEntry.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ package com.lambda.graphics.renderer.gui.rect
22

33
import com.lambda.graphics.buffer.vao.IRenderContext
44
import com.lambda.graphics.renderer.IRenderEntry
5-
import com.lambda.util.math.Vec2d
5+
import com.lambda.util.math.Rect
66
import java.awt.Color
77
import kotlin.math.min
88

99
class RectEntry(
1010
override val owner: RectRenderer,
1111
override val updateBlock: IRectEntry.() -> Unit
1212
) : IRectEntry {
13-
override var position by owner.field(Vec2d.ZERO to Vec2d.ZERO)
13+
override var position by owner.field(Rect.ZERO)
1414

1515
override var roundRadius by owner.field(0.0)
1616

@@ -33,16 +33,16 @@ class RectEntry(
3333
leftBottom .alpha < MIN_ALPHA
3434
) return@use
3535

36-
val pos1 = position.first
37-
val pos2 = position.second
36+
val pos1 = position.leftTop
37+
val pos2 = position.rightBottom
3838

3939
val size = pos2 - pos1
4040
if (size.x < MIN_SIZE || size.y < MIN_SIZE) return@use
4141

4242
val halfSize = size * 0.5
43-
val minSize = min(halfSize.x, halfSize.y)
43+
val maxRadius = min(halfSize.x, halfSize.y)
4444

45-
val round = min(roundRadius, minSize)
45+
val round = min(roundRadius, maxRadius)
4646

4747
val p1 = pos1 - 0.5
4848
val p2 = pos2 + 0.5
@@ -64,10 +64,7 @@ class RectEntry(
6464
}
6565

6666
interface IRectEntry : IRenderEntry<IRectEntry> {
67-
var position: Pair<Vec2d, Vec2d>
68-
69-
val size get() = position.second - position.first
70-
val center get() = position.first + size * 0.5
67+
var position: Rect
7168

7269
var roundRadius: Double
7370

common/src/main/kotlin/com/lambda/util/Mouse.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Mouse {
1010
val Right = Button(GLFW.GLFW_MOUSE_BUTTON_RIGHT)
1111
val Middle = Button(GLFW.GLFW_MOUSE_BUTTON_MIDDLE)
1212
}
13+
14+
val isMainButton get() = key == GLFW.GLFW_MOUSE_BUTTON_LEFT || key == GLFW.GLFW_MOUSE_BUTTON_RIGHT
1315
}
1416

1517
enum class Action {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.lambda.util.math
2+
3+
import com.lambda.util.math.MathUtils.lerp
4+
5+
data class Rect(private val pos1: Vec2d, private val pos2: Vec2d) {
6+
constructor(size: Vec2d) : this(Vec2d.ZERO, size)
7+
8+
val left = pos1.x
9+
val top = pos1.y
10+
val right = pos2.x
11+
val bottom = pos2.y
12+
13+
val leftTop get() = Vec2d(left, top)
14+
val rightTop get() = Vec2d(right, top)
15+
val rightBottom get() = Vec2d(right, bottom)
16+
val leftBottom get() = Vec2d(left, bottom)
17+
18+
val size get() = Vec2d(right - left, bottom - top)
19+
val center get() = lerp(pos1, pos2, 0.5)
20+
21+
operator fun plus(vec2d: Vec2d) = Rect(pos1 + vec2d, pos2 + vec2d)
22+
operator fun minus(vec2d: Vec2d) = Rect(pos1 - vec2d, pos2 - vec2d)
23+
24+
fun moveFirst(vec2d: Vec2d) = Rect(pos1 + vec2d, pos2)
25+
fun moveSecond(vec2d: Vec2d) = Rect(pos1, pos2 + vec2d)
26+
27+
fun extend(amount: Double) = Rect(pos1 - amount, pos2 + amount)
28+
fun shrink(amount: Double) = extend(-amount)
29+
30+
fun contains(point: Vec2d) = point.x in left..right && point.y in top..bottom
31+
32+
companion object {
33+
val ZERO = Rect(Vec2d.ZERO, Vec2d.ZERO)
34+
35+
fun basedOn(base: Vec2d, width: Double, height: Double) =
36+
Rect(base, base + Vec2d(width, height))
37+
38+
fun basedOn(base: Vec2d, size: Vec2d) =
39+
Rect(base, base + size)
40+
}
41+
}

0 commit comments

Comments
 (0)