TGUI 1.11
Loading...
Searching...
No Matches
Timer.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#include <functional>
33#include <memory>
34#include <vector>
35
37
38namespace tgui
39{
49 class TGUI_API Timer : public std::enable_shared_from_this<Timer>
50 {
51 public:
52
53 using Ptr = std::shared_ptr<Timer>; // Only provided for potential consistence in user code
54
55 Timer(const Timer&) = delete;
56 Timer& operator=(const Timer&) = delete;
57
67 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void()>& callback, Duration interval, bool enable = true);
68
78 TGUI_NODISCARD static std::shared_ptr<Timer> create(const std::function<void(std::shared_ptr<Timer>)>& callback, Duration interval, bool enable = true);
79
89 static void scheduleCallback(const std::function<void()>& callback, Duration interval = Duration());
90
96 void setInterval(Duration interval);
97
102 TGUI_NODISCARD Duration getInterval() const;
103
109 void setEnabled(bool enabled);
110
115 TGUI_NODISCARD bool isEnabled() const;
116
122 void setCallback(const std::function<void()>& callback);
123
129 void setCallback(const std::function<void(std::shared_ptr<Timer>)>& callback);
130
134 void restart();
135
142 static bool updateTime(Duration elapsedTime);
143
149 TGUI_NODISCARD static Optional<Duration> getNextScheduledTime();
150
155 static void clearTimers();
156
158 protected:
159
161 // Default constructor, used by static create and scheduleCallback functions
163 Timer() = default;
164
166 private:
167
168 static std::vector<std::shared_ptr<Timer>> m_activeTimers;
169
170 bool m_repeats = false;
171 bool m_enabled = false;
172 Duration m_interval;
173 std::function<void()> m_callback;
174
175 Duration m_remainingDuration;
176
178 };
179
181
182}
183
185
186#endif // TGUI_TIMER_HPP
Wrapper for durations.
Definition Duration.hpp:53
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:36