Skip to content

Commit 5d74559

Browse files
committed
HUD & many cgui refactorings
1 parent 4d4ec6a commit 5d74559

File tree

26 files changed

+352
-107
lines changed

26 files changed

+352
-107
lines changed

common/src/main/kotlin/com/lambda/config/serializer/gui/TagWindowSerializer.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lambda.config.serializer.gui
33
import com.google.gson.*
44
import com.lambda.gui.impl.clickgui.LambdaClickGui
55
import com.lambda.gui.impl.clickgui.windows.tag.TagWindow
6+
import com.lambda.gui.impl.hudgui.LambdaHudGui
67
import com.lambda.module.tag.ModuleTag
78
import com.lambda.util.math.Vec2d
89
import java.lang.reflect.Type
@@ -30,7 +31,15 @@ object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWind
3031
typeOfT: Type?,
3132
context: JsonDeserializationContext?,
3233
) = json?.asJsonObject?.let {
33-
TagWindow(ModuleTag(it["tag"].asString), LambdaClickGui).apply {
34+
val tag = ModuleTag(it["tag"].asString)
35+
36+
val gui = when (tag) {
37+
in ModuleTag.defaults -> LambdaClickGui
38+
in ModuleTag.hudDefaults -> LambdaHudGui
39+
else -> return@let null
40+
}
41+
42+
TagWindow(tag, gui).apply {
3443
width = it["width"].asDouble
3544
height = it["height"].asDouble
3645
isOpen = it["isOpen"].asBoolean

common/src/main/kotlin/com/lambda/config/settings/collections/ListSetting.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class ListSetting<T : Any>(
2121
}
2222

2323
override fun toJson(): JsonElement {
24-
value = defaultValue // Hack the Delegates.observable
2524
return gson.toJsonTree(value)
2625
}
2726
}

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.lambda.friend.FriendRegistry
77
import com.lambda.graphics.renderer.gui.font.LambdaFont
88
import com.lambda.graphics.renderer.gui.font.LambdaMoji
99
import com.lambda.gui.GuiConfigurable
10+
import com.lambda.gui.HudGuiConfigurable
1011
import com.lambda.interaction.PlayerPacketManager
1112
import com.lambda.interaction.RotationManager
1213
import com.lambda.module.ModuleRegistry
@@ -23,6 +24,7 @@ object Loader {
2324
LambdaFont.Loader,
2425
LambdaMoji.Loader,
2526
GuiConfigurable,
27+
HudGuiConfigurable,
2628
FriendRegistry,
2729
SoundRegistry,
2830
)

common/src/main/kotlin/com/lambda/event/events/RenderEvent.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ abstract class RenderEvent : Event {
4040
}
4141

4242
abstract class GUI(val scale: Double) : RenderEvent() {
43+
class HUD(scaleFactor: Double) : GUI(scaleFactor)
44+
4345
class Scaled(scaleFactor: Double) : GUI(scaleFactor)
4446
class Fixed : GUI(1.0)
4547

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ object RenderMain {
2525
translate(0.0, 0.0, -3000.0)
2626

2727
setupGL {
28-
rescale(GuiSettings.scale)
29-
RenderEvent.GUI.Scaled(GuiSettings.scale).post()
30-
3128
rescale(1.0)
3229
RenderEvent.GUI.Fixed().post()
30+
31+
rescale(GuiSettings.scale)
32+
RenderEvent.GUI.HUD(GuiSettings.scale).post()
33+
RenderEvent.GUI.Scaled(GuiSettings.scale).post()
3334
}
3435
}
3536

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.lambda.gui
2+
3+
import com.lambda.config.Configurable
4+
import com.lambda.config.configurations.GuiConfig
5+
import com.lambda.core.Loadable
6+
import com.lambda.gui.impl.AbstractClickGui
7+
import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow
8+
import com.lambda.gui.impl.clickgui.windows.tag.TagWindow
9+
import com.lambda.module.tag.ModuleTag
10+
import com.lambda.util.math.Vec2d
11+
12+
abstract class AbstractGuiConfigurable(
13+
private val ownerGui: AbstractClickGui,
14+
private val tags: Set<ModuleTag>,
15+
override val name: String
16+
) : Configurable(GuiConfig), Loadable {
17+
var mainWindows by setting("windows", defaultWindows)
18+
open var customWindows = mutableListOf<CustomModuleWindow>()
19+
20+
private val defaultWindows get() =
21+
tags.mapIndexed { index, tag ->
22+
TagWindow(tag, ownerGui).apply {
23+
val step = 5.0
24+
position = Vec2d((width + step) * index, 0.0) + step
25+
}
26+
}
27+
}
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,11 @@
11
package com.lambda.gui
22

3-
import com.lambda.config.Configurable
4-
import com.lambda.config.configurations.GuiConfig
5-
import com.lambda.core.Loadable
63
import com.lambda.gui.impl.clickgui.LambdaClickGui
74
import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow
8-
import com.lambda.gui.impl.clickgui.windows.tag.TagWindow
95
import com.lambda.module.tag.ModuleTag
10-
import com.lambda.util.math.Vec2d
116

12-
object GuiConfigurable : Configurable(GuiConfig), Loadable {
13-
override val name = "gui"
14-
private val ownerGui = LambdaClickGui
15-
16-
val mainWindows by setting("windows", defaultWindows).apply {
17-
onValueChange { _, _ ->
18-
ownerGui.updateWindows()
19-
}
20-
}
21-
22-
val customWindows by setting("custom windows", listOf<CustomModuleWindow>()).apply {
23-
onValueChange { _, _ ->
24-
ownerGui.updateWindows()
25-
}
26-
}
27-
28-
private val defaultWindows get() =
29-
ModuleTag.defaults.mapIndexed { index, tag ->
30-
TagWindow(tag, ownerGui).apply {
31-
val step = 5.0
32-
position = Vec2d((width + step) * index, 0.0) + step
33-
}
34-
}
7+
object GuiConfigurable : AbstractGuiConfigurable(
8+
LambdaClickGui, ModuleTag.defaults, "gui"
9+
) {
10+
override var customWindows by setting("custom windows", listOf<CustomModuleWindow>())
3511
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.lambda.gui
2+
3+
import com.lambda.gui.impl.hudgui.LambdaHudGui
4+
import com.lambda.module.tag.ModuleTag
5+
6+
object HudGuiConfigurable : AbstractGuiConfigurable(
7+
LambdaHudGui, ModuleTag.hudDefaults, "hudgui"
8+
)

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.lambda.gui.api
22

33
import com.lambda.Lambda.mc
4-
import com.lambda.event.EventFlow.syncListeners
4+
import com.lambda.event.Muteable
55
import com.lambda.event.events.RenderEvent
66
import com.lambda.event.events.TickEvent
7-
import com.lambda.event.listener.UnsafeListener
7+
import com.lambda.event.listener.SafeListener.Companion.listener
88
import com.lambda.graphics.animation.AnimationTicker
99
import com.lambda.gui.api.component.core.IComponent
10+
import com.lambda.gui.impl.AbstractClickGui
1011
import com.lambda.module.Module
1112
import com.lambda.util.KeyCode
1213
import com.lambda.util.Mouse
@@ -18,27 +19,29 @@ import net.minecraft.client.gui.DrawContext
1819
import net.minecraft.client.gui.screen.Screen
1920
import net.minecraft.text.Text
2021

21-
@Suppress("LeakingThis")
2222
abstract class LambdaGui(
2323
override val name: String,
2424
private val owner: Module? = null
25-
) : Screen(Text.of(name)), IComponent, Nameable {
26-
private var screenSize = Vec2d.ZERO
25+
) : Screen(Text.of(name)), IComponent, Nameable, Muteable {
26+
protected var screenSize = Vec2d.ZERO
2727
override val rect get() = Rect(Vec2d.ZERO, screenSize)
2828

2929
val isOpen get() = mc.currentScreen == this
30+
override val isMuted: Boolean get() = !isOpen
31+
private var closingAction: (() -> Unit)? = null
3032

3133
val animation = AnimationTicker()
3234

33-
private val renderListener = UnsafeListener(0, this, false) { event ->
34-
event as RenderEvent.GUI.Scaled
35-
screenSize = event.screenSize
36-
onEvent(GuiEvent.Render())
37-
}
35+
init {
36+
listener<RenderEvent.GUI.Scaled> { event ->
37+
screenSize = event.screenSize
38+
onEvent(GuiEvent.Render())
39+
}
3840

39-
private val tickListener = UnsafeListener(0, this, false) {
40-
animation.tick()
41-
onEvent(GuiEvent.Tick())
41+
listener<TickEvent.Pre> {
42+
animation.tick()
43+
onEvent(GuiEvent.Tick())
44+
}
4245
}
4346

4447
/**
@@ -47,37 +50,50 @@ abstract class LambdaGui(
4750
* No safe context required (TODO: let user open clickgui via main menu)
4851
*/
4952
fun show() {
53+
owner?.enable()
5054
if (isOpen) return
51-
mc.currentScreen?.close()
5255

53-
recordRenderCall { // wait for the previous screen to be closed
54-
mc.setScreen(this)
56+
when (val screen = mc.currentScreen) {
57+
is AbstractClickGui -> {
58+
screen.close()
59+
60+
screen.setCloseTask {
61+
mc.setScreen(this)
62+
}
63+
}
64+
65+
else -> {
66+
screen?.close()
67+
68+
recordRenderCall {
69+
mc.setScreen(this)
70+
}
71+
}
5572
}
5673
}
5774

5875
final override fun onDisplayed() {
5976
onEvent(GuiEvent.Show())
60-
61-
with(syncListeners) {
62-
subscribe<RenderEvent.GUI.Scaled>(renderListener)
63-
subscribe<TickEvent.Pre>(tickListener)
64-
}
6577
}
6678

67-
final override fun removed() {
79+
override fun removed() {
6880
onEvent(GuiEvent.Hide())
6981

7082
// quick crashfix (is there any other way to prevent gui being closed twice?)
7183
mc.currentScreen = null
7284
owner?.disable()
7385
mc.currentScreen = this
7486

75-
with(syncListeners) {
76-
unsubscribe(renderListener)
77-
unsubscribe(tickListener)
87+
closingAction?.let {
88+
recordRenderCall(it)
89+
closingAction = null
7890
}
7991
}
8092

93+
fun setCloseTask(block: () -> Unit) {
94+
closingAction = block
95+
}
96+
8197
final override fun render(context: DrawContext?, mouseX: Int, mouseY: Int, delta: Float) {
8298
// Let's remove background tint
8399
}
@@ -93,7 +109,6 @@ abstract class LambdaGui(
93109
return true
94110
}
95111

96-
97112
final override fun charTyped(chr: Char, modifiers: Int): Boolean {
98113
onEvent(GuiEvent.CharTyped(chr))
99114
return true

common/src/main/kotlin/com/lambda/gui/api/component/ListWindow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lambda.gui.api.component
22

33
import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.button.ListButton
5-
import com.lambda.gui.impl.clickgui.AbstractClickGui
5+
import com.lambda.gui.impl.AbstractClickGui
66

77
abstract class ListWindow <T : ListButton> (
88
owner: AbstractClickGui

0 commit comments

Comments
 (0)