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

commonMain.ru.casperix.spine.animation.AttachmentTimeline.kt Maven / Gradle / Ivy

There is a newer version: 0.9.0
Show newest version
package ru.casperix.spine.animation

import ru.casperix.spine.MixBlend
import ru.casperix.spine.MixDirection
import ru.casperix.spine.Slot
import ru.casperix.spine.json.component.SlotAttachment

class AttachmentTimeline(
    override val slotIndex: Int,
    override val frames: List,
) : AbstractTimeline(frames), SlotTimeline {
    val attachmentNames: List = frames.map { it.name ?: "" }

    override fun apply(context: AnimationContext) = context.getSlotContext(slotIndex)?.run {
        context.apply {
            if (direction == MixDirection.out) {
                if (blend == MixBlend.setup) setAttachment(slot, slot.data.attachmentName)
                return@run
            }

            if (time < frames[0].time) {
                if (blend == MixBlend.setup || blend == MixBlend.first) {
                    setAttachment(slot, slot.data.attachmentName);
                }
                return@run
            }

            frames.asReversed().forEachIndexed { index, nextFrame ->
                if (time >= nextFrame.time) {
                    val actualIndex = frames.lastIndex - index
                    setAttachment(slot, attachmentNames.getOrNull(actualIndex))
                    return@run
                }
            }
        }
    } ?: Unit

    private fun setAttachment(slot: Slot, attachmentName: String?) {
        slot.setupAttachment(attachmentName)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy