{-# LANGUAGE Arrows #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TypeFamilies #-}

{- | A pure @gloss@ backend for Rhine.

To run pure Rhine apps with @gloss@,
write a clocked signal function ('ClSF') in the 'GlossClock' and use 'flowGloss'.
-}
module FRP.Rhine.Gloss.Pure (
  GlossM,
  paint,
  clear,
  paintAll,
  GlossClock (..),
  GlossClSF,
  currentEvent,
  flowGloss,
  flowGlossWithWorldMSF,
) where

-- base
import qualified Control.Category as Category

-- transformers
import Control.Monad.Trans.Class
import Control.Monad.Trans.Reader
import Control.Monad.Trans.Writer

-- dunai
import qualified Control.Monad.Trans.MSF.Reader as MSFReader
import Data.MonadicStreamFunction.InternalCore

-- rhine
import FRP.Rhine
import FRP.Rhine.Reactimation.ClockErasure

-- rhine-gloss
import FRP.Rhine.Gloss.Common

-- * @gloss@ effects

-- FIXME How about a Reader (MSF () (Either Float Event))? That might unify the two backends and make the pure one more flexible.

-- | A pure monad in which all effects caused by the @gloss@ backend take place.
newtype GlossM a = GlossM {forall a.
GlossM a -> ReaderT (Float, Maybe Event) (Writer Picture) a
unGlossM :: (ReaderT (Float, Maybe Event)) (Writer Picture) a}
  deriving (forall a b. a -> GlossM b -> GlossM a
forall a b. (a -> b) -> GlossM a -> GlossM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> GlossM b -> GlossM a
$c<$ :: forall a b. a -> GlossM b -> GlossM a
fmap :: forall a b. (a -> b) -> GlossM a -> GlossM b
$cfmap :: forall a b. (a -> b) -> GlossM a -> GlossM b
Functor, Functor GlossM
forall a. a -> GlossM a
forall a b. GlossM a -> GlossM b -> GlossM a
forall a b. GlossM a -> GlossM b -> GlossM b
forall a b. GlossM (a -> b) -> GlossM a -> GlossM b
forall a b c. (a -> b -> c) -> GlossM a -> GlossM b -> GlossM c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b. GlossM a -> GlossM b -> GlossM a
$c<* :: forall a b. GlossM a -> GlossM b -> GlossM a
*> :: forall a b. GlossM a -> GlossM b -> GlossM b
$c*> :: forall a b. GlossM a -> GlossM b -> GlossM b
liftA2 :: forall a b c. (a -> b -> c) -> GlossM a -> GlossM b -> GlossM c
$cliftA2 :: forall a b c. (a -> b -> c) -> GlossM a -> GlossM b -> GlossM c
<*> :: forall a b. GlossM (a -> b) -> GlossM a -> GlossM b
$c<*> :: forall a b. GlossM (a -> b) -> GlossM a -> GlossM b
pure :: forall a. a -> GlossM a
$cpure :: forall a. a -> GlossM a
Applicative, Applicative GlossM
forall a. a -> GlossM a
forall a b. GlossM a -> GlossM b -> GlossM b
forall a b. GlossM a -> (a -> GlossM b) -> GlossM b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> GlossM a
$creturn :: forall a. a -> GlossM a
>> :: forall a b. GlossM a -> GlossM b -> GlossM b
$c>> :: forall a b. GlossM a -> GlossM b -> GlossM b
>>= :: forall a b. GlossM a -> (a -> GlossM b) -> GlossM b
$c>>= :: forall a b. GlossM a -> (a -> GlossM b) -> GlossM b
Monad)

-- | Add a picture to the canvas.
paint :: Picture -> GlossM ()
paint :: Picture -> GlossM ()
paint = forall a.
ReaderT (Float, Maybe Event) (Writer Picture) a -> GlossM a
GlossM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell

-- FIXME This doesn't what you think it does

-- | Clear the canvas.
clear :: GlossM ()
clear :: GlossM ()
clear = Picture -> GlossM ()
paint Picture
Blank

-- | Clear the canvas and then paint.
paintAll :: Picture -> GlossM ()
paintAll :: Picture -> GlossM ()
paintAll Picture
pic = GlossM ()
clear forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Picture -> GlossM ()
paint Picture
pic

-- * Clocks

{- | The overall clock of a pure @rhine@ 'ClSF' that can be run by @gloss@.
   It ticks both on events (@tag = Just Event@) and simulation steps (@tag = Nothing@).
-}
data GlossClock = GlossClock

instance Semigroup GlossClock where
  GlossClock
_ <> :: GlossClock -> GlossClock -> GlossClock
<> GlossClock
_ = GlossClock
GlossClock

instance Clock GlossM GlossClock where
  type Time GlossClock = Float
  type Tag GlossClock = Maybe Event
  initClock :: GlossClock
-> RunningClockInit GlossM (Time GlossClock) (Tag GlossClock)
initClock GlossClock
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (m :: * -> *) b a. Monad m => m b -> MSF m a b
constM (forall a.
ReaderT (Float, Maybe Event) (Writer Picture) a -> GlossM a
GlossM forall (m :: * -> *) r. Monad m => ReaderT r m r
ask) forall {k} (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (forall v s (m :: * -> *). (VectorSpace v s, Monad m) => MSF m v v
sumS forall (a :: * -> * -> *) b c b' c'.
Arrow a =>
a b c -> a b' c' -> a (b, b') (c, c')
*** forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
Category.id), Float
0)

instance GetClockProxy GlossClock

-- * Signal functions

{- |
The type of a 'ClSF' you can implement to get a @gloss@ app,
if you chose to handle events and simulation steps in the same subsystem.

You can, but don't need to paint via 'GlossM':
You can also simply output the picture and it will be painted on top.
-}
type GlossClSF = ClSF GlossM GlossClock () Picture

{- | Observe whether there was an event this tick,
   and which one.
-}
currentEvent :: ClSF GlossM GlossClock () (Maybe Event)
currentEvent :: ClSF GlossM GlossClock () (Maybe Event)
currentEvent = forall (m :: * -> *) cl a. Monad m => ClSF m cl a (Tag cl)
tagS

-- * Reactimation

{- | The main function that will start the @gloss@ backend and run the 'SN'
   (in the case of the combined clock).
-}
flowGloss ::
  GlossSettings ->
  -- | The @gloss@-compatible 'Rhine'.
  GlossClSF ->
  IO ()
flowGloss :: GlossSettings -> GlossClSF -> IO ()
flowGloss GlossSettings
settings GlossClSF
clsf = forall {cl} {b}.
Clock GlossM cl =>
GlossSettings -> cl -> MSF GlossM (Time cl, Tag cl) b -> IO ()
flowGlossWithWorldMSF GlossSettings
settings GlossClock
GlossClock forall a b. (a -> b) -> a -> b
$ proc (Time GlossClock
time, Tag GlossClock
tag) -> do
  forall (m :: * -> *) a b. Monad m => (a -> m b) -> MSF m a b
arrM (forall a b. a -> b -> a
const GlossM ()
clear) -< ()
  Picture
pic <- forall (m :: * -> *) cl a b.
(Monad m, Clock m cl) =>
ClockProxy cl
-> Time cl -> ClSF m cl a b -> MSF m (Time cl, Tag cl, a) b
eraseClockClSF forall cl. GetClockProxy cl => ClockProxy cl
getClockProxy Float
0 GlossClSF
clsf -< (Time GlossClock
time, Tag GlossClock
tag, ())
  forall (m :: * -> *) a b. Monad m => (a -> m b) -> MSF m a b
arrM Picture -> GlossM ()
paint -< Picture
pic

-- FIXME Hide?

-- | Helper function
flowGlossWithWorldMSF :: GlossSettings -> cl -> MSF GlossM (Time cl, Tag cl) b -> IO ()
flowGlossWithWorldMSF GlossSettings {Int
Display
Color
stepsPerSecond :: GlossSettings -> Int
backgroundColor :: GlossSettings -> Color
display :: GlossSettings -> Display
stepsPerSecond :: Int
backgroundColor :: Color
display :: Display
..} cl
clock MSF GlossM (Time cl, Tag cl) b
msf =
  forall world.
Display
-> Color
-> Int
-> world
-> (world -> Picture)
-> (Event -> world -> world)
-> (Float -> world -> world)
-> IO ()
play Display
display Color
backgroundColor Int
stepsPerSecond (MSF (Writer Picture) ((Float, Maybe Event), ()) b
worldMSF, Picture
Blank) forall {a} {b}. (a, b) -> b
getPic forall {a} {a} {d} {a} {b}.
Num a =>
a
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, b)
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, d)
handleEvent forall {a} {d} {a} {a} {b}.
a
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, b)
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, d)
simStep
  where
    worldMSF :: MSF (Writer Picture) ((Float, Maybe Event), ()) b
