Skip to content

Commit 63021f7

Browse files
committed
Misc stuff
1 parent 9e4ae2c commit 63021f7

File tree

9 files changed

+63
-37
lines changed

9 files changed

+63
-37
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.google.gson.*
44
import com.lambda.gui.impl.clickgui.LambdaClickGui
55
import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow
66
import com.lambda.module.ModuleRegistry
7-
import com.lambda.module.tag.ModuleTag
87
import com.lambda.util.math.Vec2d
98
import java.lang.reflect.Type
109

@@ -48,10 +47,10 @@ object CustomModuleWindowSerializer : JsonSerializer<CustomModuleWindow>, JsonDe
4847
width = it["width"].asDouble
4948
height = it["height"].asDouble
5049
isOpen = it["isOpen"].asBoolean
51-
position = Vec2d(
50+
forceSetPosition(Vec2d(
5251
it["position"].asJsonArray[0].asDouble,
5352
it["position"].asJsonArray[1].asDouble
54-
)
53+
))
5554
}
5655
} ?: throw JsonParseException("Invalid window data")
5756
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.lambda.config.serializer.gui
22

33
import com.google.gson.*
44
import com.lambda.gui.impl.clickgui.LambdaClickGui
5-
import com.lambda.gui.impl.clickgui.windows.ModuleWindow
65
import com.lambda.gui.impl.clickgui.windows.tag.TagWindow
76
import com.lambda.module.tag.ModuleTag
87
import com.lambda.util.math.Vec2d
@@ -35,10 +34,10 @@ object TagWindowSerializer : JsonSerializer<TagWindow>, JsonDeserializer<TagWind
3534
width = it["width"].asDouble
3635
height = it["height"].asDouble
3736
isOpen = it["isOpen"].asBoolean
38-
position = Vec2d(
37+
forceSetPosition(Vec2d(
3938
it["position"].asJsonArray[0].asDouble,
4039
it["position"].asJsonArray[1].asDouble
41-
)
40+
))
4241
}
4342
} ?: throw JsonParseException("Invalid window data")
4443
}

common/src/main/kotlin/com/lambda/graphics/animation/Animation.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class Animation(initialValue: Double, val update: (Double) -> Double) {
4646
else lerp(it, target, speed())
4747
}.apply(::register)
4848

49-
// Exponent animation will never reach target value
50-
private const val CLAMP = 0.001
49+
// Exponent animation never reaches target value
50+
private const val CLAMP = 0.01
5151
}
5252
}
5353

