Skip to content

Commit 9e4ae2c

Browse files
committed
Settings open animation & refactor
1 parent 363bb61 commit 9e4ae2c

File tree

7 files changed

+37
-35
lines changed

7 files changed

+37
-35
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import com.lambda.graphics.animation.Animation.Companion.exp
44
import com.lambda.graphics.gl.Scissor.scissor
55
import com.lambda.graphics.renderer.gui.font.IFontEntry
66
import com.lambda.gui.api.GuiEvent
7-
import com.lambda.gui.api.component.button.ButtonComponent
87
import com.lambda.gui.api.component.core.list.ChildComponent
98
import com.lambda.gui.api.component.core.list.ChildLayer
109
import com.lambda.gui.api.layer.RenderLayer
@@ -47,11 +46,11 @@ abstract class WindowComponent <T : ChildComponent> (
4746
val subLayer = RenderLayer()
4847

4948
val animation = owner.animation
50-
val guiAnimation get() = owner.guiAnimation
49+
open val showAnimation get() = owner.showAnimation
5150

5251
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
5352
private var renderHeightAnimation by animation.exp({ 0.0 }, ::actualHeight, 0.6, ::isOpen)
54-
private val renderHeight get() = lerp(0.0, renderHeightAnimation, guiAnimation)
53+
private val renderHeight get() = lerp(0.0, renderHeightAnimation, showAnimation)
5554

5655
val contentComponents = ChildLayer<T> { child ->
5756
child.rect in contentRect && accessible && isOpen
@@ -67,15 +66,15 @@ abstract class WindowComponent <T : ChildComponent> (
6766
position = rect
6867
roundRadius = ClickGui.windowRadius
6968

70-
val alpha = (guiAnimation * 2.0).coerceIn(0.0, 1.0)
69+
val alpha = (showAnimation * 2.0).coerceIn(0.0, 1.0)
7170
color(GuiSettings.backgroundColor.multAlpha(alpha))
7271
}
7372

7473
// Title
7574
titleFont = renderer.font {
7675
text = title
7776
position = titleBar.center - widthVec * 0.5
78-
color = Color.WHITE.setAlpha(guiAnimation)
77+
color = Color.WHITE.setAlpha(showAnimation)
7978
}
8079
}
8180

@@ -134,6 +133,14 @@ abstract class WindowComponent <T : ChildComponent> (
134133
//titleBarComponents.onEvent(e)
135134
}
136135

136+
fun destroy() {
137+
owner.apply {
138+
scheduleAction {
139+
windows.removeChild(this@WindowComponent)
140+
}
141+
}
142+
}
143+
137144
override fun onRemove() {
138145
layer.destroy()
139146
subLayer.destroy()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ abstract class ButtonComponent(
3636
private var pressAnimation by animation.exp(0.0, 1.0, 0.5, ::pressed)
3737
private val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
3838
private val showAnimationRaw by animation.exp(0.0, 1.0, 0.7, owner::isOpen)
39-
private val showAnimation get() = lerp(0.0, showAnimationRaw, owner.guiAnimation)
39+
private val showAnimation get() = lerp(0.0, showAnimationRaw, owner.showAnimation)
4040

4141
private var lastHoveredTime = 0L
4242
private val renderHovered get() = hovered ||

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class ListButton(owner: WindowComponent<*>) : ButtonComponent(owner) {
1212
override val size get() = Vec2d(FILL_PARENT, ClickGui.buttonHeight)
1313

1414
var heightOffset = 0.0
15-
private val targetHeightOffset get() = heightOffset * owner.guiAnimation * owner.isOpen.toInt()
15+
private val targetHeightOffset get() = heightOffset * owner.showAnimation * owner.isOpen.toInt()
1616
private var renderHeightOffset by animation.exp(::targetHeightOffset, 0.5)
1717

1818
override fun onEvent(e: GuiEvent) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
1616
private var activeWindow: WindowComponent<*>? = null
1717

1818
private var closing = false
19-
var guiAnimation by animation.exp(0.0, 1.0, {
19+
var showAnimation by animation.exp(0.0, 1.0, {
2020
if (closing) ClickGui.closeSpeed else ClickGui.openSpeed
2121
}) { !closing }; private set
2222

@@ -30,11 +30,11 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
3030
is GuiEvent.Show -> {
3131
activeWindow = null
3232
closing = false
33-
guiAnimation = 0.0
33+
showAnimation = 0.0
3434
}
3535

3636
is GuiEvent.Tick -> {
37-
if (closing && guiAnimation < 0.01) mc.setScreen(null)
37+
if (closing && showAnimation < 0.01) mc.setScreen(null)
3838
}
3939

4040
is GuiEvent.MouseClick -> {

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

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,22 @@ package com.lambda.gui.impl.clickgui
22

33
import com.lambda.gui.GuiConfigurable
44
import com.lambda.gui.api.GuiEvent
5+
import com.lambda.gui.api.component.WindowComponent
6+
import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow
57

68
object LambdaClickGui : AbstractClickGui() {
79
override fun onEvent(e: GuiEvent) {
8-
if (e is GuiEvent.Show || e is GuiEvent.Tick) {
9-
updateWindows()
10-
}
11-
10+
if (e is GuiEvent.Show || e is GuiEvent.Tick) updateWindows()
1211
super.onEvent(e)
1312
}
1413

1514
fun updateWindows() {
1615
windows.apply {
1716
val windows = GuiConfigurable.mainWindows + GuiConfigurable.customWindows
18-
val new = windows.subtract(children.toSet())
19-
children.addAll(new)
17+
children.addAll(windows.subtract(children.toSet()))
2018

21-
children.forEach { window ->
22-
if (window !in windows) {
23-
scheduleAction {
24-
removeChild(window)
25-
}
26-
}
27-
}
19+
children.filter { it !in windows && it is CustomModuleWindow }
20+
.forEach(WindowComponent<*>::destroy)
2821
}
2922
}
3023
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,18 @@ class ModuleButton(val module: Module, owner: WindowComponent<*>) : ListButton(o
1717
Mouse.Button.Left -> if (hovered) module.toggle()
1818
Mouse.Button.Right -> {
1919
gui.apply {
20-
windows.children.forEach { child ->
21-
if (child is SettingsWindow && child.button == this@ModuleButton) {
22-
gui.scheduleAction {
23-
gui.windows.removeChild(child)
20+
// 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 {
25+
scheduleAction {
26+
windows.addChild(SettingsWindow(this@ModuleButton, this).apply {
27+
position = e.mouse
28+
})
2429
}
2530
}
26-
}
27-
28-
scheduleAction {
29-
windows.addChild(SettingsWindow(this@ModuleButton, this).apply {
30-
position = e.mouse
31-
})
32-
}
3331
}
34-
3532
}
3633
}
3734
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.lambda.gui.impl.clickgui.windows
22

3+
import com.lambda.graphics.animation.Animation.Companion.exp
34
import com.lambda.gui.api.GuiEvent
45
import com.lambda.gui.api.component.ListWindow
56
import com.lambda.gui.api.component.button.ListButton
67
import com.lambda.gui.impl.clickgui.AbstractClickGui
78
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
9+
import com.lambda.module.modules.client.ClickGui
810

911
class SettingsWindow(
1012
val button: ModuleButton,
@@ -16,9 +18,12 @@ class SettingsWindow(
1618
override var width = button.owner.width
1719
override var height = 0.0
1820

21+
override val showAnimation by animation.exp(0.0, 1.0, ClickGui.openSpeed, ::isOpen)
22+
1923
override fun onEvent(e: GuiEvent) {
2024
if (e is GuiEvent.Tick) {
2125
height = contentComponents.children.sumOf { it.size.y }
26+
if (showAnimation < 0.05 && !isOpen) destroy()
2227
}
2328

2429
super.onEvent(e)

0 commit comments

Comments
 (0)