sourcelib/Svg.xtl
1⍝# Svg: pictures written as SVG text, and the clicks on them, shared by
2⍝# the games played by clicking a map (capitals, stargazer). A picture's
3⍝# view v is x, y, width and height in the game's own units (its viewBox);
4⍝# layout is scaled to the view (l:u_nit), so buttons, menus and text look
5⍝# the same at every zoom. Rectangles are rows of x, y, width and height.
6
7ᵗ⁼u̲se< "Text"
8
9⍝ :: Box Char -> Float
10⍝# The number written in a text (a cell of a TOML table).
11⍝# >> "g:" u_se< "Svg"
12⍝# >> g:n_um e_nclose "48.87"
13⍝# 48.87
14ˡn̲um ← { x → f̲irst n̲umbers d̲isclose x }
15⍝ :: Float -> Char
16⍝# A whole number as text (a coordinate in a picture).
17⍝# >> "g:" u_se< "Svg"
18⍝# >> g:w_hole 2.5
19⍝# 3
20ˡw̲hole ← { x → f̲ormat f̲loor 0.5 + x }
21⍝ :: Num a => a -> Float
22⍝# Units of the view per point of a picture 1000 points wide.
23⍝# >> "g:" u_se< "Svg"
24⍝# >> g:u_nit 0 0 2000 800.0
25⍝# 2.0
26ˡu̲nit ← { v → (3 s̲elect v) ÷ 1000 }
27⍝ :: Float -> a -> Box Char
28⍝# An SVG rectangle r (x, y, width, height) with the attributes st.
29⍝# >> "g:" u_se< "Svg"
30⍝# >> d_isclose (0 0 20 10.0) g:r_ect "fill=\"red\""
31⍝# <rect x="0" y="0" width="20" height="10" fill="red"/>
32ˡr̲ect ← { r st → e̲nclose @ f̲ormat< "<rect x=\"{l:w_hole f_irst r}\" y=\"{l:w_hole 2 s_elect r}\" width=\"{l:w_hole 3 s_elect r}\" height=\"{l:w_hole 4 s_elect r}\" {st}/>" }
33⍝ :: Float -> a -> Box Char
34⍝# An SVG text at x, y in size z (xyz), with a white halo to read over a
35⍝# map.
36⍝# >> "g:" u_se< "Svg"
37⍝# >> d_isclose (10 20 30.0) g:w_ords "hi"
38⍝# <text x="10" y="20" font-size="30" paint-order="stroke" stroke="#ffffff" stroke-width="8">hi</text>
39ˡw̲ords ← { xyz t → e̲nclose @ f̲ormat< "<text x=\"{l:w_hole f_irst xyz}\" y=\"{l:w_hole 2 s_elect xyz}\" font-size=\"{l:w_hole 3 s_elect xyz}\" paint-order=\"stroke\" stroke=\"#ffffff\" stroke-width=\"{l:w_hole 0.25 * 3 s_elect xyz}\">{t}</text>" }
40⍝ :: Num a => a -> a -> Int
41⍝# Which of the rectangles r holds the point xy: 1 to the number of
42⍝# rows, 0 for none.
43⍝# >> "g:" u_se< "Svg"
44⍝# >> (2 4 r_eshape 0 0 10 10 20 0 10 10.0) g:h_it 25 5.0
45⍝# 2
46ˡh̲it ← { r xy →
47 in ← ((f̲irst xy) ≥ 1 s̲elect₂ r) ∧ ((f̲irst xy) ≤ (1 s̲elect₂ r) + 3 s̲elect₂ r) ∧ ((2 s̲elect xy) ≥ 2 s̲elect₂ r) ∧ (2 s̲elect xy) ≤ (2 s̲elect₂ r) + 4 s̲elect₂ r
48 f̲irst (w̲here in) c̲at 0
49}
50⍝ :: Float -> Int -> Float
51⍝# A row of n buttons along the top of the view v.
52⍝# >> "g:" u_se< "Svg"
53⍝# >> (0 0 2000 800.0) g:b_uttons 2
54⍝# 20.0 20.0 240.0 68.0
55⍝# 280.0 20.0 240.0 68.0
56ˡb̲uttons ← { v n →
57 u ← ˡu̲nit v
58 k ← f̲loat (r̲ange n) − 1
59 o̲\ (4 c̲at n) r̲eshape ((f̲irst v) + u × 10 + 130 × k) c̲at ((2 s̲elect v) + u × 10 + 0 × k) c̲at (u × 120 + 0 × k) c̲at u × 34 + 0 × k
60}
61⍝ :: Float -> Float -> Float
62⍝# A menu of four choices beside the point xy, kept inside the view v.
63⍝# >> "g:" u_se< "Svg"
64⍝# >> (0 0 2000 800.0) g:m_enu 1000 400.0
65⍝# 1032.0 360.0 600.0 80.0
66⍝# 1032.0 448.0 600.0 80.0
67⍝# 1032.0 536.0 600.0 80.0
68⍝# 1032.0 624.0 600.0 80.0
69ˡm̲enu ← { v xy →
70 u ← ˡu̲nit v
71 x ← f̲irst xy
72 right ← f̲loat ((f̲irst v) + 3 s̲elect v) > x + u × 330
73 x0 ← x + u × (332 × right) − 316
74 y0 ← ((2 s̲elect xy) − u × 20) m̲in ((2 s̲elect v) + (4 s̲elect v)) − u × 190
75 k ← f̲loat (r̲ange 4) − 1
76 o̲\ 4 4 r̲eshape (x0 + 0 × k) c̲at (y0 + u × 44 × k) c̲at (u × 300 + 0 × k) c̲at u × 40 + 0 × k
77}
78⍝# A button's fill: the one shown filled.
79ʰf̲ill ← { on → d̲isclose (1 + on) s̲elect "#ffffff" "#a8dadc" }
80⍝ :: Float -> Box a -> Box Char
81⍝# Buttons b (rectangles) labeled with the texts names: the one numbered
82⍝# by the view's fifth item (from 1) filled; v is the view and that number.
83⍝# >> "g:" u_se< "Svg"
84⍝# >> t_ally (0 0 2000 800 1.0) g:l_abeled "a" "b"
85⍝# 4
86ˡl̲abeled ← { vb names →
87 v ← 4 t̲ake vb
88 u ← ˡu̲nit v
89 b ← v ˡb̲uttons t̲ally names
90 k ← r̲ange t̲ally names
91 r ← '{ j → (j s̲elect b) ˡr̲ect @ f̲ormat< "class=\"region\" rx=\"{l:w_hole u * 6}\" fill=\"{h:f_ill (f_loat j) = 5 s_elect vb}\" stroke=\"#264653\"" } e̲ach k
92 r c̲at '{ j → (((1 s̲elect j s̲elect b) + u × 12) c̲at ((2 s̲elect j s̲elect b) + u × 24) c̲at u × 18) ˡw̲ords d̲isclose j s̲elect names } e̲ach k
93}
94⍝ :: Float -> Box a -> Box Char
95⍝# A menu m (four rectangles) of the texts names, each a button.
96⍝# >> "g:" u_se< "Svg"
97⍝# >> t_ally ((0 0 2000 800.0) g:m_enu 100 100.0) g:c_hoices "w" "x" "y" "z"
98⍝# 8
99ˡc̲hoices ← { m names →
100 u ← (3 s̲elect 1 s̲elect m) ÷ 300
101 r ← '{ j → (j s̲elect m) ˡr̲ect @ f̲ormat< "class=\"choice\" rx=\"{l:w_hole u * 6}\" fill=\"#fffbe6\" stroke=\"#264653\"" } e̲ach r̲ange 4
102 r c̲at '{ j → (((1 s̲elect j s̲elect m) + u × 12) c̲at ((2 s̲elect j s̲elect m) + u × 27) c̲at u × 20) ˡw̲ords d̲isclose j s̲elect names } e̲ach r̲ange 4
103}
104⍝ :: Float -> Box Char -> Char
105⍝# The picture: an SVG document of the view v holding the pieces p.
106⍝# >> "g:" u_se< "Svg"
107⍝# >> (0 0 20 10.0) g:p_icture e_nclose "<circle/>"
108⍝# <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 10 " width="1000" height="400" font-family="sans-serif"><circle/></svg>
109ˡp̲icture ← { v p →
110 vb ← ᵗj̲oin '{ x → e̲nclose (ˡw̲hole x) c̲at " " } e̲ach v
111 ᵗj̲oin (e̲nclose @ f̲ormat< "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"{vb}\" width=\"1000\" height=\"400\" font-family=\"sans-serif\">") c̲at p c̲at e̲nclose "</svg>"
112}
f̲ormat< expands to
("<rect x=\"" c̲at (f̲ormat (ˡw̲hole f̲irst r)) c̲at "\" y=\"" c̲at (f̲ormat (ˡw̲hole 2 s̲elect r)) c̲at "\" width=\"" c̲at (f̲ormat (ˡw̲hole 3 s̲elect r)) c̲at "\" height=\"" c̲at (f̲ormat (ˡw̲hole 4 s̲elect r)) c̲at "\" " c̲at (f̲ormat (st)) c̲at "/>")
f̲ormat< expands to
("<text x=\"" c̲at (f̲ormat (ˡw̲hole f̲irst xyz)) c̲at "\" y=\"" c̲at (f̲ormat (ˡw̲hole 2 s̲elect xyz)) c̲at "\" font-size=\"" c̲at (f̲ormat (ˡw̲hole 3 s̲elect xyz)) c̲at "\" paint-order=\"stroke\" stroke=\"#ffffff\" stroke-width=\"" c̲at (f̲ormat (ˡw̲hole 0.25 × 3 s̲elect xyz)) c̲at "\">" c̲at (f̲ormat (t)) c̲at "</text>")
f̲ormat< expands to
("class=\"region\" rx=\"" c̲at (f̲ormat (ˡw̲hole u × 6)) c̲at "\" fill=\"" c̲at (f̲ormat (ʰf̲ill (f̲loat j) = 5 s̲elect vb)) c̲at "\" stroke=\"#264653\"")
f̲ormat< expands to
("class=\"choice\" rx=\"" c̲at (f̲ormat (ˡw̲hole u × 6)) c̲at "\" fill=\"#fffbe6\" stroke=\"#264653\"")
f̲ormat< expands to
("<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"" c̲at (f̲ormat (vb)) c̲at "\" width=\"1000\" height=\"400\" font-family=\"sans-serif\">")