25#ifndef TGUI_SIGNAL_HPP
26#define TGUI_SIGNAL_HPP
30#include <TGUI/Global.hpp>
31#include <TGUI/String.hpp>
32#include <TGUI/Color.hpp>
33#include <TGUI/Vector2.hpp>
34#include <TGUI/Animation.hpp>
35#include <TGUI/Filesystem.hpp>
37#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
38 #include <unordered_map>
39 #include <type_traits>
51TGUI_MODULE_EXPORT
namespace tgui
76 m_name{std::move(name)}
78 if (1 + extraParameters > m_parameters.size())
79 m_parameters.resize(1 + extraParameters);
110 template <
typename Func,
typename... BoundArgs>
111 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
113 return connect(func, args...);
124 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
125 unsigned int connect(
const Func& func,
const BoundArgs&... args)
127 const auto id = ++m_lastSignalId;
128#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
129 if constexpr(
sizeof...(BoundArgs) == 0)
130 m_handlers[
id] = func;
134 m_handlers[id] = [=]{ invokeFunc(func, args...); };
148 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::shared_ptr<Widget>&,
const String&)>>::value>* =
nullptr>
149 unsigned int connectEx(
const Func& func,
const BoundArgs&... args)
152 return connect([func, name=m_name, args...](){ invokeFunc(func, args..., getWidget(), name); });
225 template <
typename Type>
228 return *
static_cast<const std::decay_t<Type>*
>(m_parameters[paramIndex]);
231#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
232 template <
typename Func,
typename... Args>
233 static void invokeFunc(Func&& func, Args&&... args)
235 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
239 template <
typename Func,
typename... Args,
typename std::enable_if_t<std::is_member_pointer<std::decay_t<Func>>::value>* =
nullptr>
240 static void invokeFunc(Func&& func, Args&&... args)
242 (std::mem_fn(func))(std::forward<Args>(args)...);
245 template <
typename Func,
typename... Args,
typename std::enable_if_t<!std::is_member_pointer<std::decay_t<Func>>::value>* =
nullptr>
246 static void invokeFunc(Func&& func, Args&&... args)
248 std::forward<Func>(func)(std::forward<Args>(args)...);
255 bool m_enabled =
true;
257 std::unordered_map<
unsigned int, std::function<void()>> m_handlers;
259 static unsigned int m_lastSignalId;
260 static std::deque<const void*> m_parameters;
269 template <
typename T>
278 Signal{std::move(name), 1}
290 template <
typename Func,
typename... BoundArgs>
291 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
304 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
305 unsigned int connect(
const Func& func,
const BoundArgs&... args)
318 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&..., T)>>::value>* =
nullptr>
319 unsigned int connect(
const Func& func,
const BoundArgs&... args)
334 if (m_handlers.empty())
337 m_parameters[1] =
static_cast<const void*
>(¶m);
348 template <
typename T1,
typename T2>
357 Signal{std::move(name), 2}
369 template <
typename Func,
typename... BoundArgs>
370 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
383 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
384 unsigned int connect(
const Func& func,
const BoundArgs&... args)
397 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&..., T1, T2)>>::value>* =
nullptr>
398 unsigned int connect(
const Func& func,
const BoundArgs&... args)
414 if (m_handlers.empty())
417 m_parameters[1] =
static_cast<const void*
>(¶m1);
418 m_parameters[2] =
static_cast<const void*
>(¶m2);
447 Signal{std::move(name), 1}
459 template <
typename Func,
typename... BoundArgs>
460 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
462 return connect(func, args...);
473 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
474 unsigned int connect(
const Func& func,
const BoundArgs&... args)
476 return Signal::connect(func, args...);
487 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::shared_ptr<ChildWindow>&)>>::value>* =
nullptr>
488 unsigned int connect(
const Func& func,
const BoundArgs&... args)
490 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceChildWindow()); });
508 static std::shared_ptr<ChildWindow> dereferenceChildWindow();
529 Signal{std::move(name), 3}
541 template <
typename Func,
typename... BoundArgs>
542 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
544 return connect(func, args...);
555 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
556 unsigned int connect(
const Func& func,
const BoundArgs&... args)
558 return Signal::connect(func, args...);
569 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
int)>>::value>* =
nullptr>
570 unsigned int connect(
const Func& func,
const BoundArgs&... args)
572 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<int>(1)); });
583 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
584 unsigned int connect(
const Func& func,
const BoundArgs&... args)
586 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(2)); });
597 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&,
const String&)>>::value>* =
nullptr>
598 unsigned int connect(
const Func& func,
const BoundArgs&... args)
600 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(2), dereferenceParam<String>(3)); });
636 Signal{std::move(name), 3}
647 template<
typename Func,
typename... BoundArgs>
648 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
650 return connect(func, args...);
661 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
662 unsigned int connect(
const Func& func,
const BoundArgs&... args)
664 return Signal::connect(func, args...);
675 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
int)>>::value>* =
nullptr>
676 unsigned int connect(
const Func& func,
const BoundArgs&... args)
678 return Signal::connect([=] { invokeFunc(func, args..., dereferenceParam<int>(1)); });
689 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::shared_ptr<Panel>&)>>::value>* =
nullptr>
690 unsigned int connect(
const Func& func,
const BoundArgs&... args)
692 return Signal::connect([=] { invokeFunc(func, args..., dereferencePanel()); });
703 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
704 unsigned int connect(
const Func& func,
const BoundArgs&... args)
706 return Signal::connect([=] { invokeFunc(func, args..., dereferenceParam<String>(3)); });
717 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
int,
const std::shared_ptr<Panel>&)>>::value>* =
nullptr>
718 unsigned int connect(
const Func& func,
const BoundArgs&... args)
720 return Signal::connect([=] { invokeFunc(func, args..., dereferenceParam<int>(1), dereferencePanel()); });
731 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::shared_ptr<Panel>&,
const String&)>>::value>* =
nullptr>
732 unsigned int connect(
const Func& func,
const BoundArgs&... args)
734 return Signal::connect([=] { invokeFunc(func, args..., dereferencePanel(), dereferenceParam<String>(3)); });
745 template<
typename Func,
typename... BoundArgs, std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
int,
const std::shared_ptr<Panel>&,
const String&)>>::value>* =
nullptr>
746 unsigned int connect(
const Func& func,
const BoundArgs&... args)
748 return Signal::connect([=] { invokeFunc(func, args..., dereferenceParam<int>(1), dereferencePanel(), dereferenceParam<String>(3)); });
761 bool emit(
const Widget* widget,
int index,
const std::shared_ptr<Panel>& panel,
const String&
id);
769 static std::shared_ptr<Panel> dereferencePanel();
790 Signal{std::move(name), 3}
802 template <
typename Func,
typename... BoundArgs>
803 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
805 return connect(func, args...);
816 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
817 unsigned int connect(
const Func& func,
const BoundArgs&... args)
819 return Signal::connect(func, args...);
830 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
831 unsigned int connect(
const Func& func,
const BoundArgs&... args)
833 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(1)); });
844 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const Filesystem::Path&)>>::value>* =
nullptr>
845 unsigned int connect(
const Func& func,
const BoundArgs&... args)
847 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<Filesystem::Path>(2)); });
858 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::vector<Filesystem::Path>&)>>::value>* =
nullptr>
859 unsigned int connect(
const Func& func,
const BoundArgs&... args)
861 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<std::vector<Filesystem::Path>>(3)); });
872 bool emit(
const Widget* widget,
const std::vector<Filesystem::Path>& paths);
891 Signal{std::move(name), 2}
903 template <
typename Func,
typename... BoundArgs>
904 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
906 return connect(func, args...);
917 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
918 unsigned int connect(
const Func& func,
const BoundArgs&... args)
920 return Signal::connect(func, args...);
931 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
ShowEffectType)>>::value>* =
nullptr>
932 unsigned int connect(
const Func& func,
const BoundArgs&... args)
934 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<ShowEffectType>(1)); });
945 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
bool)>>::value>* =
nullptr>
946 unsigned int connect(
const Func& func,
const BoundArgs&... args)
948 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<bool>(2)); });
959 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
ShowEffectType,
bool)>>::value>* =
nullptr>
960 unsigned int connect(
const Func& func,
const BoundArgs&... args)
962 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<ShowEffectType>(1), dereferenceParam<bool>(2)); });
993 Signal{std::move(name), 1}
1005 template <
typename Func,
typename... BoundArgs>
1006 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
1008 return connect(func, args...);
1019 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
1020 unsigned int connect(
const Func& func,
const BoundArgs&... args)
1022 return Signal::connect(func, args...);
1033 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
AnimationType)>>::value>* =
nullptr>
1034 unsigned int connect(
const Func& func,
const BoundArgs&... args)
1036 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<AnimationType>(1)); });
1067 Signal{std::move(name), 2}
1079 template <
typename Func,
typename... BoundArgs>
1080 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
1082 return connect(func, args...);
1093 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
1094 unsigned int connect(
const Func& func,
const BoundArgs&... args)
1096 return Signal::connect(func, args...);
1107 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
1108 unsigned int connect(
const Func& func,
const BoundArgs&... args)
1110 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(1)); });
1121 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const std::vector<String>&)>>::value>* =
nullptr>
1122 unsigned int connect(
const Func& func,
const BoundArgs&... args)
1124 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<std::vector<String>>(2)); });
Child window widget.
Definition ChildWindow.hpp:45
Object to represent paths on a filesystem.
Definition Filesystem.hpp:58
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:986
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:1020
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:1006
SignalAnimationType(String &&name)
Constructor.
Definition Signal.hpp:992
bool emit(const Widget *widget, AnimationType type)
Call all connected signal handlers.
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:440
SignalChildWindow(String &&name)
Constructor.
Definition Signal.hpp:446
bool emit(ChildWindow *param)
Call all connected signal handlers.
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:488
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:460
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:474
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:783
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:803
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:817
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:859
bool emit(const Widget *widget, const std::vector< Filesystem::Path > &paths)
Call all connected signal handlers.
SignalFileDialogPaths(String &&name)
Constructor.
Definition Signal.hpp:789
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:1060
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:1122
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:1094
SignalItemHierarchy(String &&name)
Constructor.
Definition Signal.hpp:1066
bool emit(const Widget *widget, const String &item, const std::vector< String > &fullItem)
Call all connected signal handlers.
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:1080
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:522
bool emit(const Widget *widget, int index, const String &item, const String &id)
Call all connected signal handlers.
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:542
SignalItem(String &&name)
Constructor.
Definition Signal.hpp:528
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:556
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:630
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:690
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:662
SignalPanelListBoxItem(String &&name)
Constructor.
Definition Signal.hpp:635
bool emit(const Widget *widget, int index, const std::shared_ptr< Panel > &panel, const String &id)
Call all connected signal handlers.
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:648
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:884
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:918
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:904
bool emit(const Widget *widget, ShowEffectType type, bool visible)
Call all connected signal handlers.
SignalShowEffect(String &&name)
Constructor.
Definition Signal.hpp:890
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:350
bool emit(const Widget *widget, T1 param1, T2 param2)
Call all connected signal handlers.
Definition Signal.hpp:412
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:384
SignalTyped2(String &&name)
Constructor.
Definition Signal.hpp:356
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:370
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:271
SignalTyped(String &&name)
Constructor.
Definition Signal.hpp:277
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:291
bool emit(const Widget *widget, T param)
Call all connected signal handlers.
Definition Signal.hpp:332
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:305
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:61
bool emit(const Widget *widget)
Call all connected signal handlers.
Signal(const Signal &other)
Copy constructor which will not copy the signal handlers.
static const std::decay_t< Type > & dereferenceParam(std::size_t paramIndex)
Turns the void* parameters back into its original type right before calling the callback function.
Definition Signal.hpp:226
unsigned int operator()(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:111
bool isEnabled() const
Returns whether this signal calls the connected functions when triggered.
Definition Signal.hpp:209
void disconnectAll()
Disconnect all signal handler from this signal.
void setEnabled(bool enabled)
Changes whether this signal calls the connected functions when triggered.
Definition Signal.hpp:196
bool disconnect(unsigned int id)
Disconnect a signal handler from this signal.
Signal & operator=(const Signal &other)
Copy assignment operator which will not copy the signal handlers.
String getName() const
Returns the name given to the signal.
Definition Signal.hpp:183
unsigned int connect(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:125
virtual ~Signal()=default
Default destructor.
Signal(String &&name, std::size_t extraParameters=0)
Constructor.
Definition Signal.hpp:75
Signal & operator=(Signal &&other) noexcept=default
Default move assignment operator.
Signal(Signal &&other) noexcept=default
Default move constructor.
static std::shared_ptr< Widget > getWidget()
Extracts the widget stored in the first parameter.
unsigned int connectEx(const Func &func, const BoundArgs &... args)
Connects a signal handler that will be called when this signal is emitted.
Definition Signal.hpp:149
Wrapper class to store strings.
Definition String.hpp:96
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
ShowEffectType
Type of effect to show/hide widget.
Definition Animation.hpp:46
AnimationType
Type of animation.
Definition Animation.hpp:64