-
Notifications
You must be signed in to change notification settings - Fork 1
/
circledrawer.ml
142 lines (124 loc) · 4.84 KB
/
circledrawer.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
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
let (_locale:string) = GMain.init ()
type circle = { x:float; y: float; mutable r: float}
type action = { undo: unit -> unit; redo: unit -> unit}
let redo_list = ref []
let undo_list = ref []
let circles = Hashtbl.create 10
let selected_circle = ref None
let w = GWindow.window ~title:"Circle Drawer" ()
let table = GPack.table ~row_spacings:4 ~col_spacings:4 ~rows:2 ~columns:2 ~homogeneous:false ~show:true ~packing:w#add ()
let button_undo = GButton.button ~label:("Undo") ~packing:(table#attach ~left:0 ~top:0) ()
let button_redo = GButton.button ~label:("Redo") ~packing:(table#attach ~left:1 ~top:0) ()
let drawing = GMisc.drawing_area ~packing:(table#attach ~left:0 ~right:2~top:1) ()
let () = drawing#misc#set_size_request ~width:200 ~height:200 ()
let draw widget cr =
ignore widget;
Option.iter (fun circle ->
Cairo.set_source_rgb cr 0.5 0.5 0.5;
Cairo.arc cr circle.x circle.y ~r:circle.r ~a1:0. ~a2:(2.*.Float.pi);
Cairo.fill cr
) !selected_circle;
Hashtbl.iter (fun _coord circle ->
Cairo.arc cr circle.x circle.y ~r:circle.r ~a1:0. ~a2:(2.*.Float.pi);
Cairo.set_source_rgb cr 0. 0. 0.;
Cairo.stroke cr
) circles;
true
let _ = drawing#misc#connect#draw ~callback:(draw drawing)
let check_undo () =
button_undo#set_sensitive (!undo_list <> []) ;
button_redo#set_sensitive (!redo_list <> [])
let () = check_undo ()
let _ = button_undo#connect#clicked ~callback:(fun () ->
begin
match !undo_list with
| hd::tl -> hd.undo (); redo_list:=hd::!redo_list; undo_list:=tl
| [] -> ()
end;
check_undo() )
let _ = button_redo#connect#clicked ~callback:(fun () ->
begin
match !redo_list with
| hd::tl -> hd.redo (); undo_list:=hd::!undo_list; redo_list:=tl
| [] -> ()
end;
check_undo () )
let calc_selection mouse_x mouse_y =
selected_circle := None;
let min_d = ref None in
Hashtbl.iter (fun _coord circle ->
let mouse_r = Float.sqrt((circle.x-.mouse_x)**2.+.(circle.y-.mouse_y)**2.) in
if mouse_r < circle.r then
let d = circle.r-.mouse_r in
match !min_d with
| Some d' -> if d<d' then
min_d := Some d; selected_circle := Some circle
| None -> selected_circle := Some circle) circles
let change_diameter x y radius =
let c = Hashtbl.find circles (x,y) in
c.r <- radius;
drawing#misc#queue_draw ()
let create_circle x y r =
Hashtbl.add circles (x,y) {x;y;r};
drawing#misc#queue_draw ()
let delete_circle x y =
Hashtbl.remove circles (x,y);
begin
match !selected_circle with
| Some c when c.x=x && c.y=y -> selected_circle:=None
| _ -> ()
end;
drawing#misc#queue_draw ()
let create_diameter_window () =
match !selected_circle with
| Some c ->
let old_r = c.r in
let window = GWindow.window ~title:"" ~modal:true ~width:250 ~height:100 () in
let vbox = GPack.vbox ~border_width:10 ~packing:window#add () in
let label = GMisc.label ~text:(Printf.sprintf "Adjust diameter of circle at (%.0f, %.0f)" c.x c.y) ~packing:vbox#add () in
let slider_adj = GData.adjustment ~lower:0. ~value:c.r ~upper:200. () in
let slider = GRange.scale `HORIZONTAL ~adjustment:slider_adj ~packing:(vbox#pack ~padding:4 ~expand:true) () in
let _ = window#connect#destroy ~callback:(fun () ->
undo_list := { undo= (fun () -> change_diameter c.x c.y old_r);
redo= (fun () -> change_diameter c.x c.y slider_adj#value)}::!undo_list;
redo_list := [];
check_undo ()) in
let _ = slider#connect#value_changed ~callback:(fun () -> change_diameter c.x c.y slider_adj#value;
drawing#misc#queue_draw()) in
window#show ();
ignore (label, slider)
| None -> ()
let _ = drawing#event#add [`BUTTON_PRESS]
let _ = drawing#event#connect#button_press ~callback:(fun ev ->
let mouse_x = GdkEvent.Button.x ev in
let mouse_y = GdkEvent.Button.y ev in
let button = GdkEvent.Button.button ev in
if button = 3 then
match !selected_circle with
| Some c when Float.sqrt((c.x-.mouse_x)**2.+.(c.y-.mouse_y)**2.) < c.r
->
let menu = GMenu.menu () in
let menuitem = GMenu.menu_item ~label:"Adjust diameter..." ~use_mnemonic:true ~packing:menu#append () in
ignore @@ menuitem#connect#activate ~callback:(fun () -> create_diameter_window ());
menu#popup ~button:0 ~time:0l
| _ -> ()
else
begin
calc_selection mouse_x mouse_y;
if !selected_circle = None then
begin
create_circle mouse_x mouse_y 20.;
undo_list := { undo = (fun () -> delete_circle mouse_x mouse_y);
redo = (fun () -> create_circle mouse_x mouse_y 20.) } :: ! undo_list;
redo_list := [];
check_undo ()
end
else
drawing#misc#queue_draw()
end;
true)
let () = ignore (button_undo, button_redo)
let () =
ignore @@ w#connect#destroy ~callback: GMain.quit;
w#show ();
GMain.main ()