module DzenDhall.Animation.Slider where
import DzenDhall.Config
import DzenDhall.AST
import DzenDhall.Data
import DzenDhall.Extra
import Data.Vector (Vector, (!), null, length)
import Lens.Micro
run
:: Slider
-> Int
-> Vector AST
-> AST
run _ _ asts
| Data.Vector.null asts = mempty
run slider frame asts
| slider ^. fadeIn . fadeFrameCount == 0 &&
slider ^. fadeOut . fadeFrameCount == 0 =
let astIx = (frame `div` positive (slider ^. sliderDelay))
`mod` Data.Vector.length asts
in asts ! astIx
run slider frame asts =
let inFrameCount = positive $ slider ^. fadeIn . fadeFrameCount
delayFrameCount = slider ^. sliderDelay
outFrameCount = positive $ slider ^. fadeOut . fadeFrameCount
totalFrames = positive $
inFrameCount + delayFrameCount + outFrameCount
astIx = (frame `div` totalFrames) `mod` Data.Vector.length asts
ast = asts ! astIx
frameNumber = frame `mod` totalFrames
outStartFrame = inFrameCount + delayFrameCount
yShift =
if | frameNumber <= inFrameCount
-> renderFade (slider ^.fadeIn) frameNumber inFrameCount
| frameNumber < outStartFrame
-> 0
| otherwise
-> renderFade (slider ^. fadeOut) (totalFrames - frameNumber) outFrameCount
in
ASTProp (P (XY (0, yShift))) ast
renderFade
:: Fade
-> Int
-> Int
-> Int
renderFade fade relativeFrameNumber frameCount =
let height = fade ^. fadePixelHeight
dk = direction (fade ^. fadeDirection)
in
dk * (height - height * relativeFrameNumber `div` frameCount)
direction :: VerticalDirection -> Int
direction VUp = -1
direction _ = 1