Skip to content

Commit b9bfa68

Browse files
committed
Fix: Empty click gui at first launch
1 parent 179a54e commit b9bfa68

File tree

6 files changed

+43
-40
lines changed

6 files changed

+43
-40
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ abstract class ButtonComponent(
4242
private val renderHovered get() = hovered || System.currentTimeMillis() - lastHoveredTime < 110
4343

4444
// Removes button shrinking if there's no space between buttons
45-
protected val shrink get() = lerp(0.0, interactAnimation, ClickGui.buttonStep)
45+
protected val shrinkAnimation get() = lerp(0.0, interactAnimation, ClickGui.buttonStep)
4646

4747
init {
4848
// Active color
4949
renderer.filled {
50-
position = rect.shrink(shrink)
50+
position = rect.shrink(shrinkAnimation)
5151
shade = GuiSettings.shade
5252
color(GuiSettings.mainColor.multAlpha(activeAnimation * 0.3 * showAnimation))
5353
}
5454

5555
// Hover glint
5656
renderer.filled {
5757
val hoverRect = Rect.basedOn(rect.leftTop, rect.size.x * hoverRectAnimation, rect.size.y)
58-
position = hoverRect.shrink(shrink)
58+
position = hoverRect.shrink(shrinkAnimation)
5959
shade = GuiSettings.shade
6060

6161
val alpha = interactAnimation * 0.2 * showAnimation

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class ModuleButton(
6767

6868
position = lerp(left, right, activeAnimation)
6969
.clamp(rect)
70-
.shrink(interactAnimation)
70+
.shrink(shrinkAnimation)
7171

7272
// 0.0 .. 1.0 .. 0.0 animation
7373
val alpha = 1.0 - (abs(activeAnimation - 0.5) * 2.0)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class Slider <V : Any, T : AbstractSetting<V>>(
2828

2929
init {
3030
renderer.filled {
31-
position = rect.moveSecond(Vec2d(-rect.size.x * (1.0 - renderProgress), 0.0)).shrink(shrink)
31+
position = rect.moveSecond(Vec2d(-rect.size.x * (1.0 - renderProgress), 0.0)).shrink(shrinkAnimation)
3232
shade = GuiSettings.shade
3333
color(GuiSettings.mainColor.multAlpha(showAnimation * 0.3))
3434
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,48 @@ import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.ListWindow
55
import com.lambda.gui.impl.clickgui.AbstractClickGui
66
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
7+
import com.lambda.module.Module
78

89
abstract class ModuleWindow(
910
override var title: String,
1011
override var width: Double = 110.0,
1112
override var height: Double = 300.0,
1213
gui: AbstractClickGui
1314
) : ListWindow<ModuleButton>(gui) {
14-
override fun onEvent(e: GuiEvent) {
15+
private var lastUpdate = 0L
16+
17+
abstract fun getModuleList(): Collection<Module>
18+
19+
private fun updateModules() {
20+
val time = System.currentTimeMillis()
21+
if (time - lastUpdate < 1000L) return
22+
lastUpdate = time
23+
24+
contentComponents.apply {
25+
val modules = getModuleList()
26+
27+
// Add missing module buttons
28+
modules.filter { module ->
29+
children.all { button ->
30+
button.module != module
31+
}
32+
}.map { ModuleButton(it, contentComponents) }
33+
.forEach(contentComponents::addChild)
34+
35+
// Remove deleted modules
36+
children.forEach { button ->
37+
if (button.module !in modules) {
38+
this@ModuleWindow.gui.scheduleAction {
39+
removeChild(button)
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
override fun onEvent(e: GuiEvent) {
47+
if (e is GuiEvent.Show || e is GuiEvent.Tick) updateModules()
48+
1549
if (e is GuiEvent.Tick) {
1650
contentComponents.children.sortBy {
1751
it.module.name
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.lambda.gui.impl.clickgui.windows.tag
22

3-
import com.lambda.gui.api.GuiEvent
43
import com.lambda.gui.impl.clickgui.AbstractClickGui
5-
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
64
import com.lambda.gui.impl.clickgui.windows.ModuleWindow
75
import com.lambda.module.Module
86

@@ -11,29 +9,5 @@ class CustomModuleWindow(
119
val modules: MutableList<Module> = mutableListOf(),
1210
gui: AbstractClickGui
1311
) : ModuleWindow(title, gui = gui) {
14-
override fun onEvent(e: GuiEvent) {
15-
if (e is GuiEvent.Tick) updateModules()
16-
super.onEvent(e)
17-
}
18-
19-
private fun updateModules() {
20-
contentComponents.apply {
21-
// Add missing module buttons
22-
modules.filter { module ->
23-
children.all { button ->
24-
button.module != module
25-
}
26-
}.map { ModuleButton(it, contentComponents) }
27-
.forEach(contentComponents::addChild)
28-
29-
// Remove deleted modules
30-
children.forEach { button ->
31-
if (button.module !in modules) {
32-
this@CustomModuleWindow.gui.scheduleAction {
33-
removeChild(button)
34-
}
35-
}
36-
}
37-
}
38-
}
12+
override fun getModuleList() = modules
3913
}
Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lambda.gui.impl.clickgui.windows.tag
22

33
import com.lambda.gui.impl.clickgui.AbstractClickGui
4-
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
54
import com.lambda.gui.impl.clickgui.windows.ModuleWindow
65
import com.lambda.module.ModuleRegistry
76
import com.lambda.module.tag.ModuleTag
@@ -10,10 +9,6 @@ class TagWindow(
109
val tag: ModuleTag,
1110
owner: AbstractClickGui
1211
) : ModuleWindow(tag.name, gui = owner) {
13-
init {
14-
ModuleRegistry.modules
15-
.filter { it.defaultTags.firstOrNull() == tag }
16-
.map { ModuleButton(it, contentComponents) }
17-
.forEach(contentComponents::addChild)
18-
}
12+
override fun getModuleList() = ModuleRegistry.modules
13+
.filter { it.defaultTags.firstOrNull() == tag }
1914
}

0 commit comments

Comments
 (0)