[gnome-sudoku] Added play/pause button using original timer.
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-sudoku] Added play/pause button using original timer.
- Date: Mon, 15 Sep 2014 21:40:07 +0000 (UTC)
commit e12896deeca2ddbe399183af37206a130af14d0e
Author: amishas157 <amishas157 gmail com>
Date: Mon Sep 1 01:38:14 2014 +0530
Added play/pause button using original timer.
https://bugzilla.gnome.org/show_bug.cgi?id=732779
data/gnome-sudoku.ui | 51 ++++++++++++++++++++++++++++++-
lib/sudoku-game.vala | 62 ++++++++++++++++++++++++++++++++++++++-
src/gnome-sudoku.vala | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 187 insertions(+), 4 deletions(-)
---
diff --git a/data/gnome-sudoku.ui b/data/gnome-sudoku.ui
index d860180..84c3b08 100644
--- a/data/gnome-sudoku.ui
+++ b/data/gnome-sudoku.ui
@@ -82,6 +82,27 @@
</child>
</object>
</child>
+ <child>
+ <object class="GtkLabel" id="clock_label">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="halign">center</property>
+ </object>
+ <packing>
+ <property name="pack-type">end</property>
+ </packing>
+ </child>
+ <child>
+ <object class="GtkImage" id="clock_image">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="icon_name">preferences-system-time-symbolic</property>
+ <property name="icon_size">1</property>
+ </object>
+ <packing>
+ <property name="pack-type">end</property>
+ </packing>
+ </child>
</object>
</child>
<child>
@@ -183,6 +204,32 @@
<property name="orientation">vertical</property>
<property name="spacing">6</property>
<child>
+ <object class="GtkButton" id="play_pause_button">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="receives_default">True</property>
+ <property name="action_name">app.pause</property>
+ <property name="use_underline">True</property>
+ <property name="width-request">120</property>
+ <property name="height-request">60</property>
+ <child>
+ <object class="GtkLabel" id="play_pause_label">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="margin">12</property>
+ <property name="label"
translatable="yes">_Pause</property>
+ <property name="use_underline">True</property>
+ </object>
+ </child>
+ </object>
+ <packing>
+ <property name="expand">False</property>
+ <property name="fill">True</property>
+ <property name="pack_type">end</property>
+ <property name="position">0</property>
+ </packing>
+ </child>
+ <child>
<object class="GtkButton" id="clear_button">
<property name="visible">True</property>
<property name="use_underline">True</property>
@@ -197,7 +244,7 @@
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
- <property name="position">0</property>
+ <property name="position">1</property>
</packing>
</child>
<child>
@@ -215,7 +262,7 @@
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
- <property name="position">1</property>
+ <property name="position">2</property>
</packing>
</child>
</object> <!-- End of controls_box -->
diff --git a/lib/sudoku-game.vala b/lib/sudoku-game.vala
index 125d51e..7658156 100644
--- a/lib/sudoku-game.vala
+++ b/lib/sudoku-game.vala
@@ -6,6 +6,21 @@ public class SudokuGame : Object
{
public SudokuBoard board;
public GLib.Timer timer;
+ private uint clock_timeout;
+
+ public signal void tick ();
+ public signal void paused_changed ();
+
+ private bool _paused = false;
+ public bool paused
+ {
+ private set
+ {
+ _paused = value;
+ paused_changed ();
+ }
+ get { return _paused; }
+ }
private struct UndoItem
{
@@ -35,6 +50,7 @@ public class SudokuGame : Object
timer = new Timer();
undostack = new ArrayList<UndoItem?> ();
redostack = new ArrayList<UndoItem?> ();
+ board.completed.connect (() => stop_clock ());
}
public void insert (int row, int col, int val)
@@ -65,7 +81,8 @@ public class SudokuGame : Object
public void reset ()
{
- timer.reset();
+ board.previous_played_time = 0;
+ timer.start ();
undostack.clear ();
redostack.clear ();
for (var l1 = 0; l1 < board.rows; l1++)
@@ -117,4 +134,47 @@ public class SudokuGame : Object
{
return board.previous_played_time + timer.elapsed ();
}
+
+ private bool timeout_cb ()
+ {
+ /* Notify on the next tick */
+ var elapsed = get_total_time_played ();
+ var next = (int) (elapsed + 1.0);
+ var wait = next - elapsed;
+ clock_timeout = Timeout.add_seconds ((int) (wait), timeout_cb);
+
+ tick ();
+
+ return false;
+ }
+
+ public void start_clock ()
+ {
+ if (timer == null)
+ timer = new Timer ();
+ timer.start ();
+ timeout_cb ();
+ }
+
+ public void stop_clock ()
+ {
+ if (timer == null)
+ return;
+ if (clock_timeout != 0)
+ Source.remove (clock_timeout);
+ paused = true;
+ clock_timeout = 0;
+ timer.stop ();
+ tick ();
+ }
+
+ public void continue_clock ()
+ {
+ if (timer == null)
+ timer = new Timer ();
+ else
+ timer.continue ();
+ paused = false;
+ timeout_cb ();
+ }
}
diff --git a/src/gnome-sudoku.vala b/src/gnome-sudoku.vala
index 7567a3d..0786e0c 100644
--- a/src/gnome-sudoku.vala
+++ b/src/gnome-sudoku.vala
@@ -8,6 +8,10 @@ public class Sudoku : Gtk.Application
private bool is_maximized;
private int window_width;
private int window_height;
+ private Gtk.Button play_pause_button;
+ private Gtk.Label play_pause_label;
+ private Gtk.Label clock_label;
+ private Gtk.Image clock_image;
private ApplicationWindow window;
@@ -29,6 +33,8 @@ public class Sudoku : Gtk.Application
private SimpleAction clear_action;
private SimpleAction print_action;
private SimpleAction print_multiple_action;
+ private SimpleAction pause_action;
+ private SimpleAction new_game_action;
private string header_bar_subtitle;
@@ -43,6 +49,7 @@ public class Sudoku : Gtk.Application
{"undo", undo_cb },
{"redo", redo_cb },
{"print", print_cb },
+ {"pause", toggle_pause_cb },
{"print-multiple", print_multiple_cb },
{"help", help_cb },
{"about", about_cb },
@@ -140,12 +147,18 @@ public class Sudoku : Gtk.Application
game_box = (Box) builder.get_object ("game_box");
undo_redo_box = (Box) builder.get_object ("undo_redo_box");
back_button = (Button) builder.get_object ("back_button");
+ clock_label = (Gtk.Label) builder.get_object ("clock_label");
+ clock_image = (Gtk.Image) builder.get_object ("clock_image");
+ play_pause_button = (Gtk.Button) builder.get_object ("play_pause_button");
+ play_pause_label = (Gtk.Label) builder.get_object ("play_pause_label");
undo_action = (SimpleAction) lookup_action ("undo");
redo_action = (SimpleAction) lookup_action ("redo");
+ new_game_action = (SimpleAction) lookup_action ("new-game");
clear_action = (SimpleAction) lookup_action ("reset");
print_action = (SimpleAction) lookup_action ("print");
print_multiple_action = (SimpleAction) lookup_action ("print-multiple");
+ pause_action = (SimpleAction) lookup_action ("pause");
saver = new SudokuSaver ();
var savegame = saver.get_savedgame ();
@@ -208,6 +221,58 @@ public class Sudoku : Gtk.Application
return false;
}
+ private void paused_changed_cb ()
+ {
+ if (game.paused)
+ {
+ display_unpause_button ();
+ clear_action.set_enabled (false);
+ undo_action.set_enabled (false);
+ redo_action.set_enabled (false);
+ new_game_action.set_enabled (false);
+ }
+ else if (game.get_total_time_played () > 0)
+ {
+ display_pause_button ();
+ clear_action.set_enabled (!game.board.is_empty ());
+ undo_action.set_enabled (!game.is_undostack_null ());
+ redo_action.set_enabled (!game.is_redostack_null ());
+ new_game_action.set_enabled (true);
+ }
+ }
+
+ private void toggle_pause_cb ()
+ {
+ if (game.paused)
+ game.continue_clock ();
+ else
+ game.stop_clock ();
+ }
+
+ private void tick_cb ()
+ {
+ var elapsed_time = (int) game.get_total_time_played ();
+ var hours = elapsed_time / 3600;
+ var minutes = (elapsed_time - hours * 3600) / 60;
+ var seconds = elapsed_time - hours * 3600 - minutes * 60;
+ if (hours > 0)
+ clock_label.set_text ("%02d∶\xE2\x80\x8E%02d∶\xE2\x80\x8E%02d".printf (hours, minutes, seconds));
+ else
+ clock_label.set_text ("%02d∶\xE2\x80\x8E%02d".printf (minutes, seconds));
+ }
+
+ private void display_pause_button ()
+ {
+ play_pause_button.show ();
+ play_pause_label.label = _("_Pause");
+ }
+
+ private void display_unpause_button ()
+ {
+ play_pause_button.show ();
+ play_pause_label.label = _("_Resume");
+ }
+
private void start_game (SudokuBoard board)
{
undo_action.set_enabled (false);
@@ -221,7 +286,10 @@ public class Sudoku : Gtk.Application
game = new SudokuGame (board);
back_cb ();
- game.timer.start ();
+ game.tick.connect (tick_cb);
+ game.paused_changed.connect (paused_changed_cb);
+
+ game.start_clock ();
view = new SudokuView (game);
view.set_size_request (480, 480);
@@ -276,6 +344,10 @@ public class Sudoku : Gtk.Application
header_bar_subtitle = header_bar.get_subtitle ();
header_bar.set_subtitle (null);
print_action.set_enabled (false);
+ clock_label.hide ();
+ clock_image.hide ();
+ if (game != null)
+ game.stop_clock ();
}
private void new_game_cb ()
@@ -330,6 +402,10 @@ public class Sudoku : Gtk.Application
undo_redo_box.visible = true;
header_bar.set_subtitle (header_bar_subtitle);
print_action.set_enabled (true);
+ clock_label.show ();
+ clock_image.show ();
+ if (game != null)
+ game.continue_clock ();
}
private void undo_cb ()
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]