[gnome-chess] game: Add pause game button



commit f6eb2afa07a4aaa6bcccc23a350689a64c7b97e2
Author: Plamena Manolova <plamena n manolova intel com>
Date:   Mon Jul 15 12:33:49 2013 +0100

    game: Add pause game button
    
    Adds a pause game button as well as the functionality
    to pause/start game when the game window loses/gains focus.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=701578

 data/gnome-chess.ui    |   18 +++++++++
 src/chess-clock.vala   |   26 ++++++++++++
 src/chess-game.vala    |    1 +
 src/chess-view-2d.vala |    2 +-
 src/chess-view-3d.vala |    2 +-
 src/gnome-chess.vala   |  100 ++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 147 insertions(+), 2 deletions(-)
---
diff --git a/data/gnome-chess.ui b/data/gnome-chess.ui
index 5b00393..9d3c23c 100644
--- a/data/gnome-chess.ui
+++ b/data/gnome-chess.ui
@@ -171,6 +171,24 @@
                 <property name="homogeneous">True</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkToolButton" id="pause_game_button">
+                <property name="visible">True</property>
+                <property name="sensitive">True</property>
+                <property name="can_focus">False</property>
+                <property name="tooltip_text" translatable="yes">Pause the game</property>
+                <property name="is_important">True</property>
+                <property name="label" translatable="yes" comments="The pause game button">Pause</property>
+                <property name="use_underline">True</property>
+                <property name="stock_id">gtk-media-pause</property>
+                <accelerator key="p" signal="clicked" modifiers="GDK_CONTROL_MASK"/>
+                <signal name="clicked" handler="pause_game_cb" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
           </object>
           <packing>
             <property name="expand">False</property>
diff --git a/src/chess-clock.vala b/src/chess-clock.vala
index c35bb46..6a37805 100644
--- a/src/chess-clock.vala
+++ b/src/chess-clock.vala
@@ -168,4 +168,30 @@ public class ChessClock : Object
 
         timer.reset ();
     }
+
+    public void toggle_paused (bool paused)
+    {
+        if (timer == null)
+            return;
+
+        if (paused)
+        {
+            timer.stop ();
+            Source.remove (expire_timeout);
+            expire_timeout = 0;
+            Source.remove (tick_timeout);
+            tick_timeout = 0;
+        }
+        else
+        {
+            timer  continue ();
+            if (active_color == Color.WHITE)
+                expire_timeout = Timeout.add (white_duration - _white_used,
+                                              timer_expired_cb);
+            else
+                expire_timeout = Timeout.add (black_duration - _black_used,
+                                              timer_expired_cb);
+            tick_cb ();
+        }
+    }
 }
