[gnome-taquin] Add 'restart' action.



commit 072b5082d5b82482d9e306451ef5331780a0607f
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Sat Jan 19 13:01:53 2019 +0100

    Add 'restart' action.
    
    Add new action to the history menubutton.

 data/game-headerbar.ui |  9 +++++++--
 src/game-window.vala   | 46 ++++++++++++++++++++++++++++++++++++----------
 src/taquin-game.vala   | 42 +++++++++++++++++++++++++++++++-----------
 src/taquin-main.vala   | 11 +++++++++++
 src/taquin-view.vala   |  9 ++++++---
 5 files changed, 91 insertions(+), 26 deletions(-)
---
diff --git a/data/game-headerbar.ui b/data/game-headerbar.ui
index 9ffcae2..23a996b 100644
--- a/data/game-headerbar.ui
+++ b/data/game-headerbar.ui
@@ -24,6 +24,11 @@
         <attribute name="label" translatable="yes">_Undo</attribute>
         <attribute name="action">ui.undo</attribute>
       </item>
+      <item>
+        <!-- Translators: during a game, entry in the menu of the history menubutton (with a mnemonic that 
appears pressing Alt) -->
+        <attribute name="label" translatable="yes">_Restart</attribute>
+        <attribute name="action">ui.restart</attribute>
+      </item>
     </section>
   </menu>
   <template class="GameHeaderBar" parent="BaseHeaderBar">
@@ -82,8 +87,8 @@
         <property name="visible">True</property>
         <property name="halign">center</property>
         <property name="valign">center</property>
-        <!-- Translators: during a game, label of the Start Over button (with a mnemonic that appears 
pressing Alt) -->
-        <property name="label" translatable="yes">_Start Over</property>
+        <!-- Translators: during a game, label of the New Game button (with a mnemonic that appears pressing 
Alt) -->
+        <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 -->
diff --git a/src/game-window.vala b/src/game-window.vala
index 6cd2aaf..3487480 100644
--- a/src/game-window.vala
+++ b/src/game-window.vala
@@ -130,6 +130,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
 
     public void cannot_undo_more ()
     {
+        restart_action.set_enabled (false);
         undo_action.set_enabled (false);
         game_view.show_game_content (/* grab focus */ true);
     }
@@ -188,12 +189,14 @@ private class GameWindow : BaseWindow, AdaptativeWidget
     public signal void wait ();
     public signal void back ();
 
+    public signal void restart ();
     public signal void undo ();
     public signal void redo ();
     public signal void hint ();
 
-    public SimpleAction undo_action;
-    public SimpleAction redo_action;
+    public SimpleAction restart_action;
+    public SimpleAction    undo_action;
+    public SimpleAction    redo_action;
 
     private bool back_action_disabled = true;
 
@@ -203,11 +206,13 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         action_group.add_action_entries (ui_action_entries, this);
         insert_action_group ("ui", action_group);
 
-        undo_action = (SimpleAction) action_group.lookup_action ("undo");
-        redo_action = (SimpleAction) action_group.lookup_action ("redo");
+        restart_action = (SimpleAction) action_group.lookup_action ("restart");
+           undo_action = (SimpleAction) action_group.lookup_action ("undo");
+           redo_action = (SimpleAction) action_group.lookup_action ("redo");
 
-        undo_action.set_enabled (false);
-        redo_action.set_enabled (false);
+        restart_action.set_enabled (false);
+           undo_action.set_enabled (false);
+           redo_action.set_enabled (false);
     }
 
     private const GLib.ActionEntry [] ui_action_entries =
@@ -215,8 +220,10 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         { "new-game", new_game_cb },
         { "start-game", start_game_cb },
 
-        { "undo", undo_cb },
-        { "redo", redo_cb },
+        { "restart", restart_cb },
+        {    "undo",    undo_cb },
+        {    "redo",    redo_cb },
+
         { "hint", hint_cb }
     };
 
@@ -245,8 +252,9 @@ private class GameWindow : BaseWindow, AdaptativeWidget
 
         game_finished = false;
 
-        undo_action.set_enabled (false);
-        redo_action.set_enabled (false);
+        restart_action.set_enabled (false);
+           undo_action.set_enabled (false);
+           redo_action.set_enabled (false);
 
         play ();        // FIXME lag (see in Taquin…)
 
@@ -254,6 +262,23 @@ private class GameWindow : BaseWindow, AdaptativeWidget
         show_view ();
     }
 
+    private void restart_cb (/* SimpleAction action, Variant? variant */)
+    {
+        if (game_view.is_in_in_window_mode ())
+            return;
+        if (!game_view.game_content_visible_if_true ())
+            return;
+
+        game_finished = false;
+
+        if (headerbar.new_game_button_is_focus ())
+            game_view.show_game_content (/* grab focus */ true);
+        redo_action.set_enabled (true);
+        restart_action.set_enabled (false);
+
+        restart ();
+    }
+
     private void undo_cb (/* SimpleAction action, Variant? variant */)
     {
         if (game_view.is_in_in_window_mode ())
@@ -279,6 +304,7 @@ private class GameWindow : BaseWindow, AdaptativeWidget
 
         if (headerbar.new_game_button_is_focus ())
             game_view.show_game_content (/* grab focus */ true);
+        restart_action.set_enabled (true);
         undo_action.set_enabled (true);
 
         redo ();
diff --git a/src/taquin-game.vala b/src/taquin-game.vala
index 2a318fb..6888098 100644
--- a/src/taquin-game.vala
+++ b/src/taquin-game.vala
@@ -54,7 +54,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);
+    public signal void move (bool x_axis, int number, int x_gap, int y_gap, bool restarting);
     public signal void empty_tile ();
     public signal void cannot_move (int x, int y);
     public signal void cannot_undo_more ();
