{-# 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
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative
#endif
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) = forall a. State PathState a -> PathBuilder a
PB forall a b. (a -> b) -> a -> 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 = forall a. State PathState a -> PathBuilder a
PB forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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) = forall a. State PathState a -> PathBuilder a
PB forall a b. (a -> b) -> a -> b
$ State PathState (a -> b)
f 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 = 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 = forall a. State PathState a -> PathBuilder a
PB forall a b. (a -> b) -> a -> b
$ State PathState a
x forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall a. PathBuilder a -> State PathState a
pathBuilder 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 = forall a. State PathState a -> PathBuilder a
PB forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) s. Monad m => (s -> s) -> StateT s m ()
modify forall a b. (a -> b) -> a -> b
$ \PathState
ps -> PathState
ps { currentPath :: TPath
currentPath = TPath -> TPath
f (PathState -> TPath
currentPath PathState
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 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 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 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 forall a b. (a -> b) -> a -> b
$ forall s a. State s a -> s -> s
execState (forall a. PathBuilder a -> State PathState a
pathBuilder PathBuilder a
pb) (TPath -> PathState
PS forall a b. (a -> b) -> a -> b
$ TPoint -> TPath
Start TPoint
p)