Skip to content

Commit 49eb6d4

Browse files
committed
Window creation tests
1 parent 81b4bde commit 49eb6d4

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,27 @@ import com.lambda.graphics.animation.Animation.Companion.exp
55
import com.lambda.gui.api.GuiEvent
66
import com.lambda.gui.api.LambdaGui
77
import com.lambda.gui.api.component.WindowComponent
8-
import com.lambda.gui.api.component.core.list.ChildComponent
98
import com.lambda.gui.api.component.core.list.ChildLayer
109
import com.lambda.module.modules.client.ClickGui
1110

1211
abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, ClickGui) {
13-
protected val windows = ChildLayer<WindowComponent<*>> { child ->
12+
val windows = ChildLayer<WindowComponent<*>> { child ->
1413
child == activeWindow && !closing
1514
}
1615

17-
protected var activeWindow: WindowComponent<*>? = null
16+
private var activeWindow: WindowComponent<*>? = null
1817

1918
private var closing = false
2019
var guiAnimation by animation.exp(0.0, 1.0, {
2120
if (closing) ClickGui.closeSpeed else ClickGui.openSpeed
2221
}) { !closing }; private set
2322

23+
private val actionPool = ArrayDeque<() -> Unit>()
24+
fun scheduleAction(block: () -> Unit) = actionPool.add(block)
25+
2426
override fun onEvent(e: GuiEvent) {
27+
while (actionPool.isNotEmpty()) actionPool.last().invoke()
28+
2529
when (e) {
2630
is GuiEvent.Show -> {
2731
activeWindow = null

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ object LambdaClickGui : AbstractClickGui() {
1818
val new = windows.subtract(children.toSet())
1919
children.addAll(new)
2020

21-
val iterator = children.iterator()
22-
while (iterator.hasNext()) {
23-
val next = iterator.next()
24-
if (next !in windows) removeChild(next)
21+
children.forEach { window ->
22+
if (window !in windows) {
23+
scheduleAction {
24+
removeChild(window)
25+
}
26+
}
2527
}
2628
}
2729
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.lambda.gui.impl.clickgui.buttons
22

33
import com.lambda.gui.api.component.WindowComponent
44
import com.lambda.gui.api.component.button.ListButton
5+
import com.lambda.gui.impl.clickgui.windows.SettingsWindow
56
import com.lambda.module.Module
67
import com.lambda.util.Mouse
78

@@ -13,7 +14,12 @@ class ModuleButton(val module: Module, owner: WindowComponent<*>) : ListButton(o
1314
when (mouse) {
1415
Mouse.Button.Left -> if (hovered) module.toggle()
1516
Mouse.Button.Right -> {
16-
// open settings window
17+
val gui = owner.owner
18+
19+
gui.scheduleAction {
20+
val settingsWindow = SettingsWindow(this, gui)
21+
gui.windows.addChild(settingsWindow)
22+
}
1723
}
1824
}
1925
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.lambda.gui.impl.clickgui.windows
2+
3+
import com.lambda.gui.api.GuiEvent
4+
import com.lambda.gui.api.component.ListWindow
5+
import com.lambda.gui.api.component.button.ListButton
6+
import com.lambda.gui.impl.clickgui.AbstractClickGui
7+
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
8+
9+
class SettingsWindow(
10+
button: ModuleButton,
11+
owner: AbstractClickGui
12+
) : ListWindow<ListButton>(owner) {
13+
private val module = button.module
14+
15+
override val title = module.name
16+
override var width = button.owner.width
17+
override var height = 0.0
18+
19+
override fun onEvent(e: GuiEvent) {
20+
if (e is GuiEvent.Tick) {
21+
height = contentComponents.children.sumOf { it.size.y }
22+
}
23+
24+
super.onEvent(e)
25+
}
26+
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ class CustomModuleWindow(
2929
.forEach(contentComponents::addChild)
3030

3131
// Remove deleted modules
32-
val iterator = children.iterator()
33-
while (iterator.hasNext()) {
34-
val next = iterator.next()
35-
if (next.module !in modules) removeChild(next)
32+
children.forEach { button ->
33+
if (button.module !in modules) {
34+
owner.scheduleAction {
35+
removeChild(button)
36+
}
37+
}
3638
}
3739
}
3840
}

0 commit comments

Comments
 (0)