{-# LANGUAGE Arrows #-}
module LiveCoding.Gloss.Debugger where

-- base
import Control.Arrow
import Data.Data

-- transformers
import Control.Monad.Trans.Writer.Strict
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Strict

-- syb
import Data.Generics.Text

-- gloss
import Graphics.Gloss

-- essence-of-live-coding
import LiveCoding

-- essence-of-live-coding-gloss
import LiveCoding.Gloss.PictureM

statePicture :: Data s => s -> Picture
statePicture :: s -> Picture
statePicture = Float -> Float -> Picture -> Picture
translate (-Float
100) Float
200 (Picture -> Picture) -> (s -> Picture) -> s -> Picture
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Float -> Picture -> Picture
scale Float
0.2 Float
0.2 (Picture -> Picture) -> (s -> Picture) -> s -> Picture
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Color -> Picture -> Picture
color Color
red (Picture -> Picture) -> (s -> Picture) -> s -> Picture
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Picture
text (String -> Picture) -> (s -> String) -> s -> Picture
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> String
forall s. Data s => s -> String
stateShow

statePlay :: Debugger PictureM
statePlay :: Debugger PictureM
statePlay = (forall s. Data s => LiveProgram (StateT s PictureM))
-> Debugger PictureM
forall (m :: * -> *).
(forall s. Data s => LiveProgram (StateT s m)) -> Debugger m
Debugger ((forall s. Data s => LiveProgram (StateT s PictureM))
 -> Debugger PictureM)
-> (forall s. Data s => LiveProgram (StateT s PictureM))
-> Debugger PictureM
forall a b. (a -> b) -> a -> b
$ Cell (StateT s PictureM) () () -> LiveProgram (StateT s PictureM)
forall (m :: * -> *). Monad m => Cell m () () -> LiveProgram m
liveCell (Cell (StateT s PictureM) () () -> LiveProgram (StateT s PictureM))
-> Cell (StateT s PictureM) () ()
-> LiveProgram (StateT s PictureM)
forall a b. (a -> b) -> a -> b
$ Integer -> Cell (StateT s PictureM) () (Maybe Picture)
forall s.
Data s =>
Integer -> Cell (StateT s PictureM) () (Maybe Picture)
every Integer
2 Cell (StateT s PictureM) () (Maybe Picture)
-> Cell (StateT s PictureM) (Maybe Picture) ()
-> Cell (StateT s PictureM) () ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> Picture -> Cell (StateT s PictureM) (Maybe Picture) Picture
forall a (m :: * -> *).
(Data a, Monad m) =>
a -> Cell m (Maybe a) a
keep Picture
blank Cell (StateT s PictureM) (Maybe Picture) Picture
-> Cell (StateT s PictureM) Picture ()
-> Cell (StateT s PictureM) (Maybe Picture) ()
forall k (cat :: k -> k -> *) (a :: k) (b :: k) (c :: k).
Category cat =>
cat a b -> cat b c -> cat a c
>>> (Picture -> StateT s PictureM ())
-> Cell (StateT s PictureM) Picture ()
forall a (m :: * -> *) b. (a -> m b) -> Cell m a b
arrM (ReaderT [Event] (WriterT Picture IO) () -> StateT s PictureM ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ReaderT [Event] (WriterT Picture IO) () -> StateT s PictureM ())
-> (Picture -> ReaderT [Event] (WriterT Picture IO) ())
-> Picture
-> StateT s PictureM ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. WriterT Picture IO () -> ReaderT [Event] (WriterT Picture IO) ()
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (WriterT Picture IO () -> ReaderT [Event] (WriterT Picture IO) ())
-> (Picture -> WriterT Picture IO ())
-> Picture
-> ReaderT [Event] (WriterT Picture IO) ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Picture -> WriterT Picture IO ()
forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
tell)

every :: Data s => Integer -> Cell (StateT s PictureM) () (Maybe Picture)
every :: Integer -> Cell (StateT s PictureM) () (Maybe Picture)
every Integer
maxN = proc () -> do
  Integer
n <- Cell (StateT s PictureM) Integer Integer
forall (m :: * -> *) a. (Monad m, Num a, Data a) => Cell m a a
sumC -< Integer
1
  if Integer
n Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`mod` Integer
maxN Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0
  then do
    s
s <- Cell (StateT s PictureM) () s
forall (m :: * -> *) s a. Monad m => Cell (StateT s m) a s
getC -< ()
    let pic :: Picture
pic = s -> Picture
forall s. Data s => s -> Picture
statePicture s
s
    Cell (StateT s PictureM) (Maybe Picture) (Maybe Picture)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA -< Picture -> Maybe Picture
forall a. a -> Maybe a
Just Picture
pic
  else
    Cell (StateT s PictureM) (Maybe Picture) (Maybe Picture)
forall (a :: * -> * -> *) b. Arrow a => a b b
returnA  -< Maybe Picture
forall a. Maybe a
Nothing