@@ -134,12 +134,13 @@ public class Game : Object
     public void request_move (int x, int y)
     {
         if (game_type == GameType.FIFTEEN)
-            fifteen_move (x, y, false);
+            fifteen_move (x, y, /* undoing */ false);
         else
-            sixteen_move (x, y, false);
+            sixteen_move (x, y, /* undoing */ false);
     }
 
-    private void fifteen_move (int x, int y, bool undoing = false)
+    private void fifteen_move (int x, int y, bool undoing = false, bool restarting = false)
+        requires (!restarting || undoing)
     {
         if (x < 0 || x >= size || y < 0 || y >= size)
             return;
@@ -178,11 +179,13 @@ public class Game : Object
         }
         tiles[x_gap, y_gap] = -1;
 
-        move (move_x_axis, move_number, x_gap, y_gap);
-        check_complete ();
+        move (move_x_axis, move_number, x_gap, y_gap, restarting);
+        if (!undoing)
+            check_complete ();
     }
 
-    private void sixteen_move (int x, int y, bool undoing = false)
+    private void sixteen_move (int x, int y, bool undoing = false, bool restarting = false)
+        requires (!restarting || undoing)
     {
         /* TODO touch */
         if (x >= 0 && x < size && y >= 0 && y < size)
@@ -241,8 +244,9 @@ public class Game : Object
         }
         if (!undoing)
             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);
-        check_complete ();
+        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 ();
     }
 
     private void check_complete ()
@@ -271,9 +275,9 @@ public class Game : Object
             return;
 
         if (game_type == GameType.FIFTEEN)
-            fifteen_move (((!) state).x, ((!) state).y, true);
+            fifteen_move (((!) state).x, ((!) state).y, /* undoing */ true);
         else
-            sixteen_move (((!) state).x, ((!) state).y, true);
+            sixteen_move (((!) state).x, ((!) state).y, /* undoing */ true);
 
         state = previous_state;
         previous_state = state == null ? null : ((!) state).previous;
@@ -282,6 +286,22 @@ public class Game : Object
             cannot_undo_more ();
     }
 
+    public void restart ()
+    {
+        while (state != null)
+        {
+            if (game_type == GameType.FIFTEEN)
+                fifteen_move (((!) state).x, ((!) state).y, /* undoing */ true, /* restarting */ true);
+            else
+                sixteen_move (((!) state).x, ((!) state).y, /* undoing */ true, /* restarting */ true);
+
+            state = previous_state;
+            previous_state = state == null ? null : ((!) state).previous;
+        }
+
+        cannot_undo_more ();
+    }
+
     private void add_move (int x_gap, int y_gap)
     {
         previous_state = state == null ? null : state;
diff --git a/src/taquin-main.vala b/src/taquin-main.vala
index 1006505..c673de6 100644
--- a/src/taquin-main.vala
+++ b/src/taquin-main.vala
@@ -156,6 +156,7 @@ private class Taquin : Gtk.Application, BaseApplication
                                  night_light_monitor);
         window.play.connect (start_game);
         window.undo.connect (undo_cb);
+        window.restart.connect (restart_cb);
 
         set_accels_for_action ("base.copy",             {        "<Primary>c"       });
         set_accels_for_action ("base.copy-alt",         { "<Shift><Primary>c"       });
@@ -166,6 +167,7 @@ private class Taquin : Gtk.Application, BaseApplication
         set_accels_for_action ("base.paste",            {        "<Primary>v"       });
         set_accels_for_action ("base.paste-alt",        { "<Shift><Primary>v"       });
         set_accels_for_action ("ui.undo",               {        "<Primary>z"       });
+     // set_accels_for_action ("ui.restart" // TODO
         set_accels_for_action ("ui.redo",               { "<Shift><Primary>z"       });
         set_accels_for_action ("base.escape",           {                 "Escape"  });
         set_accels_for_action ("base.toggle-hamburger", {                 "F10",
@@ -277,6 +279,13 @@ private class Taquin : Gtk.Application, BaseApplication
     * * Signals from window
     \*/
 
+    private void restart_cb ()
+        requires (game != null)
+    {
+        ((!) game).restart ();
+        play_sound ("sliding-n");
+    }
+
     private void undo_cb ()
         requires (game != null)
     {
@@ -291,6 +300,7 @@ private class Taquin : Gtk.Application, BaseApplication
     private void move_cb ()
     {
         window.set_subtitle (null);
+        window.restart_action.set_enabled (true);
         window.undo_action.set_enabled (true);
         play_sound ("sliding-1");       // TODO sliding-n??
     }
@@ -306,6 +316,7 @@ 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 48fe921..8f7f89c 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)
+    private void move_cb (bool x_axis, int number, int x_gap, int y_gap, bool restarting)
     {
         this.x_axis = x_axis;
         this.number = number;
@@ -447,8 +447,11 @@ public class TaquinView : Gtk.DrawingArea
             x_arrow = x_gap;
             y_arrow = y_gap;
         }
-        animation_offset = 0;
-        animate = true;
+        if (!restarting)
+        {
+            animation_offset = 0;
+            animate = true;
+        }
         queue_draw ();
     }
 


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