[gnome-nibbles] Fix New Game/Pause actions during countdowns



commit 984d3b20980941b23f3ef7a98b68f40041f0d0f8
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Wed Feb 17 20:58:51 2016 +0200

    Fix New Game/Pause actions during countdowns
    
    There are currently two issues with the 'New Game' and 'Pause' buttons
    if clicked during the countdown prior to the start of any level:
    
    (1) Clicking 'Pause' immediately ends the countdown and starts the game,
    thus messing the game state as the game is running while the button
    still says pause. Clicking the button again pauses the game very
    briefly, then immediately unpauses it.
    
    (2) Clicking 'New Game' -> 'Cancel' immediately starts the game (while
    the countdown is still running) therefore causing the game to start again
    (kind of) once more when the countdown reaches zero. This oddly results
    in an increased move speed of the worms and some labels overlaying the
    game screen which is bad.
    
    Note that clicking 'New Game' -> 'New Game' works just fine.
    
    As discussed in the bug report comments issue (1) will be fixed by always
    disabling the pause button during countdowns.
    
    As for issue (2) this commit fixes it by freezing the countdown while
    the new game dialog is open and making sure that clicking 'Cancel' will
    not start the game if the countdown hasn't yet expired, following that
    the game will only start when the countdown reaches zero.
    
    Also use g_timeout_add_seconds instead of g_timeout_add to reduce power
    usage.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=762016

 src/gnome-nibbles.vala |   65 +++++++++++++++++++++++++++++------------------
 1 files changed, 40 insertions(+), 25 deletions(-)
---
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index bd6c556..903845a 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -74,6 +74,7 @@ public class Nibbles : Gtk.Application
 
     private uint countdown_id = 0;
     private const int COUNTDOWN_TIME = 3;
+    private int seconds = 0;
 
     private const ActionEntry action_entries[] =
     {
@@ -255,6 +256,29 @@ public class Nibbles : Gtk.Application
         base.shutdown ();
     }
 
+    private bool countdown_cb ()
+    {
+        seconds--;
+
+        if (seconds == 0)
+        {
+            statusbar_stack.set_visible_child_name ("scoreboard");
+            view.name_labels.hide ();
+
+            game.add_bonus (true);
+            game.start ();
+
+            pause_action.set_enabled (true);
+            back_action.set_enabled (true);
+
+            countdown_id = 0;
+            return Source.REMOVE;
+        }
+
+        countdown.set_label (seconds.to_string ());
+        return Source.CONTINUE;
+    }
+
     /*\
     * * Window events
     \*/
@@ -350,29 +374,13 @@ public class Nibbles : Gtk.Application
     {
         statusbar_stack.set_visible_child_name ("countdown");
 
-        var seconds = COUNTDOWN_TIME;
-        view.name_labels.show ();
-        countdown_id = Timeout.add (1000, () => {
-            countdown.set_label (seconds.to_string ());
-            if (seconds == 0)
-            {
-                statusbar_stack.set_visible_child_name ("scoreboard");
-                view.name_labels.hide ();
-                countdown.set_label (COUNTDOWN_TIME.to_string ());
-
-                game.add_bonus (true);
-                game.start ();
+        new_game_action.set_enabled (true);
 
-                new_game_action.set_enabled (true);
-                pause_action.set_enabled (true);
-                back_action.set_enabled (true);
+        seconds = COUNTDOWN_TIME;
+        view.name_labels.show ();
 
-                countdown_id = 0;
-                return Source.REMOVE;
-            }
-            seconds--;
-            return Source.CONTINUE;
-        });
+        countdown.set_label (COUNTDOWN_TIME.to_string ());
+        countdown_id = Timeout.add_seconds (1, countdown_cb);
     }
 
     private void restart_game ()
@@ -385,6 +393,12 @@ public class Nibbles : Gtk.Application
 
     private void new_game_cb ()
     {
+        if (countdown_id != 0)
+        {
+            Source.remove (countdown_id);
+            countdown_id = 0;
+        }
+
         if (game.is_running)
             game.stop ();
 
@@ -403,7 +417,11 @@ public class Nibbles : Gtk.Application
             if ((response_id == Gtk.ResponseType.CANCEL || response_id == Gtk.ResponseType.DELETE_EVENT)
                 && !game.is_paused)
             {
-                game.start ();
+                if (seconds == 0)
+                    game.start ();
+                else
+                    countdown_id = Timeout.add_seconds (1, countdown_cb);
+
                 view.grab_focus ();
             }
 
@@ -855,9 +873,6 @@ public class Nibbles : Gtk.Application
             label.destroy ();
             button.destroy ();
 
-            new_game_action.set_enabled (true);
-            pause_action.set_enabled (true);
-
             headerbar.set_title (_("Level %d").printf (game.current_level));
 
             restart_game ();


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