-
Notifications
You must be signed in to change notification settings - Fork 3
/
Animation.kt
151 lines (128 loc) · 6.23 KB
/
Animation.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* This file is part of PolyUI
* PolyUI - Fast and lightweight UI framework
* Copyright (C) 2023-2024 Polyfrost and its contributors.
* <https://polyfrost.org> <https://github.com/Polyfrost/polui-jvm>
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* PolyUI is licensed under the terms of version 3 of the GNU Lesser
* General Public License as published by the Free Software Foundation,
* AND the simple request that you adequately accredit us if you use PolyUI.
* See details here <https://github.com/Polyfrost/polyui-jvm/ACCREDITATION.md>.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License. If not, see <https://www.gnu.org/licenses/>.
*/
package org.polyfrost.polyui.animate
/**
* # Animation
*
* An animation used by the PolyUI system.
*
* PolyUI comes with many default [animations][org.polyfrost.polyui.animate.Easing], which you can use. To give the user some choice, you can also use the [Animations.create()][Animation.Type.create] to dynamically create them.
*
* check out [ComponentOps][org.polyfrost.polyui.operations.ComponentOp] for more information on how to use animations with components.
*/
abstract class Animation(var durationNanos: Long, var from: Float, var to: Float) : Cloneable {
inline val range get() = to - from
var passedTime = 0f
protected set
inline val isFinished get() = passedTime >= durationNanos
/**
* finish the animation now.
* @since 1.0.5
*/
fun finishNow() {
passedTime = durationNanos.toFloat()
update(0L)
}
fun reset() {
passedTime = 0f
value = from
}
/**
* Set [from] to the current [value], extend [to] with [by], and reset the [passedTime] to 0.
*
* Essentially makes the animation curve start again with the new value appended.
* Might look odd with in-out ease functions.
*
* @since 1.6.1
*/
fun extend(by: Float) {
this.from = this.value
this.to += by
passedTime = 0f
}
fun reverse() {
val temp = from
from = to
to = temp
reset()
}
var value = from
private set
fun update(deltaTimeNanos: Long): Float {
if (isFinished) return to
passedTime += deltaTimeNanos
value = getValue((passedTime / durationNanos).coerceIn(0f, 1f)) * range + from
return value
}
protected abstract fun getValue(percent: Float): Float
public abstract override fun clone(): Animation
enum class Type {
Linear, Default,
EaseOutQuad, EaseInQuad, EaseInOutQuad,
EaseOutCubic, EaseInCubic, EaseInOutCubic,
EaseOutQuart, EaseInQuart, EaseInOutQuart,
EaseOutQuint, EaseInQuint, EaseInOutQuint,
EaseOutBack, EaseInBack, EaseInOutBack,
EaseOutBump, EaseInBump, EaseInOutBump,
EaseOutExpo, EaseInExpo, EaseInOutExpo,
EaseOutSine, EaseInSine, EaseInOutSine,
EaseOutCirc, EaseInCirc, EaseInOutCirc,
EaseOutElastic, EaseInElastic, EaseInOutElastic;
/** create an animation based on the type.
* @see Animations */
fun create(durationNanos: Long, start: Float = 0f, end: Float = 1f): Animation {
return when (this) {
Linear -> Linear(durationNanos, start, end)
EaseInBack -> Easing.Back(Easing.Type.In, durationNanos, start, end)
EaseOutBack -> Easing.Back(Easing.Type.Out, durationNanos, start, end)
EaseInOutBack -> Easing.Back(Easing.Type.InOut, durationNanos, start, end)
EaseInBump -> Easing.Bump(Easing.Type.In, durationNanos, start, end)
EaseOutBump -> Easing.Bump(Easing.Type.Out, durationNanos, start, end)
EaseInOutBump -> Easing.Bump(Easing.Type.InOut, durationNanos, start, end)
EaseInQuad -> Easing.Quad(Easing.Type.In, durationNanos, start, end)
EaseOutQuad -> Easing.Quad(Easing.Type.Out, durationNanos, start, end)
EaseInOutQuad -> Easing.Quad(Easing.Type.InOut, durationNanos, start, end)
EaseInCubic -> Easing.Cubic(Easing.Type.In, durationNanos, start, end)
EaseOutCubic -> Easing.Cubic(Easing.Type.Out, durationNanos, start, end)
EaseInOutCubic -> Easing.Cubic(Easing.Type.InOut, durationNanos, start, end)
EaseInQuart -> Easing.Quart(Easing.Type.In, durationNanos, start, end)
EaseOutQuart -> Easing.Quart(Easing.Type.Out, durationNanos, start, end)
EaseInOutQuart -> Easing.Quart(Easing.Type.InOut, durationNanos, start, end)
EaseInQuint -> Easing.Quint(Easing.Type.In, durationNanos, start, end)
EaseOutQuint -> Easing.Quint(Easing.Type.Out, durationNanos, start, end)
EaseInOutQuint -> Easing.Quint(Easing.Type.InOut, durationNanos, start, end)
EaseInCirc -> Easing.Circ(Easing.Type.In, durationNanos, start, end)
EaseOutCirc -> Easing.Circ(Easing.Type.Out, durationNanos, start, end)
Default, EaseInOutCirc -> Easing.Circ(Easing.Type.InOut, durationNanos, start, end)
EaseInExpo -> Easing.Expo(Easing.Type.In, durationNanos, start, end)
EaseOutExpo -> Easing.Expo(Easing.Type.Out, durationNanos, start, end)
EaseInOutExpo -> Easing.Expo(Easing.Type.InOut, durationNanos, start, end)
EaseInSine -> Easing.Sine(Easing.Type.In, durationNanos, start, end)
EaseOutSine -> Easing.Sine(Easing.Type.Out, durationNanos, start, end)
EaseInOutSine -> Easing.Sine(Easing.Type.InOut, durationNanos, start, end)
EaseInElastic -> Easing.Elastic(Easing.Type.In, durationNanos, start, end)
EaseOutElastic -> Easing.Elastic(Easing.Type.Out, durationNanos, start, end)
EaseInOutElastic -> Easing.Elastic(Easing.Type.InOut, durationNanos, start, end)
}
}
}
}
typealias Animations = Animation.Type