Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed Jul 15, 2024
1 parent 653d427 commit 67f348c
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,31 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.IntSize
import io.github.alexzhirkevich.compottie.avp.animator.ObjectAnimator
import org.jetbrains.compose.resources.DefaultComposeEnvironment
import org.jetbrains.compose.resources.DrawableResource
import org.jetbrains.compose.resources.getResourceItemByEnvironment
import kotlin.math.roundToInt



@Composable
public fun rememberAnimatedVectorPainter(
resource : DrawableResource,
animations : List<ObjectAnimator<*,*>>,
isAtAnd : Boolean
) : Painter {
TODO()
val environment = LocalComposeEnvironment.cu
val path = resource.getResourceItemByEnvironment(environment).path
}


private class AnimatedVectorPainter(
vector: AnimatedImageVector,
private val vector: AnimatedImageVector,
density : Density,
time : () -> Float
) : Painter() {

override val intrinsicSize: Size = density.run {
DpSize(
vector.defaultWidth,
Expand All @@ -37,8 +46,8 @@ private class AnimatedVectorPainter(
}

private val viewportSize = IntSize(
intrinsicSize.width.roundToInt(),
intrinsicSize.height.roundToInt()
vector.viewportWidth.roundToInt(),
vector.viewportHeight.roundToInt()
)

private val root = AnimatedGroupComponent(vector.root)
Expand Down
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<*,*>
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,27 @@ import androidx.compose.animation.core.Easing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.ui.util.lerp

internal sealed class FloatAnimator : ObjectAnimator<Float, Float>()
public sealed class FloatAnimator : ObjectAnimator<Float, Float>()

public fun FloatAnimator(
duration: Float,
delay : Float,
valueFrom: Float,
valueTo: Float,
interpolator: Easing
) : FloatAnimator = DynamicFloatAnimator(
duration = duration,
valueFrom = valueFrom,
valueTo = valueTo,
delay = delay,
interpolator = interpolator
)

internal class DynamicFloatAnimator(
override val duration: Float,
override val valueFrom: Float,
override val valueTo: Float,
override val startOffset: Float,
override val delay: Float,
override val interpolator: Easing
) : FloatAnimator() {

Expand All @@ -22,7 +36,7 @@ internal class DynamicFloatAnimator(
internal class StaticFloatAnimator(
private val value : Float
) : FloatAnimator() {
override val startOffset: Float get() = 0f
override val delay: Float get() = 0f
override val duration: Float get() = 0f
override val valueFrom: Float get() = value
override val valueTo: Float get() = value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ package io.github.alexzhirkevich.compottie.avp.animator

import androidx.compose.animation.core.Easing

internal abstract class ObjectAnimator<T, out R> {
public sealed class ObjectAnimator<T, out R> {

abstract val startOffset: Float
public abstract val delay: Float

abstract val duration: Float
public abstract val duration: Float

abstract val valueFrom: T
public abstract val valueFrom: T

abstract val valueTo: T
public abstract val valueTo: T

abstract val interpolator: Easing
public abstract val interpolator: Easing

protected abstract fun interpolate(progress: Float): R

fun animate(time: Float): R {
internal fun animate(time: Float): R {

val progress = if (time < startOffset) {
val progress = if (time < delay) {
0f
} else {
((time - startOffset) / duration).coerceIn(0f, 1f)
((time - delay) / duration).coerceIn(0f, 1f)
}

return interpolate(interpolator.transform(progress))
Expand Down
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class DynamicPathAnimator(
override val duration: Float,
override val valueFrom: List<PathNode>,
override val valueTo: List<PathNode>,
override val startOffset: Float,
override val delay: Float,
override val interpolator: Easing
) : PathAnimator() {

Expand All @@ -36,7 +36,7 @@ internal class DynamicPathAnimator(
internal class StaticPathAnimator(
val value : List<PathNode>
) : PathAnimator() {
override val startOffset: Float get() = 0f
override val delay: Float get() = 0f
override val duration: Float get() = 0f
override val valueFrom: List<PathNode> get() = value
override val valueTo: List<PathNode> get() = value
Expand Down
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
}

Loading

0 comments on commit 67f348c

Please sign in to comment.