summaryrefslogtreecommitdiff
path: root/src/timer.cpp
blob: 204522008b5d1bcfde98b8adee0d6b0ec3a215b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <core/timer.h>
#include <compscreen.h>
#include "privatescreen.h"

CompTimer::CompTimer () :
    mActive (false),
    mMinTime (0),
    mMaxTime (0),
    mMinLeft (0),
    mMaxLeft (0),
    mCallBack (NULL)
{
}

CompTimer::~CompTimer ()
{
    if (mActive)
	screen->priv->removeTimer (this);
}

void
CompTimer::setTimes (unsigned int min, unsigned int max)
{
    bool wasActive = mActive;
    if (mActive)
	stop ();
    mMinTime = min;
    mMaxTime = (min <= max)? max : min;

    if (wasActive)
	start ();
}
	
void
CompTimer::setCallback (CompTimer::CallBack callback)
{
    bool wasActive = mActive;
    if (mActive)
	stop ();
    mCallBack = callback;

    if (wasActive)
	start ();
}

void
CompTimer::start ()
{
    stop ();
    mActive = true;
    screen->priv->addTimer (this);
}

void
CompTimer::start (unsigned int min, unsigned int max)
{
    stop ();
    setTimes (min, max);
    start ();
}

void
CompTimer::start (CompTimer::CallBack callback,
		  unsigned int min, unsigned int max)
{
    stop ();
    setTimes (min, max);
    setCallback (callback);
    start ();
}

void
CompTimer::stop ()
{
    mActive = false;
    screen->priv->removeTimer (this);
}

unsigned int
CompTimer::minTime ()
{
    return mMinTime;
}

unsigned int
CompTimer::maxTime ()
{
    return mMaxTime;
}

unsigned int
CompTimer::minLeft ()
{
    return (mMinLeft < 0)? 0 : mMinLeft;
}

unsigned int
CompTimer::maxLeft ()
{
    return (mMaxLeft < 0)? 0 : mMaxLeft;
}

bool
CompTimer::active ()
{
    return mActive;
}