packages feed

geomancy-layout-0.2.0: test/Menu/Kit.hs

module Menu.Kit
  ( UI
  , Widget(..)
  , Look(..)
  , Mark(..)
  , Paint(..)
  , Font(..)
  , paint
  , faded
  , rounded
  , rimmed
  , font
  , heavy
  , serif
  , textWidth
  , lineHeight
  , widget
  , label
  , named
  , fill
  , art
  , glyph
  , roomy
  , backdrop
  , divider
  ) where

import Data.Char (isDigit, isLower, isUpper)
import Geomancy.Layout.Alignment qualified as Alignment
import Geomancy.Layout.Box qualified as Box
import Geomancy.Layout.View
import Geomancy.Vec2 (vec2, withVec2)
import Geomancy.Vec4 (Vec4)

type UI = View () Widget

data Widget = Widget
  { name :: String
  , look :: Look
  , room :: Vec4
  }
  deriving (Eq, Show)

data Look
  = Fill Paint
  | Art Paint String
  | Label Font String
  | Glyph Paint Float Mark String
  deriving (Eq, Show)

data Mark = Disc | Ring | Tile | Diamond | Star | PointLeft | PointRight | Blank
  deriving (Eq, Show)

data Paint = Paint
  { tint :: String
  , alpha :: Float
  , corner :: Float
  , rim :: Maybe String
  }
  deriving (Eq, Show)

data Font = Font
  { points :: Float
  , ink :: String
  , bold :: Bool
  , roman :: Bool
  }
  deriving (Eq, Show)

paint :: String -> Paint
paint tint = Paint{tint, alpha = 1, corner = 0, rim = Nothing}

faded :: Float -> Paint -> Paint
faded alpha p = p{alpha}

rounded :: Float -> Paint -> Paint
rounded corner p = p{corner}

rimmed :: String -> Paint -> Paint
rimmed colour p = p{rim = Just colour}

font :: Float -> String -> Font
font points ink = Font{points, ink, bold = False, roman = False}

heavy :: Font -> Font
heavy f = f{bold = True}

serif :: Font -> Font
serif f = f{roman = True}

textWidth :: Font -> String -> Float
textWidth f s = f.points * sum (map glyphWidth s) * if f.bold then 1.07 else 1
  where
    glyphWidth c
      | c == ' ' = 0.3
      | c == '…' = 0.9
      | isUpper c = 0.68
      | isDigit c = 0.56
      | isLower c = 0.52
      | otherwise = 0.4

lineHeight :: Font -> Float
lineHeight f = f.points * 1.3

shortest :: String -> String
shortest s =
  case s of
    _ : _ : _ -> take 1 s <> "…"
    _ -> s

fitText :: Font -> Float -> String -> String
fitText f width s
  | textWidth f s <= width = s
  | otherwise =
      case [cut | n <- [length s - 1, length s - 2 .. 1], let cut = take n s <> "…", textWidth f cut <= width] of
        cut : _ -> cut
        [] -> if textWidth f s < textWidth f (shortest s) then s else shortest s

instance LayoutView Widget where
  viewFlexibility w =
    (base w.look){clearance = Box.TRBL w.room}
    where
      base = \case
        Fill{} -> infinite_
        Art{} -> infinite_
        Label f s ->
          range_
            (vec2 (textWidth f $ shortest s) (lineHeight f))
            (vec2 (textWidth f s) (lineHeight f))
        Glyph _ side _ _ -> fixed_ (vec2 side side)

  proposeView proposed w =
    case w.look of
      Fill{} -> (proposed, w)
      Art{} -> (proposed, w)
      Label f s ->
        let
          shown = withVec2 proposed \width _height -> fitText f width s
        in
          (vec2 (textWidth f shown) (lineHeight f), w{look = Label f shown})
      Glyph _ side _ _ -> (vec2 side side, w)

  placeView _placed = id

widget :: String -> Look -> UI
widget name look = view_ Widget{name, look, room = 0}

label :: Font -> String -> UI
label f s = widget s (Label f s)

named :: String -> UI -> UI
named name = \case
  View{ann, stuff} -> View{ann, stuff = stuff{name}}
  other -> other

fill :: String -> Paint -> UI
fill name p = widget name (Fill p)

art :: String -> Paint -> UI
art name p = widget name (Art p name)

glyph :: String -> Paint -> Float -> Mark -> String -> UI
glyph name p side mark caption = widget name (Glyph p side mark caption)

roomy :: Vec4 -> UI -> UI
roomy trbl = \case
  View{ann, stuff} -> View{ann, stuff = stuff{room = trbl}}
  other -> other

backdrop :: String -> Paint -> UI -> UI
backdrop name p = background Alignment.center (fill name p)

divider :: String -> Paint -> UI
divider name p = flexible_ (between (vec2 0 2) (vec2 inf 2)) Alignment.center (fill name p)