forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.cpp
162 lines (130 loc) · 3.15 KB
/
timer.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Aseprite UI Library
// Copyright (C) 2018-2022 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ui/timer.h"
#include "base/time.h"
#include "ui/manager.h"
#include "ui/message.h"
#include "ui/system.h"
#include "ui/widget.h"
#include <algorithm>
#include <limits>
#include <vector>
namespace ui {
typedef std::vector<Timer*> Timers;
static Timers timers; // Registered timers
static int running_timers = 0;
Timer::Timer(int interval, Widget* owner)
: m_owner(owner ? owner: Manager::getDefault())
, m_interval(interval)
, m_running(false)
, m_lastTick(0)
{
ASSERT(m_owner != nullptr);
assert_ui_thread();
timers.push_back(this);
}
Timer::~Timer()
{
assert_ui_thread();
auto it = std::find(timers.begin(), timers.end(), this);
ASSERT(it != timers.end());
if (it != timers.end())
timers.erase(it);
// Stop the timer and remove it from the message queue.
stop();
}
void Timer::start()
{
assert_ui_thread();
// Infinite timer? Do nothing.
if (m_interval == 0)
return;
m_lastTick = base::current_tick();
if (!m_running) {
m_running = true;
++running_timers;
}
}
void Timer::stop()
{
assert_ui_thread();
if (m_running) {
m_running = false;
--running_timers;
// Remove messages of this timer in the queue. The expected behavior
// is that when we stop a timer, we'll not receive more messages
// about it (even if there are enqueued messages waiting in the
// message queue).
Manager::getDefault()->removeMessagesForTimer(this);
}
}
void Timer::tick()
{
assert_ui_thread();
onTick();
}
void Timer::setInterval(int interval)
{
m_interval = interval;
}
void Timer::onTick()
{
// Fire Tick signal.
Tick();
}
void Timer::pollTimers()
{
assert_ui_thread();
// Generate messages for timers
if (running_timers != 0) {
ASSERT(!timers.empty());
base::tick_t t = base::current_tick();
for (auto timer : timers) {
if (timer && timer->isRunning()) {
ASSERT(timer->interval() > 0);
int64_t count = ((t - timer->m_lastTick) / timer->m_interval);
if (count > 0) {
timer->m_lastTick += count * timer->m_interval;
ASSERT(timer->m_owner != nullptr);
Message* msg = new TimerMessage(count, timer);
msg->setRecipient(timer->m_owner);
Manager::getDefault()->enqueueMessage(msg);
}
}
}
}
}
bool Timer::haveTimers()
{
return !timers.empty();
}
bool Timer::getNextTimeout(double& timeout)
{
if (running_timers == 0)
return false;
base::tick_t t = base::current_tick();
bool result = false;
timeout = std::numeric_limits<double>::max();
for (auto timer : timers) {
if (timer && timer->isRunning()) {
int64_t diff = (timer->m_lastTick + timer->m_interval) - t;
if (diff < 0) {
timeout = 0.0; // Right-now
return true;
}
else {
timeout = std::min<double>(timeout, diff / 1000.0);
result = true;
}
}
}
return result;
}
} // namespace ui