-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightbulb.ml
57 lines (45 loc) · 1.5 KB
/
lightbulb.ml
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
(* Lightbulb example using checkboxes.
This application will not work until you finish Task 5. *)
;; open Widget
;; open Gctx
(* Make a lightbulb widget controlled by a checkbox's state. *)
let mk_state_lightbulb () : widget =
let (switch_w, switch_cb) =
checkbox false "STATE LIGHT" in
(* A function to display the bulb *)
let paint_bulb (g:gctx) : unit =
let g_new = with_color g
(if switch_cb.get_value ()
then yellow
else black) in
fill_rect g_new (0, 0) (99, 99)
in
let (bulb, _) = canvas (100,100) paint_bulb
in
hpair bulb switch_w
(* In order to work with mutability, we will define a record with
one mutable field called checked. *)
type state = {mutable checked: bool}
(* Make a lightbulb that registers an item listener with the checkbox *)
let mk_listener_lightbulb () : widget =
let is_on = {checked = false} in
let (switch_w, switch_cb) =
checkbox false "LISTENER LIGHT" in
switch_cb.add_change_listener (fun b -> is_on.checked <- b);
(* A function to display the bulb *)
let paint_bulb (g:gctx) : unit =
let g_new = with_color g
(if is_on.checked
then yellow
else black) in
fill_rect g_new (0, 0) (99, 99)
in
let (bulb, _) = canvas (100,100) paint_bulb
in
hpair bulb switch_w
(* master widget *)
let w = hpair (border (mk_state_lightbulb ()))
(hpair (space (10,10))
(border (mk_listener_lightbulb ())))
(** Run the event loop to process user events. *)
;; Eventloop.run w