[gnome-games] gnome-mahjongg: Use own timer instead of the one from libgames-support
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-games] gnome-mahjongg: Use own timer instead of the one from libgames-support
- Date: Mon, 20 Aug 2012 02:22:54 +0000 (UTC)
commit dc0aa594a8386954a35caeaa23cf535786752721
Author: Robert Ancell <robert ancell canonical com>
Date: Mon Aug 20 14:20:20 2012 +1200
gnome-mahjongg: Use own timer instead of the one from libgames-support
gnome-mahjongg/src/game-view.vala | 19 +++---
gnome-mahjongg/src/game.vala | 103 +++++++++++++++++++++++++++++---
gnome-mahjongg/src/gnome-mahjongg.vala | 57 ++++++-----------
3 files changed, 124 insertions(+), 55 deletions(-)
---
diff --git a/gnome-mahjongg/src/game-view.vala b/gnome-mahjongg/src/game-view.vala
index 0fcd9b8..dac4322 100644
--- a/gnome-mahjongg/src/game-view.vala
+++ b/gnome-mahjongg/src/game-view.vala
@@ -20,6 +20,7 @@ public class GameView : Gtk.DrawingArea
{
_game = value;
_game.redraw_tile.connect (redraw_tile_cb);
+ _game.paused_changed.connect (paused_changed_cb);
queue_draw ();
}
}
@@ -31,13 +32,6 @@ public class GameView : Gtk.DrawingArea
set { _theme = value; tile_pattern = null; queue_draw (); }
}
- private bool _paused = false;
- public bool paused
- {
- get { return _paused; }
- set { _paused = value; queue_draw (); }
- }
-
public GameView ()
{
can_focus = true;
@@ -98,7 +92,7 @@ public class GameView : Gtk.DrawingArea
/* Select image for this tile, or blank image if paused */
var texture_x = get_image_offset (tile.number) * image_width;
var texture_y = 0;
- if (paused)
+ if (game.paused)
{
texture_x = get_image_offset (-1) * image_width;
texture_y = 0;
@@ -122,7 +116,7 @@ public class GameView : Gtk.DrawingArea
}
/* Draw pause overlay */
- if (paused)
+ if (game.paused)
{
cr.set_source_rgba (0, 0, 0, 0.75);
cr.paint ();
@@ -206,6 +200,11 @@ public class GameView : Gtk.DrawingArea
queue_draw_area (x, y, tile_pattern_width, tile_pattern_height);
}
+ private void paused_changed_cb ()
+ {
+ queue_draw ();
+ }
+
public override bool draw (Cairo.Context cr)
{
if (game == null)
@@ -220,7 +219,7 @@ public class GameView : Gtk.DrawingArea
public override bool button_press_event (Gdk.EventButton event)
{
- if (game == null || paused)
+ if (game == null || game.paused)
return false;
/* Ignore the 2BUTTON and 3BUTTON events. */
diff --git a/gnome-mahjongg/src/game.vala b/gnome-mahjongg/src/game.vala
index 3dd06f8..0c9c72e 100644
--- a/gnome-mahjongg/src/game.vala
+++ b/gnome-mahjongg/src/game.vala
@@ -47,17 +47,52 @@ public class Game
public int move_number;
/* Hint animation */
- private uint hint_timer = 0;
+ private uint hint_timout = 0;
public uint hint_blink_counter = 0;
+ /* Game timer */
+ private double clock_elapsed;
+ private Timer? clock;
+ private uint clock_timeout;
+
public signal void redraw_tile (Tile tile);
public signal void moved ();
+ public signal void paused_changed ();
+ public signal void tick ();
public bool started
{
get { return move_number > 1; }
}
+ public double elapsed
+ {
+ get
+ {
+ if (clock == null)
+ return 0.0;
+ return clock_elapsed + clock.elapsed ();
+ }
+ }
+
+ private bool _paused = false;
+ public bool paused
+ {
+ set
+ {
+ _paused = value;
+ if (clock != null)
+ {
+ if (value)
+ stop_clock ();
+ else
+ continue_clock ();
+ }
+ paused_changed ();
+ }
+ get { return _paused; }
+ }
+
private Tile? _selected_tile = null;
public Tile? selected_tile
{
@@ -169,6 +204,8 @@ public class Game
public void reset ()
{
+ clock = null;
+ clock_elapsed = 0.0;
selected_tile = null;
set_hint (null, null);
foreach (var tile in tiles)
@@ -196,19 +233,24 @@ public class Game
hint_tiles[0] = tile0;
hint_tiles[1] = tile1;
hint_blink_counter = 6;
- if (hint_timer != 0)
- Source.remove (hint_timer);
- hint_timer = Timeout.add (250, hint_timeout_cb);
+ if (hint_timout != 0)
+ Source.remove (hint_timout);
+ hint_timout = Timeout.add (250, hint_timeout_cb);
hint_timeout_cb ();
+
+ /* 30s penalty */
+ start_clock ();
+ clock_elapsed += 30.0;
+ tick ();
}
private bool hint_timeout_cb ()
{
if (hint_blink_counter == 0)
{
- if (hint_timer != 0)
- Source.remove (hint_timer);
- hint_timer = 0;
+ if (hint_timout != 0)
+ Source.remove (hint_timout);
+ hint_timout = 0;
return false;
}
hint_blink_counter--;
@@ -326,11 +368,56 @@ public class Game
if (tile.move_number >= move_number)
tile.move_number = 0;
+ if (complete)
+ stop_clock ();
+ else
+ start_clock ();
+
moved ();
return true;
}
-
+
+ private void start_clock ()
+ {
+ if (clock == null)
+ clock = new Timer ();
+ timeout_cb ();
+ }
+
+ private void stop_clock ()
+ {
+ if (clock == null)
+ return;
+ if (clock_timeout != 0)
+ Source.remove (clock_timeout);
+ clock_timeout = 0;
+ clock.stop ();
+ tick ();
+ }
+
+ private void continue_clock ()
+ {
+ if (clock == null)
+ clock = new Timer ();
+ else
+ clock.continue ();
+ timeout_cb ();
+ }
+
+ private bool timeout_cb ()
+ {
+ /* Notify on the next tick */
+ var elapsed = clock.elapsed ();
+ var next = (int) (elapsed + 1.0);
+ var wait = next - elapsed;
+ clock_timeout = Timeout.add ((int) (wait * 1000), timeout_cb);
+
+ tick ();
+
+ return false;
+ }
+
public bool can_undo
{
get { return move_number > 1; }
diff --git a/gnome-mahjongg/src/gnome-mahjongg.vala b/gnome-mahjongg/src/gnome-mahjongg.vala
index 64cc06f..e09b80b 100644
--- a/gnome-mahjongg/src/gnome-mahjongg.vala
+++ b/gnome-mahjongg/src/gnome-mahjongg.vala
@@ -12,7 +12,7 @@ public class Mahjongg : Gtk.Application
private Gtk.UIManager ui_manager;
private Gtk.Toolbar toolbar;
private Gtk.Label moves_label;
- private GnomeGamesSupport.Clock game_clock;
+ private Gtk.Label clock_label;
private Gtk.Dialog? preferences_dialog = null;
private GnomeGamesSupport.PauseAction pause_action;
@@ -64,14 +64,8 @@ public class Mahjongg : Gtk.Application
group_box.pack_start (moves_label, false, false, 0);
status_box.pack_start (group_box, false, false, 0);
- group_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
- var game_clock_label = new Gtk.Label (_("Time:"));
- group_box.pack_start (game_clock_label, false, false, 0);
- spacer = new Gtk.Label (" ");
- group_box.pack_start (spacer, false, false, 0);
- game_clock = new GnomeGamesSupport.Clock ();
- group_box.pack_start (game_clock, false, false, 0);
- status_box.pack_start (group_box, false, false, 0);
+ clock_label = new Gtk.Label ("");
+ status_box.pack_start (clock_label, false, false, 0);
var vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
@@ -129,6 +123,7 @@ public class Mahjongg : Gtk.Application
leave_fullscreen_action.set_visible_policy (GnomeGamesSupport.VisiblePolicy.ON_FULLSCREEN);
conf_value_changed_cb (settings, "tileset");
conf_value_changed_cb (settings, "bgcolour");
+ tick_cb ();
}
public override void activate ()
@@ -141,7 +136,7 @@ public class Mahjongg : Gtk.Application
pause_action.sensitive = game_view.game.move_number > 1;
restart_action.sensitive = game_view.game.move_number > 1;
- if (game_view.paused)
+ if (game_view.game.paused)
{
hint_action.sensitive = false;
undo_action.sensitive = false;
@@ -248,17 +243,11 @@ public class Mahjongg : Gtk.Application
private void moved_cb ()
{
- /* Start game once moved */
- if (game_clock.get_seconds () == 0)
- game_clock.start ();
-
update_ui ();
if (game_view.game.complete)
{
- game_clock.stop ();
-
- var seconds = game_clock.get_seconds ();
+ var seconds = (int) (game_view.game.elapsed + 0.5);
var p = highscores.add_time_score ((seconds / 60) * 1.0 + (seconds % 60) / 100.0);
var scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Mahjongg Scores"));
@@ -461,10 +450,6 @@ public class Mahjongg : Gtk.Application
var match = matches.nth_data (n);
game_view.game.set_hint (match.tile0, match.tile1);
}
-
- /* 30s penalty */
- game_clock.start ();
- game_clock.add_seconds (30);
}
private void about_cb ()
@@ -524,15 +509,10 @@ public class Mahjongg : Gtk.Application
private void pause_cb (GnomeGamesSupport.PauseAction action)
{
- game_view.paused = action.get_is_paused ();
+ game_view.game.paused = action.get_is_paused ();
game_view.game.set_hint (null, null);
game_view.game.selected_tile = null;
- if (game_view.paused)
- game_clock.stop ();
- else
- game_clock.start ();
-
update_ui ();
}
@@ -562,7 +542,7 @@ public class Mahjongg : Gtk.Application
private void redo_cb ()
{
- if (game_view.paused)
+ if (game_view.game.paused)
return;
game_view.game.redo ();
@@ -578,11 +558,6 @@ public class Mahjongg : Gtk.Application
private void restart_game ()
{
game_view.game.reset ();
-
- /* Prepare clock */
- game_clock.stop ();
- game_clock.reset ();
-
update_ui ();
}
@@ -602,6 +577,7 @@ public class Mahjongg : Gtk.Application
game_view.game = new Game (map);
game_view.game.moved.connect (moved_cb);
+ game_view.game.tick.connect (tick_cb);
highscores.set_category (game_view.game.map.score_name);
/* Set window title */
@@ -609,13 +585,20 @@ public class Mahjongg : Gtk.Application
/* Translators: This is the window title for Mahjongg which contains the map name, e.g. 'Mahjongg - Red Dragon' */
window.set_title (_("Mahjongg - %s").printf (display_name));
- /* Prepare clock */
- game_clock.stop ();
- game_clock.reset ();
-
update_ui ();
}
+ private void tick_cb ()
+ {
+ var elapsed = 0;
+ if (game_view.game != null)
+ elapsed = (int) (game_view.game.elapsed + 0.5);
+ var hours = elapsed / 3600;
+ var minutes = (elapsed - hours * 3600) / 60;
+ var seconds = elapsed - hours * 3600 - minutes * 60;
+ clock_label.set_text ("%s: %02d:%02d:%02d".printf (_("Time"), hours, minutes, seconds));
+ }
+
private void help_cb ()
{
try
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]