[gnome-games/glchess-vala] Start work on a chess clock



commit 14c6dc8d60a88e2c99d8bd2b7e54db710c4450c2
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Jan 7 09:10:29 2011 +1100

    Start work on a chess clock

 glchess/src/Makefile.am      |    1 +
 glchess/src/chess-clock.vala |   77 ++++++++++++++++++++++++++++++++++++++++++
 glchess/src/glchess.vala     |    6 ++--
 3 files changed, 81 insertions(+), 3 deletions(-)
---
diff --git a/glchess/src/Makefile.am b/glchess/src/Makefile.am
index a2d8315..2d06d92 100644
--- a/glchess/src/Makefile.am
+++ b/glchess/src/Makefile.am
@@ -12,6 +12,7 @@ glchess_SOURCES = \
 	chess-engine-uci.vala \
 	chess-game.vala \
 	chess-pgn.vala \
+	chess-clock.vala \
 	chess-view.vala \
 	chess-view-2d.vala \
 	chess-view-3d.vala \
diff --git a/glchess/src/chess-clock.vala b/glchess/src/chess-clock.vala
new file mode 100644
index 0000000..4704eae
--- /dev/null
+++ b/glchess/src/chess-clock.vala
@@ -0,0 +1,77 @@
+public class ChessClock : Object
+{
+    private Timer timer;
+    private uint white_duration;
+    private uint black_duration;
+    private uint white_used;
+    private uint black_used;
+    private Color active_color = Color.WHITE;
+    private uint timeout = 0;
+
+    public signal void tick ();
+    public signal void expired ();
+
+    public ChessClock (uint white_duration, uint black_duration, uint white_used = 0, uint black_used = 0)
+    {
+        this.white_duration = white_duration;
+        this.black_duration = black_duration;
+        this.white_used = white_used;
+        this.black_used = black_used;
+        timer = new Timer ();
+    }
+
+    public void start ()
+    {
+        if (timeout != 0)
+            return;
+
+        uint remaining;
+        if (active_color == Color.WHITE)
+        {
+            if (white_used > white_duration)
+                white_used = white_duration;
+            remaining = white_duration - white_used;
+        }
+        else
+        {
+            if (black_used > black_duration)
+                black_used = black_duration;
+            remaining = black_duration - black_used;
+        }
+        timer.start ();
+
+        timeout = Timeout.add (remaining, timer_expired_cb);
+    }
+
+    private bool timer_expired_cb ()
+    {
+        timeout = 0;
+        expired ();
+        return false;
+    }
+
+    public void stop ()
+    {
+        if (timeout == 0)
+            return;
+        timer.stop ();
+        Source.remove (timeout);
+        timeout = 0;
+
+        var elapsed = (uint) (timer.elapsed () * 1000000);
+        if (active_color == Color.WHITE)
+            white_used += elapsed;
+        else
+            black_used += elapsed;
+    }
+
+    public void set_color (Color color)
+    {
+        if (color == active_color)
+            return;
+
+        active_color = color;
+        stop ();
+        start ();
+    }
+}
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index 20d40b1..b9c0579 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -103,7 +103,7 @@ public class Application
     public void quit ()
     {
         if (save_duration_timeout != 0)
-            save_duration ();
+            save_duration_cb ();
 
         /* Autosave */
         // FIXME: Only once a human has moved
@@ -897,7 +897,7 @@ public class Application
         return magnitude * multiplier;
     }
 
-    private bool save_duration ()
+    private bool save_duration_cb ()
     {
         settings.set_int ("duration", get_duration ());
         Source.remove (save_duration_timeout);
@@ -911,7 +911,7 @@ public class Application
         if (save_duration_timeout != 0)
             Source.remove (save_duration_timeout);
         /* Do this delayed as it might change a lot being connected to a spin button */
-        save_duration_timeout = Timeout.add (100, save_duration);
+        save_duration_timeout = Timeout.add (100, save_duration_cb);
     }
 
     [CCode (cname = "G_MODULE_EXPORT duration_combo_changed_cb", instance_pos = -1)]



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