Skip to content

Commit 9babf6f

Browse files
committed
Setting animations rework
1 parent 9ade9f7 commit 9babf6f

File tree

12 files changed

+132
-60
lines changed

12 files changed

+132
-60
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package com.lambda.gui.api.component
33
import com.lambda.gui.api.GuiEvent
44
import com.lambda.gui.api.component.button.ListButton
55
import com.lambda.gui.impl.clickgui.AbstractClickGui
6-
import com.lambda.module.modules.client.ClickGui
76

87
abstract class ListWindow <T : ListButton> (
98
owner: AbstractClickGui
109
) : WindowComponent<T>(owner) {
1110
override fun onEvent(e: GuiEvent) {
12-
if (e is GuiEvent.Show || e is GuiEvent.Tick) {
11+
if (e is GuiEvent.Tick) {
1312
var y = 0.0
1413
contentComponents.children.forEach { button ->
1514
button.heightOffset = y
16-
y += button.size.y + ClickGui.buttonStep
15+
y += button.size.y + button.listStep
1716
}
1817
}
1918

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ abstract class WindowComponent <T : ChildComponent> (
4040
private val contentRect get() = rect.shrink(padding).moveFirst(Vec2d(0.0, titleBarHeight - padding))
4141

4242
private val titleBar get() = Rect.basedOn(rect.leftTop, rect.size.x, titleBarHeight)
43-
private val titleBarHeight get() = titleFont.height + 2 + padding * 2
43+
private val titleBarHeight get() = ClickGui.buttonHeight + padding * 2
4444
private val titleFont: IFontEntry
4545

4646
private val layer = RenderLayer()
@@ -50,13 +50,12 @@ abstract class WindowComponent <T : ChildComponent> (
5050
private val animation = owner.animation
5151
private val gui = owner
5252

53-
// Show animation for children
54-
private val showAnimation0 by animation.exp(0.0, 1.0, 0.5, ::isOpen)
55-
override val showAnimation get() = lerp(0.0, showAnimation0, gui.showAnimation)
53+
private val showAnimation by animation.exp(0.0, 1.0, 0.6, ::isOpen)
54+
override val childShowAnimation get() = lerp(0.0, showAnimation, gui.childShowAnimation)
5655

5756
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
5857
private var renderHeightAnimation by animation.exp({ 0.0 }, ::actualHeight, 0.6, ::isOpen)
59-
private val renderHeight get() = lerp(0.0, renderHeightAnimation, showAnimation)
58+
private val renderHeight get() = lerp(0.0, renderHeightAnimation, childShowAnimation)
6059

6160
val contentComponents = ChildLayer.Drawable<T>(gui, this, contentLayer, ::contentRect)
6261

@@ -71,7 +70,7 @@ abstract class WindowComponent <T : ChildComponent> (
7170
roundRadius = ClickGui.windowRadius
7271
shade = GuiSettings.shadeBackground
7372

74-
val alpha = (gui.showAnimation * 2.0).coerceIn(0.0, 1.0)
73+
val alpha = (gui.childShowAnimation * 2.0).coerceIn(0.0, 1.0)
7574
color(GuiSettings.backgroundColor.multAlpha(alpha))
7675
}
7776

@@ -81,15 +80,15 @@ abstract class WindowComponent <T : ChildComponent> (
8180
outerGlow = ClickGui.windowRadius
8281
shade = GuiSettings.shade
8382

84-
val alpha = (gui.showAnimation * 2.0).coerceIn(0.0, 1.0)
83+
val alpha = (gui.childShowAnimation * 2.0).coerceIn(0.0, 1.0)
8584
color(GuiSettings.mainColor.multAlpha(alpha))
8685
}
8786

8887
// Title
8988
titleFont = renderer.font {
9089
text = title
9190
position = titleBar.center - widthVec * 0.5
92-
color = Color.WHITE.setAlpha(gui.showAnimation)
91+
color = Color.WHITE.setAlpha(gui.childShowAnimation)
9392
}
9493
}
9594

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ abstract class ButtonComponent(
1919
abstract val size: Vec2d
2020

2121
abstract val text: String
22-
protected open val textY: Double get() = rect.size.y
2322
protected abstract var activeAnimation: Double
2423

2524
private val actualSize get() = Vec2d(if (size.x == FILL_PARENT) owner.rect.size.x else size.x, size.y)
@@ -32,7 +31,8 @@ abstract class ButtonComponent(
3231
private var hoverFontAnimation by animation.exp(0.0, 1.0, 0.5, ::renderHovered)
3332
private var pressAnimation by animation.exp(0.0, 1.0, 0.5, ::pressed)
3433
protected val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
35-
override val showAnimation: Double get() = owner.showAnimation
34+
override val childShowAnimation: Double get() = owner.childShowAnimation
35+
protected open val showAnimation get() = owner.childShowAnimation
3636

3737
private var lastHoveredTime = 0L
3838
private val renderHovered get() = hovered || System.currentTimeMillis() - lastHoveredTime < 110
@@ -51,7 +51,7 @@ abstract class ButtonComponent(
5151
position = hoverRect.shrink(interactAnimation)
5252
shade = GuiSettings.shade
5353

54-
val alpha = interactAnimation * 0.2
54+
val alpha = interactAnimation * 0.2 * showAnimation
5555
color(GuiSettings.mainColor.multAlpha(alpha))
5656
}
5757

@@ -63,7 +63,7 @@ abstract class ButtonComponent(
6363
color = lerp(Color.WHITE, GuiSettings.mainColor, activeAnimation).multAlpha(showAnimation)
6464

6565
val x = ClickGui.windowPadding + interactAnimation + hoverFontAnimation
66-
position = rect.leftTop + Vec2d(x, textY)
66+
position = rect.leftTop + Vec2d(x, rect.size.y * 0.5)
6767
}
6868
}
6969

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@ import com.lambda.graphics.animation.Animation.Companion.exp
44
import com.lambda.gui.api.GuiEvent
55
import com.lambda.gui.api.component.core.list.ChildLayer
66
import com.lambda.module.modules.client.ClickGui
7-
import com.lambda.util.math.MathUtils.toInt
7+
import com.lambda.util.math.MathUtils.lerp
88
import com.lambda.util.math.Vec2d
99

1010
abstract class ListButton(owner: ChildLayer.Drawable<*>) : ButtonComponent(owner) {
11-
override val position get() = Vec2d(0.0, renderHeightOffset)
11+
override val position get() = Vec2d(0.0, lerp(0.0, renderHeightOffset, owner.childShowAnimation))
1212
override val size get() = Vec2d(FILL_PARENT, ClickGui.buttonHeight)
1313

14+
open val listStep get() = ClickGui.buttonStep
15+
1416
var heightOffset = 0.0
15-
protected open val targetHeightOffset get() = heightOffset * owner.showAnimation * owner.isActive.toInt()
16-
private var renderHeightOffset by animation.exp(::targetHeightOffset, 0.7)
17+
protected open var renderHeightOffset by animation.exp(::heightOffset, 0.9)
1718

1819
override fun onEvent(e: GuiEvent) {
20+
if (e is GuiEvent.Show) {
21+
heightOffset = 0.0
22+
renderHeightOffset = 0.0
23+
}
1924
super.onEvent(e)
20-
if (e is GuiEvent.Show) renderHeightOffset = 0.0
2125
}
2226
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.lambda.util.math.Rect
55

66
interface IComponent {
77
val isActive: Boolean get() = true
8-
val showAnimation: Double get() = 1.0
8+
val childShowAnimation: Double get() = 1.0
99
val rect: Rect
1010

1111
fun onEvent(e: GuiEvent)

common/src/main/kotlin/com/lambda/gui/api/component/core/list/ChildLayer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open class ChildLayer <T : ChildComponent> (
1414
private val childAccessible: (T) -> Boolean = { true }
1515
) : IComponent {
1616
override val isActive get() = owner.isActive
17-
override val showAnimation get() = owner.showAnimation
17+
override val childShowAnimation get() = owner.childShowAnimation
1818
override val rect get() = childRect()
1919
val children = mutableListOf<T>()
2020

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
@@ -13,7 +13,7 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
1313
private var activeWindow: WindowComponent<*>? = null
1414
private var closing = false
1515

16-
final override var showAnimation by animation.exp(0.0, 1.0, {
16+
final override var childShowAnimation by animation.exp(0.0, 1.0, {
1717
if (closing) ClickGui.closeSpeed else ClickGui.openSpeed
1818
}) { !closing }; private set
1919

@@ -31,11 +31,11 @@ abstract class AbstractClickGui(name: String = "ClickGui") : LambdaGui(name, Cli
3131
is GuiEvent.Show -> {
3232
activeWindow = null
3333
closing = false
34-
showAnimation = 0.0
34+
childShowAnimation = 0.0
3535
}
3636

3737
is GuiEvent.Tick -> {
38-
if (closing && showAnimation < 0.01) mc.setScreen(null)
38+
if (closing && childShowAnimation < 0.01) mc.setScreen(null)
3939
}
4040

4141
is GuiEvent.MouseClick -> {

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
package com.lambda.gui.impl.clickgui.buttons
22

3+
import com.lambda.config.settings.comparable.BooleanSetting
34
import com.lambda.graphics.animation.Animation.Companion.exp
45
import com.lambda.graphics.gl.Scissor.scissor
56
import com.lambda.gui.api.GuiEvent
67
import com.lambda.gui.api.component.button.ListButton
78
import com.lambda.gui.api.component.core.list.ChildLayer
89
import com.lambda.gui.api.layer.RenderLayer
10+
import com.lambda.gui.impl.clickgui.buttons.setting.BooleanButton
911
import com.lambda.module.Module
1012
import com.lambda.module.modules.client.GuiSettings
1113
import com.lambda.util.Mouse
1214
import com.lambda.util.math.ColorUtils.multAlpha
1315
import com.lambda.util.math.MathUtils.lerp
14-
import com.lambda.util.math.MathUtils.toInt
16+
import com.lambda.util.math.Rect
1517
import com.lambda.util.math.Vec2d
1618
import kotlin.math.abs
1719

1820
class ModuleButton(val module: Module, owner: ChildLayer.Drawable<*>) : ListButton(owner) {
1921
override val text get() = module.name
20-
private val active get() = module.isEnabled
22+
private val enabled get() = module.isEnabled
2123

22-
override val size: Vec2d get() = super.size + Vec2d(0.0, renderHeight)
23-
override val textY: Double get() = super.size.y * 0.5
24+
override var activeAnimation by animation.exp(0.0, 1.0, 0.15, ::enabled)
25+
private val toggleFxDirection by animation.exp(0.0, 1.0, 0.7, ::enabled)
2426

25-
override var activeAnimation by animation.exp(0.0, 1.0, 0.15, ::active)
26-
private val toggleFxDirection by animation.exp(0.0, 1.0, 0.7, ::active)
27+
override val listStep: Double get() = super.listStep * 2.0 + renderHeight
2728

2829
private var isOpen = false
2930
override val isActive get() = isOpen
3031

32+
private val childShowAnimation0 by animation.exp(0.0, 1.0, 0.7, ::isOpen)
33+
override val childShowAnimation get() = lerp(0.0, childShowAnimation0, owner.childShowAnimation)
34+
3135
private var settingsHeight = 0.0
3236
private var renderHeight by animation.exp(::settingsHeight, 0.6)
33-
private val settingsRect get() = rect.moveFirst(Vec2d(0.0, super.size.y))
37+
private val settingsRect get() = rect
38+
.moveFirst(Vec2d(childShowAnimation * 2.0, size.y + super.listStep))
39+
.moveSecond(Vec2d(0.0, listStep))
3440

3541
private val settingsRenderer = RenderLayer()
36-
private val settingsLayer = ChildLayer.Drawable<SettingButton<*, *>>(owner.gui, this, settingsRenderer, ::settingsRect)
42+
private val settingsLayer = ChildLayer.Drawable(owner.gui, this, settingsRenderer, ::settingsRect, SettingButton<*, *>::visible)
3743

3844
init {
3945
// Toggle fx
@@ -56,36 +62,72 @@ class ModuleButton(val module: Module, owner: ChildLayer.Drawable<*>) : ListButt
5662
shade = GuiSettings.shade
5763
colorH(leftColor, rightColor)
5864
}
65+
66+
// Line
67+
renderer.filled {
68+
val pos1 = Vec2d(rect.left, settingsRect.top)
69+
val pos2 = Vec2d(rect.left + childShowAnimation * 1.0, settingsRect.bottom)
70+
val color = GuiSettings.mainColor.multAlpha(childShowAnimation * 0.6)
71+
72+
position = Rect(pos1, pos2)
73+
74+
shade = GuiSettings.shade
75+
color(color)
76+
}
77+
78+
module.settings.mapNotNull {
79+
when (it) {
80+
is BooleanSetting -> BooleanButton(it, settingsLayer)
81+
else -> null
82+
}
83+
}.forEach(settingsLayer::addChild)
5984
}
6085

6186
override fun onEvent(e: GuiEvent) {
62-
super.onEvent(e)
63-
6487
when (e) {
6588
is GuiEvent.Show -> {
6689
isOpen = false
67-
renderHeight = 0.0
90+
updateHeight(true)
6891
}
6992

7093
is GuiEvent.Tick -> {
71-
updateSettingsHeight()
94+
if (renderHeight > 0.5) {
95+
updateHeight()
96+
}
7297
}
7398

7499
is GuiEvent.Render -> {
75-
scissor(settingsRect) {
76-
settingsRenderer.render()
77-
settingsLayer.onEvent(e)
100+
var y = 0.0
101+
settingsLayer.children.filter(SettingButton<*, *>::visible).forEach { button ->
102+
button.heightOffset = y
103+
y += button.size.y + button.listStep
104+
}
105+
106+
if (renderHeight > 0.5) {
107+
scissor(settingsRect) {
108+
settingsLayer.onEvent(e)
109+
settingsRenderer.render()
110+
}
78111
}
112+
79113
return
80114
}
81115
}
116+
117+
super.onEvent(e)
118+
settingsLayer.onEvent(e)
82119
}
83120

84-
private fun updateSettingsHeight() {
85-
/*val c = settingsLayer.children
86-
settingsHeight = c.sumOf { it.size.y } + ((c.size - 1) * ClickGui.buttonStep).coerceAtLeast(0.0)*/
121+
private fun updateHeight(forceAnimation: Boolean = false) {
122+
settingsHeight = if (isOpen) {
123+
var lastStep = 0.0
124+
settingsLayer.children
125+
.filter(SettingButton<*, *>::visible)
126+
.sumOf { lastStep = it.listStep; it.size.y + it.listStep } - lastStep
127+
}
128+
else 0.0
87129

88-
settingsHeight = 30.0 * isOpen.toInt()
130+
if (forceAnimation) renderHeight = settingsHeight
89131
}
90132

91133
override fun performClickAction(e: GuiEvent.MouseClick) {
@@ -97,7 +139,8 @@ class ModuleButton(val module: Module, owner: ChildLayer.Drawable<*>) : ListButt
97139
if (abs(targetHeight - renderHeight) > 1) return
98140

99141
isOpen = !isOpen
100-
updateSettingsHeight()
142+
if (isOpen) settingsLayer.onEvent(GuiEvent.Show())
143+
updateHeight()
101144
}
102145
}
103146
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,23 @@ package com.lambda.gui.impl.clickgui.buttons
22

33
import com.lambda.config.AbstractSetting
44
import com.lambda.graphics.animation.Animation.Companion.exp
5-
import com.lambda.gui.api.GuiEvent
65
import com.lambda.gui.api.component.button.ListButton
76
import com.lambda.gui.api.component.core.list.ChildLayer
87
import com.lambda.util.math.MathUtils.lerp
9-
import com.lambda.util.math.MathUtils.toInt
108

119
abstract class SettingButton <V : Any, T : AbstractSetting<V>> (
1210
val setting: T,
1311
owner: ChildLayer.Drawable<*>
1412
): ListButton(owner) {
1513
protected var value by setting
16-
var visible = false
1714

18-
private var visibilityAnimation by animation.exp({ 0.0 }, { 1.0 }, { if (visible) 0.2 else 0.8 }, ::visible)
19-
override val showAnimation get() = lerp(0.0, super.showAnimation, visibilityAnimation)
15+
val visible; get() = setting.visibility()
2016

21-
override var accessible: Boolean = false; get() = field && visible
17+
private var visibilityAnimation by animation.exp(0.0, 1.0, 0.7, ::visible)
18+
override val showAnimation get() = lerp(0.0, super.showAnimation, visibilityAnimation)
19+
override var activeAnimation = 0.0
2220

23-
override fun onEvent(e: GuiEvent) {
24-
if (e is GuiEvent.Show || e is GuiEvent.Tick) visible = setting.visibility()
25-
if (e is GuiEvent.Show) visibilityAnimation = visible.toInt().toDouble()
26-
super.onEvent(e)
27-
}
21+
override var renderHeightOffset
22+
get() = heightOffset + lerp(-size.y, 0.0, visibilityAnimation)
23+
set(value) { heightOffset = value }
2824
}

0 commit comments

Comments
 (0)