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



commit a07b8a7af5ae93577e3a500f036349639bec0589
Author: Robert Roth <robert roth off gmail com>
Date:   Tue Jul 17 01:57:58 2018 +0300

    Even more game-view refactoring

 src/game-view-clutter.vala | 49 +++++++++++++---------------------------------
 src/game-view-gtk.vala     | 28 ++++++++++++--------------
 src/game-view.vala         | 29 +++++++++++++++++++++++++--
 3 files changed, 53 insertions(+), 53 deletions(-)
---
diff --git a/src/game-view-clutter.vala b/src/game-view-clutter.vala
index 13fe171..997ef58 100644
--- a/src/game-view-clutter.vala
+++ b/src/game-view-clutter.vala
@@ -80,7 +80,7 @@ public class ClutterGameView : Clutter.Group, GameView
         add_child (board_group);
 
         current_level = level;
-        board_view = create_board_view (current_level);
+        board_view = create_board_view (current_level) as BoardViewClutter;
         board_view.playable = true;
         board_group.add_child (board_view);
 
@@ -94,11 +94,11 @@ public class ClutterGameView : Clutter.Group, GameView
         add_child (key_cursor_view);
     }
 
-    private BoardViewClutter create_board_view (int level)
+    public BoardView create_board_view (int level)
     {
         var view = new BoardViewClutter (off_texture, on_texture);
         view.load_level (level);
-        view.game_won.connect (game_won_cb);
+        view.game_won.connect (() => game_won_cb());
         view.light_toggled.connect (light_toggled_cb);
         view.playable = false;
         return view;
@@ -118,38 +118,6 @@ public class ClutterGameView : Clutter.Group, GameView
         board_view = new_view;
     }
 
-    // The player won the game; create a new board, update the level count,
-    // and transition between the two boards in a random direction.
-    private void game_won_cb ()
-    {
-        if (timeline != null && timeline.is_playing ())
-            return;
-
-        replace_board (board_view, create_board_view (++current_level), GameView.ReplaceStyle.SLIDE_NEXT);
-    }
-
-    // The player asked to swap to a different level without completing
-    // the one in progress; this can occur either by clicking an arrow
-    // or by requesting a new game from the menu. Animate the new board
-    // in, depthwise, in the direction indicated by 'context'.
-    public void swap_board (int direction)
-    {
-        if (timeline != null && timeline.is_playing ())
-            return;
-
-        current_level += direction;
-        if (current_level <= 0)
-        {
-            current_level = 1;
-            return;
-        }
-
-        replace_board (board_view, create_board_view (current_level),
-                       direction == 1 ? GameView.ReplaceStyle.SLIDE_FORWARD 
-                                      : GameView.ReplaceStyle.SLIDE_BACKWARD);
-
-    }
-
     public void replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, bool 
fast = true)
     {
         timeline = new Clutter.Timeline (fast ? 500 : 1500);
@@ -281,4 +249,15 @@ public class ClutterGameView : Clutter.Group, GameView
     {
         return board_view;
     }
+
+    public int next_level (int direction)
+    {
+        current_level += direction;
+        return current_level;
+    }
+
+    public bool is_transitioning ()
+    {
+        return timeline != null && timeline.is_playing ();
+    }
 }
diff --git a/src/game-view-gtk.vala b/src/game-view-gtk.vala
index 37c6c0e..b1cdbc7 100644
--- a/src/game-view-gtk.vala
+++ b/src/game-view-gtk.vala
@@ -3,13 +3,6 @@ public class GtkGameView : Gtk.Stack, GameView {
     private BoardViewGtk board_view;
     private int current_level;
     private GLib.Queue<ulong> handlers = new GLib.Queue<ulong>();
-    public void swap_board (int direction)
-    {
-        current_level += direction;
-        replace_board (board_view, create_board_view (current_level),
-                       direction == 1 ? GameView.ReplaceStyle.SLIDE_FORWARD 
-                                      : GameView.ReplaceStyle.SLIDE_BACKWARD);
-    }
 
     public void replace_board (BoardView old_board, BoardView new_board, GameView.ReplaceStyle style, bool 
fast = true)
     {
@@ -67,12 +60,12 @@ public class GtkGameView : Gtk.Stack, GameView {
                 /* Clear level */
         current_level = level;
 
-        board_view = create_board_view (current_level);
+        board_view = create_board_view (current_level) as BoardViewGtk;
         board_view.playable = true;
         add (board_view);
     }
 
-    private BoardViewGtk create_board_view (int level)
+    public BoardView create_board_view (int level)
     {
         var view = new BoardViewGtk ();
         view.load_level (level);
@@ -83,16 +76,19 @@ public class GtkGameView : Gtk.Stack, GameView {
         return view;
     }
 
-    // The player won the game; create a new board, update the level count,
-    // and transition between the two boards in a random direction.
-    private bool game_won_cb ()
+    public BoardView get_board_view ()
     {
-        replace_board (board_view, create_board_view (++current_level), GameView.ReplaceStyle.SLIDE_NEXT, 
false);
-        return false;
+        return board_view;
     }
 
-    public BoardView get_board_view ()
+    public int next_level (int direction) {
+        current_level += direction;
+        return current_level;
+    }
+
+    public bool is_transitioning ()
     {
-        return board_view;
+        return transition_running;
     }
+
 }
\ No newline at end of file
diff --git a/src/game-view.vala b/src/game-view.vala
index 379000c..4e7d945 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -16,8 +16,6 @@ public interface GameView : GLib.Object {
         SLIDE_NEXT // slide over
     }
 
-    public abstract void swap_board (int direction);
-
     public abstract void replace_board (BoardView board_biew, BoardView new_board_view, ReplaceStyle style, 
bool fast = true);
 
     public abstract bool hide_cursor ();
@@ -25,7 +23,34 @@ public interface GameView : GLib.Object {
     public abstract bool move_cursor (int x, int y);
     public abstract void reset_game ();
     public abstract BoardView get_board_view ();
+    public abstract bool is_transitioning ();
+    public abstract int next_level (int direction);
+    public abstract BoardView create_board_view (int level);
+
+    // The player asked to swap to a different level without completing
+    // the one in progress; this can occur either by clicking an arrow
+    // or by requesting a new game from the menu. Animate the new board
+    // in, depthwise, in the direction indicated by 'context'.
+    public void swap_board (int direction)
+    {
+        if (is_transitioning ())
+            return;
+
+        replace_board (get_board_view (), create_board_view (next_level (direction)),
+                       direction == 1 ? GameView.ReplaceStyle.SLIDE_FORWARD
+                                      : GameView.ReplaceStyle.SLIDE_BACKWARD);
+    }
 
+    // The player won the game; create a new board, update the level count,
+    // and transition between the two boards in a random direction.
+    public bool game_won_cb ()
+    {
+        if (is_transitioning ())
+            return false;
+
+        replace_board (get_board_view (), create_board_view (next_level (1)), 
GameView.ReplaceStyle.SLIDE_NEXT);
+        return false;
+    }
 
     public void light_toggled_cb ()
     {


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