[lightsoff/wip/gtkview] Refactoring the common parts in boardviews



commit 7728d2cc93ee26d8b40a28f163c6158fb16e6f16
Author: Robert Roth <robert roth off gmail com>
Date:   Sun Jul 15 00:49:30 2018 +0300

    Refactoring the common parts in boardviews

 src/board-view-clutter.vala | 22 +++++++++++-----------
 src/board-view-gtk.vala     |  9 ++-------
 src/board-view.vala         | 14 ++++++++++++++
 src/game-view.vala          |  9 +++++++++
 src/meson.build             |  1 +
 5 files changed, 37 insertions(+), 18 deletions(-)
---
diff --git a/src/board-view-clutter.vala b/src/board-view-clutter.vala
index ddf57d5..7756615 100644
--- a/src/board-view-clutter.vala
+++ b/src/board-view-clutter.vala
@@ -71,13 +71,11 @@ private class Light : Clutter.Group
     }
 }
 
-public class BoardViewClutter : Clutter.Group
+public class BoardViewClutter : Clutter.Group, BoardView
 {
+
     private new const int size = 5;
     private PuzzleGenerator puzzle_generator;
-    private Clutter.Actor off_texture;
-    private Clutter.Actor on_texture;
-    private Light[,] lights;
 
     public bool playable = true;
 
@@ -87,8 +85,9 @@ public class BoardViewClutter : Clutter.Group
         get { return _moves;}
     }
 
-    public signal void game_won ();
-    public signal void light_toggled ();
+    private Clutter.Actor off_texture;
+    private Clutter.Actor on_texture;
+    private Light[,] lights;
 
     public BoardViewClutter (Clutter.Actor off_texture, Clutter.Actor on_texture)
     {
@@ -160,7 +159,7 @@ public class BoardViewClutter : Clutter.Group
                                "opacity", 0);
     }
 
-    private void find_light (Light light, out int x, out int y)
+    private void find_light (GLib.Object light, out int x, out int y)
     {
         x = y = 0;
         for (x = 0; x < size; x++)
@@ -206,13 +205,15 @@ public class BoardViewClutter : Clutter.Group
 
         lights[(int) x, (int) y].toggle (timeline);
 
-        check_completed ();
+        if (is_completed ()) {
+            game_won ();
+        }
 
         if (animate)
             timeline.start ();
     }
 
-    private void check_completed ()
+    private bool is_completed ()
     {
         var cleared = true;
         for (var x = 0; x < size; x++)
@@ -220,8 +221,7 @@ public class BoardViewClutter : Clutter.Group
                 if (lights[x, y].is_lit)
                     cleared = false;
 
-        if (cleared)
-            game_won ();
+        return cleared;
     }
 
     // Pseudorandomly generates and sets the state of each light based on
diff --git a/src/board-view-gtk.vala b/src/board-view-gtk.vala
index 8fa1989..8e8795f 100644
--- a/src/board-view-gtk.vala
+++ b/src/board-view-gtk.vala
@@ -8,7 +8,7 @@
  * license.
  */
 
-public class BoardViewGtk : Gtk.Grid
+public class BoardViewGtk : Gtk.Grid, BoardView
 {
     private new const int size = 5;
     private PuzzleGenerator puzzle_generator;
@@ -22,9 +22,6 @@ public class BoardViewGtk : Gtk.Grid
         get { return _moves;}
     }
 
-    public signal void game_won ();
-    public signal void light_toggled ();
-
     public BoardViewGtk ()
     {
         get_style_context ().add_class ("grid");
@@ -66,7 +63,7 @@ public class BoardViewGtk : Gtk.Grid
     {
     }
 
-    private void find_light (Gtk.ToggleButton light, out int x, out int y)
+    private void find_light (GLib.Object light, out int x, out int y)
     {
         x = y = 0;
         for (x = 0; x < size; x++)
@@ -131,8 +128,6 @@ public class BoardViewGtk : Gtk.Grid
 
     public void load_level (int level)
     {
-        _moves = 0;
-        light_toggled ();
         /* We *must* not have level < 1, as the following assumes a nonzero, nonnegative number */
         if (level < 1)
             level = 1;
diff --git a/src/board-view.vala b/src/board-view.vala
new file mode 100644
index 0000000..90ccb5a
--- /dev/null
+++ b/src/board-view.vala
@@ -0,0 +1,14 @@
+/*
+ * Copyright (C) 2018 Robert Roth
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+public interface BoardView: GLib.Object {
+
+    public signal void game_won ();
+    public signal void light_toggled ();
+}
diff --git a/src/game-view.vala b/src/game-view.vala
index a5f057f..2bbd956 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -1,3 +1,12 @@
+/*
+ * Copyright (C) 2018 Robert Roth
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 2 of the License, or (at your option) any later
+ * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
 public interface GameView : GLib.Object {
     public abstract void swap_board (int direction);
     public abstract void hide_cursor ();
diff --git a/src/meson.build b/src/meson.build
index 6e69dd8..33ac90b 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,4 +1,5 @@
 lightsoff_vala_sources = [
+  'board-view.vala',
   'board-view-clutter.vala',
   'board-view-gtk.vala',
   'lightsoff.vala',


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