From 0fbd3eceffbf389a50c50982566cbe5e707db2bf Mon Sep 17 00:00:00 2001
From: =?utf8?q?Am=C3=A9lia=20Coutard-Sander?= <git@ameliathe1st.gay>
Date: Fri, 13 Dec 2024 02:13:59 +0100
Subject: [PATCH] Interface graphique plus pratique

---
 bin/main.ml | 59 +++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 41 insertions(+), 18 deletions(-)

diff --git a/bin/main.ml b/bin/main.ml
index b0e5404..a8060eb 100644
--- a/bin/main.ml
+++ b/bin/main.ml
@@ -13,7 +13,13 @@
  * with this program. If not, see <https://www.gnu.org/licenses/>.
  *)
 
-let render (type t) (m : (module Automata.Automaton with type t = t)) (board : t Automata.board) =
+type 't interface_state = { x: int; y: int; current: 't; running: bool }
+
+let render
+    (type t)
+    (m : (module Automata.Automaton with type t = t))
+    (board : t Automata.board)
+    (inter : t interface_state) =
         let module M = (val m) in
         for x = 0 to 63 do
           for y = 0 to 63 do
@@ -21,31 +27,48 @@ let render (type t) (m : (module Automata.Automaton with type t = t)) (board : t
               (let r, g, b = M.color (Automata.get x y board) in
                Graphics.rgb (int_of_char r) (int_of_char g) (int_of_char b));
             Graphics.fill_rect (x * 16) (y * 16) 16 16;
-            Graphics.set_color Graphics.black;
+            Graphics.set_color (Graphics.rgb 127 127 127);
             Graphics.draw_rect (x * 16) (y * 16) 16 16
           done
-        done
+        done;
+        Graphics.set_color
+          (let r, g, b = M.color inter.current in
+           Graphics.rgb (int_of_char r) (int_of_char g) (int_of_char b));
+        Graphics.fill_rect ((inter.x * 16) + 5) ((inter.y * 16) + 5) 6 6;
+        Graphics.set_color (Graphics.rgb 127 127 127);
+        Graphics.draw_rect ((inter.x * 16) + 5) ((inter.y * 16) + 5) 6 6
 
-let inputs (type t) (m : (module Automata.Automaton with type t = t)) (board : t Automata.board) =
+let inputs
+    (type t)
+    (m : (module Automata.Automaton with type t = t))
+    (board : t Automata.board)
+    (inter : t interface_state) =
         let module M = (val m) in
-        if Graphics.button_down ()
-        then (
-          let mx, my = Graphics.mouse_pos () in
-          let x = mx / 16
-          and y = my / 16 in
-          while Graphics.button_down () do
-            ()
-          done;
-          Automata.set x y (M.next (Automata.get x y board)) board)
+        if Graphics.key_pressed ()
+        then
+          match Graphics.read_key () with
+          | ' ' ->
+                  Automata.set inter.x inter.y inter.current board;
+                  inter
+          | 'a' -> { inter with current = M.prev inter.current }
+          | 'e' -> { inter with current = M.next inter.current }
+          | 'z' -> { inter with y = inter.y + 1 }
+          | 'q' -> { inter with x = inter.x - 1 }
+          | 's' -> { inter with y = inter.y - 1 }
+          | 'd' -> { inter with x = inter.x + 1 }
+          | '\t' -> { inter with running = true }
+          | _ -> inter
+        else inter
 
 let run (type t) (m : (module Automata.Automaton with type t = t)) =
-        let rec aux board =
-                render m board;
+        let module M = (val m) in
+        let rec aux board inter =
+                render m board inter;
                 Graphics.synchronize ();
-                inputs m board;
-                aux (if Graphics.key_pressed () && Graphics.read_key () = '\t' then Automata.update m board else board)
+                let inter = inputs m board inter in
+                aux (if inter.running then Automata.update m board else board) { inter with running = false }
         in
-        aux (Automata.initial m)
+        aux (Automata.initial m) { x = 0; y = 0; current = M.default; running = false }
 
 let () =
         Graphics.open_graph "";
-- 
2.46.0