[lightsoff/wip/gtkview] Even more board-view refactoring



commit ce025707bae32efc0ac06a9ca0f03aa8a194eb70
Author: Robert Roth <robert roth off gmail com>
Date:   Tue Jul 17 01:28:44 2018 +0300

    Even more board-view refactoring

 src/board-view-clutter.vala | 25 ++++++-------------------
 src/board-view-gtk.vala     | 20 ++++----------------
 src/board-view.vala         | 33 +++++++++++++++++++++++++++++++--
 3 files changed, 41 insertions(+), 37 deletions(-)
---
diff --git a/src/board-view-clutter.vala b/src/board-view-clutter.vala
index 788d65e..ec97086 100644
--- a/src/board-view-clutter.vala
+++ b/src/board-view-clutter.vala
@@ -144,9 +144,7 @@ public class BoardViewClutter : Clutter.Group, BoardView
 
     private void light_button_press_cb (Clutter.TapAction tap, Clutter.Actor actor)
     {
-        int x, y;
-        find_light ((Light) actor, out x, out y);
-        move_to (x, y);
+        handle_toggle (actor);
     }
 
     // Toggle a light and those in each cardinal direction around it.
@@ -189,30 +187,19 @@ public class BoardViewClutter : Clutter.Group, BoardView
                 lights[x, y].is_lit = false;
     }
 
-
-    public GLib.Object get_light_at (int x, int y)
+    public bool is_light_active (int x, int y)
     {
-        return lights[x, y];
+        return lights[x, y].is_lit;
     }
 
-    private bool is_completed ()
+    public GLib.Object get_light_at (int x, int y)
     {
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                if (lights[x, y].is_lit)
-                    return false;
-
-        return true;
+        return lights[x, y];
     }
 
-    public void move_to (int x, int y)
+    public void increase_moves ()
     {
-        toggle_light (x, y);
         _moves += 1;
-        light_toggled ();
-        if (is_completed ()) {
-            game_won ();
-        }
     }
 
 }
diff --git a/src/board-view-gtk.vala b/src/board-view-gtk.vala
index d741f0c..fa73c06 100644
--- a/src/board-view-gtk.vala
+++ b/src/board-view-gtk.vala
@@ -51,9 +51,7 @@ public class BoardViewGtk : Gtk.Grid, BoardView
 
     public void light_toggled_cb (Gtk.ToggleButton source)
     {
-        int xl, yl;
-        find_light (source, out xl, out yl);
-        move_to (xl, yl);
+        handle_toggle (source);
     }
     // Pseudorandomly generates and sets the state of each light based on
     // a level number; hopefully this is stable between machines, but that
@@ -96,14 +94,9 @@ public class BoardViewGtk : Gtk.Grid, BoardView
         return puzzle_generator;
     }
 
-    private bool is_completed ()
+    public bool is_light_active (int x, int y)
     {
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                if (lights[x, y].active)
-                    return false;
-
-        return true;
+        return lights[x, y].active;
     }
 
     public GLib.Object get_light_at (int x, int y)
@@ -111,14 +104,9 @@ public class BoardViewGtk : Gtk.Grid, BoardView
         return lights[x, y];
     }
 
-    public void move_to (int x, int y)
+    public void increase_moves ()
     {
-        toggle_light (x, y);
         _moves += 1;
-        light_toggled ();
-        if (is_completed ()) {
-            game_won ();
-        }
     }
 
 }
diff --git a/src/board-view.vala b/src/board-view.vala
index ed62510..cc85db4 100644
--- a/src/board-view.vala
+++ b/src/board-view.vala
@@ -15,9 +15,14 @@ public interface BoardView: GLib.Object {
     public abstract PuzzleGenerator get_puzzle_generator ();
     public abstract void clear_level ();
     public abstract void toggle_light (int x, int y, bool user_initiated = true);
+    public abstract void increase_moves ();
+    public abstract bool is_light_active (int x, int y);
 
     public abstract GLib.Object get_light_at (int x, int y);
 
+    public signal void game_won ();
+    public signal void light_toggled ();
+
         // Pseudorandomly generates and sets the state of each light based on
     // a level number; hopefully this is stable between machines, but that
     // depends on GLib's PRNG stability. Also, provides some semblance of
@@ -43,6 +48,13 @@ public interface BoardView: GLib.Object {
                     toggle_light (x, y, false);
     }
 
+    public void handle_toggle (GLib.Object light)
+    {
+        int x, y;
+        find_light (light, out x, out y);
+        move_to (x, y);
+    }
+
     public void find_light (GLib.Object light, out int x, out int y)
     {
         x = y = 0;
@@ -52,6 +64,23 @@ public interface BoardView: GLib.Object {
                     return;
     }
 
-    public signal void game_won ();
-    public signal void light_toggled ();
+    public void move_to (int x, int y)
+    {
+        toggle_light (x, y);
+        increase_moves ();
+        light_toggled ();
+        if (is_completed ()) {
+            game_won ();
+        }
+    }
+
+    public bool is_completed ()
+    {
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
+                if (is_light_active (x, y))
+                    return false;
+
+        return true;
+    }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]