module Miso.AFrame.Core.Animation where
import Data.Aeson
import Miso
import Miso.String
import GHCJS.Marshal (ToJSVal(..))
import GHC.Generics (Generic)
import Miso.AFrame.Core.Types
import Miso.AFrame.Core.Internal.Utils
data AnimationRepeatCount
= Finite Int
| Indefinite
deriving (Generic)
data AnimationFill
= AnimationBackwards
| AnimationBoth
| AnimationForwards
| AnimationNone
deriving (Generic)
data AnimationBasicEasing
= Ease
| EaseIn
| EaseOut
| EaseInOut
deriving (Generic)
data AnimationEasingGroup
= Cubic
| Quad
| Quart
| Quint
| Sine
| Expo
| Circ
| Elastic
| Back
| Bounce
deriving (Generic)
data AnimationEasing = AnimationEasing
{ animationEasingBasic :: AnimationBasicEasing
, animationEasingGroup :: Maybe AnimationEasingGroup
} deriving (Generic)
data AnimationDirection
= AnimationAlternate
| AnimationAlternateReverse
| AnimationNormal
| AnimationReverse
deriving (Show, Generic)
data AnimationAttrs = AnimationAttrs
{
animationBegin :: Maybe MisoString
,
animationEnd :: Maybe MisoString
,
animationDelay :: Maybe Milliseconds
,
animationDirection :: Maybe AnimationDirection
,
animationDur :: Maybe Milliseconds
,
animationEasing :: Maybe AnimationEasing
,
animationFill :: Maybe AnimationFill
,
animationRepeat :: AnimationRepeatCount
} deriving (Generic)
defaultAnimationAttrs :: AnimationAttrs
defaultAnimationAttrs = AnimationAttrs
{ animationBegin = Nothing
, animationEnd = Nothing
, animationDelay = Nothing
, animationDirection = Nothing
, animationDur = Nothing
, animationEasing = Nothing
, animationFill = Nothing
, animationRepeat = Finite 0
}
class ToJSVal a => CanAnimate a
instance CanAnimate Vec3
instance CanAnimate Bool
instance CanAnimate Float
instance CanAnimate Color
animation
:: CanAnimate a
=> MisoString
-> Maybe a
-> a
-> AnimationAttrs
-> View action
animation attrName mfrom to animAttrs = node_ "a-animation" attrs []
where
attrs
= prop "attribute" attrName
: prop "to" to
: attrsFromJSON animAttrs
++ fromProp
fromProp = case mfrom of
Nothing -> []
Just from -> [prop "from" from]
instance ToJSON AnimationFill where toJSON = gtoJSON
instance ToJSON AnimationBasicEasing where toJSON = gtoJSON
instance ToJSON AnimationEasingGroup where toJSON = gtoJSON
instance ToJSON AnimationDirection where toJSON = gtoJSON
instance ToJSON AnimationAttrs where toJSON = gtoJSON
instance ToJSON AnimationRepeatCount where
toJSON (Finite n) = toJSON n
toJSON Indefinite = "indefinite"
instance ToJSON AnimationEasing where
toJSON AnimationEasing{..} =
case (toJSON <$> animationEasingGroup, toJSON animationEasingBasic) of
(Just (String g), String b) -> String (g <> b)
(Nothing, b) -> b
_ -> Null