module Diagrams.Lens
( _P
, _envelopeVMove
, _alignedVMove
, _corners
, _boxExtents
, _attr
, _mkAttr
, _mkTAttr
, _location
, _mkSubdiagram
, _Loc
, _mkFixedSeg
, _straight
, _bezier3
, _lineVertices
, _lineOffsets
, _lineSegments
) where
import Control.Applicative
import Control.Lens
import Data.AffineSpace.Point (Point (P))
import Data.Basis
import Diagrams.BoundingBox
import Diagrams.Core.Style
import Diagrams.Prelude
_P :: Iso (Point v) (Point v') v v'
_P = iso (\(P x) -> x) P
_envelopeVMove
:: (Monoid a, HasOrigin a, Enveloped a, Num (Scalar (V a)))
=> V a -> Traversal' a (V a)
_envelopeVMove v f x = case envelopeVMay v x of
(Just p) -> (\p' -> moveOriginBy (p ^-^ p') x) <$> f p
Nothing -> pure x
_alignedVMove
:: (Monoid a, HasOrigin a, Enveloped a, Num (Scalar (V a)))
=> V a -> Scalar (V a) -> Traversal' a (V a)
_alignedVMove v d f x = case appEnvelope $ getEnvelope x of
(Just env) -> (\p' -> moveOriginBy (p ^-^ p') x) <$> f p
where
p = v ^* lerp' (env (negateV v)) (env v) ((d + 1) / 2)
lerp' l u t = (1 t) * u + t * l
Nothing -> pure x
_corners
:: ( HasBasis v, Ord (Basis v), AdditiveGroup (Scalar v), Ord (Scalar v)
, HasBasis v', Ord (Basis v'), AdditiveGroup (Scalar v'), Ord (Scalar v'))
=> Traversal (BoundingBox v) (BoundingBox v') (Point v) (Point v')
_corners f (getCorners -> Just (l, t)) = fromCorners <$> f l <*> f t
_corners _ _ = pure emptyBox
_boxExtents
:: (HasBasis v, Ord (Basis v), AdditiveGroup (Scalar v), Ord (Scalar v))
=> Lens' (BoundingBox v) v
_boxExtents = lens boxExtents setExtent
where
setExtent (getCorners -> Just (l, _)) x = fromCorners l (l .+^ x)
setExtent _ _ = emptyBox
_attr :: AttributeClass a => Lens' (Style v) (Maybe a)
_attr = lens getAttr setAttr'
where
setAttr' style (Just x) = setAttr x style
setAttr' style Nothing = style
_mkAttr
:: AttributeClass a => Prism' (Attribute v) a
_mkAttr = prism' mkAttr unwrapAttr
_mkTAttr
:: (AttributeClass a, Transformable a, V a ~ v)
=> Prism' (Attribute v) a
_mkTAttr = prism' mkTAttr unwrapAttr
_location
:: (HasLinearMap v, InnerSpace v, OrderedField (Scalar v))
=> Lens' (Subdiagram b v m) (Point v)
_location = lens location (flip Diagrams.Prelude.moveTo)
_mkSubdiagram
:: (HasLinearMap v, InnerSpace v, OrderedField (Scalar v), Semigroup m)
=> Iso' (QDiagram b v m) (Subdiagram b v m)
_mkSubdiagram = iso mkSubdiagram getSub
_Loc :: Iso (Located a) (Located a') (Point (V a), a) (Point (V a'), a')
_Loc = iso viewLoc (uncurry $ flip Diagrams.Prelude.at)
_mkFixedSeg
:: (AdditiveGroup v, AdditiveGroup v')
=> Iso
(Located (Segment Closed v))
(Located (Segment Closed v'))
(FixedSegment v)
(FixedSegment v')
_mkFixedSeg = iso mkFixedSeg fromFixedSeg
_straight :: Prism' (Segment Closed v) v
_straight = prism' straight fromStraight
where
fromStraight :: Segment c a -> Maybe a
fromStraight (Linear (OffsetClosed x)) = Just x
fromStraight _ = Nothing
_bezier3 :: Prism' (Segment Closed v) (v, v, v)
_bezier3 = prism' (\(c1, c2, c3) -> bezier3 c1 c2 c3) fromBezier3
where
fromBezier3 :: Segment c a -> Maybe (a, a, a)
fromBezier3 (Cubic c1 c2 (OffsetClosed c3)) = Just (c1, c2, c3)
fromBezier3 _ = Nothing
_lineVertices
:: ( InnerSpace v, OrderedField (Scalar v)
, InnerSpace v', OrderedField (Scalar v'))
=> Iso
(Located (Trail' Line v)) (Located (Trail' Line v'))
[Point v] [Point v']
_lineVertices = iso lineVertices fromVertices
_lineOffsets
:: ( InnerSpace v, OrderedField (Scalar v)
, InnerSpace v', OrderedField (Scalar v'))
=> Iso
(Trail' Line v) (Trail' Line v')
[v] [v']
_lineOffsets = iso lineOffsets lineFromOffsets
_lineSegments
:: ( InnerSpace v, OrderedField (Scalar v)
, InnerSpace v', OrderedField (Scalar v'))
=> Iso
(Trail' Line v) (Trail' Line v')
[Segment Closed v] [Segment Closed v']
_lineSegments = iso lineSegments lineFromSegments