All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.peanuuutz.fork.ui.animation.AnimationState.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2020 The Android Open Source Project
 * Modifications Copyright 2022 Peanuuutz
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.peanuuutz.fork.ui.animation

import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import net.peanuuutz.fork.ui.animation.TimeConstants.UnspecifiedTimeMillis
import net.peanuuutz.fork.ui.animation.vector.AnimationVector
import net.peanuuutz.fork.ui.animation.vector.AnimationVector1D
import net.peanuuutz.fork.ui.animation.vector.VectorConvertor
import net.peanuuutz.fork.ui.animation.vector.copy
import net.peanuuutz.fork.ui.animation.vector.copyFrom
import net.peanuuutz.fork.ui.animation.vector.newVector

sealed interface AnimationState> : State {
    val convertor: VectorConvertor

    override val value: S

    val valueVector: V
        get() = convertor.convertToVector(value)

    val targetValue: S

    val velocityVector: V

    val velocity: S
        get() = convertor.convertFromVector(velocityVector)

    var isRunning: Boolean
}

fun > AnimationState(
    convertor: VectorConvertor,
    initialValue: S,
    initialVelocityVector: V? = null
): AnimationState {
    return AnimationStateImpl(
        convertor = convertor,
        initialValue = initialValue,
        initialVelocityVector = initialVelocityVector,
        isRunning = false,
        lastFrameTimeMillis = UnspecifiedTimeMillis,
        finishedTimeMillis = UnspecifiedTimeMillis
    )
}

fun AnimationState(
    initialValue: Float,
    initialVelocity: Float = 0.0f
): AnimationState {
    return AnimationState(
        convertor = Float.VectorConvertor,
        initialValue = initialValue,
        initialVelocityVector = Float.VectorConvertor.convertToVector(initialVelocity)
    )
}

// ======== Internal ========

internal class AnimationStateImpl>(
    override val convertor: VectorConvertor,
    initialValue: S,
    initialVelocityVector: V? = null,
    isRunning: Boolean = false,
    var lastFrameTimeMillis: Int = UnspecifiedTimeMillis,
    var finishedTimeMillis: Int = UnspecifiedTimeMillis
) : AnimationState {
    override var value: S by mutableStateOf(initialValue)

    override var velocityVector: V by mutableStateOf(
        value = initialVelocityVector?.copy() ?: convertor.newVector(initialValue)
    )

    override var targetValue: S by mutableStateOf(initialValue)

    override var isRunning: Boolean by mutableStateOf(isRunning)
}

internal fun > AnimationStateImpl.copy(
    value: S = this.value,
    velocityVector: V = this.velocityVector,
    isRunning: Boolean = this.isRunning,
    lastFrameTimeMillis: Int = this.lastFrameTimeMillis,
    finishedTimeMillis: Int = this.finishedTimeMillis
): AnimationStateImpl {
    return AnimationStateImpl(
        convertor = convertor,
        initialValue = value,
        initialVelocityVector = velocityVector,
        isRunning = isRunning,
        lastFrameTimeMillis = lastFrameTimeMillis,
        finishedTimeMillis = finishedTimeMillis
    )
}

internal fun > AnimationStateImpl.copyFrom(
    source: AnimationStateImpl
) {
    value = source.value
    velocityVector.copyFrom(source.velocityVector)
    lastFrameTimeMillis = source.lastFrameTimeMillis
    finishedTimeMillis = source.finishedTimeMillis
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy