Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhirkevich committed May 29, 2024
1 parent 69f34b0 commit b9faa62
Show file tree
Hide file tree
Showing 44 changed files with 1,100 additions and 849 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.alexzhirkevich.compottie.internal.platform

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.LinearGradientShader
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.RadialGradientShader
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.setFrom
import androidx.compose.ui.graphics.toArgb


private val tempMatrix = android.graphics.Matrix()

actual fun MakeLinearGradient(
from : Offset,
to : Offset,
colors : List<Color>,
colorStops: List<Float>,
tileMode: TileMode,
matrix: Matrix
) = LinearGradientShader(
from = from,
to = to,
colorStops = colorStops,
tileMode = tileMode,
colors = colors
).apply {
tempMatrix.setValues(matrix.values)
setLocalMatrix(tempMatrix)
}

actual fun MakeRadialGradient(
center : Offset,
radius : Float,
colors : List<Color>,
colorStops: List<Float>,
tileMode: TileMode,
matrix: Matrix
) = RadialGradientShader(
center = center,
radius = radius,
colorStops = colorStops,
tileMode = tileMode,
colors = colors
).apply {
tempMatrix.setValues(matrix.values)
setLocalMatrix(tempMatrix)
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import androidx.compose.runtime.produceState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Alignment
import androidx.compose.ui.geometry.MutableRect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.drawscope.scale
import androidx.compose.ui.graphics.drawscope.translate
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.util.fastForEachReversed
import io.github.alexzhirkevich.compottie.LottieComposition
import io.github.alexzhirkevich.compottie.LottieCompositionResult
import io.github.alexzhirkevich.compottie.internal.graphics.draw
import io.github.alexzhirkevich.compottie.internal.schema.layers.ShapeLayer
import io.github.alexzhirkevich.compottie.internal.content.DrawingContent
import io.github.alexzhirkevich.compottie.internal.utils.preScale
import io.github.alexzhirkevich.compottie.internal.utils.preTranslate
import kotlin.math.roundToInt


Expand Down Expand Up @@ -61,38 +63,61 @@ private object EmptyPainter : ProgressPainter() {
}
private class LottiePainter(
private val composition: LottieComposition,
private val clipToCompositionBounds : Boolean = true
) : ProgressPainter() {

override val intrinsicSize: Size =
Size(composition.lottieData.width.toFloat(), composition.lottieData.height.toFloat())
override val intrinsicSize: Size = Size(
composition.lottieData.width.toFloat(),
composition.lottieData.height.toFloat()
)

override var progress: Float by mutableStateOf(0f)

private val matrix = Matrix()

private var alpha by mutableStateOf(1f)

override fun applyAlpha(alpha: Float): Boolean {
this.alpha = alpha
return true
}


override fun DrawScope.onDraw() {

val dat = composition.lottieData

val frame = (dat.outPoint * progress.coerceIn(0f, 1f) - dat.inPoint)
.coerceAtLeast(0f).roundToInt()

val scale = ContentScale.Fit.computeScaleFactor(intrinsicSize, size)
val offset = Alignment.Center.align(
IntSize(
(intrinsicSize.width).roundToInt(),
(intrinsicSize.height).roundToInt()
),
IntSize(
size.width.roundToInt(),
size.height.roundToInt()
),
layoutDirection
)

scale(scale.scaleX, scale.scaleY) {
translate(offset.x.toFloat(), offset.y.toFloat()) {
dat.layers.fastForEachReversed {
when (it) {
is ShapeLayer -> it.draw(this, frame)
drawIntoCanvas { canvas ->

matrix.reset()

val scale = ContentScale.Fit.computeScaleFactor(intrinsicSize, size)

val offset = Alignment.Center.align(
IntSize(
(intrinsicSize.width).roundToInt(),
(intrinsicSize.height).roundToInt()
),
IntSize(
size.width.roundToInt(),
size.height.roundToInt()
),
layoutDirection
)


// matrix.preScale(scale.scaleX, scale.scaleY)
// matrix.preTranslate(offset.x.toFloat(), offset.y.toFloat())

val dat = composition.lottieData

val frame = (dat.outPoint * progress.coerceIn(0f, 1f) - dat.inPoint)
.coerceAtLeast(0f).roundToInt()

scale(scale.scaleX, scale.scaleY) {
translate(offset.x.toFloat(), offset.y.toFloat()) {
dat.layers.fastForEachReversed {
if (it is DrawingContent) {
it.draw(canvas, matrix, alpha, frame)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.Path
import io.github.alexzhirkevich.compottie.internal.platform.addPath
import io.github.alexzhirkevich.compottie.internal.schema.helpers.AnimatedTransform
import io.github.alexzhirkevich.compottie.internal.schema.helpers.Transform
import io.github.alexzhirkevich.compottie.internal.utils.Utils
import io.github.alexzhirkevich.compottie.internal.utils.preConcat
import io.github.alexzhirkevich.compottie.internal.utils.union

internal class ContentGroup(
override val name: String?,
val hidden: Boolean,
val contents: List<Content>,
val transform: Transform?,
) : DrawingContent, PathContent {
val transform: AnimatedTransform?,
) : PathAndDrawingContext {

private val rect = MutableRect(0f,0f,0f,0f)
private val offscreenRect = MutableRect(0f,0f,0f,0f)
Expand All @@ -35,15 +37,14 @@ internal class ContentGroup(

matrix.setFrom(parentMatrix)


if (transform != null) {
matrix *= transform.matrix(frame)
matrix.preConcat(transform.matrix(frame))
transform.opacity?.interpolated(frame)?.let {
layerAlpha *= it
layerAlpha = (layerAlpha * it/100f).coerceIn(0f,1f)
}
}

val isRenderingWithOffScreen = hasTwoOrMoreDrawableContent()
val isRenderingWithOffScreen = false//hasTwoOrMoreDrawableContent()

if (isRenderingWithOffScreen) {
offscreenRect.set(0f, 0f, 0f, 0f)
Expand All @@ -66,13 +67,12 @@ internal class ContentGroup(
}
}

override fun getPath(time: Int): Path {
override fun getPath(frame: Int): Path {

// TODO: cache this somehow.
matrix.reset()

if (transform != null) {
matrix.setFrom(transform.matrix(time))
matrix.setFrom(transform.matrix(frame))
}
path.reset()
if (hidden) {
Expand All @@ -81,7 +81,7 @@ internal class ContentGroup(
for (i in contents.indices.reversed()) {
val content = contents[i]
if (content is PathContent) {
path.addPath(content.getPath(time), matrix)
path.addPath(content.getPath(frame), matrix)
}
}
return path
Expand All @@ -107,7 +107,7 @@ internal class ContentGroup(
) {
matrix.setFrom(parentMatrix)
if (transform != null) {
matrix *= transform.matrix(frame)
matrix.preConcat(transform.matrix(frame))
}

rect.set(0f,0f,0f,0f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.github.alexzhirkevich.compottie.internal.content

internal interface PathAndDrawingContext : DrawingContent, PathContent {
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package io.github.alexzhirkevich.compottie.internal.content

import androidx.compose.ui.graphics.Path
import io.github.alexzhirkevich.compottie.internal.content.Content

interface PathContent : Content {
fun getPath(time: Int) : Path
internal interface PathContent : Content {
fun getPath(frame: Int) : Path
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.alexzhirkevich.compottie.internal.platform

import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Matrix
import androidx.compose.ui.graphics.Shader
import androidx.compose.ui.graphics.TileMode


expect fun MakeLinearGradient(
from : Offset,
to : Offset,
colors : List<Color>,
colorStops: List<Float>,
tileMode: TileMode = TileMode.Clamp,
matrix: Matrix
) : Shader


expect fun MakeRadialGradient(
center : Offset,
radius : Float,
colors : List<Color>,
colorStops: List<Float>,
tileMode: TileMode = TileMode.Clamp,
matrix: Matrix
) : Shader
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import io.github.alexzhirkevich.compottie.internal.schema.shapes.Ellipse
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Fill
import io.github.alexzhirkevich.compottie.internal.schema.shapes.GradientFill
import io.github.alexzhirkevich.compottie.internal.schema.shapes.GradientStroke
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Group
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Path
import io.github.alexzhirkevich.compottie.internal.schema.shapes.GroupShape
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Rect
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Round
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Shape
import io.github.alexzhirkevich.compottie.internal.schema.shapes.SolidStroke
import io.github.alexzhirkevich.compottie.internal.schema.shapes.TransformShape
import io.github.alexzhirkevich.compottie.internal.schema.shapes.Trim
import io.github.alexzhirkevich.compottie.internal.schema.shapes.TrimPath
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
Expand All @@ -39,16 +37,16 @@ val LottieJson = Json {
subclass(AnimatedVector2.Default::class)
}
polymorphic(Shape::class){
subclass(Path::class)
// subclass(Path::class)
subclass(Ellipse::class)
subclass(Fill::class)
subclass(GradientFill::class)
subclass(Group::class)
subclass(GroupShape::class)
subclass(Rect::class)
subclass(Round::class)
// subclass(Round::class)
subclass(SolidStroke::class)
subclass(GradientStroke::class)
subclass(Trim::class)
subclass(TrimPath::class)
subclass(TransformShape::class)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.Path

interface ModifierContent {
fun applyTo(path: Path, paint: Paint, time: Int)
}
Loading

0 comments on commit b9faa62

Please sign in to comment.