Skip to content

Commit 3559754

Browse files
committed
Better animations, refactor
1 parent 06acbfe commit 3559754

File tree

6 files changed

+47
-19
lines changed

6 files changed

+47
-19
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.lambda.util.Mouse
1515
import com.lambda.util.math.MathUtils.toInt
1616
import com.lambda.util.math.Rect
1717
import com.lambda.util.math.Vec2d
18+
import kotlin.math.abs
1819

1920
abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IListComponent<T> {
2021
abstract val title: String
@@ -41,7 +42,8 @@ abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IL
4142
override val children = mutableListOf<T>()
4243
val subLayer = RenderLayer(true)
4344

44-
private val renderHeight by animation.exp({ 0.0 }, { height + padding * 2 * isOpen.toInt() }, 0.5, ::isOpen)
45+
private val actualHeight get() = height + padding * 2 * isOpen.toInt()
46+
private var renderHeight by animation.exp({ 0.0 }, ::actualHeight, 0.5, ::isOpen)
4547

4648
init {
4749
// Background
@@ -63,6 +65,7 @@ abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IL
6365
super<IListComponent>.onShow()
6466

6567
dragOffset = null
68+
renderHeight = 0.0
6669
}
6770

6871
override fun onHide() {
@@ -72,12 +75,12 @@ abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IL
7275
override fun onTick() {
7376
animation.tick()
7477

75-
children.forEach { child ->
76-
child.visible = isChildAccessible(child)
78+
setChildrenAccessibility { child ->
79+
child.rect in contentRect
7780
}
7881

7982
children
80-
.filter(ChildComponent::visible)
83+
.filter(ChildComponent::accessible)
8184
.forEach(IComponent::onTick)
8285
}
8386

@@ -115,13 +118,22 @@ abstract class WindowComponent <T : ChildComponent> : InteractiveComponent(), IL
115118
if (mouse in titleBar && action == Mouse.Action.Click) {
116119
when(button) {
117120
Mouse.Button.Left -> dragOffset = mouse - position
118-
Mouse.Button.Right -> isOpen = !isOpen
121+
Mouse.Button.Right -> {
122+
// Don't let user spam
123+
val targetHeight = if (isOpen) actualHeight else 0.0
124+
if (abs(targetHeight - renderHeight) > 1) return
125+
126+
isOpen = !isOpen
127+
}
119128
}
120129
}
121130

122131
super<IListComponent>.onMouseClick(button, action, mouse)
123132
}
124133

125-
override fun isChildAccessible(child: T) =
126-
child.rect in contentRect
134+
private fun setChildrenAccessibility(flag: (T) -> Boolean) {
135+
children.forEach { child ->
136+
child.accessible = flag(child)
137+
}
138+
}
127139
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.lambda.gui.api.component.InteractiveComponent
44

55
abstract class ChildComponent : InteractiveComponent(), IChildComponent {
66
// mostly used to create an animation when an element appears
7-
var visible = false; set(value) {
7+
var accessible = false; set(value) {
88
if (field == value) return
99
field = value
1010

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.lambda.util.math.Vec2d
88
interface IListComponent <T : IComponent> : IComponent {
99
val children: List<T>
1010

11-
fun isChildAccessible(child: T): Boolean = true
11+
fun isChildAccessible(child: T): Boolean = (child as? ChildComponent)?.accessible ?: true
1212

1313
override fun onShow() {
1414
children.forEach(IComponent::onShow)

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ abstract class ButtonComponent(final override val owner: WindowComponent<*>) : C
2929
private val animation = AnimationTicker()
3030

3131
private var activeAnimation by animation.exp(0.0, 1.0, 0.1, ::active)
32-
private var hoverAnimation by animation.exp({ 0.0 }, { 1.0 }, { if (renderHovered || active) 0.5 else 0.1 }, ::renderHovered)
32+
private var hoverRectAnimation by animation.exp({ 0.0 }, { 1.0 }, { if (renderHovered) 0.5 else 0.1 }, ::renderHovered)
33+
private var hoverFontAnimation by animation.exp(0.0, 1.0, 0.5, ::renderHovered)
3334
private var pressAnimation by animation.exp(0.0, 1.0, 0.5, ::pressed)
34-
private val interactAnimation get() = lerp(hoverAnimation, 1.5, pressAnimation) * 0.4
35+
private val interactAnimation get() = lerp(hoverRectAnimation, 1.5, pressAnimation) * 0.4
3536

3637
private var lastHoveredTime = 0L
3738
private val renderHovered get() = hovered ||
@@ -46,7 +47,7 @@ abstract class ButtonComponent(final override val owner: WindowComponent<*>) : C
4647

4748
// Hover glint
4849
layer.rect.build {
49-
val hoverRect = Rect.basedOn(rect.leftTop, rect.size.x * hoverAnimation, rect.size.y)
50+
val hoverRect = Rect.basedOn(rect.leftTop, rect.size.x * hoverRectAnimation, rect.size.y)
5051
position = hoverRect.shrink(interactAnimation)
5152

5253
val alpha = interactAnimation * 0.3
@@ -80,7 +81,7 @@ abstract class ButtonComponent(final override val owner: WindowComponent<*>) : C
8081

8182
color = lerp(Color.WHITE, GuiSettings.mainColor, activeAnimation)
8283

83-
val x = rect.left + ClickGui.windowPadding + interactAnimation + hoverAnimation
84+
val x = rect.left + ClickGui.windowPadding + interactAnimation + hoverFontAnimation * 2.0
8485
position = Vec2d(x, rect.center.y)
8586
}
8687
}
@@ -89,7 +90,12 @@ abstract class ButtonComponent(final override val owner: WindowComponent<*>) : C
8990

9091
override fun onShow() {
9192
super.onShow()
92-
activeAnimation = 0.0
93+
reset()
94+
}
95+
96+
override fun onHide() {
97+
super.onHide()
98+
reset()
9399
}
94100

95101
override fun onTick() {
@@ -107,6 +113,13 @@ abstract class ButtonComponent(final override val owner: WindowComponent<*>) : C
107113
if (hovered) lastHoveredTime = time
108114
}
109115

116+
private fun reset() {
117+
activeAnimation = 0.0
118+
hoverRectAnimation = 0.0
119+
pressAnimation = 0.0
120+
lastHoveredTime = 0L
121+
}
122+
110123
companion object {
111124
const val FILL_PARENT = -1.0
112125
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package com.lambda.gui.impl.clickgui
33
import com.lambda.config.Configurable
44
import com.lambda.config.configurations.GuiConfig
55
import com.lambda.gui.impl.clickgui.windows.TagWindow
6-
import com.lambda.module.tag.ModuleTag
76

87
object GuiConfigurable : Configurable(GuiConfig) {
98
override val name = "gui"
10-
val windows = setting("windows", listOf(TagWindow(ModuleTag("Test Window"))))
9+
val windows = setting("windows", listOf(TagWindow()))
1110
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.lambda.gui.impl.clickgui.windows
22

3+
import com.lambda.gui.api.LambdaGui
34
import com.lambda.gui.api.component.WindowComponent
5+
import com.lambda.gui.api.component.core.list.IChildComponent
6+
import com.lambda.gui.impl.clickgui.LambdaClickGui
47
import com.lambda.gui.impl.clickgui.buttons.ModuleButton
58
import com.lambda.module.ModuleRegistry
69
import com.lambda.module.modules.client.ClickGui
@@ -10,11 +13,12 @@ class TagWindow(
1013
val tags: Set<ModuleTag> = setOf(),
1114
override var title: String = "Untitled",
1215
override var width: Double = 110.0,
13-
override var height: Double = 300.0
14-
) : WindowComponent<ModuleButton>() {
16+
override var height: Double = 300.0,
17+
override val owner: LambdaGui = LambdaClickGui
18+
) : WindowComponent<ModuleButton>(), IChildComponent {
1519
init {
1620
ModuleRegistry.modules.filter { module ->
17-
module.customTags.value.any(tags::contains)
21+
module.customTags.value.any(tags::contains) || tags.isEmpty()
1822
}.forEach {
1923
children.add(ModuleButton(it, this))
2024
}

0 commit comments

Comments
 (0)