[iagno] Make final animation reliable.



commit 2692adcc8d4ab0efde4050c48de24d0586ccd5c9
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sun Feb 10 06:11:36 2019 +0100

    Make final animation reliable.
    
    For the final animation, until now,
    all tiles were launching a Timeout,
    causing animation to randomly fail.

 src/game-view.vala | 43 ++++++++++++++++++++++++++-----------------
 src/game.vala      |  7 +++----
 src/iagno.vala     |  1 +
 3 files changed, 30 insertions(+), 21 deletions(-)
---
diff --git a/src/game-view.vala b/src/game-view.vala
index 1cff608..fb8f405 100644
--- a/src/game-view.vala
+++ b/src/game-view.vala
@@ -123,6 +123,7 @@ private class GameView : Gtk.DrawingArea
             for (int x = 0; x < _game.size; x++)
                 for (int y = 0; y < _game.size; y++)
                     pixmaps[x, y] = get_pixmap (_game.get_owner (x, y));
+            _game.notify ["is-complete"].connect (game_is_complete_cb);
             _game.square_changed.connect (square_changed_cb);
 
             show_highlight = false;
@@ -384,23 +385,44 @@ private class GameView : Gtk.DrawingArea
         }
     }
 
-    private void square_changed_cb (int x, int y, Player replacement)
+    private void game_is_complete_cb ()
+    {
+        if (!game.is_complete)
+            return;
+
+        if (game.n_light_tiles == 0 || game.n_dark_tiles == 0)  // complete win
+            return;
+
+        /*
+         * Show the actual final positions of the pieces before flipping the board.
+         * Otherwise, it could seem like the final player placed the other's piece.
+         */
+        Timeout.add_seconds (2, () =>  {
+            flip_final_result_now = true;
+            for (int x = 0; x < game.size; x++)
+                for (int y = 0; y < game.size; y++)
+                    update_square (x, y);
+            return Source.REMOVE;
+        });
+    }
+
+    private void square_changed_cb (int x, int y, Player replacement, bool undoing)
     {
         if (replacement == Player.NONE)
         {
             highlight_x = x;
             highlight_y = y;
         }
-        update_square (x, y);
+        update_square (x, y, undoing);  // FIXME undoing is only for when undoing the counter turn
     }
 
-    private void update_square (int x, int y)
+    private void update_square (int x, int y, bool undoing = false)
         requires (game_is_set)
     {
         int pixmap = get_pixmap (game.get_owner (x, y));
 
         /* Show the result by laying the tiles with winning color first */
-        if (flip_final_result_now && game.is_complete)
+        if (flip_final_result_now && game.is_complete && !undoing)
         {
             int n = y * game.size + x;
             Player winning_color = Player.LIGHT;
@@ -429,19 +451,6 @@ private class GameView : Gtk.DrawingArea
         }
 
         set_square (x, y, pixmap);
-
-        if (game.is_complete && game.n_light_tiles > 0 && game.n_dark_tiles > 0)
-        {
-            /*
-             * Show the actual final positions of the pieces before flipping the board.
-             * Otherwise, it could seem like the final player placed the other's piece.
-             */
-            Timeout.add_seconds (2, () =>  {
-                flip_final_result_now = true;
-                update_square (x, y);
-                return Source.REMOVE;
-            });
-        }
     }
 
     private void set_square (int x, int y, int pixmap)
diff --git a/src/game.vala b/src/game.vala
index e759d3c..39f2339 100644
--- a/src/game.vala
+++ b/src/game.vala
@@ -41,13 +41,12 @@ private class Game : Object
 
     /* Indicate who's the next player who can move */
     [CCode (notify = false)] internal bool current_player_can_move { internal get; private set; default = 
true; }
-    // there's a race for the final "counter" turn, and looks like notifying here helps, not sure why // 
TODO fix the race
     [CCode (notify = true)] internal bool is_complete { internal get; private set; default = false; }
 
     /* Indicate that a player should move */
     internal signal void turn_ended ();
     /* Indicate a square has changed */
-    internal signal void square_changed (int x, int y, Player new_color);
+    internal signal void square_changed (int x, int y, Player new_color, bool undoing);
 
     /*\
     * * Number of tiles on the board
@@ -340,7 +339,7 @@ private class Game : Object
         history_index++;
         undo_stack[history_index] = x + y * size;
         tiles[x, y] = current_color;
-        square_changed (x, y, current_color);
+        square_changed (x, y, current_color, /* undoing */ false);
     }
 
     /*\
@@ -395,6 +394,6 @@ private class Game : Object
         var x = tile_number % size;
         var y = tile_number / size;
         tiles [x, y] = replacement_color;
-        square_changed (x, y, replacement_color);
+        square_changed (x, y, replacement_color, /* undoing */ true);
     }
 }
diff --git a/src/iagno.vala b/src/iagno.vala
index d4d07ff..ec3bad2 100644
--- a/src/iagno.vala
+++ b/src/iagno.vala
@@ -431,6 +431,7 @@ private class Iagno : Gtk.Application
             prepare_move ();
         else if (game.is_complete)
             game_complete (/* play sound */ true);
+            // view is updated by connecting to game.notify ["is-complete"]
         else
             pass ();
     }


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