TGUI  0.10-dev
Timer.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25
26#ifndef TGUI_TIMER_HPP
27#define TGUI_TIMER_HPP
28
29#include <TGUI/Config.hpp>
30#include <TGUI/Duration.hpp>
31#include <TGUI/Optional.hpp>
32
33#include <functional>
34#include <memory>
35#include <vector>
36
38
39namespace tgui
40{
50 class TGUI_API Timer : public std::enable_shared_from_this<Timer>
51 {
52 public:
53
54 typedef std::shared_ptr<Timer> Ptr; // Only provided for potential consistence in user code
55
56 Timer(const Timer&) = delete;
57 Timer& operator=(const Timer&) = delete;
58
59
69 static std::shared_ptr<Timer> create(const std::function<void()>& callback, Duration interval, bool enable = true);
70
71
81 static std::shared_ptr<Timer> create(const std::function<void(std::shared_ptr<Timer>)>& callback, Duration interval, bool enable = true);
82
83
93 static void scheduleCallback(std::function<void()> callback, Duration interval = Duration());
94
95
101 void setInterval(Duration interval);
102
103
109
110
116 void setEnabled(bool enabled);
117
118
123 bool isEnabled() const;
124
125
131 void setCallback(const std::function<void()>& callback);
132
133
139 void setCallback(const std::function<void(std::shared_ptr<Timer>)>& callback);
140
141
145 void restart();
146
147
154 static bool updateTime(Duration elapsedTime);
155
156
162 static Optional<Duration> getNextScheduledTime();
163
164
169 static void clearTimers();
170
171
173 protected:
174
176 // Default constructor, used by static create and scheduleCallback functions
178 Timer() = default;
179
180
182 private:
183
184 static std::vector<std::shared_ptr<Timer>> m_activeTimers;
185
186 bool m_repeats = false;
187 bool m_enabled = false;
188 Duration m_interval;
189 std::function<void()> m_callback;
190
191 Duration m_remainingDuration;
192
193
195 };
196
197
199
200}
201
203
204#endif // TGUI_TIMER_HPP
Wrapper for durations.
Definition: Duration.hpp:52
Executes callbacks after a certain amount of time.
Definition: Timer.hpp:51
bool isEnabled() const
Returns whether the timer is running.
Duration getInterval() const
Returns the interval at which a timer callback is send.
static std::shared_ptr< Timer > create(const std::function< void()> &callback, Duration interval, bool enable=true)
Sets the time that the mouse has to stand still before the tooltip becomes visible.
void setCallback(const std::function< void(std::shared_ptr< Timer >)> &callback)
Changes the callback function that should be called by the timer at each interval.
void restart()
Restarts the timer.
void setInterval(Duration interval)
Changes the interval at which a timer callback is send.
void setEnabled(bool enabled)
Starts or stops a timer.
static void scheduleCallback(std::function< void()> callback, Duration interval=Duration())
Starts a timer.
void setCallback(const std::function< void()> &callback)
Changes the callback function that should be called by the timer at each interval.
static std::shared_ptr< Timer > create(const std::function< void(std::shared_ptr< Timer >)> &callback, Duration interval, bool enable=true)
Sets the time that the mouse has to stand still before the tooltip becomes visible.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36