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 ) whereimport Data.Char (isDigit, isLower, isUpper)import Geomancy.Layout.Alignment qualified as Alignmentimport Geomancy.Layout.Box qualified as Boximport Geomancy.Layout.Viewimport Geomancy.Vec2 (vec2, withVec2)import Geomancy.Vec4 (Vec4)type UI = View () Widgetdata 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 -> Paintpaint tint = Paint{tint, alpha = 1, corner = 0, rim = Nothing}faded :: Float -> Paint -> Paintfaded alpha p = p{alpha}rounded :: Float -> Paint -> Paintrounded corner p = p{corner}rimmed :: String -> Paint -> Paintrimmed colour p = p{rim = Just colour}font :: Float -> String -> Fontfont points ink = Font{points, ink, bold = False, roman = False}heavy :: Font -> Fontheavy f = f{bold = True}serif :: Font -> Fontserif f = f{roman = True}textWidth :: Font -> String -> FloattextWidth 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.4lineHeight :: Font -> FloatlineHeight f = f.points * 1.3shortest :: String -> Stringshortest s = case s of _ : _ : _ -> take 1 s <> "…" _ -> sfitText :: Font -> Float -> String -> StringfitText 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 sinstance 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 = idwidget :: String -> Look -> UIwidget name look = view_ Widget{name, look, room = 0}label :: Font -> String -> UIlabel f s = widget s (Label f s)named :: String -> UI -> UInamed name = \case View{ann, stuff} -> View{ann, stuff = stuff{name}} other -> otherfill :: String -> Paint -> UIfill name p = widget name (Fill p)art :: String -> Paint -> UIart name p = widget name (Art p name)glyph :: String -> Paint -> Float -> Mark -> String -> UIglyph name p side mark caption = widget name (Glyph p side mark caption)roomy :: Vec4 -> UI -> UIroomy trbl = \case View{ann, stuff} -> View{ann, stuff = stuff{room = trbl}} other -> otherbackdrop :: String -> Paint -> UI -> UIbackdrop name p = background Alignment.center (fill name p)divider :: String -> Paint -> UIdivider name p = flexible_ (between (vec2 0 2) (vec2 inf 2)) Alignment.center (fill name p)