mirror of
https://gitlab.com/cc-ru/ocelot/ocelot-desktop.git
synced 2025-12-20 11:09:20 +01:00
63 lines
1.3 KiB
Scala
63 lines
1.3 KiB
Scala
package ocelot.desktop.util.animation
|
|
|
|
import ocelot.desktop.ui.UiHandler
|
|
import ocelot.desktop.util.animation.easing.{Easing, EasingFunction}
|
|
|
|
class ValueAnimation(init: Float = 0f, speed: Float = 7f) {
|
|
private var _value = init
|
|
private var start = init
|
|
private var end = init
|
|
private var time = 0f
|
|
private var reached = false
|
|
private var _easing: EasingFunction = Easing.easeInOutQuad
|
|
private var nextEasing = _easing
|
|
|
|
def easing: EasingFunction = _easing
|
|
|
|
def easing_=(value: EasingFunction): Unit = {
|
|
nextEasing = value
|
|
updateEasing()
|
|
}
|
|
|
|
def value: Float = _value
|
|
|
|
def jump(to: Float): Unit = {
|
|
_value = to
|
|
start = to
|
|
end = to
|
|
updateReached()
|
|
}
|
|
|
|
def goto(to: Float): Unit = {
|
|
start = _value
|
|
end = to
|
|
time = 0
|
|
updateReached()
|
|
}
|
|
|
|
def update(): Unit = {
|
|
if (reached) return
|
|
|
|
val dist = (end - start).abs
|
|
val dt = (UiHandler.dt * speed) / dist
|
|
time = (time + dt).min(1).max(0)
|
|
|
|
_value = if (start > end)
|
|
start - time * dist
|
|
else
|
|
start + time * dist
|
|
|
|
updateReached()
|
|
if (reached) _easing = nextEasing
|
|
}
|
|
|
|
private def updateReached(): Unit = {
|
|
reached = (_value - end).abs < 0.001
|
|
}
|
|
|
|
private def updateEasing(): Unit = {
|
|
updateReached()
|
|
if (reached) _easing = nextEasing
|
|
}
|
|
}
|