#@name anim_lib_test2
@inputs 
@outputs Progress:string FrameFrac Interpolation NextFrame PrevFrame Test Clerp
@persist Animation:table [TimeLast TimeCurrent TimeDelta TimeTotal]
@trigger none

if (first()) {
    
    holoCreate(1)
    holoCreate(2)
    
    holoParent(1, entity())
    holoParent(2, holoEntity(1))
    
    holoPos(2, holoEntity(1):toWorld(vec(0, 0, 50)))
    
    
    KeyFrame1 = table(
        table( "hologram" = 1, "parent" = entity(), "angle" = ang() ),
        table( "hologram" = 2, "parent" = holoEntity(1), "angle" = ang() )
    )
    
    KeyFrame2 = table(
        table( "hologram" = 1, "parent" = entity(), "angle" = ang(90, 90, 0) ),
        table( "hologram" = 2, "parent" = holoEntity(1), "angle" = ang(-90, 0, 0) )
    )
    
       
    Animation = table(

        "setKeyFrames" = table(
            0 = KeyFrame1,
            1 = KeyFrame2,
            2 = KeyFrame2,
            3 = KeyFrame1
        ),
        
        "setAnimDuration" = 2,    # duration in seconds      
        
        "varAnimProgress" = 0,
        "varAnimFraction" = 0,

        "varNextFrame" = 0,
        "varPrevFrame" = 0,
        "varHoldFrame" = 0,
        "varFrameProgress" = 0,
        "varFrameFraction" = 0

    ) 
    
    Animation["setFrameCount", number]    = Animation["setKeyFrames", table]:count() - 1
    Animation["setFrameDuration", number] = Animation["setAnimDuration", number] / Animation["setFrameCount", number]


    function void update(UpdateRate) {
        interval(UpdateRate)

        TimeCurrent = curtime()
        TimeDelta   = min(TimeCurrent - TimeLast, UpdateRate/1000)
        TimeLast    = TimeCurrent
        TimeTotal  += TimeDelta
    }    

    function number linear( Y0, Y1, T ) {
        return Y0 + T * (Y1 - Y0)
    }

    function number cosine( Y0, Y1, T ) {
        return linear( Y0, Y1, -cosr( pi() * T ) / 2 + 0.5 )
    }
    
}

update(90)

if (TimeTotal > 1) {

Animation["varAnimProgress", number] = min(Animation["varAnimProgress", number] + TimeDelta, Animation["setAnimDuration", number])
Animation["varAnimFraction", number] = Animation["varAnimProgress", number]/Animation["setAnimDuration", number]

#Clerp = cosine(0, Animation["setAnimDuration", number] , Animation["varAnimFraction", number])
#FrameFrac = Clerp/Animation["setFrameDuration", number]

FrameFrac = Animation["varAnimProgress", number]/Animation["setFrameDuration", number]

Progress  = floor(Animation["varAnimFraction", number]*100) + "%"

NextFrame = min(floor(FrameFrac) + 1, Animation["setFrameCount", number])
PrevFrame = NextFrame - 1
Interpolation = 1 - (NextFrame - FrameFrac)

local KeyFrames = Animation["setKeyFrames", table]
local NextFrameT = KeyFrames[NextFrame, table]
local PrevFrameT = KeyFrames[PrevFrame, table]
    
for (I = 1, NextFrameT:count()) {
    local Next = NextFrameT[I, table]
    local Prev = PrevFrameT[I, table]
    
    holoAng(Next["hologram", number], Next["parent", entity]:toWorld(
        slerp(quat(Prev["angle", angle]), quat(Next["angle", angle]), Interpolation):toAngle()
    ))
}

}
#Frame = floor(FrameFrac) + 1 #ceil(FrameFrac)


#[
Animation["varNextFrame", number] = min(floor(FrameFrac) + 1, Animation["setFrameCount", number])
Animation["varPrevFrame", number] = Animation["varNextFrame", number] - 1

Interpolation = FrameFrac % 1

local KeyFrames = Animation["setKeyFrames", table]
local NextFrame = KeyFrames[Animation["varNextFrame", number], table]
local PrevFrame = KeyFrames[Animation["varPrevFrame", number], table]

for (I = 1, NextFrame:count()) {
    local Next = NextFrame[I, table]
    local Prev = PrevFrame[I, table]
    
    holoAng(Next["hologram", number], Next["parent", entity]:toWorld(
        slerp(quat(Prev["angle", angle]), quat(Next["angle", angle]), Interpolation):toAngle()
    ))
}
]#

