1
public class ChessClock : Object
3
private uint _white_duration;
4
public uint white_duration
6
get { return _white_duration; }
8
private uint _black_duration;
9
public uint black_duration
11
get { return _black_duration; }
14
private uint _white_used;
15
public uint white_used
19
if (active_color == Color.WHITE)
20
return _white_used + (uint) (timer.elapsed () * 1000);
26
public uint white_used_in_seconds
28
get { return (white_used + 500) / 1000; }
31
private uint _black_used;
32
public uint black_used
36
if (active_color == Color.WHITE)
39
return _black_used + (uint) (timer.elapsed () * 1000);
43
public uint black_used_in_seconds
45
get { return (black_used + 500) / 1000; }
48
public Color _active_color = Color.WHITE;
49
public Color active_color
51
get { return _active_color; }
54
if (value == active_color)
58
_active_color = value;
64
private uint expire_timeout = 0;
65
private uint tick_timeout = 0;
67
public signal void tick ();
68
public signal void expired ();
70
public ChessClock (uint white_duration, uint black_duration, uint white_used = 0, uint black_used = 0)
72
_white_duration = white_duration * 1000;
73
_black_duration = black_duration * 1000;
74
_white_used = white_used;
75
_black_used = black_used;
79
private bool is_started
81
get { return expire_timeout != 0; }
92
/* Notify when this timer has expired */
93
if (active_color == Color.WHITE)
94
expire_timeout = Timeout.add (white_duration - _white_used, timer_expired_cb);
96
expire_timeout = Timeout.add (black_duration - _black_used, timer_expired_cb);
98
/* Wake up each second */
102
private bool timer_expired_cb ()
109
private bool tick_cb ()
111
if (tick_timeout != 0)
114
uint elapsed = (uint) (timer.elapsed () * 1000);
116
if (active_color == Color.WHITE)
117
used = _white_used + elapsed;
119
used = _black_used + elapsed;
120
var next_tick_time = ((used / 1000) + 1) * 1000;
121
tick_timeout = Timeout.add (next_tick_time - used, tick_cb);
132
Source.remove (expire_timeout);
134
Source.remove (tick_timeout);
137
var elapsed = (uint) (timer.elapsed () * 1000);
138
if (active_color == Color.WHITE)
140
_white_used += elapsed;
141
if (_white_used > white_duration)
142
_white_used = white_duration;
146
_black_used += elapsed;
147
if (_black_used > black_duration)
148
_black_used = black_duration;