[gnome-nibbles/wip/vala] Implement pause



commit 2a91bf758228b123de7bbcfc43d6428c6430e493
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Aug 12 18:02:03 2015 +0200

    Implement pause

 data/nibbles.ui        |   24 ++++++++++++++++++++++++
 src/gnome-nibbles.vala |   39 +++++++++++++++++++++++++++++++++++++++
 src/nibbles-game.vala  |   19 ++++++++++++++++++-
 3 files changed, 81 insertions(+), 1 deletions(-)
---
diff --git a/data/nibbles.ui b/data/nibbles.ui
index d7ca255..9e639ff 100644
--- a/data/nibbles.ui
+++ b/data/nibbles.ui
@@ -11,6 +11,20 @@
                 <property name="label" translatable="yes">_New Game</property>
                 <property name="action-name">app.new-game</property>
             </object>
+            <packing>
+                <property name="pack-type">start</property>
+            </packing>
+        </child>
+        <child>
+            <object class="GtkButton" id="pause_button">
+                <property name="visible">False</property>
+                <property name="use-underline">True</property>
+                <property name="label" translatable="yes">_Pause</property>
+                <property name="action-name">app.pause</property>
+            </object>
+            <packing>
+                <property name="pack-type">end</property>
+            </packing>
         </child>
     </object>
     <object class="GtkApplicationWindow" id="nibbles-window">
@@ -298,6 +312,16 @@
                                                         <property name="name">countdown</property>
                                                     </packing>
                                                 </child>
+                                                <child>
+                                                    <object class="GtkLabel">
+                                                        <property name="visible">True</property>
+                                                        <property name="label" 
translatable="yes">Paused</property>
+                                                        <style><class name="menu-title"/></style>
+                                                    </object>
+                                                    <packing>
+                                                        <property name="name">paused</property>
+                                                    </packing>
+                                                </child>
                                             </object>
                                         </child>
                                     </object>
diff --git a/src/gnome-nibbles.vala b/src/gnome-nibbles.vala
index 86cc0e1..c759f83 100644
--- a/src/gnome-nibbles.vala
+++ b/src/gnome-nibbles.vala
@@ -29,6 +29,7 @@ public class Nibbles : Gtk.Application
     private Gtk.ApplicationWindow window;
     private Gtk.HeaderBar headerbar;
     private Gtk.Button new_game_button;
+    private Gtk.Button pause_button;
     private Gtk.Stack main_stack;
     private Gtk.Box game_box;
     private Games.GridFrame frame;
@@ -54,10 +55,14 @@ public class Nibbles : Gtk.Application
     private const int COUNTDOWN_TIME = 3;
     private uint countdown_id = 0;
 
+    private SimpleAction new_game_action;
+    private SimpleAction pause_action;
+
     private const ActionEntry action_entries[] =
     {
         {"start-game", start_game_cb},
         {"new-game", new_game_cb},
+        {"pause", pause_cb},
         {"scores", scores_cb},
         {"about", about_cb},
         {"quit", quit}
@@ -117,6 +122,8 @@ public class Nibbles : Gtk.Application
         }
 
         set_accels_for_action ("app.quit", {"<Primary>q"});
+        new_game_action = (SimpleAction) lookup_action ("new-game");
+        pause_action = (SimpleAction) lookup_action ("pause");
 
         var builder = new Gtk.Builder.from_resource ("/org/gnome/nibbles/ui/nibbles.ui");
         window = builder.get_object ("nibbles-window") as Gtk.ApplicationWindow;
@@ -129,6 +136,7 @@ public class Nibbles : Gtk.Application
 
         headerbar = (Gtk.HeaderBar) builder.get_object ("headerbar");
         new_game_button = (Gtk.Button) builder.get_object ("new_game_button");
+        pause_button = (Gtk.Button) builder.get_object ("pause_button");
         main_stack = (Gtk.Stack) builder.get_object ("main_stack");
         game_box = (Gtk.Box) builder.get_object ("game_box");
         statusbar_stack = (Gtk.Stack) builder.get_object ("statusbar_stack");
