(* ===================================================== *)
(*      Apprentissage de la programmation avec OCaml     *)
(*      Catherine Dubois & Valérie Ménissier-Morain      *)
(*                Éditions Hermès Sciences               *)
(*                        Mars 2004                      *)
(* ===================================================== *)
(* Fichier MLSRC/CC/CC_temps/affichage_jeu.ml            *)
(* ===================================================== *)

open Outils;;
open Representation;;
open Circuits;;
open Graphics;;
open Affichage_base;;

let espace_pièces = 8;;

let largeur_zone_de_jeu () =
  état.zone_de_jeu.colonnes*taille_pièce+
  (état.zone_de_jeu.colonnes-1)*espace_pièces
and hauteur_zone_de_jeu () =
  état.zone_de_jeu.lignes*taille_pièce+
  (état.zone_de_jeu.lignes-1)*espace_pièces;;

let largeur_caractère = 11
and hauteur_caractère = 18;;

let largeur_cartouche =
  largeur_caractère*(String.length " PAS DE CIRCUIT ");;

let espace_horizontal () =
  (size_x()-largeur_cartouche-(largeur_zone_de_jeu ()))/2;;

let espace_vertical () = (size_y()-(hauteur_zone_de_jeu ()))/2;;

let dessiner_zone_de_jeu () =
  let x = ref (espace_horizontal ())
  and y = ref (espace_vertical ()) in
  (* où placer le coin en bas à gauche de la zone de jeu *)
   for c = état.zone_de_jeu.débutc to état.zone_de_jeu.finc do
     for
 l = état.zone_de_jeu.débutl to état.zone_de_jeu.finl do
       begin match
 état.zone_de_jeu.tableau.(c).(l) with
       | Vide -> ()
       | Occupée couleur -> dessiner_pièce (!x, !y) couleur
       end;
       y := !y+taille_pièce+espace_pièces;
    done;
    y := espace_vertical ();
    x := !x+taille_pièce+espace_pièces
  done;;

let écrire_à_gauche_cartouche haut s =
  set_font "-*-courier-medium-r-*-*-18-*";
  let (w, h) = text_size s in
  moveto (size_x()-largeur_cartouche+largeur_caractère) (haut-h/2);
  set_color foreground;
  draw_string s;;

let écrire_à_droite_cartouche haut s =
  set_font "-*-courier-medium-r-*-*-18-*";
  let (w, h) = text_size s in
  let
 posx = size_x()-largeur_caractère-w
  and posy = haut-h/2 in
  set_color background;
  fill_rect posx posy w h;
  moveto posx posy;
  set_color foreground;
  draw_string s;;

let dessiner_cartouche () =
  set_color foreground;
  moveto (size_x() - largeur_cartouche) 0;
  rlineto 0 (size_y());
  écrire_à_gauche_cartouche (size_y()/3) "NIVEAU";
  écrire_à_droite_cartouche (size_y()/3) (string_of_int état.niveau);;

let coordonnées_centre_pièce (c,l) =
  ((espace_horizontal ())+(c-1)*taille_pièce+
   (c-1)*espace_pièces+taille_pièce/2,
   (espace_vertical ())+(l-1)*taille_pièce+
   (l-1)*espace_pièces+taille_pièce/2);;

let dessiner_circuit circuit =
  match List.map coordonnées_centre_pièce circuit with
  | [] -> ()
  | (x,y)::circuit' ->
    set_line_width 3; moveto x y;
    List.iter (function (x,y) -> lineto x y) circuit';
    set_line_width 1;;

let coordonnées_pièce (c,l) =
  (espace_horizontal ()+(c-1)*taille_pièce+(c-1)*espace_pièces,
   espace_vertical ()+(l-1)*taille_pièce+(l-1)*espace_pièces);;

let effacer_pièce_sélectionnée (c,l) =
  let (x, y) = coordonnées_pièce (c,l) in
  set_color background;
  fill_rect x y taille_pièce taille_pièce;
  dessiner_sélection (x, y);
  set_color foreground;;

let effacer_circuit circuit =
  set_color background;
  dessiner_circuit circuit;
  set_color foreground;;

let écrire_pas_de_circuit () =
  let gauche_cartouche =
    size_x() - largeur_cartouche+largeur_caractère in
  let
 affiche couleur =
    set_color couleur;
    set_font "-*-courier-bold-r-*-*-18-*";
    moveto gauche_cartouche (2*size_y()/3-hauteur_caractère/2);
    draw_string "PAS DE CIRCUIT" in
      affiche foreground; sleep 0.1;
      affiche background; sleep 0.1;
      affiche foreground; sleep 0.1;
      affiche background; sleep 0.1;
      affiche foreground; sleep 0.1;
      affiche background;;

Ce document a été traduit de LATEX par HEVEA.