-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphics_plus.ml
154 lines (125 loc) · 4.55 KB
/
graphics_plus.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
143
144
145
146
147
148
149
150
151
152
153
154
(* ==================================================
* GRAPHICS PLUS
* ==================================================
* Super-ensemble de la bibliothèque Graphics d'Ocaml
* permettant de gérer des boutons simples, un menu de
* boutons et l'affichage d'images bitmap (.bmp).
* Les boutons créer peuvent être stylisés et apparaître
* comme actifs ou inactifs pour le programme.
* -------------------------------------------------- *)
open Graphics;;
module type STYLE = sig
val blue : color;;
val red : color;;
val green : color;;
val yellow : color;;
val button_background : color;;
val button_textcolor : color;;
val button_inactive_textcolor : color;;
val button_bordercolor : color;;
val button_hovercolor : color;;
val default_width_menu_buttons : color;;
val default_height_menu_buttons : color;;
end
module MakeStyle (Style : STYLE) = struct
(* _________________________________________
AFFICHAGE GLOBAL
_________________________________________ *)
let set_blackscreen () =
auto_synchronize false;
set_color black;
let size_X = size_x () and size_Y = size_y () in
fill_rect 0 0 size_X size_Y;
synchronize ();;
(* _________________________________________
BOUTONS
_________________________________________ *)
(* ----------- Types ----------- *)
type button = {
coord : (int * int);
size : (int * int);
text : string;
action : unit -> unit;
mutable active : bool
};;
type menu = button list;;
(* ----------- Fonctions ----------- *)
let create_menu_button c s a : button =
{coord = c;
size = (Style.default_width_menu_buttons, Style.default_height_menu_buttons);
text = s;
action = a;
active = true};;
let coord_in_surface x y pos size : bool =
let (blx, bly) = pos and (l, h) = size in
(x > blx && x < blx+l && y > bly && y < bly+h);;
let coord_in_button x y button : bool =
coord_in_surface x y button.coord button.size;;
let draw_button_primitive background foreground button =
let (blx, bly) = button.coord and (l, h) = button.size in
set_color background;
fill_rect blx bly l h;
set_color Style.button_bordercolor;
draw_rect blx bly l h;
set_color foreground;
let (string_x, string_y) = text_size button.text in
moveto (blx+(l/2)-(string_x/2)) (bly+(h/2)-(string_y/2));
draw_string button.text;
synchronize ();;
let top_of button : (int * int) =
let (px, py) = button.coord in
let (l, h) = button.size in
(px, py+h);;
let draw_button button =
if (button.active) then
draw_button_primitive Style.button_background Style.button_textcolor button
else
draw_button_primitive Style.button_background Style.button_inactive_textcolor button;;
let hover button : unit =
if (button.active) then
draw_button_primitive Style.button_hovercolor Style.button_textcolor button
else
draw_button_primitive Style.button_hovercolor Style.button_inactive_textcolor button;;
let unhover = draw_button;;
let check_hover x y buttons : unit =
List.iter (fun b -> if coord_in_button x y b then hover b else unhover b) buttons;;
let rec check_buttons x y buttons : unit =
List.iter (fun b -> if coord_in_button x y b then b.action ()) buttons;;
let disable_button button : unit = button.active <- false;;
let enable_button button : unit = button.active <- true;;
(* _________________________________________
MENU
_________________________________________ *)
let draw_menu menu =
List.iter (fun b -> if b.active then draw_button b else draw_button b) menu;;
let disable_menu menu = List.iter disable_button menu;;
let enable_menu menu = List.iter enable_button menu;;
(* _________________________________________
IMAGES
_________________________________________ *)
let green_to_exclude : int = 0x00FF21;;
let make_picture filename (w, h) : image =
let m = Array.make_matrix h w transp in
let channel = open_in_bin filename in
try (
seek_in channel 54;
for i = 0 to h-1 do
for j = 0 to w-1 do
let b = input_byte channel in
let g = input_byte channel in
let r = input_byte channel in
let pixel = rgb r g b in
(m.(h-1-i).(j) <- (if pixel = green_to_exclude then transp else pixel))
done;
if w <> h then let _ = input_byte channel in ();
done;
close_in channel;
make_image m)
with End_of_file ->
close_in channel;
make_image m;;
let draw_picture filename (imageH, imageW) (screen_x, screen_y) =
let logo = make_picture filename (imageH, imageW) in
draw_image logo screen_x screen_y;
synchronize ();;
end