-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathgui_demo.rhm
More file actions
355 lines (309 loc) · 12.2 KB
/
gui_demo.rhm
File metadata and controls
355 lines (309 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#lang rhombus/static
import:
draw
gui:
expose:
<~
~>
// ----------------------------------------
fun draw_face(dc :: draw.DC, config :: Map):
let draw.Size(w, h) = dc.size
let s = math.max(10, math.min(w, h) - 20)
let scale = config["scale"] / 50
let w = w / scale
let h = h / scale
let x = (w-s)/2
let y = (h-s)/2
let π = math.pi
dc.save_and_restore:
dc.scale(scale)
unless config["enabled"]
| dc.alpha := 0.3
let background:
match config["mood"]
| "Happy":
let center = [w/2, h/2]
draw.RadialGradient([center, s/2],
[center, math.max(w, h)],
[[0, draw.Color("white")],
[1, draw.Color("yellow")]])
| "Sad":
draw.LinearGradient([0, 0],
[0, h],
[[0.5, draw.Color("white")],
[1, draw.Color("darkblue")]])
// draw a gradient background
dc.pen := draw.Pen.none
dc.brush := draw.Brush(~gradient: background)
dc.rectangle([0, 0, w, h])
// draw keyed text
dc.font := dc.font with (size = dc.font.size * 2)
dc.text(config["keyed"])
// draw ghost dots (showing recent mouse movements)
when config["track mouse"]
| let dot_size = [4, 4]
for values(brush :~ draw.Brush:
draw.Brush(~color: draw.Color(0, 0, 0, 0.25))):
each [x, y] in (config["ghosts"] :: List).reverse()
dc.brush := brush
let x = x/scale
let y = y/scale
dc.ellipse([[x-2, y-2], dot_size])
brush with (color = brush.color with (alpha = brush.color.alpha * 0.7))
// Rotated face:
dc.save_and_restore:
dc.translate(w/2, h/2)
dc.rotate(config["rotation"])
dc.translate(-w/2, -h/2)
// face shape
dc.brush := draw.Brush(~color: "orange")
let face_rect = [x, y, s, s]
match config["shape"]
| "Circle": dc.ellipse(face_rect)
| "Square": dc.rectangle(face_rect)
| "Rounded": dc.rounded_rectangle(face_rect)
// mouth
dc.pen := draw.Pen(~color: "Black")
dc.brush := draw.Brush.none
match config["mood"]
| "Happy":
dc.arc([x + 0.2*s, y + 0.2*s, 0.6*s, 0.6*s], π * -3/4, π * -1/4)
| "Sad":
dc.arc([x + 0.2*s, y + 0.7*s, 0.6*s, 0.6*s], π * 1/4, π * 3/4)
// eyes
when config["eyes"]
| dc.pen := draw.Pen.none
dc.brush := draw.Brush(~color: "black")
let eye_size = [0.1*s, 0.1*s]
dc.ellipse([[x+0.3*s, y+0.3*s], eye_size])
dc.ellipse([[x+0.6*s, y+0.3*s], eye_size])
// moustache
when config["moustache"]
| let p = draw.Path()
p.move_to([0, 0])
p.curve_to([20, -10], [80, -10], [100, 0])
p.curve_to([120, 10], [180, 0], [200, -20])
p.curve_to([180, 20], [120, 40], [90, 30])
p.curve_to([60, 20], [20, 20], [0, 0])
p.close()
p.scale(s/500, s/500)
dc.pen := draw.Pen.none
dc.brush := draw.Brush(~color: "brown")
dc.path(p, ~dx: x+0.5*s, ~dy: y+0.6*s)
p.scale(-1, 1)
dc.path(p, ~dx: x+0.5*s, ~dy: y+0.6*s)
let name = config["name"]
let [h_pos, v_pos] = config["name pos"]
let (nw, nh, _, _) = dc.text_extent(name)
dc.text(name,
~dx: match h_pos
| "Left": 0
| "Center": (w - nw) / 2
| "Right": w - nw,
~dy: match v_pos
| "Top": 0
| "Center": (h - nh) / 2
| "Bottom": h - nh)
// ----------------------------------------
gui.Obs.def at_tab = "Happy"
gui.Obs.def enabled = #true
gui.Obs.def track_mouse = #true
gui.Obs.def rotation = 0
def menu_bar:
fun rotate_item(label, dir, shortcut):
gui.MenuItem("Rotate " ++ label,
~action:
fun ():
rotation <~ fun (rot): rot + dir * math.pi/8,
~shortcut: shortcut)
gui.MenuBar(
gui.Menu(
"Config",
gui.CheckableMenuItem("Enabled",
~checked: enabled.value,
~action:
fun (on):
enabled.value := on),
gui.CheckableMenuItem("Track Mouse",
~checked: track_mouse.value,
~action:
fun (on):
track_mouse.value := on),
gui.MenuItemSeparator(),
rotate_item("Clockwise", -1, Char"R"),
rotate_item("Counterclockwise", 1, gui.MenuItem.default_shortcut_prefix() ++ [#'shift, Char"R"])
)
)
// ----------------------------------------
gui.Obs.def name = "Smiley"
def name_input:
gui.Input(name.value,
~label: "Name:",
~choices: ["Smiley", "Frowny"],
~action: fun (action, str):
name.value := str)
def pos_button:
gui.Button("Position...",
~action: fun ():
gui.render(format_dialog,
renderer))
def summary_button:
gui.Button("Summary",
~action: fun ():
gui.render(summary_win))
def top_panel = gui.HPanel(name_input,
gui.Progress(name ~> (fun (s :: String): math.min(10, s.length())),
~max_value: 10,
~min_size: [80, #false],
~stretch: [#false, #false]),
pos_button,
summary_button,
~stretch: [#true, #false])
// ----------------------------------------
gui.Obs.def name_pos = ["Right", "Bottom"]
gui.Obs.def next_name_pos = name_pos.value
def name_pos_v = gui.RadioChoice(["Top", "Center", "Bottom"],
~stretch: [#false, #false],
~selection: name_pos ~> fun ([h, v]): v,
~action: fun (v):
next_name_pos <~ fun ([h, _]): [h, v])
def name_pos_h = gui.RadioChoice(["Left", "Center", "Right"],
~style: [#'horizontal],
~selection: name_pos ~> fun ([h, v]): h,
~action: fun (h):
next_name_pos <~ fun ([_, v]): [h, v])
def format_dialog = gui.Dialog(~title: "Position",
gui.VPanel(
name_pos_v,
gui.Spacer(~min_size: [0, 20]),
name_pos_h
),
gui.Spacer(~min_size: [0, 20]),
gui.HPanel(
gui.Button("Ok",
~action: fun ():
name_pos.value := next_name_pos.value
format_dialog.show(#false),
~style: [#'default]),
gui.Button("Cancel",
~action: fun ():
next_name_pos.value := name_pos.value
format_dialog.show(#false))
))
// ----------------------------------------
def (tabs, config):
let shape = gui.Choice(gui.Obs(["Circle", "Square", "Rounded"]),
~selection: "Circle")
let eyes = gui.Checkbox("Eyes", ~checked: #true)
let stache = gui.Checkbox("Moustache", ~checked: #false)
let scale = gui.Slider(~value: 50,
~min_value: 1,
~max_value: 100,
// Could write `gui.Slider.Style.plain`,
// etc., but we write symbols for convenience:
~style: [#'plain, #'horizontal])
let spacer = gui.Spacer()
let happy_sad_popup:
gui.PopupMenu(gui.MenuItem("Be Happy",
~action: fun ():
at_tab.value := "Happy"),
gui.MenuItem("Too Sad",
~action: fun ():
at_tab.value := "Sad"))
gui.Obs.let ghosts = []
fun ghost_mouse(ev :: gui.MouseEvent, area):
ghosts <~ (fun (l :: List):
let keep :~ List:
if l.length() < 10 | l | l.rest
keep ++ [[ev.x, ev.y]])
when ev.kind == gui.MouseEvent.Kind.right_down
| canvas.popup(happy_sad_popup,
ev.x,
ev.y)
gui.Obs.let keyed = ""
fun accum_key(ev :: gui.KeyEvent, area):
keyed <~ (fun (s :: String):
match ev.code
| Char"\r":
name_input.focus()
s
| Char"\b" || Char"\x7F" /* ASCII delete */:
if s.length() == 0 | s | s.substring(0, s.length() - 1)
| c :: Char: s ++ to_string(c)
| ~else: s)
def config:
{ "enabled": enabled,
"mood": at_tab,
"shape": shape.at_selection,
"eyes": eyes.at_checked,
"moustache": stache.at_checked,
"scale": scale.at_value,
"ghosts": ghosts,
"keyed": keyed,
"name": name,
"name pos": name_pos,
"track mouse": track_mouse,
"rotation": rotation }
|> gui.Obs.combine(_)
|> (_.rename("comfig"))
def canvas = gui.VPanel(config
|> gui.Canvas(_,
draw_face,
~mouse: ghost_mouse,
~key: accum_key),
gui.HPanel(shape, eyes, stache,
spacer,
gui.Label("-"), scale, gui.Label("+"),
~stretch: [#true, #false]))
let happy_button = gui.Button("Be Happy",
~action: fun ():
at_tab.value := "Happy")
let happy_image:
let bm = draw.Bitmap([60, 60])
draw_face(bm.make_dc(), { "enabled": #true,
"mood": "Happy",
"shape": "Circle",
"eyes": #true,
"moustache": #false,
"scale": 50,
"ghosts": #false,
"keyed": "",
"name": "",
"name pos": ["Center", "Center"],
"track mouse": #false,
"rotation": 0 })
bm
let img:
let img_src = gui.Obs(happy_image)
gui.Image(img_src,
~margin: [10, 10],
~window_callbacks:
gui.WindowCallbacks(~accepts_drop_file: #true,
~drop_file: fun (path): img_src.value := path))
values(
gui.TabPanel(["Happy", "Sad"],
~selection: at_tab,
~action: fun (_, _, sel): at_tab.value := sel,
at_tab ~> (match _
| "Happy": canvas
| "Sad": gui.HPanel(canvas,
gui.VPanel(~stretch: [#false, #true],
happy_button,
img)))),
config
)
// ----------------------------------------
def summary_win = gui.Window(
gui.Table(["Key", "Value"],
config ~> (fun (m :: Map):
for Array (key in m.keys(#true)):
Array(key, to_string(m[key]))),
~min_size: [400, 300])
)
// ----------------------------------------
def win = gui.Window(top_panel,
tabs,
~enable: enabled,
~menu_bar: menu_bar,
~size: [800, 600])
def renderer = win.render()