TGUI  1.6.1
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#ifndef TGUI_TIMER_HPP
26#define TGUI_TIMER_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/Duration.hpp>
30#include <TGUI/Optional.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <functional>
34 #include <memory>
35 #include <vector>
36#endif
37
39
40TGUI_MODULE_EXPORT namespace tgui
41{
51 class TGUI_API Timer : public std::enable_shared_from_this<Timer>
52 {
53 public:
54
55 using Ptr = std::shared_ptr<Timer>; // Only provided for potential consistence in user code
56
57 Timer(const Timer&) = delete;
58 Timer& operator=(const Timer&) = delete;
59
69 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void()>& callback, Duration interval, bool enable = true);
70
80 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void(std::shared_ptr<Timer>)>& callback, Duration interval, bool enable = true);
81
91 static void scheduleCallback(const std::function<void()>& callback, Duration interval = Duration());
92
98 void setInterval(Duration interval);
99
104 TGUI_NODISCARD Duration getInterval() const;
105
111 void setEnabled(bool enabled);
112
117 TGUI_NODISCARD bool isEnabled() const;
118
124 void setCallback(const std::function<void()>& callback);
125
131 void setCallback(const std::function<void(std::shared_ptr<Timer>)>& callback);
132
136 void restart();
137
144 static bool updateTime(Duration elapsedTime);
145
151 TGUI_NODISCARD static Optional<Duration> getNextScheduledTime();
152
157 static void clearTimers();
158
160 protected:
161
163 // Default constructor, used by static create and scheduleCallback functions
165 Timer() = default;
166
168 private:
169
170 static std::vector<std::shared_ptr<Timer>> m_activeTimers;
171
172 bool m_repeats = false;
173 bool m_enabled = false;
174 Duration m_interval;
175 std::function<void()> m_callback;
176
177 Duration m_remainingDuration;
178
180 };
181
183
184}
185
187
188#endif // TGUI_TIMER_HPP
Wrapper for durations.
Definition Duration.hpp:55
Executes callbacks after a certain amount of time.
Definition Timer.hpp:52
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)
Creates a new timer.
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(const 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)
Creates a new timer.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38