-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
653d427
commit 67f348c
Showing
17 changed files
with
223 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...avp/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/avp/animator/AnimatorSpec.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.github.alexzhirkevich.compottie.avp.animator | ||
|
||
public interface AnimatorSpec { | ||
|
||
public suspend fun load() : ObjectAnimator<*,*> | ||
} |
93 changes: 0 additions & 93 deletions
93
...vp/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/avp/animator/BrushAnimator.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...vp/src/commonMain/kotlin/io/github/alexzhirkevich/compottie/avp/animator/PaintAnimator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package io.github.alexzhirkevich.compottie.avp.animator | ||
|
||
import androidx.compose.animation.core.Easing | ||
import androidx.compose.animation.core.LinearEasing | ||
import androidx.compose.ui.geometry.Offset | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.LinearGradientShader | ||
import androidx.compose.ui.graphics.Shader | ||
import androidx.compose.ui.graphics.TileMode | ||
import androidx.compose.ui.graphics.lerp as colorLerp | ||
import kotlin.math.min | ||
import androidx.compose.ui.geometry.lerp as offsetLerp | ||
import androidx.compose.ui.util.lerp as floatLerp | ||
|
||
internal sealed interface ColorData { | ||
|
||
sealed interface GradientColorData : ColorData { | ||
val colorStops : List<Pair<Float, Color>> | ||
} | ||
|
||
class Solid(val color: Color) : ColorData | ||
|
||
class LinearGradient( | ||
override val colorStops : List<Pair<Float, Color>>, | ||
val start : Offset, | ||
val end : Offset, | ||
val tileMode: TileMode | ||
) : GradientColorData { | ||
val colors = colorStops.map { it.second } | ||
val stops = colorStops.map { it.first } | ||
} | ||
} | ||
|
||
|
||
internal class PaintData( | ||
var color: Color = Color.Transparent, | ||
var shader : Shader? = null | ||
) | ||
|
||
internal sealed class PaintAnimator : ObjectAnimator<ColorData, PaintData>() | ||
|
||
internal class DynamicPaintAnimator( | ||
override val duration: Float, | ||
override val valueFrom: ColorData, | ||
override val valueTo: ColorData, | ||
override val delay: Float, | ||
override val interpolator: Easing | ||
) : ObjectAnimator<ColorData, PaintData>() { | ||
|
||
private val paintData = PaintData() | ||
|
||
private val colors = if ( | ||
valueFrom is ColorData.GradientColorData && | ||
valueTo is ColorData.GradientColorData | ||
) { | ||
List(min(valueFrom.colorStops.size, valueTo.colorStops.size)) { | ||
Color.Transparent | ||
}.toMutableList() | ||
} else { | ||
mutableListOf() | ||
} | ||
private val colorStops = colors.map { 0f }.toMutableList() | ||
|
||
override fun interpolate(progress: Float): PaintData { | ||
|
||
paintData.color = Color.Transparent | ||
paintData.shader = null | ||
|
||
if (valueFrom is ColorData.Solid && valueTo is ColorData.Solid) { | ||
paintData.color = colorLerp(valueFrom.color, valueTo.color, progress) | ||
} | ||
if (valueFrom is ColorData.LinearGradient && valueTo is ColorData.LinearGradient) { | ||
|
||
repeat(colors.size) { | ||
colors[it] = colorLerp( | ||
valueFrom.colors[it], | ||
valueTo.colors[it], | ||
progress | ||
) | ||
colorStops[it] = floatLerp( | ||
valueFrom.stops[it], | ||
valueTo.stops[it], | ||
progress | ||
) | ||
} | ||
paintData.shader = LinearGradientShader( | ||
from = offsetLerp(valueFrom.start, valueTo.start, progress), | ||
to = offsetLerp(valueFrom.end, valueTo.end, progress), | ||
colors = colors, | ||
colorStops = colorStops, | ||
tileMode = valueTo.tileMode | ||
) | ||
} | ||
|
||
return paintData | ||
} | ||
} | ||
|
||
internal class StaticPaintAnimator( | ||
val value : ColorData | ||
) : PaintAnimator(){ | ||
|
||
override val delay: Float get() = 0f | ||
override val duration: Float get() = 0f | ||
override val valueFrom: ColorData get() = value | ||
override val valueTo: ColorData get() = value | ||
override val interpolator: Easing get() = LinearEasing | ||
|
||
private val paint = PaintData() | ||
override fun interpolate(progress: Float): PaintData { | ||
paint.color = Color.Transparent | ||
paint.shader = null | ||
when (value){ | ||
is ColorData.LinearGradient -> { | ||
paint.shader = LinearGradientShader( | ||
from = value.start, | ||
to = value.end, | ||
colors = value.colors, | ||
colorStops = value.stops, | ||
tileMode = value.tileMode | ||
) | ||
} | ||
is ColorData.Solid -> { | ||
paint.color = value.color | ||
} | ||
} | ||
|
||
return paint | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...mmonMain/kotlin/io/github/alexzhirkevich/compottie/avp/animator/rememberObjectAnimator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.alexzhirkevich.compottie.avp.animator | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
|
||
@Composable | ||
public fun rememberObjectAnimator(xml : suspend () -> String) : AnimatorSpec { | ||
return remember { | ||
object : AnimatorSpec { | ||
override suspend fun load(): ObjectAnimator<*, *> { | ||
xml().encodeToByteArray(). | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
public fun rememberObjectAnimator(xml : ByteArray) : AnimatorSpec { | ||
l | ||
} | ||
|
Oops, something went wrong.