packages feed

geomancy-layout-0.2.1 / test / Menu / Svg.hs

{-# LANGUAGE OverloadedStrings #-}module Menu.Svg  ( renderMenu  ) whereimport Control.Monad (when)import Data.Foldable (traverse_)import Data.String (IsString, fromString)import Geomancy.Layout.Box (Box)import Geomancy.Layout.Box qualified as Boximport Geomancy.Layout.View (View(..))import Geomancy.Vec2 (Vec2, withVec2)import Lucid.Svgimport Menu.KitrenderMenu :: Bool -> Vec2 -> View Box Widget -> Svg ()renderMenu outlines screen tree =  withVec2 screen \w h -> do    doctype_    with      ( svg11_ do          definitions          rect_ [width_ "100%", height_ "100%", fill_ "#101014"]          content tree          when outlines (frames tree)      )      [ version_ "1.1"      , width_ (num w)      , height_ (num h)      , viewBox_ ("0 0 " <> num w <> " " <> num h)      ]definitions :: Svg ()definitions =  defs_ do    linearGradient_ [id_ "shade", x1_ "0", y1_ "0", x2_ "0", y2_ "1"] do      stop_ [offset_ "0", stop_color_ "#000", stop_opacity_ "0"]      stop_ [offset_ "1", stop_color_ "#000", stop_opacity_ "0.6"]    pattern_ [id_ "hatch", width_ "28", height_ "28", patternUnits_ "userSpaceOnUse"] do      path_ [d_ "M-7,7 L7,-7 M0,28 L28,0 M21,35 L35,21", stroke_ "#fff", stroke_opacity_ "0.08", stroke_width_ "6"]content :: View Box Widget -> Svg ()content = \case  View{ann, stuff} -> drawWidget ann stuff  Spacer{} -> pure ()  HStack{children} -> traverse_ content children  VStack{children} -> traverse_ content children  ZStack{children} -> traverse_ content children  Grid{rows} -> traverse_ content (concat rows)  Overlay{primary, secondary} -> content primary >> content secondary  Background{primary, secondary} -> content secondary >> content primary  Frame{inner} -> content inner  FlexibleFrame{inner} -> content inner  Padding{inner} -> content inner  Offset{inner} -> content inner  Span{inner} -> content inner  AspectRatio{inner} -> content innerdrawWidget :: Box -> Widget -> Svg ()drawWidget box w =  case w.look of    Fill p -> panel box p    Art p caption -> do      panel box p      panel box (paint "url(#hatch)"){corner = p.corner}      panel box (paint "url(#shade)"){corner = p.corner}      corners box \_t r b l ->        text_          [ x_ (num $ (l + r) / 2)          , y_ (num $ b - 14)          , font_size_ "15"          , font_family_ "sans-serif"          , font_style_ "italic"          , text_anchor_ "middle"          , fill_ "#fff"          , fill_opacity_ "0.45"          ]          (fromString caption)    Label f s ->      corners box \t r _b l ->        text_          [ x_ (num l)          , y_ (num $ t + f.points * 1.02)          , font_size_ (num f.points)          , font_family_ (if f.roman then "Georgia, serif" else "Helvetica, Arial, sans-serif")          , font_weight_ (if f.bold then "bold" else "normal")          , fill_ (fromString f.ink)          , textLength_ (num $ max 0 (r - l))          , lengthAdjust_ "spacingAndGlyphs"          ]          (fromString s)    Glyph p side mark caption -> do      shape box p mark      when (not $ null caption) $        corners box \t r b l ->          text_            [ x_ (num $ (l + r) / 2)            , y_ (num $ (t + b) / 2 + side * 0.17)            , font_size_ (num $ side * 0.46)            , font_family_ "Helvetica, Arial, sans-serif"            , font_weight_ "bold"            , text_anchor_ "middle"            , fill_ "#fff"            , fill_opacity_ "0.85"            ]            (fromString caption)shape :: Box -> Paint -> Mark -> Svg ()shape box p = \case  Disc -> corners box \t r b l -> circle_ ([cx_ (num $ (l + r) / 2), cy_ (num $ (t + b) / 2), r_ (num $ (r - l) / 2)] <> painted p)  Ring -> corners box \t r b l -> circle_ ([cx_ (num $ (l + r) / 2), cy_ (num $ (t + b) / 2), r_ (num $ (r - l) / 2 - 1.5), fill_ "none", stroke_ (fromString p.tint), stroke_width_ "3"])  Tile -> panel box p  Diamond -> polygonIn box p [(0.5, 0), (1, 0.5), (0.5, 1), (0, 0.5)]  Star -> polygonIn box p [ (0.5 + 0.5 * k * sin a, 0.5 - 0.5 * k * cos a) | i <- [0 .. 9 :: Int], let a = fromIntegral i * pi / 5, let k = if even i then 1 else 0.45 ]  PointLeft -> polygonIn box p [(1, 0), (0, 0.5), (1, 1)]  PointRight -> polygonIn box p [(0, 0), (1, 0.5), (0, 1)]  Blank -> pure ()polygonIn :: Box -> Paint -> [(Float, Float)] -> Svg ()polygonIn box p unit =  corners box \t r b l ->    polygon_      ( points_ (fromString $ unwords [show (l + u * (r - l)) <> "," <> show (t + v * (b - t)) | (u, v) <- unit])          : painted p      )panel :: Box -> Paint -> Svg ()panel box p =  corners box \t r b l ->    rect_      ( [ x_ (num l)        , y_ (num t)        , width_ (num $ max 0 (r - l))        , height_ (num $ max 0 (b - t))        , rx_ (num p.corner)        ]          <> painted p      )painted :: Paint -> [Attribute]painted p =  [fill_ (fromString p.tint), fill_opacity_ (num p.alpha)]    <> maybe [] (\colour -> [stroke_ (fromString colour), stroke_width_ "2"]) p.rimframes :: View Box Widget -> Svg ()frames node = do  traverse_ frames (subviews node)  corners node.ann \t r b l ->    rect_      ( [ x_ (num l)        , y_ (num t)        , width_ (num $ max 0 (r - l))        , height_ (num $ max 0 (b - t))        , fill_ "none"        , stroke_ (outlineColour node)        , stroke_width_ "1"        , stroke_opacity_ "0.9"        ]          <> [stroke_dasharray_ "4 3" | dashed node]      )dashed :: View ann stuff -> Booldashed = \case  Spacer{} -> True  Padding{} -> True  Offset{} -> True  _ -> FalseoutlineColour :: IsString s => View ann stuff -> soutlineColour = \case  View{} -> "#ffffff"  Spacer{} -> "#7fb7ff"  HStack{} -> "#3dff6e"  VStack{} -> "#5c7cff"  ZStack{} -> "#ff5c5c"  Grid{} -> "#c8a2ff"  Overlay{} -> "#ffa23d"  Background{} -> "#a2ff3d"  Frame{} -> "#ff4fd8"  FlexibleFrame{} -> "#ffe14f"  AspectRatio{} -> "#4fffe9"  Padding{} -> "#9a9a9a"  Offset{} -> "#4fd2ff"  Span{} -> "#d2ff4f"subviews :: View ann stuff -> [View ann stuff]subviews = \case  View{} -> []  Spacer{} -> []  HStack{children} -> children  VStack{children} -> children  ZStack{children} -> children  Grid{rows} -> concat rows  Overlay{primary, secondary} -> [primary, secondary]  Background{primary, secondary} -> [secondary, primary]  Frame{inner} -> [inner]  FlexibleFrame{inner} -> [inner]  Padding{inner} -> [inner]  Offset{inner} -> [inner]  Span{inner} -> [inner]  AspectRatio{inner} -> [inner]corners :: Box -> (Float -> Float -> Float -> Float -> r) -> rcorners = Box.withTRBLnum :: IsString s => Float -> snum x =  fromString $    if x == fromIntegral (round x :: Int) then show (round x :: Int) else show x