[gnome-taquin] Add moves count.



commit 8954ad147b9e33199950b33f9a5137745e269aaf
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Mon Jan 21 16:35:07 2019 +0100

    Add moves count.
    
    As planned, display number of moves done.

 data/game-headerbar.ui  | 14 +++-----------
 src/game-headerbar.vala |  8 ++++++++
 src/game-window.vala    |  7 +++++++
 src/taquin-game.vala    | 46 ++++++++++++++++++++++++++++++++++------------
 src/taquin-main.vala    |  6 +++---
 src/taquin-view.vala    | 10 ++++++++--
 6 files changed, 63 insertions(+), 28 deletions(-)
---
diff --git a/data/game-headerbar.ui b/data/game-headerbar.ui
index 23a996b..c78e126 100644
--- a/data/game-headerbar.ui
+++ b/data/game-headerbar.ui
@@ -39,9 +39,8 @@
         <property name="valign">center</property>
         <property name="focus-on-click">False</property>
         <property name="menu-model">history-menu</property>
-        <style>
-          <class name="image-button"/>
-        </style>
+        <property name="label">0</property>
+        <property name="sensitive">False</property>
         <child internal-child="accessible">
           <object class="AtkObject">
             <!-- Translators: accessible name of the history menubutton -->
@@ -50,13 +49,6 @@
             <property name="AtkObject::accessible-description" translatable="yes">Game history 
menu</property>
           </object>
         </child>
-        <child>
-          <object class="GtkImage">
-            <property name="visible">True</property>
-            <property name="icon-name">document-open-recent-symbolic</property>
-            <property name="icon-size">1</property>
-          </object>
-        </child>
       </object>
       <packing>
         <property name="pack-type">end</property>
@@ -91,7 +83,7 @@
         <property name="label" translatable="yes">_New Game</property>
         <property name="use-underline">True</property>
         <property name="action-name">ui.new-game</property>
-        <!-- Translators: during a game, tooltip text of the Start Over button -->
+        <!-- Translators: during a game, tooltip text of the New Game button -->
         <property name="tooltip-text" translatable="yes">Start a new game</property>
       </object>
     </child>
diff --git a/src/game-headerbar.vala b/src/game-headerbar.vala
index 8eea029..da9c110 100644
--- a/src/game-headerbar.vala
+++ b/src/game-headerbar.vala
@@ -32,6 +32,8 @@ private class GameHeaderBar : BaseHeaderBar
 
     construct
     {
+        ((Label) new_game_button.get_child ()).set_ellipsize (Pango.EllipsizeMode.END); // can happen on 
small screen with big moves count
+
         init_modes ();
 
         if (window_name != "")
@@ -184,6 +186,12 @@ private class GameHeaderBar : BaseHeaderBar
         return new_game_button.is_focus;
     }
 
+    internal void set_moves_count (ref uint moves_count)
+    {
+        history_button.set_label (moves_count.to_string ());
+        history_button.set_sensitive (moves_count != 0);
+    }
+
     /*\
     * * hamburger menu
     \*/
diff --git a/src/game-window.vala b/src/game-window.vala
index 3487480..7f269c8 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -135,6 +135,11 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         game_view.show_game_content (/* grab focus */ true);
     }
 
+    public void set_moves_count (uint moves_count)
+    {
+        headerbar.set_moves_count (ref moves_count);
+    }
+
     public void set_subtitle (string? subtitle)
     {
         if (subtitle != null)
@@ -270,6 +275,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
             return;
 
         game_finished = false;
+        hide_notification ();
 
         if (headerbar.new_game_button_is_focus ())
             game_view.show_game_content (/* grab focus */ true);
@@ -287,6 +293,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
             return;
 
         game_finished = false;
+        hide_notification ();
 
         if (headerbar.new_game_button_is_focus ())
             game_view.show_game_content (/* grab focus */ true);
diff --git a/src/taquin-game.vala b/src/taquin-game.vala
index 6888098..4351c99 100644
--- a/src/taquin-game.vala
+++ b/src/taquin-game.vala
@@ -47,6 +47,7 @@ public class Game : Object
     /* undoing */
     private UndoItem? state = null;
     private UndoItem? previous_state = null;
+    private uint moves_count = 0;
 
     /* position of the empty tile, if any */
     private int x_gap = 0;
@@ -54,7 +55,7 @@ public class Game : Object
 
     /* signals */
     public signal void complete ();
-    public signal void move (bool x_axis, int number, int x_gap, int y_gap, bool restarting);
+    public signal void move (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation);
     public signal void empty_tile ();
     public signal void cannot_move (int x, int y);
     public signal void cannot_undo_more ();
@@ -157,11 +158,18 @@ public class Game : Object
         }
 
         /* we do the move before notifying */
