TGUI  1.3-dev
Loading...
Searching...
No Matches
Timer.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
34 #include <functional>
35 #include <memory>
36 #include <vector>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
52 class TGUI_API Timer : public std::enable_shared_from_this<Timer>
53 {
54 public:
55
56 using Ptr = std::shared_ptr<Timer>; // Only provided for potential consistence in user code
57
58 Timer(const Timer&) = delete;
59 Timer& operator=(const Timer&) = delete;
60
61
71 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void()>& callback, Duration interval, bool enable = true);
72
73
83 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void(std::shared_ptr<Timer>)>& callback, Duration interval, bool enable = true);
84
85
95 static void scheduleCallback(const std::function<void()>& callback, Duration interval = Duration());
96
97
103 void setInterval(Duration interval);
104
105
110 TGUI_NODISCARD Duration getInterval() const;
111
112
118 void setEnabled(bool enabled);
119
120
125 TGUI_NODISCARD bool isEnabled() const;
126
127
133 void setCallback(const std::function<void()>& callback);
134
135
141 void setCallback(const std::function<void(std::shared_ptr<Timer>)>& callback);
142
143
147 void restart();
148
149
156 static bool updateTime(Duration elapsedTime);
157
158
164 TGUI_NODISCARD static Optional<Duration> getNextScheduledTime();
165
166
171 static void clearTimers();
172
173
175 protected:
176
178 // Default constructor, used by static create and scheduleCallback functions
180 Timer() = default;
181
182
184 private:
185
186 static std::vector<std::shared_ptr<Timer>> m_activeTimers;
187
188 bool m_repeats = false;
189 bool m_enabled = false;
190 Duration m_interval;
191 std::function<void()> m_callback;
192
193 Duration m_remainingDuration;
194
195
197 };
198
199
201
202}
203
205
206#endif // TGUI_TIMER_HPP
Wrapper for durations.
Definition Duration.hpp:56
Executes callbacks after a certain amount of time.
Definition Timer.hpp:53
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.
TGUI_NODISCARD Duration getInterval() const
Returns the interval at which a timer callback is send.
void setInterval(Duration interval)
Changes the interval at which a timer callback is send.
TGUI_NODISCARD bool isEnabled() const
Returns whether the timer is running.
void setEnabled(bool enabled)
Starts or stops a timer.
static void scheduleCallback(const std::function< void()> &callback, Duration interval=Duration())
Starts a timer.
static TGUI_NODISCARD std::shared_ptr< Timer > create(const std::function< void()> &callback, Duration interval, bool enable=true)
Creates a new timer.
void setCallback(const std::function< void()> &callback)
Changes the callback function that should be called by the timer at each interval.
static TGUI_NODISCARD std::shared_ptr< Timer > create(const std::function< void(std::shared_ptr< Timer >)> &callback, Duration interval, bool enable=true)
Creates a new timer.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39