@@ -151,6 +159,12 @@ public class Nibbles : Gtk.Application
         game.log_score.connect (log_score_cb);
         game.restart_game.connect (restart_game_cb);
         game.level_completed.connect (level_completed_cb);
+        game.notify["is_paused"].connect (() => {
+            if (game.is_paused)
+                statusbar_stack.set_visible_child_name ("paused");
+            else
+                statusbar_stack.set_visible_child_name ("scoreboard");
+        });
 
         view = new NibblesView (game);
         view.configure_event.connect (configure_event_cb);
@@ -300,8 +314,13 @@ public class Nibbles : Gtk.Application
                 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);
+                pause_action.set_enabled (true);
+
                 countdown_id = 0;
                 return Source.REMOVE;
             }
@@ -329,6 +348,8 @@ public class Nibbles : Gtk.Application
 
     private void new_game_cb ()
     {
+        game.pause ();
+
         var dialog = new Gtk.MessageDialog (window,
                                             Gtk.DialogFlags.MODAL,
                                             Gtk.MessageType.WARNING,
@@ -341,6 +362,8 @@ public class Nibbles : Gtk.Application
         dialog.response.connect ((response_id) => {
             if (response_id == Gtk.ResponseType.OK)
                 show_new_game_screen_cb ();
+            if (response_id == Gtk.ResponseType.CANCEL)
+                game.unpause ();
 
             dialog.destroy ();
         });
@@ -348,6 +371,17 @@ public class Nibbles : Gtk.Application
         dialog.show ();
     }
 
+    private void pause_cb ()
+    {
+        if (game != null)
+        {
+            if (game.is_running)
+                game.pause ();
+            else
+                game.unpause ();
+        }
+    }
+
     private void show_first_run_screen ()
     {
         main_stack.set_visible_child_name ("first_run");
@@ -364,7 +398,11 @@ public class Nibbles : Gtk.Application
         if (game.is_running)
             game.stop ();
 
+        new_game_action.set_enabled (false);
+        pause_action.set_enabled (false);
+
         new_game_button.hide ();
+        pause_button.hide ();
 
         var type = main_stack.get_transition_type ();
         main_stack.set_transition_type (Gtk.StackTransitionType.NONE);
@@ -406,6 +444,7 @@ public class Nibbles : Gtk.Application
     private void show_game_view ()
     {
         new_game_button.show ();
+        pause_button.show ();
 
         main_stack.set_visible_child_name ("game_box");
     }
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 13dd8ba..00886d4 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -66,6 +66,7 @@ public class NibblesGame : Object
     public int speed = 1;
 
     public bool is_running = false;
+    public bool is_paused { get; private set; }
 
     public bool fakes = false;
 
@@ -88,6 +89,8 @@ public class NibblesGame : Object
         worms = new Gee.LinkedList<Worm> ();
         worm_props = new Gee.HashMap<Worm, WormProperties?> ();
 
+        is_paused = false;
+
         Random.set_seed ((uint32) time_t ());
         load_properties (settings);
     }
@@ -100,7 +103,6 @@ public class NibblesGame : Object
     {
         stderr.printf("[Debug] Game started\n");
         is_running = true;
-        add_bonus (true);
 
         main_id = Timeout.add (GAMEDELAY * speed, main_loop_cb);
         Source.set_name_by_id (main_id, "[Nibbles] main_loop_cb");
@@ -126,6 +128,18 @@ public class NibblesGame : Object
         }
     }
 
+    public void pause ()
+    {
+        is_paused = true;
+        stop ();
+    }
+
+    public void unpause ()
+    {
+        is_paused = false;
+        start ();
+    }
+
     private void end ()
     {
         stop ();
@@ -537,6 +551,9 @@ public class NibblesGame : Object
 
     public bool handle_keypress (uint keyval)
     {
+        if (is_paused)
+            return false;
+
         foreach (var worm in worms)
         {
             if (worm.is_human)


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