+        bool was_complete = check_complete ();
+
         var move_x_axis = x != x_gap;
         var move_number = move_x_axis ? x_gap - x : y_gap - y;
 
-        if (!undoing)
+        if (undoing)
+            moves_count--;
+        else
+        {
             add_move (/* move_x_axis, move_number,*/ x_gap, y_gap);
+            moves_count++;
+        }
 
         if (move_x_axis)
         {
@@ -179,9 +187,9 @@ public class Game : Object
         }
         tiles[x_gap, y_gap] = -1;
 
-        move (move_x_axis, move_number, x_gap, y_gap, restarting);
-        if (!undoing)
-            check_complete ();
+        move (move_x_axis, move_number, x_gap, y_gap, moves_count, restarting || (was_complete && undoing));
+        if (check_complete ())
+            complete ();
     }
 
     private void sixteen_move (int x, int y, bool undoing = false, bool restarting = false)
@@ -203,6 +211,8 @@ public class Game : Object
             return;
 
         /* we do the move before notifying */
+        bool was_complete = check_complete ();
+
         var new_coord = 0;
         if (move_x_axis)
         {
@@ -242,19 +252,31 @@ public class Game : Object
                 new_coord = 0;
             }
         }
-        if (!undoing)
+
+        if (undoing)
+            moves_count--;
+        else
+        {
             add_move (move_x_axis ? (new_coord == 0 ? -1 : size) : x, move_x_axis ? y : (new_coord == 0 ? -1 
: size));
-        move (move_x_axis, new_coord == 0 ? size - 1 : 1 - size, move_x_axis ? new_coord : x, move_x_axis ? 
y : new_coord, restarting);
-        if (!undoing)
-            check_complete ();
+            moves_count++;
+        }
+
+        move (move_x_axis,
+              new_coord == 0 ? size - 1 : 1 - size,
+              move_x_axis ? new_coord : x,
+              move_x_axis ? y : new_coord,
+              moves_count,
+              restarting || (was_complete && undoing));
+        if (check_complete ())
+            complete ();
     }
 
-    private void check_complete ()
+    private bool check_complete ()
     {
         for (var i = 1; i < size * size; i++)
             if (i != tiles[i % size, i / size])
-                return;
-        complete ();
+                return false;
+        return true;
     }
 
     /*\
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index c673de6..cad1edd 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -240,6 +240,7 @@ private class Taquin : Gtk.Application, BaseApplication
         int size = settings.get_int ("size");
         game = new Game (type, size);
         view.game = (!) game;
+        window.set_moves_count (0);
 
         string filename = "";
         var dirlist = theme_dirlist.copy ();
@@ -297,8 +298,9 @@ private class Taquin : Gtk.Application, BaseApplication
     * * Signals from game
     \*/
 
-    private void move_cb ()
+    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation)
     {
+        window.set_moves_count (moves_count);
         window.set_subtitle (null);
         window.restart_action.set_enabled (true);
         window.undo_action.set_enabled (true);
@@ -316,8 +318,6 @@ private class Taquin : Gtk.Application, BaseApplication
         window.finish_game ();
         /* Translators: notification, as a subtitle of the headerbar; on both games, if the user solves the 
puzzle */
         window.set_subtitle (_("Bravo! You finished the game!"));
-        window.restart_action.set_enabled (false); // Taquin specific
-        window.undo_action.set_enabled (false);    // Taquin specific
         play_sound ("gameover");
     }
 
diff --git a/src/taquin-view.vala b/src/taquin-view.vala
index 8f7f89c..3d8e3c6 100644
--- a/src/taquin-view.vala
+++ b/src/taquin-view.vala
@@ -435,7 +435,7 @@ public class TaquinView : Gtk.DrawingArea
         cr.line_to (grid_border_main + tile_size * (number + 2.0 / 3), inside ? y1 : y2);
     }
 
-    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, bool restarting)
+    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, uint moves_count, bool 
disable_animation)
     {
         this.x_axis = x_axis;
         this.number = number;
@@ -447,7 +447,13 @@ public class TaquinView : Gtk.DrawingArea
             x_arrow = x_gap;
             y_arrow = y_gap;
         }
-        if (!restarting)
+        if (disable_animation)
+        {
+            finished = false;
+            animation_end_offset = 0;
+            animate_end = false;
+        }
+        else
         {
             animation_offset = 0;
             animate = true;


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