(** {C Interface graphique pour le jeu {i same game}.} *) (** Unité de longueur. *) let gr_unit = 15 (** Incrément d'une unité [gr_unit]. *) let gr_incr r = r := !r + gr_unit (** {1 Position et taille du tableau de jeu.} *) (** Coordonnées coin inférieur gauche. *) let gb_x0, gb_y0 = gr_unit, gr_unit (** Largeur (en pixels). *) let gb_width = gr_unit * Glob.nb_col (** Hauteur (en pixels). *) let gb_height = gr_unit * Glob.nb_lig (** {1 Définition des boutons.} *) (** Structure descriptive d'un bouton. *) type bt_descr = { x0 : int; y0 : int; width : int; height : int; color : Graphics.color; label : string } (** Bouton "next": activé pour effacer une sélection ou jouer une nouvelle partie. *) let next_bt = { x0 = gb_x0 + gb_width + gr_unit; y0 = gb_y0; width = 2 * gr_unit; height = gr_unit; color = Graphics.rgb 115 115 115; label = "next>" } (** Bouton "quit": activé pour quitter le jeu. *) let quit_bt = { x0 = next_bt.x0; y0 = next_bt.y0 + gr_unit + (gr_unit / 2) ; width = next_bt.width; height = next_bt.height; color = Graphics.red; label = "quit!" } (** {1 Taille de la fenêtre graphique.} *) let gw_width = gr_unit + gb_width + gr_unit + next_bt.width + gr_unit let gw_height = gr_unit + gb_height + gr_unit (** {1 Interaction.} *) (** Vaut [true] si la position [(x,y)] est dans le rectangle [r_x r_y r_w r_h]. *) let is_in_rect (x,y) r_x r_y r_w r_h = ((r_x < x) && (x < r_x + r_w)) && ((r_y < y) && (y < r_y + r_h)) (** Vaut [true] si la position [(x,y)] est dans le tableau de jeu. *) let is_in_gb pos = is_in_rect pos gb_x0 gb_y0 gb_width gb_height (** Vaut [true] si la position [(x,y)] est dans le bouton "next". *) let is_in_next_bt pos = is_in_rect pos next_bt.x0 next_bt.y0 next_bt.width next_bt.height (** Vaut [true] si la position [(x,y)] est dans le bouton "quit". *) let is_in_quit_bt pos = is_in_rect pos quit_bt.x0 quit_bt.y0 quit_bt.width quit_bt.height (** Convertit la position [(x,y)] en coordonnées colonne/ligne du tableau de jeu [Glob.gb]. *) let gb_cell (x,y) = (x / gr_unit, y / gr_unit) (** Renvoie l'action liée à l'évènement souris correspondant à la position d'un "clic" (on a laissé des affichages sur la sortie standard pour debuggage). *) let click() = let s = Graphics.wait_next_event [Graphics.Button_down] in Printf.printf"Click: %d,%d " s.Graphics.mouse_x s.Graphics.mouse_y; flush stdout; let pos = s.Graphics.mouse_x, s.Graphics.mouse_y in if is_in_next_bt pos then (Printf.printf"Next ";flush stdout; Glob.Next) else if is_in_quit_bt pos then (Printf.printf"Quit ";flush stdout; Glob.Quit) else if is_in_gb pos then let icol, ilig = gb_cell pos in (Printf.printf"Sel(%d,%d) " icol ilig;flush stdout; Glob.Sel (icol, ilig)) else (Printf.printf"Null ";flush stdout; Glob.Null) (** {1 Dessin.} *) (** Table des couleurs des pièces non marquées. *) let ucolor = [|Graphics.white; Graphics.red; Graphics.green; Graphics.blue|] (** Table des couleurs des pièces marquées. *) let mcolor = [|Graphics.white; Graphics.rgb 0 255 255; Graphics.rgb 255 0 255; Graphics.rgb 255 255 0|] (** Dessine une pièce de couleur [c] dans un rectangle de coin inférieur gauche en [(x,y)]. *) let draw_cell c x y = if (c < 0) then Graphics.set_color mcolor.(-c) else Graphics.set_color ucolor.(c); let r = gr_unit/2 in Graphics.draw_circle (x+r) (y+r) r (** Dessine le contenu du tableau de jeu [Glob.gb]. *) let draw_gb gb = let x = ref gb_x0 and y = ref gb_y0 in for ilig = 1 to Glob.nb_lig do for icol = 1 to Glob.nb_col do draw_cell gb.(icol).(ilig) !x !y; gr_incr x done; x := gb_x0; gr_incr y done ;; (** Redessine le contenu du tableau de jeu [Glob.gb]. *) let redraw_gb gb = Graphics.set_color Graphics.background; Graphics.fill_rect gb_x0 gb_y0 gb_width gb_height; draw_gb gb (** Dessine le bouton décrit par [bt]. *) let draw_bt bt = Graphics.set_color bt.color; Graphics.fill_rect bt.x0 bt.y0 bt.width bt.height; Graphics.set_color Graphics.foreground; Graphics.draw_rect bt.x0 bt.y0 bt.width bt.height; let w, h = Graphics.text_size bt.label in let dx = (bt.width - w) / 2 in let dy = (bt.height - h) / 2 in Graphics.moveto (bt.x0 + dx) (bt.y0 + dy); Graphics.draw_string bt.label (** Dessine le bouton "next". *) let draw_next_bt() = draw_bt next_bt (** Dessine le bouton "quit". *) let draw_quit_bt() = draw_bt quit_bt