~ubuntu-branches/ubuntu/precise/gnome-games/precise-proposed

« back to all changes in this revision

Viewing changes to glchess/src/chess-clock.vala

  • Committer: Package Import Robot
  • Author(s): Rodrigo Moya
  • Date: 2011-05-30 13:32:04 UTC
  • mfrom: (1.3.4)
  • mto: (163.1.3 precise)
  • mto: This revision was merged to the branch mainline in revision 143.
  • Revision ID: package-import@ubuntu.com-20110530133204-celaq1v1dsxc48q1
Tags: upstream-3.0.2
ImportĀ upstreamĀ versionĀ 3.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
public class ChessClock : Object
 
2
{
 
3
    private uint _white_duration;
 
4
    public uint white_duration
 
5
    {
 
6
        get { return _white_duration; }
 
7
    }
 
8
    private uint _black_duration;
 
9
    public uint black_duration
 
10
    {
 
11
        get { return _black_duration; }
 
12
    }
 
13
 
 
14
    private uint _white_used;
 
15
    public uint white_used
 
16
    {
 
17
        get
 
18
        {
 
19
            if (active_color == Color.WHITE)
 
20
                return _white_used + (uint) (timer.elapsed () * 1000);
 
21
            else
 
22
                return _white_used;
 
23
        }
 
24
    }
 
25
 
 
26
    public uint white_used_in_seconds
 
27
    {
 
28
        get { return (white_used + 500) / 1000; }
 
29
    }
 
30
 
 
31
    private uint _black_used;
 
32
    public uint black_used
 
33
    {
 
34
        get
 
35
        {
 
36
            if (active_color == Color.WHITE)
 
37
                return _black_used;
 
38
            else
 
39
                return _black_used + (uint) (timer.elapsed () * 1000);
 
40
        }
 
41
    }
 
42
 
 
43
    public uint black_used_in_seconds
 
44
    {
 
45
        get { return (black_used + 500) / 1000; }
 
46
    }
 
47
 
 
48
    public Color _active_color = Color.WHITE;
 
49
    public Color active_color
 
50
    {
 
51
        get { return _active_color; }
 
52
        set
 
53
        {
 
54
            if (value == active_color)
 
55
                return;
 
56
 
 
57
            stop ();
 
58
            _active_color = value;
 
59
            start ();
 
60
        }
 
61
    }
 
62
 
 
63
    private Timer timer;
 
64
    private uint expire_timeout = 0;
 
65
    private uint tick_timeout = 0;
 
66
 
 
67
    public signal void tick ();
 
68
    public signal void expired ();
 
69
 
 
70
    public ChessClock (uint white_duration, uint black_duration, uint white_used = 0, uint black_used = 0)
 
71
    {
 
72
        _white_duration = white_duration * 1000;
 
73
        _black_duration = black_duration * 1000;
 
74
        _white_used = white_used;
 
75
        _black_used = black_used;
 
76
        timer = new Timer ();
 
77
    }
 
78
    
 
79
    private bool is_started
 
80
    {
 
81
        get { return expire_timeout != 0; }
 
82
    }
 
83
    
 
84
    public void start ()
 
85
    {
 
86
        if (is_started)
 
87
            return;
 
88
 
 
89
        /* Start stopwatch */
 
90
        timer.start ();
 
91
 
 
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);
 
95
        else
 
96
            expire_timeout = Timeout.add (black_duration - _black_used, timer_expired_cb);
 
97
 
 
98
        /* Wake up each second */
 
99
        tick_cb ();
 
100
    }
 
101
 
 
102
    private bool timer_expired_cb ()
 
103
    {
 
104
        stop ();
 
105
        expired ();
 
106
        return false;
 
107
    }
 
108
 
 
109
    private bool tick_cb ()
 
110
    {
 
111
        if (tick_timeout != 0)
 
112
            tick ();
 
113
 
 
114
        uint elapsed = (uint) (timer.elapsed () * 1000);
 
115
        uint used;
 
116
        if (active_color == Color.WHITE)
 
117
            used = _white_used + elapsed;
 
118
        else
 
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);
 
122
 
 
123
        return false;
 
124
    }
 
125
 
 
126
    public void stop ()
 
127
    {
 
128
        if (!is_started)
 
129
            return;
 
130
 
 
131
        timer.stop ();
 
132
        Source.remove (expire_timeout);
 
133
        expire_timeout = 0;
 
134
        Source.remove (tick_timeout);
 
135
        tick_timeout = 0;
 
136
 
 
137
        var elapsed = (uint) (timer.elapsed () * 1000);
 
138
        if (active_color == Color.WHITE)
 
139
        {
 
140
            _white_used += elapsed;
 
141
            if (_white_used > white_duration)
 
142
                _white_used = white_duration;
 
143
        }
 
144
        else
 
145
        {
 
146
            _black_used += elapsed;
 
147
            if (_black_used > black_duration)
 
148
                _black_used = black_duration;
 
149
        }
 
150
 
 
151
        timer.reset ();
 
152
    }
 
153
}