Skip to content

Commit 8e9c89c

Browse files
committed
Layouts, dsl builders
1 parent c97a29c commit 8e9c89c

File tree

4 files changed

+417
-0
lines changed

4 files changed

+417
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.lambda.module.modules.client
2+
3+
import com.lambda.module.Module
4+
import com.lambda.module.tag.ModuleTag
5+
import com.lambda.newgui.LambdaScreen.Companion.toScreen
6+
import com.lambda.newgui.gui
7+
import com.lambda.newgui.layout
8+
import com.lambda.util.math.ColorUtils.setAlpha
9+
import com.lambda.util.math.Rect
10+
import com.lambda.util.math.Vec2d
11+
import java.awt.Color
12+
13+
object NewCGui : Module(
14+
name = "NewCGui",
15+
description = "ggs",
16+
defaultTags = setOf(ModuleTag.CLIENT)
17+
) {
18+
private val clickGuiLayout =
19+
gui {
20+
layout {
21+
rect { Rect(Vec2d.ONE * 10.0, Vec2d.ONE * 200.0) }
22+
23+
onRender {
24+
filled.build(rect, color = Color.WHITE.setAlpha(0.5))
25+
}
26+
27+
layout(true) {
28+
rect { Rect(Vec2d.ONE * 10.0, Vec2d.ONE * 200.0) }
29+
30+
onRender {
31+
filled.build(rect, color = Color.BLACK)
32+
}
33+
34+
layout(true) {
35+
rect { Rect(Vec2d.ONE * 10.0, Vec2d.ONE * 200.0) }
36+
37+
onRender {
38+
filled.build(rect, color = Color.WHITE.setAlpha(0.5))
39+
}
40+
41+
layout(true) {
42+
rect { Rect(Vec2d.ONE * 10.0, Vec2d.ONE * 200.0) }
43+
44+
onRender {
45+
filled.build(rect, color = Color.BLACK)
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
val CLICK_GUI = clickGuiLayout.toScreen("New Click Gui")
54+
55+
init {
56+
onEnable {
57+
CLICK_GUI.show()
58+
}
59+
}
60+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.lambda.newgui
2+
3+
import com.lambda.graphics.RenderMain
4+
import com.lambda.util.math.Rect
5+
import com.lambda.util.math.Vec2d
6+
7+
@DslMarker
8+
annotation class UIBuilder
9+
10+
/**
11+
* Creates a "gui" represented by layout component
12+
*/
13+
@UIBuilder
14+
fun gui(block: Layout.() -> Unit) =
15+
Layout(owner = null, useBatching = false, batchChildren = false).apply {
16+
var screenSize = Vec2d.ONE * 10000.0
17+
18+
rect {
19+
Rect(Vec2d.ZERO, screenSize)
20+
}
21+
22+
onRender {
23+
screenSize = RenderMain.screenSize
24+
}
25+
}.apply(block)
26+
27+
28+
/**
29+
* Creates empty layout
30+
*
31+
* @param useBatching
32+
* Increases performance by using parent's renderer instead of creating a new one.
33+
*
34+
* @param batchChildren
35+
* Whether allow children to use the renderer of this layout
36+
*/
37+
@UIBuilder
38+
fun Layout.layout(
39+
useBatching: Boolean = false,
40+
batchChildren: Boolean = false,
41+
block: Layout.() -> Unit,
42+
) = Layout(this, useBatching, batchChildren)
43+
.apply(children::add).apply(block)
44+
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.lambda.newgui
2+
3+
import com.lambda.Lambda.mc
4+
import com.lambda.event.Muteable
5+
import com.lambda.event.events.RenderEvent
6+
import com.lambda.event.events.TickEvent
7+
import com.lambda.event.listener.SafeListener.Companion.listener
8+
import com.lambda.gui.api.GuiEvent
9+
import com.lambda.util.KeyCode
10+
import com.lambda.util.Mouse
11+
import com.lambda.util.Nameable
12+
import com.lambda.util.math.Vec2d
13+
import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall
14+
import net.minecraft.client.gui.DrawContext
15+
import net.minecraft.client.gui.screen.Screen
16+
import net.minecraft.text.Text
17+
18+
class LambdaScreen(
19+
override val name: String,
20+
val layout: Layout
21+
) : Screen(Text.of(name)), Nameable, Muteable {
22+
override val isMuted: Boolean get() = !isOpen
23+
24+
private var screenSize = Vec2d.ZERO
25+
val isOpen get() = mc.currentScreen == this
26+
27+
init {
28+
listener<RenderEvent.GUI.Scaled> { event ->
29+
screenSize = event.screenSize
30+
layout.onEvent(GuiEvent.Render())
31+
}
32+
33+
listener<TickEvent.Pre> {
34+
layout.onEvent(GuiEvent.Tick())
35+
}
36+
}
37+
38+
fun show() {
39+
mc.currentScreen?.close()
40+
41+
recordRenderCall {
42+
mc.setScreen(this)
43+
}
44+
}
45+
46+
override fun onDisplayed() {
47+
layout.onEvent(GuiEvent.Show())
48+
}
49+
50+
override fun removed() {
51+
layout.onEvent(GuiEvent.Hide())
52+
}
53+
54+
override fun shouldPause() = false
55+
56+
override fun render(context: DrawContext?, mouseX: Int, mouseY: Int, delta: Float) {
57+
// Let's remove background tint
58+
}
59+
60+
override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
61+
val translated = KeyCode.virtualMapUS(keyCode, scanCode)
62+
layout.onEvent(GuiEvent.KeyPress(translated))
63+
64+
if (keyCode == KeyCode.ESCAPE.keyCode) {
65+
close()
66+
}
67+
68+
return true
69+
}
70+
71+
override fun charTyped(chr: Char, modifiers: Int): Boolean {
72+
layout.onEvent(GuiEvent.CharTyped(chr))
73+
return true
74+
}
75+
76+
override fun mouseClicked(mouseX: Double, mouseY: Double, button: Int): Boolean {
77+
layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Click, rescaleMouse(mouseX, mouseY)))
78+
return true
79+
}
80+
81+
override fun mouseReleased(mouseX: Double, mouseY: Double, button: Int): Boolean {
82+
layout.onEvent(GuiEvent.MouseClick(Mouse.Button(button), Mouse.Action.Release, rescaleMouse(mouseX, mouseY)))
83+
return true
84+
}
85+
86+
override fun mouseMoved(mouseX: Double, mouseY: Double) {
87+
layout.onEvent(GuiEvent.MouseMove(rescaleMouse(mouseX, mouseY)))
88+
}
89+
90+
override fun mouseScrolled(
91+
mouseX: Double,
92+
mouseY: Double,
93+
horizontalAmount: Double,
94+
verticalAmount: Double
95+
): Boolean {
96+
layout.onEvent(GuiEvent.MouseScroll(rescaleMouse(mouseX, mouseY), verticalAmount))
97+
return true
98+
}
99+
100+
private fun rescaleMouse(mouseX: Double, mouseY: Double): Vec2d {
101+
val mcMouse = Vec2d(mouseX, mouseY)
102+
val mcWindow = Vec2d(mc.window.scaledWidth, mc.window.scaledHeight)
103+
104+
val uv = mcMouse / mcWindow
105+
return uv * screenSize
106+
}
107+
108+
companion object {
109+
fun Layout.toScreen(name: String) = LambdaScreen(name, this)
110+
}
111+
}

0 commit comments

Comments
 (0)