[gnome-games] gnotravex: 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] gnotravex: Use own timer instead of the one from libgames-support
- Date: Mon, 20 Aug 2012 02:55:27 +0000 (UTC)
commit fdc3c2c88ca918418e1efef94e24e8d0c5f92ac9
Author: Robert Ancell <robert ancell canonical com>
Date: Mon Aug 20 14:53:16 2012 +1200
gnotravex: Use own timer instead of the one from libgames-support
gnotravex/src/gnotravex.vala | 43 +++++++++------------
gnotravex/src/puzzle-view.vala | 20 ++-------
gnotravex/src/puzzle.vala | 82 +++++++++++++++++++++++++++++++++++++++-
3 files changed, 105 insertions(+), 40 deletions(-)
---
diff --git a/gnotravex/src/gnotravex.vala b/gnotravex/src/gnotravex.vala
index 0816919..97fb002 100644
--- a/gnotravex/src/gnotravex.vala
+++ b/gnotravex/src/gnotravex.vala
@@ -5,7 +5,7 @@ public class Gnotravex : Gtk.Application
private Settings settings;
private Puzzle puzzle;
- private GnomeGamesSupport.Clock clock;
+ private Gtk.Label clock_label;
private GnomeGamesSupport.Scores highscores;
private PuzzleView view;
@@ -148,16 +148,10 @@ public class Gnotravex : Gtk.Application
time_box.show ();
time_align.add (time_box);
- var time_label = new Gtk.Label (_("Time:"));
- time_label.show ();
- time_box.pack_start (time_label, false, false, 0);
-
- var label = new Gtk.Label (" ");
- label.show ();
- time_box.pack_start (label, false, false, 0);
- clock = new GnomeGamesSupport.Clock ();
- clock.show ();
- time_box.pack_start (clock, false, false, 0);
+ clock_label = new Gtk.Label ("");
+ clock_label.show ();
+ time_box.pack_start (clock_label, false, false, 0);
+ tick_cb ();
new_game ();
}
@@ -180,19 +174,27 @@ public class Gnotravex : Gtk.Application
var size = settings.get_int (KEY_GRID_SIZE);
highscores.set_category (scorecats[size - 2].key);
puzzle = new Puzzle (size);
+ puzzle.tick.connect (tick_cb);
puzzle.solved.connect (solved_cb);
view.puzzle = puzzle;
pause.change_state (false);
- clock.reset ();
- clock.start ();
}
- private void solved_cb (Puzzle puzzle)
+ private void tick_cb ()
{
- clock.stop ();
+ var elapsed = 0;
+ if (puzzle != null)
+ elapsed = (int) (puzzle.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));
+ }
- var seconds = clock.get_seconds ();
+ private void solved_cb (Puzzle puzzle)
+ {
+ var seconds = (int) (puzzle.elapsed + 0.5);
var pos = highscores.add_time_score ((seconds / 60) * 1.0 + (seconds % 60) / 100.0);
var scores_dialog = new GnomeGamesSupport.ScoresDialog (window, highscores, _("Tetravex Scores"));
@@ -246,19 +248,12 @@ public class Gnotravex : Gtk.Application
private void solve_cb ()
{
puzzle.solve ();
- clock.stop ();
}
private void pause_cb ()
{
solve.set_enabled (!pause_action.get_is_paused ());
- view.is_paused = pause_action.get_is_paused ();
-
- if (pause_action.get_is_paused ())
- clock.stop ();
- else
- clock.start ();
-
+ puzzle.paused = pause_action.get_is_paused ();
pause.set_state (pause_action.get_is_paused ());
}
diff --git a/gnotravex/src/puzzle-view.vala b/gnotravex/src/puzzle-view.vala
index ab4389c..3e9bfaa 100644
--- a/gnotravex/src/puzzle-view.vala
+++ b/gnotravex/src/puzzle-view.vala
@@ -57,6 +57,7 @@ public class PuzzleView : Gtk.DrawingArea
}
}
_puzzle.tile_moved.connect (tile_moved_cb);
+ _puzzle.paused_changed.connect (() => { queue_draw (); });
queue_resize ();
}
}
@@ -81,17 +82,6 @@ public class PuzzleView : Gtk.DrawingArea
private Timer animation_timer;
private uint animation_timeout = 0;
- private bool _is_paused = false;
- public bool is_paused
- {
- get { return _is_paused; }
- set
- {
- _is_paused = value;
- queue_draw ();
- }
- }
-
public PuzzleView ()
{
set_events (Gdk.EventMask.EXPOSURE_MASK | Gdk.EventMask.BUTTON_PRESS_MASK | Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_RELEASE_MASK);
@@ -296,7 +286,7 @@ public class PuzzleView : Gtk.DrawingArea
context.save ();
context.translate ((int) (image.x + 0.5), (int) (image.y + 0.5));
- if (is_paused)
+ if (puzzle.paused)
theme.draw_paused_tile (context, size);
else
theme.draw_tile (context, size, tile);
@@ -317,7 +307,7 @@ public class PuzzleView : Gtk.DrawingArea
context.save ();
context.translate ((int) (image.x + 0.5), (int) (image.y + 0.5));
- if (is_paused)
+ if (puzzle.paused)
theme.draw_paused_tile (context, size);
else
theme.draw_tile (context, size, tile);
@@ -325,7 +315,7 @@ public class PuzzleView : Gtk.DrawingArea
}
/* Draw pause overlay */
- if (is_paused)
+ if (puzzle.paused)
{
context.set_source_rgba (0, 0, 0, 0.75);
context.paint ();
@@ -425,7 +415,7 @@ public class PuzzleView : Gtk.DrawingArea
public override bool button_press_event (Gdk.EventButton event)
{
- if (is_paused)
+ if (puzzle.paused)
return false;
/* Ignore double click events */
diff --git a/gnotravex/src/puzzle.vala b/gnotravex/src/puzzle.vala
index b5ebb4d..be7d2bb 100644
--- a/gnotravex/src/puzzle.vala
+++ b/gnotravex/src/puzzle.vala
@@ -25,9 +25,44 @@ public class Puzzle
get { return _size; }
}
private Tile[,] board;
+
+ /* Game timer */
+ private double clock_elapsed;
+ private Timer? clock;
+ private uint clock_timeout;
+
+ 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; }
+ }
public signal void tile_moved (Tile tile, uint x, uint y);
public signal void solved ();
+ public signal void paused_changed ();
+ public signal void tick ();
public bool is_solved
{
@@ -161,7 +196,9 @@ public class Puzzle
public void switch_tiles (uint x0, uint y0, uint x1, uint y1)
{
if (x0 == x1 && y0 == y1)
- return;
+ return;
+
+ start_clock ();
var t0 = board[x0, y0];
var t1 = board[x1, y1];
@@ -174,7 +211,10 @@ public class Puzzle
tile_moved (t1, x0, y0);
if (is_solved)
+ {
+ stop_clock ();
solved ();
+ }
}
public bool can_move_up
@@ -277,4 +317,44 @@ public class Puzzle
tile_moved (tile, tile.x, tile.y);
}
}
+
+ 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;
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]