diff --git a/src/chess-game.vala b/src/chess-game.vala
index 8777751..a71ce6f 100644
--- a/src/chess-game.vala
+++ b/src/chess-game.vala
@@ -1253,6 +1253,7 @@ public enum ChessRule
 public class ChessGame
 {
     public bool is_started;
+    public bool is_paused = false;
     public ChessResult result;
     public ChessRule rule;
     public List<ChessState> move_stack;
diff --git a/src/chess-view-2d.vala b/src/chess-view-2d.vala
index 0f63d4d..9ed2f33 100644
--- a/src/chess-view-2d.vala
+++ b/src/chess-view-2d.vala
@@ -252,7 +252,7 @@ private class ChessView2D : ChessView
 
     public override bool button_press_event (Gdk.EventButton event)
     {
-        if (scene.game == null || event.button != 1)
+        if (scene.game == null || event.button != 1 || scene.game.is_paused)
             return false;
 
         int file = (int) Math.floor((event.x - 0.5 * get_allocated_width () + square_size * 4) / 
square_size);
diff --git a/src/chess-view-3d.vala b/src/chess-view-3d.vala
index 39392a2..048ea43 100644
--- a/src/chess-view-3d.vala
+++ b/src/chess-view-3d.vala
@@ -519,7 +519,7 @@ private class ChessView3D : ChessView
 
     public override bool button_press_event (Gdk.EventButton event)
     {
-        if (scene.game == null || event.button != 1)
+        if (scene.game == null || event.button != 1 || scene.game.is_paused)
             return false;
 
         if (!start_gl ())
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index 99ffb7e..3882251 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -26,6 +26,7 @@ public class Application : Gtk.Application
     private ChessView view;
     private Gtk.Widget save_button;
     private Gtk.Widget undo_button;
+    private Gtk.Widget pause_button;
     private Gtk.Widget claim_draw_button;
     private Gtk.Widget resign_button;
     private Gtk.Widget fullscreen_button;
@@ -64,6 +65,20 @@ public class Application : Gtk.Application
     private ChessPlayer? human_player = null;
     private ChessEngine? opponent_engine = null;
     private bool is_fullscreen = false;
+    private bool is_paused = false;
+    private bool widget_sensitivity[8];
+
+    private enum SensitivityIndex
+    {
+        UNDO,
+        CLAIM_DRAW,
+        RESIGN,
+        FIRST_MOVE,
+        PREV_MOVE,
+        NEXT_MOVE,
+        LAST_MOVE,
+        HISTORY
+    }
 
     private const ActionEntry[] app_entries =
     {
@@ -79,6 +94,22 @@ public class Application : Gtk.Application
         this.game_file = game_file;
     }
 
+    public bool on_window_focus_out (Gdk.EventFocus focus)
+    {
+        if (!is_paused)
+            pause_game_cb (pause_button);
+
+        return false;
+    }
+
+    public bool on_window_focus_in (Gdk.EventFocus focus)
+    {
+        if (is_paused)
+            pause_game_cb (pause_button);
+
+        return false;
+    }
+
     public override void startup ()
     {
         base.startup ();
@@ -117,6 +148,7 @@ public class Application : Gtk.Application
         window = (Gtk.Window) builder.get_object ("gnome_chess_app");
         save_button = (Gtk.Widget) builder.get_object ("save_game_button");
         undo_button = (Gtk.Widget) builder.get_object ("undo_move_button");
+        pause_button = (Gtk.Widget) builder.get_object ("pause_game_button");
         claim_draw_button = (Gtk.Widget) builder.get_object ("claim_draw_button");
         resign_button = (Gtk.Widget) builder.get_object ("resign_button");
         fullscreen_button = (Gtk.Widget) builder.get_object ("fullscreen_button");
@@ -133,6 +165,8 @@ public class Application : Gtk.Application
         builder.connect_signals (this);
 
         add_window (window);
+        window.focus_out_event.connect (on_window_focus_out);
+        window.focus_in_event.connect (on_window_focus_in);
 
         info_bar = new Gtk.InfoBar ();
         var content_area = (Gtk.Container) info_bar.get_content_area ();
@@ -1129,6 +1163,72 @@ public class Application : Gtk.Application
             game.opponent.undo ();
     }
 
+    private void stash_button_sensitivity ()
+    {
+        widget_sensitivity[SensitivityIndex.UNDO] = undo_button.sensitive;
+        widget_sensitivity[SensitivityIndex.CLAIM_DRAW] =
+            claim_draw_button.sensitive;
+        widget_sensitivity[SensitivityIndex.RESIGN] = resign_button.sensitive;
+        widget_sensitivity[SensitivityIndex.FIRST_MOVE] =
+            first_move_button.sensitive;
+        widget_sensitivity[SensitivityIndex.PREV_MOVE] =
+            prev_move_button.sensitive;
+        widget_sensitivity[SensitivityIndex.NEXT_MOVE] =
+            next_move_button.sensitive;
+        widget_sensitivity[SensitivityIndex.LAST_MOVE] =
+            last_move_button.sensitive;
+        widget_sensitivity[SensitivityIndex.HISTORY] = history_combo.sensitive;
+    }
+
+    private void revert_button_sensitivity ()
+    {
+        undo_button.sensitive = widget_sensitivity[SensitivityIndex.UNDO];
+        claim_draw_button.sensitive =
+            widget_sensitivity[SensitivityIndex.CLAIM_DRAW];
+        resign_button.sensitive = widget_sensitivity[SensitivityIndex.RESIGN];
+        first_move_button.sensitive =
+            widget_sensitivity[SensitivityIndex.FIRST_MOVE];
+        prev_move_button.sensitive =
+            widget_sensitivity[SensitivityIndex.PREV_MOVE];
+        next_move_button.sensitive =
+            widget_sensitivity[SensitivityIndex.NEXT_MOVE];
+        last_move_button.sensitive =
+            widget_sensitivity[SensitivityIndex.LAST_MOVE];
+        history_combo.sensitive = widget_sensitivity[SensitivityIndex.HISTORY];
+    }
+
+    [CCode (cname = "G_MODULE_EXPORT pause_game_cb", instance_pos = -1)]
+    public void pause_game_cb (Gtk.Widget widget)
+    {
+        is_paused = !is_paused;
+        game.is_paused = is_paused;
+        Gtk.ToolButton tool_button = pause_button as Gtk.ToolButton;
+        if (is_paused)
+        {
+            if (game.clock != null)
+                game.clock.toggle_paused (true);
+            tool_button.stock_id = "gtk-media-play";
+            tool_button.label = "Start";
+            stash_button_sensitivity ();
+            undo_button.sensitive = false;
+            claim_draw_button.sensitive = false;
+            resign_button.sensitive = false;
+            first_move_button.sensitive = false;
+            prev_move_button.sensitive = false;
+            next_move_button.sensitive = false;
+            last_move_button.sensitive = false;
+            history_combo.sensitive = false;
+        }
+        else
+        {
+            if (game.clock != null)
+                game.clock.toggle_paused (false);
+            tool_button.stock_id = "gtk-media-pause";
+            tool_button.label = "Pause";
+            revert_button_sensitivity ();
+        }
+    }
+
     public void quit_cb ()
     {
         quit_game ();


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