Debian Codegraph @

src/Diagrams/Animation/Active.hs

raw symbols SCIP
1 {-# LANGUAGE TypeFamilies #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Diagrams.Animation.Active
7 -- Copyright : (c) 2011 Brent Yorgey
8 -- License : BSD-style (see LICENSE)
9 -- Maintainer : [email protected]
10 --
11 -- A few utilities and class instances for 'Active' (from the @active@
12 -- package). In particular, this module defines
13 --
14 -- * An instance of 'V' for 'Active': @'V' ('Active' a) = 'V' a@
15 --
16 -- * 'HasOrigin', 'Transformable', and 'HasStyle' instances for
17 -- 'Active' which all work pointwise.
18 --
19 -- * A 'TrailLike' instance for @'Active' p@ where @p@ is also
20 -- 'TrailLike', which simply lifts a pathlike thing to a constant
21 -- active value.
22 --
23 -- * A 'Juxtaposable' instance for @'Active' a@ where @a@ is also
24 -- 'Juxtaposable'. An active value can be juxtaposed against
25 -- another by doing the juxtaposition pointwise over time. The
26 -- era of @juxtapose v a1 a2@ will be the same as the era of @a2@,
27 -- unless @a2@ is constant, in which case it will be the era of
28 -- @a1@. (Note that @juxtapose v a1 a2@ and @liftA2 (juxtapose v)
29 -- a1 a2@ therefore have different semantics: the second is an
30 -- active value whose era is the /combination/ of the eras of @a1@
31 -- and @a2@).
32 --
33 -- * An 'Alignable' instance for @'Active' a@ where @a@ is also
34 -- 'Alignable'; the active value is aligned pointwise over time.
35
36 -----------------------------------------------------------------------------
37
38 module Diagrams.Animation.Active where
39
40 import Diagrams.Core
41 import Diagrams.TrailLike
42
43 import Data.Active
44
45 type instance V (Active a) = V a
46 type instance N (Active a) = N a
47
48 -- Yes, these are all orphan instances. Get over it. We don't want to
49 -- put them in the 'active' package because 'active' is supposed to be
50 -- generally useful and shouldn't depend on diagrams. We'd also
51 -- rather not put them in diagrams-core so that diagrams-core doesn't
52 -- have to depend on active.
53
54 instance HasOrigin a => HasOrigin (Active a) where
55 moveOriginTo = fmap . moveOriginTo
56
57 instance Transformable a => Transformable (Active a) where
58 transform = fmap . transform
59
60 instance HasStyle a => HasStyle (Active a) where
61 applyStyle = fmap . applyStyle
62
63 instance TrailLike t => TrailLike (Active t) where
64 trailLike = pure . trailLike
65
66 -- | An active value can be juxtaposed against another by doing the
67 -- juxtaposition pointwise over time. The era of @juxtapose v a1
68 -- a2@ will be the same as the era of @a2@, unless @a2@ is constant,
69 -- in which case it will be the era of @a1@. (Note that @juxtapose
70 -- v a1 a2@ and @liftA2 (juxtapose v) a1 a2@ therefore have
71 -- different semantics: the second is an active value whose era is
72 -- the /combination/ of the eras of @a1@ and @a2@).
73 instance Juxtaposable a => Juxtaposable (Active a) where
74
75 juxtapose v a1 a2 =
76 onActive -- a1
77 (\c1 -> -- if a1 is constant, just juxtapose a2 pointwise with its value
78 juxtapose v c1 <$> a2
79 )
80 -- if a1 is dynamic...
81 (onDynamic $ \s1 e1 d1 ->
82 onActive -- a2
83 (\c2 -> -- if a2 is constant, juxtapose pointwise with a1. Since
84 -- the result will no longer be constant, the result
85 -- needs an era: we use a1's.
86 mkActive s1 e1 (\t -> juxtapose v (d1 t) c2)
87 )
88
89 -- otherwise, juxtapose pointwise, without changing a2's era
90 (onDynamic $ \s2 e2 d2 ->
91 mkActive s2 e2 (\t -> juxtapose v (d1 t) (d2 t))
92 )
93 a2
94 )
95 a1
96
97 -- instance Alignable a => Alignable (Active a) where
98 -- alignBy v d a = alignBy v d <$> a