common/src/main/kotlin/com/lambda/graphics/renderer/Renderer.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,15 @@ abstract class Renderer <T: IRenderEntry<T>> (
2424
protected abstract fun newEntry(block: T.() -> Unit): T
2525

2626
override fun build(block: T.() -> Unit): T {
27-
checkDestroyed()
2827
return newEntry(block).process(entrySet::add)
2928
}
3029

3130
override fun remove(entry: T): T {
32-
checkDestroyed()
3331
return entry.process(entrySet::remove)
3432
}
3533

3634
override fun render() {
37-
checkDestroyed()
35+
if (destroyed) return
3836

3937
if (rebuild) {
4038
rebuild = false
@@ -57,19 +55,19 @@ abstract class Renderer <T: IRenderEntry<T>> (
5755
protected open fun preRender() {}
5856

5957
override fun update() {
60-
checkDestroyed()
58+
if (destroyed) return
6159
entrySet.forEach(IRenderEntry<T>::update)
6260
}
6361

6462
override fun clear() {
65-
checkDestroyed()
63+
if (destroyed) return
6664

6765
entrySet.clear()
6866
vao.clear()
6967
}
7068

7169
override fun destroy() {
72-
checkDestroyed()
70+
if (destroyed) return
7371

7472
entrySet.clear()
7573
vao.destroy()
@@ -88,8 +86,4 @@ abstract class Renderer <T: IRenderEntry<T>> (
8886
if (prev == curr) return@observable
8987
rebuild = true
9088
}
91-
92-
private fun checkDestroyed() {
93-
check(!destroyed) { "Using the renderer after it is destroyed" }
94-
}
9589
}

common/src/main/kotlin/com/lambda/gui/GuiConfigurable.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object GuiConfigurable : Configurable(GuiConfig), Loadable {
2929
ModuleTag.defaults.mapIndexed { index, tag ->
3030
TagWindow(tag, ownerGui).apply {
3131
val step = 3.0
32-
position = Vec2d((width + step) * index, 0.0) + step
32+
forceSetPosition(Vec2d((width + step) * index, 0.0) + step)
3333
}
3434
}
3535
}

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.gui.api.component
22

3+
import com.lambda.Lambda.mc
34
import com.lambda.graphics.animation.Animation.Companion.exp
45
import com.lambda.graphics.gl.Scissor.scissor
56
import com.lambda.graphics.renderer.gui.font.IFontEntry
@@ -17,6 +18,7 @@ import com.lambda.util.math.MathUtils.lerp
1718
import com.lambda.util.math.MathUtils.toInt
1819
import com.lambda.util.math.Rect
1920
import com.lambda.util.math.Vec2d
21+
import com.lambda.util.primitives.extension.partialTicks
2022
import java.awt.Color
2123
import kotlin.math.abs
2224

@@ -29,12 +31,13 @@ abstract class WindowComponent <T : ChildComponent> (
2931
abstract var height: Double
3032

3133
var position = Vec2d.ZERO
34+
private var prevPosition = position
3235

3336
var isOpen = true
3437
private var dragOffset: Vec2d? = null
3538
private val padding get() = ClickGui.windowPadding
3639

37-
final override val rect get() = Rect.basedOn(position, width, renderHeight + titleBarHeight)
40+
final override val rect get() = Rect.basedOn(renderPosition, width, renderHeight + titleBarHeight)
3841
val contentRect get() = rect.shrink(padding).moveFirst(Vec2d(0.0, titleBarHeight - padding))
3942

4043
private val titleBar get() = Rect.basedOn(rect.leftTop, rect.size.x, titleBarHeight)
@@ -51,6 +54,7 @@ abstract class WindowComponent <T : ChildComponent> (
5154
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
5255
private var renderHeightAnimation by animation.exp({ 0.0 }, ::actualHeight, 0.6, ::isOpen)
5356
private val renderHeight get() = lerp(0.0, renderHeightAnimation, showAnimation)
57+
private val renderPosition get() = lerp(prevPosition, position, mc.partialTicks)
5458

5559
val contentComponents = ChildLayer<T> { child ->
5660
child.rect in contentRect && accessible && isOpen
@@ -86,9 +90,13 @@ abstract class WindowComponent <T : ChildComponent> (
8690
dragOffset = null
8791
}
8892

93+
is GuiEvent.Tick -> {
94+
prevPosition = position
95+
}
96+
8997
is GuiEvent.Render -> {
90-
layer.assignOffset(position)
91-
subLayer.assignOffset(position)
98+
layer.assignOffset(renderPosition)
99+
subLayer.assignOffset(renderPosition)
92100

93101
// TODO: fix blur
94102
// BlurPostProcessor.render(rect, ClickGui.windowBlur, guiAnimation)
@@ -133,6 +141,24 @@ abstract class WindowComponent <T : ChildComponent> (
133141
//titleBarComponents.onEvent(e)
134142
}
135143

144+
fun forceSetPosition(pos: Vec2d) {
145+
position = pos
146+
prevPosition = position
147+
}
148+
149+
fun focus() {
150+
// move window into foreground
151+
owner.apply {
152+
scheduleAction {
153+
windows.children.apply {
154+
this@WindowComponent
155+
.apply(::remove)
156+
.apply(::add)
157+
}
158+
}
159+
}
160+
}
161+
136162
fun destroy() {
137163
owner.apply {
138164
scheduleAction {

common/src/main/kotlin/com/lambda/gui/impl/clickgui/AbstractClickGui.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.lambda.gui.api.GuiEvent
66
import com.lambda.gui.api.LambdaGui
77
import com.lambda.gui.api.component.WindowComponent
88
import com.lambda.gui.api.component.core.list.ChildLayer
9+
import com.lambda.gui.impl.clickgui.windows.SettingsWindow
910
import com.lambda.module.modules.client.ClickGui
1011

1112
abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, ClickGui) {
@@ -31,20 +32,18 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
3132
activeWindow = null
3233
closing = false
3334
showAnimation = 0.0
35+
36+
windows.children
37+
.filterIsInstance<SettingsWindow>()
38+
.forEach(WindowComponent<*>::destroy)
3439
}
3540

3641
is GuiEvent.Tick -> {
3742
if (closing && showAnimation < 0.01) mc.setScreen(null)
3843
}
3944

4045
is GuiEvent.MouseClick -> {
41-
// move active window into foreground
42-
activeWindow?.let {
43-
windows.children.apply {
44-
remove(it)
45-
add(it)
46-
}
47-
}
46+
activeWindow?.focus()
4847
}
4948

5049
is GuiEvent.MouseMove -> {

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/ModuleButton.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.lambda.gui.api.component.button.ListButton
66
import com.lambda.gui.impl.clickgui.windows.SettingsWindow
77
import com.lambda.module.Module
88
import com.lambda.util.Mouse
9+
import com.mojang.blaze3d.systems.RenderSystem.recordRenderCall
910

1011
class ModuleButton(val module: Module, owner: WindowComponent<*>) : ListButton(owner) {
1112
override val text get() = module.name
@@ -18,16 +19,22 @@ class ModuleButton(val module: Module, owner: WindowComponent<*>) : ListButton(o
1819
Mouse.Button.Right -> {
1920
gui.apply {
2021
// Open new settings window or move existing one to the cursor
21-
windows.children
22-
.firstOrNull { it is SettingsWindow && it.button == this@ModuleButton }
23-
?.let { it.position = e.mouse }
24-
?: run {
22+
val settingsWindow = windows.children.filterIsInstance<SettingsWindow>()
23+
.firstOrNull { it.button == this@ModuleButton }?.apply {
24+
position = e.mouse
25+
} ?: SettingsWindow(this@ModuleButton, this).apply {
26+
forceSetPosition(e.mouse)
27+
2528
scheduleAction {
26-
windows.addChild(SettingsWindow(this@ModuleButton, this).apply {
27-
position = e.mouse
28-
})
29+
windows.addChild(this)
2930
}
3031
}
32+
33+
// we have to wait this tag window to be focused after handling a click event
34+
// to place settings window over it
35+
recordRenderCall {
36+
settingsWindow.focus()
37+
}
3138
}
3239
}
3340
}

common/src/main/kotlin/com/lambda/gui/impl/clickgui/windows/SettingsWindow.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.lambda.gui.api.component.button.ListButton
77
import com.lambda.gui.impl.clickgui.AbstractClickGui
88
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
99
import com.lambda.module.modules.client.ClickGui
10+
import com.lambda.util.math.MathUtils.lerp
1011

1112
class SettingsWindow(
1213
val button: ModuleButton,
@@ -18,7 +19,8 @@ class SettingsWindow(
1819
override var width = button.owner.width
1920
override var height = 0.0
2021

21-
override val showAnimation by animation.exp(0.0, 1.0, ClickGui.openSpeed, ::isOpen)
22+
private val showAnimation0 by animation.exp(0.0, 1.0, ClickGui.openSpeed, ::isOpen)
23+
override val showAnimation get() = lerp(0.0, showAnimation0, owner.showAnimation)
2224

2325
override fun onEvent(e: GuiEvent) {
2426
if (e is GuiEvent.Tick) {

0 commit comments

Comments
 (0)