{-# LANGUAGE CPP #-}
module Text.LaTeX.Packages.TikZ.PathBuilder (
PathBuilder
, bpath
, line
, pcycle
, rectangle
, circle
, ellipse
, node
, grid
) where
import Text.LaTeX.Base.Syntax (LaTeX)
import Text.LaTeX.Packages.TikZ.Syntax
import Control.Monad.Trans.State
newtype PathState = PS { PathState -> TPath
currentPath :: TPath }
newtype PathBuilder a = PB { forall a. PathBuilder a -> State PathState a
pathBuilder :: State PathState a }
instance Functor PathBuilder where
fmap :: forall a b. (a -> b) -> PathBuilder a -> PathBuilder b
fmap a -> b
f (PB State PathState a
st) = State PathState b -> PathBuilder b
forall a. State PathState a -> PathBuilder a
PB (State PathState b -> PathBuilder b)
-> State PathState b -> PathBuilder b
forall a b. (a -> b) -> a -> b
$ (a -> b) -> State PathState a -> State PathState b
forall a b.
(a -> b)
-> StateT PathState Identity a -> StateT PathState Identity b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f State PathState a
st
instance Applicative PathBuilder where
pure :: forall a. a -> PathBuilder a
pure = State PathState a -> PathBuilder a
forall a. State PathState a -> PathBuilder a
PB (State PathState a -> PathBuilder a)
-> (a -> State PathState a) -> a -> PathBuilder a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> State PathState a
forall a. a -> StateT PathState Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(PB State PathState (a -> b)
f) <*> :: forall a b. PathBuilder (a -> b) -> PathBuilder a -> PathBuilder b
<*> (PB State PathState a
x) = State PathState b -> PathBuilder b
forall a. State PathState a -> PathBuilder a
PB (State PathState b -> PathBuilder b)
-> State PathState b -> PathBuilder b
forall a b. (a -> b) -> a -> b
$ State PathState (a -> b)
f State PathState (a -> b) -> State PathState a -> State PathState b
forall a b.
StateT PathState Identity (a -> b)
-> StateT PathState Identity a -> StateT PathState Identity b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> State PathState a
x
instance Monad PathBuilder where
return :: forall a. a -> PathBuilder a
return = a -> PathBuilder a
forall a. a -> PathBuilder a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(PB State PathState a
x) >>= :: forall a b. PathBuilder a -> (a -> PathBuilder b) -> PathBuilder b
>>= a -> PathBuilder b
f = State PathState b -> PathBuilder b
forall a. State PathState a -> PathBuilder a
PB (State PathState b -> PathBuilder b)
-> State PathState b -> PathBuilder b
forall a b. (a -> b) -> a -> b
$ State PathState a
x State PathState a -> (a -> State PathState b) -> State PathState b
forall a b.
StateT PathState Identity a
-> (a -> StateT PathState Identity b)
-> StateT PathState Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= PathBuilder b -> State PathState b
forall a. PathBuilder a -> State PathState a
pathBuilder (PathBuilder b -> State PathState b)
-> (a -> PathBuilder b) -> a -> State PathState b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> PathBuilder b
f
applyToPath :: (TPath -> TPath) -> PathBuilder ()
applyToPath :: (TPath -> TPath) -> PathBuilder ()
applyToPath TPath -> TPath
f = State PathState () -> PathBuilder ()
forall a. State PathState a -> PathBuilder a
PB (State PathState () -> PathBuilder ())
-> State PathState () -> PathBuilder ()
forall a b. (a -> b) -> a -> b
$ (PathState -> PathState) -> State PathState ()
forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify ((PathState -> PathState) -> State PathState ())
-> (PathState -> PathState) -> State PathState ()
forall a b. (a -> b) -> a -> b
$ \PathState
ps -> PathState
ps { currentPath = f (currentPath ps) }
pcycle :: PathBuilder ()
pcycle :: PathBuilder ()
pcycle = (TPath -> TPath) -> PathBuilder ()
applyToPath TPath -> TPath
Cycle
line :: TPoint -> PathBuilder ()
line :: TPoint -> PathBuilder ()
line TPoint
p = (TPath -> TPath) -> PathBuilder ()
applyToPath (TPath -> TPoint -> TPath
`Line` TPoint
p)
rectangle :: TPoint -> PathBuilder ()
rectangle :: TPoint -> PathBuilder ()
rectangle TPoint
p = (TPath -> TPath) -> PathBuilder ()
applyToPath (TPath -> TPoint -> TPath
`Rectangle` TPoint
p)
circle :: Double -> PathBuilder ()
circle :: Double -> PathBuilder ()
circle Double
r = (TPath -> TPath) -> PathBuilder ()
applyToPath (TPath -> Double -> TPath
`Circle` Double
r)
ellipse :: Double
-> Double
-> PathBuilder ()
ellipse :: Double -> Double -> PathBuilder ()
ellipse Double
r1 Double
r2 = (TPath -> TPath) -> PathBuilder ()
applyToPath ((TPath -> TPath) -> PathBuilder ())
-> (TPath -> TPath) -> PathBuilder ()
forall a b. (a -> b) -> a -> b
$ \TPath
x -> TPath -> Double -> Double -> TPath
Ellipse TPath
x Double
r1 Double
r2
grid :: [GridOption] -> TPoint -> PathBuilder ()
grid :: [GridOption] -> TPoint -> PathBuilder ()
grid [GridOption]
xs TPoint
p = (TPath -> TPath) -> PathBuilder ()
applyToPath ((TPath -> TPath) -> PathBuilder ())
-> (TPath -> TPath) -> PathBuilder ()
forall a b. (a -> b) -> a -> b
$ \TPath
x -> TPath -> [GridOption] -> TPoint -> TPath
Grid TPath
x [GridOption]
xs TPoint
p
node :: LaTeX -> PathBuilder ()
node :: LaTeX -> PathBuilder ()
node LaTeX
l = (TPath -> TPath) -> PathBuilder ()
applyToPath ((TPath -> TPath) -> PathBuilder ())
-> (TPath -> TPath) -> PathBuilder ()
forall a b. (a -> b) -> a -> b
$ \TPath
x -> TPath -> LaTeX -> TPath
Node TPath
x LaTeX
l
bpath :: TPoint -> PathBuilder a -> TPath
bpath :: forall a. TPoint -> PathBuilder a -> TPath
bpath TPoint
p PathBuilder a
pb = PathState -> TPath
currentPath (PathState -> TPath) -> PathState -> TPath
forall a b. (a -> b) -> a -> b
$ State PathState a -> PathState -> PathState
forall s a. State s a -> s -> s
execState (PathBuilder a -> State PathState a
forall a. PathBuilder a -> State PathState a
pathBuilder PathBuilder a
pb) (TPath -> PathState
PS (TPath -> PathState) -> TPath -> PathState
forall a b. (a -> b) -> a -> b
$ TPoint -> TPath
Start TPoint
p)