worldMSF = forall (m :: * -> *) r a b.
Monad m =>
MSF (ReaderT r m) a b -> MSF m (r, a) b
MSFReader.runReaderS forall a b. (a -> b) -> a -> b
$ forall (m2 :: * -> *) (m1 :: * -> *) a b.
(Monad m2, Monad m1) =>
(forall c. m1 c -> m2 c) -> MSF m1 a b -> MSF m2 a b
morphS forall a.
GlossM a -> ReaderT (Float, Maybe Event) (Writer Picture) a
unGlossM forall a b. (a -> b) -> a -> b
$ proc () -> do
      (Time cl
time, Tag cl
tag) <- forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall a b. (a, b) -> a
fst forall a b. (a -> b) -> a -> b
$ forall w a. Writer w a -> (a, w)
runWriter forall a b. (a -> b) -> a -> b
$ forall a b c. (a -> b -> c) -> b -> a -> c
flip forall r (m :: * -> *) a. ReaderT r m a -> r -> m a
runReaderT (Float
0, forall a. Maybe a
Nothing) forall a b. (a -> b) -> a -> b
$ forall a.
GlossM a -> ReaderT (Float, Maybe Event) (Writer Picture) a
unGlossM forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) cl.
Clock m cl =>
cl -> RunningClockInit m (Time cl) (Tag cl)
initClock cl
clock -< ()
      MSF GlossM (Time cl, Tag cl) b
msf -< (Time cl
time, Tag cl
tag)
    getPic :: (a, b) -> b
getPic (a
_, b
pic) = b
pic
    stepWith :: (a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, d)
stepWith (a
diff, b
maybeEvent) (MSF (WriterT d Identity) ((a, b), ()) a
msf, b
_) = forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first forall {a} {b}. (a, b) -> b
snd forall a b. (a -> b) -> a -> b
$ forall w a. Writer w a -> (a, w)
runWriter forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a b. MSF m a b -> a -> m (b, MSF m a b)
unMSF MSF (WriterT d Identity) ((a, b), ()) a
msf ((a
diff, b
maybeEvent), ())
    handleEvent :: a
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, b)
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, d)
handleEvent a
event = forall {a} {b} {d} {a} {b}.
(a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, d)
stepWith (a
0, forall a. a -> Maybe a
Just a
event)
    simStep :: a
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, b)
-> (MSF (WriterT d Identity) ((a, Maybe a), ()) a, d)
simStep a
diff = forall {a} {b} {d} {a} {b}.
(a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, b)
-> (MSF (WriterT d Identity) ((a, b), ()) a, d)
stepWith (a
diff, forall a. Maybe a
Nothing)