[lightsoff/wip/gtkview] More boardview refactorings



commit 37cbbf67dde0a60570d87a70ec8e36241c7fc907
Author: Robert Roth <robert roth off gmail com>
Date:   Sun Jul 15 01:12:44 2018 +0300

    More boardview refactorings

 src/board-view-clutter.vala | 63 +++++++++++++++++++++--------------------
 src/board-view-gtk.vala     | 68 +++++++++++++++++++++++++--------------------
 src/board-view.vala         |  2 ++
 3 files changed, 71 insertions(+), 62 deletions(-)
---
diff --git a/src/board-view-clutter.vala b/src/board-view-clutter.vala
index 7756615..89a780c 100644
--- a/src/board-view-clutter.vala
+++ b/src/board-view-clutter.vala
@@ -74,7 +74,6 @@ private class Light : Clutter.Group
 public class BoardViewClutter : Clutter.Group, BoardView
 {
 
-    private new const int size = 5;
     private PuzzleGenerator puzzle_generator;
 
     public bool playable = true;
@@ -159,15 +158,6 @@ public class BoardViewClutter : Clutter.Group, BoardView
                                "opacity", 0);
     }
 
-    private void find_light (GLib.Object light, out int x, out int y)
-    {
-        x = y = 0;
-        for (x = 0; x < size; x++)
-            for (y = 0; y < size; y++)
-                if (lights[x, y] == light)
-                    return;
-    }
-
     private void light_button_press_cb (Clutter.TapAction tap, Clutter.Actor actor)
     {
         int x, y;
@@ -175,13 +165,6 @@ public class BoardViewClutter : Clutter.Group, BoardView
         move_to (x, y);
     }
 
-    public void move_to (int x, int y)
-    {
-        toggle_light (x, y);
-        _moves += 1;
-        light_toggled ();
-    }
-
     // Toggle a light and those in each cardinal direction around it.
     private void toggle_light (int x, int y, bool animate = true)
     {
@@ -205,25 +188,10 @@ public class BoardViewClutter : Clutter.Group, BoardView
 
         lights[(int) x, (int) y].toggle (timeline);
 
-        if (is_completed ()) {
-            game_won ();
-        }
-
         if (animate)
             timeline.start ();
     }
 
-    private bool is_completed ()
-    {
-        var cleared = true;
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                if (lights[x, y].is_lit)
-                    cleared = false;
-
-        return cleared;
-    }
-
     // 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
@@ -252,4 +220,35 @@ public class BoardViewClutter : Clutter.Group, BoardView
                 if (sol[x, y])
                     toggle_light (x, y, false);
     }
+
+    private void find_light (GLib.Object light, out int x, out int y)
+    {
+        x = y = 0;
+        for (x = 0; x < size; x++)
+            for (y = 0; y < size; y++)
+                if (lights[x, y] == light)
+                    return;
+    }
+
+    private bool is_completed ()
+    {
+        var cleared = true;
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
+                if (lights[x, y].is_lit)
+                    cleared = false;
+
+        return cleared;
+    }
+
+    public void move_to (int x, int y)
+    {
+        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 8e8795f..359015d 100644
--- a/src/board-view-gtk.vala
+++ b/src/board-view-gtk.vala
@@ -10,7 +10,6 @@
 
 public class BoardViewGtk : Gtk.Grid, BoardView
 {
-    private new const int size = 5;
     private PuzzleGenerator puzzle_generator;
     private Gtk.ToggleButton[,] lights;
 
@@ -63,37 +62,11 @@ public class BoardViewGtk : Gtk.Grid, BoardView
     {
     }
 
-    private void find_light (GLib.Object light, out int x, out int y)
-    {
-        x = y = 0;
-        for (x = 0; x < size; x++)
-            for (y = 0; y < size; y++)
-                if (lights[x, y] == light)
-                    return;
-    }
-
-    private bool is_completed ()
-    {
-        var cleared = true;
-        for (var x = 0; x < size; x++)
-            for (var y = 0; y < size; y++)
-                if (lights[x, y].active)
-                    cleared = false;
-
-        return cleared;
-    }
-
     public void light_toggled_cb (Gtk.ToggleButton source)
     {
         int xl, yl;
         find_light (source, out xl, out yl);
-
-        toggle_light (xl, yl, true);
-        _moves += 1;
-        light_toggled ();
-        if (is_completed ()) {
-            game_won ();
-        }
+        move_to (xl, yl);
     }
     // Pseudorandomly generates and sets the state of each light based on
     // a level number; hopefully this is stable between machines, but that
@@ -101,7 +74,7 @@ public class BoardViewGtk : Gtk.Grid, BoardView
     // symmetry for some levels.
 
      // Toggle a light and those in each cardinal direction around it.
-    private void toggle_light (int x, int y, bool clicked = false)
+    private void toggle_light (int x, int y, bool clicked = true)
     {
         for (var xi = 0; xi < size; xi++)
             for (var yi = 0; yi < size; yi++)
@@ -126,6 +99,10 @@ public class BoardViewGtk : Gtk.Grid, BoardView
                 lights[xi, yi].toggled.connect (light_toggled_cb);
     }
 
+    // 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
+    // symmetry for some levels.
     public void load_level (int level)
     {
         /* We *must* not have level < 1, as the following assumes a nonzero, nonnegative number */
@@ -152,6 +129,37 @@ public class BoardViewGtk : Gtk.Grid, BoardView
         for (var x = 0; x < size; x++)
             for (var y = 0; y < size; y++)
                 if (sol[x, y])
-                    toggle_light (x, y);
+                    toggle_light (x, y, false);
     }
+
+    private void find_light (GLib.Object light, out int x, out int y)
+    {
+        x = y = 0;
+        for (x = 0; x < size; x++)
+            for (y = 0; y < size; y++)
+                if (lights[x, y] == light)
+                    return;
+    }
+
+    private bool is_completed ()
+    {
+        var cleared = true;
+        for (var x = 0; x < size; x++)
+            for (var y = 0; y < size; y++)
+                if (lights[x, y].active)
+                    cleared = false;
+
+        return cleared;
+    }
+
+    public void move_to (int x, int y)
+    {
+        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 90ccb5a..dac5de5 100644
--- a/src/board-view.vala
+++ b/src/board-view.vala
@@ -9,6 +9,8 @@
  */
 public interface BoardView: GLib.Object {
 
+    protected new const int size = 5;
+
     public signal void game_won ();
     public signal void light_toggled ();
 }


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