aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2020-01-19 21:46:03 -0800
committerGitHub <noreply@github.com>2020-01-19 21:46:03 -0800
commitc2710cfcaf06f3dbd3a912a6231c170ee6cd6a45 (patch)
tree9726de18196a34db738e4561600cd1aa9ab1c162 /core/src/main/kotlin
parentf9dded97065f45848261e09e9358033c821312c9 (diff)
parentb7fd1c5c7e848d7cf12c49a1e9e319d4e11d0b5c (diff)
downloadkau-c2710cfcaf06f3dbd3a912a6231c170ee6cd6a45.tar.gz
kau-c2710cfcaf06f3dbd3a912a6231c170ee6cd6a45.tar.bz2
kau-c2710cfcaf06f3dbd3a912a6231c170ee6cd6a45.zip
Merge pull request #241 from AllanWang/progress-animator
Add int functions for ProgressAnimator
Diffstat (limited to 'core/src/main/kotlin')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt120
1 files changed, 82 insertions, 38 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
index bb4cd88..dad825e 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt
@@ -33,41 +33,50 @@ class ProgressAnimator private constructor() : ValueAnimator() {
companion object {
- fun ofFloat(builder: ProgressAnimator.() -> Unit): ProgressAnimator = ProgressAnimator().apply {
- setFloatValues(0f, 1f)
- addUpdateListener { apply(it.animatedValue as Float) }
- addListener(object : AnimatorListenerAdapter() {
- override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
- isCancelled = false
- startActions.runAll()
- }
+ fun ofFloat(builder: ProgressAnimator.() -> Unit = {}): ProgressAnimator =
+ ProgressAnimator().apply {
+ setFloatValues(0f, 1f)
+ addUpdateListener { apply(it.animatedValue as Float) }
+ addListener(object : AnimatorListenerAdapter() {
+ override fun onAnimationStart(animation: Animator?, isReverse: Boolean) {
+ isCancelled = false
+ startActions.runAll()
+ }
+
+ override fun onAnimationCancel(animation: Animator?) {
+ isCancelled = true
+ cancelActions.runAll()
+ }
+
+ override fun onAnimationEnd(animation: Animator?) {
+ endActions.runAll()
+ isCancelled = false
+ }
+ })
+ }.apply(builder)
- override fun onAnimationCancel(animation: Animator?) {
- isCancelled = true
- cancelActions.runAll()
- }
+ /**
+ * Gets output of a linear function starting at [start] when [progress] is 0 and [end] when [progress] is 1 at point [progress].
+ */
+ fun progress(start: Float, end: Float, progress: Float): Float =
+ start + (end - start) * progress
- override fun onAnimationEnd(animation: Animator?) {
- endActions.runAll()
- isCancelled = false
+ fun progress(start: Float, end: Float, progress: Float, min: Float, max: Float): Float =
+ when {
+ min == max -> throw IllegalArgumentException("Progress range cannot be 0 (min == max == $min")
+ progress <= min -> start
+ progress >= max -> end
+ else -> {
+ val trueProgress = (progress - min) / (max - min)
+ start + (end - start) * trueProgress
}
- })
- }.apply(builder)
+ }
/**
- * Gets output of a linear function starting at [start] when [progress] is 0 and [end] when [progress] is 1 at point [progress].
+ * Progress variant that takes in and returns int
*/
- fun progress(start: Float, end: Float, progress: Float): Float = start + (end - start) * progress
-
- fun progress(start: Float, end: Float, progress: Float, min: Float, max: Float): Float = when {
- min == max -> throw IllegalArgumentException("Progress range cannot be 0 (min == max == $min")
- progress <= min -> start
- progress >= max -> end
- else -> {
- val trueProgress = (progress - min) / (max - min)
- start + (end - start) * trueProgress
- }
- }
+ fun progress(start: Int, end: Int, progress: Float): Int =
+ (start + (end - start) * progress).toInt()
}
private val animators: MutableList<ProgressDisposableAction> = mutableListOf()
@@ -85,7 +94,7 @@ class ProgressAnimator private constructor() : ValueAnimator() {
/**
* Converts an action to a disposable action
*/
- private fun ProgressAction.asDisposable(): ProgressDisposableAction = { this(it); false }
+ private fun <T> ((T) -> Unit).asDisposable(): (T) -> Boolean = { this(it); false }
private fun ProgressRunnable.asDisposable(): ProgressDisposableRunnable = { this(); false }
@@ -117,6 +126,9 @@ class ProgressAnimator private constructor() : ValueAnimator() {
fun withAnimator(from: Float, to: Float, action: ProgressAction) =
withDisposableAnimator(from, to, action.asDisposable())
+ fun withAnimator(from: Int, to: Int, action: ProgressIntAction) =
+ withDisposableAnimator(from, to, action.asDisposable())
+
fun withDisposableAnimator(action: ProgressDisposableAction) = animators.add(action)
fun withDisposableAnimator(from: Float, to: Float, action: ProgressDisposableAction) {
@@ -127,30 +139,59 @@ class ProgressAnimator private constructor() : ValueAnimator() {
}
}
- fun withRangeAnimator(min: Float, max: Float, start: Float, end: Float, progress: Float, action: ProgressAction) {
- if (min >= max) {
- throw IllegalArgumentException("Range animator must have min < max; currently min=$min, max=$max")
+ fun withDisposableAnimator(from: Int, to: Int, action: ProgressIntDisposableAction) {
+ if (to != from) {
+ animators.add {
+ action(progress(from, to, it))
+ }
}
+ }
+
+ fun withRangeAnimator(
+ min: Float,
+ max: Float,
+ start: Float,
+ end: Float,
+ action: ProgressAction
+ ) {
+ require(min < max) { "Range animator must have min < max; currently min=$min, max=$max" }
withDisposableAnimator {
when {
- it > max -> true
+ it > max -> {
+ action(end) // apply action in case frames were skipped
+ true
+ }
it < min -> false
else -> {
- action(progress(start, end, progress, min, max))
+ action(progress(start, end, it, min, max))
false
}
}
}
}
- fun withPointAnimator(point: Float, action: ProgressAction) {
+ @Deprecated(
+ level = DeprecationLevel.WARNING,
+ message = "Renamed to withPointAction",
+ replaceWith = ReplaceWith("withPointAction(point, action)")
+ )
+ fun withPointAnimator(point: Float, action: ProgressAction) =
+ withPointAction(point, isAscendingProgress = true, action = action)
+
+ fun withPointAction(point: Float, isAscendingProgress: Boolean = true, action: ProgressAction) {
animators.add {
- action.runIf(it >= point, it)
+ action.runIf(
+ (isAscendingProgress && (it >= point) || !isAscendingProgress && (it <= point)),
+ it
+ )
}
}
fun withDelayedStartAction(skipCount: Int, action: ProgressAction) {
var count = 0
+ withStartAction {
+ count = 0
+ }
animators.add {
action.runIf(count++ >= skipCount, it)
}
@@ -163,7 +204,8 @@ class ProgressAnimator private constructor() : ValueAnimator() {
fun withDisposableStartAction(action: ProgressDisposableRunnable) = startActions.add(action)
- fun withCancelAction(action: ProgressRunnable) = withDisposableCancelAction(action.asDisposable())
+ fun withCancelAction(action: ProgressRunnable) =
+ withDisposableCancelAction(action.asDisposable())
fun withDisposableCancelAction(action: ProgressDisposableRunnable) = cancelActions.add(action)
@@ -185,5 +227,7 @@ class ProgressAnimator private constructor() : ValueAnimator() {
private typealias ProgressAction = (Float) -> Unit
private typealias ProgressDisposableAction = (Float) -> Boolean
+private typealias ProgressIntAction = (Int) -> Unit
+private typealias ProgressIntDisposableAction = (Int) -> Boolean
private typealias ProgressRunnable = () -> Unit
private typealias ProgressDisposableRunnable = () -> Boolean