{-# LANGUAGE TupleSections #-}
module Control.Varying.Time where
import Control.Varying.Core
import Control.Varying.Event hiding (after, before)
import Control.Applicative
import Data.Time.Clock
deltaUTC :: Fractional t => Var IO b t
deltaUTC = delta getCurrentTime (\a b -> realToFrac $ diffUTCTime a b)
delta :: (Num t, Fractional t, Applicative m, Monad m)
=> m a -> (a -> a -> t) -> Var m b t
delta m f = Var $ \_ -> do
t <- m
return (0, delta' t)
where delta' t = Var $ \_ -> do
t' <- m
let dt = t' `f` t
return (dt, delta' t')
before :: (Applicative m, Monad m, Num t, Ord t) => t -> Var m t (Event ())
before t = Var $ \dt -> do
if t - dt >= 0
then return (Event (), before $ t - dt)
else return (NoEvent, never)
after :: (Applicative m, Monad m, Num t, Ord t) => t -> Var m t (Event ())
after t = Var $ \dt -> do
if t - dt <= 0
then return (Event (), pure $ Event ())
else return (NoEvent, after $ t - dt)