26#ifndef TGUI_SIGNAL_HPP
27#define TGUI_SIGNAL_HPP
31#include <TGUI/Global.hpp>
32#include <TGUI/String.hpp>
33#include <TGUI/Color.hpp>
34#include <TGUI/Vector2.hpp>
35#include <TGUI/Animation.hpp>
36#include <TGUI/Filesystem.hpp>
74 m_name{std::move(name)}
76 if (1 + extraParameters > m_parameters.size())
77 m_parameters.resize(1 + extraParameters);
113 template <
typename Func,
typename... BoundArgs>
114 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
116 return connect(func, args...);
128 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
129 unsigned int connect(
const Func& func,
const BoundArgs&... args)
131 const auto id = ++m_lastSignalId;
132#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
133 if constexpr(
sizeof...(BoundArgs) == 0)
134 m_handlers[id] = func;
138 m_handlers[id] = [=]{ invokeFunc(func, args...); };
153 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&..., std::shared_ptr<Widget>,
const String&)>>::value>* =
nullptr>
154 unsigned int connectEx(
const Func& func,
const BoundArgs&... args)
157 return connect([func, name=m_name, args...](){ invokeFunc(func, args..., getWidget(), name); });
238 template <
typename Type>
241 return *
static_cast<const std::decay_t<Type>*
>(m_parameters[paramIndex]);
245#if defined(__cpp_lib_invoke) && (__cpp_lib_invoke >= 201411L)
246 template <
typename Func,
typename... Args>
247 static void invokeFunc(Func&& func, Args&&... args)
249 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
253 template <
typename Func,
typename... Args,
typename std::enable_if<std::is_member_pointer<typename std::decay<Func>::type>::value>::type* =
nullptr>
254 static void invokeFunc(Func&& func, Args&&... args)
256 std::mem_fn(func)(std::forward<Args>(args)...);
259 template <
typename Func,
typename... Args,
typename std::enable_if<!std::is_member_pointer<typename std::decay<Func>::type>::value>::type* =
nullptr>
260 static void invokeFunc(Func&& func, Args&&... args)
262 std::forward<Func>(func)(std::forward<Args>(args)...);
270 bool m_enabled =
true;
272 std::map<
unsigned int, std::function<void()>> m_handlers;
274 static unsigned int m_lastSignalId;
275 static std::deque<const void*> m_parameters;
285 template <
typename T>
294 Signal{std::move(name), 1}
307 template <
typename Func,
typename... BoundArgs>
308 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
322 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
323 unsigned int connect(
const Func& func,
const BoundArgs&... args)
337 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&..., T)>>::value>* =
nullptr>
338 unsigned int connect(
const Func& func,
const BoundArgs&... args)
340 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<T>(1)); });
354 if (m_handlers.empty())
357 m_parameters[1] =
static_cast<const void*
>(¶m);
369 template <
typename T1,
typename T2>
378 Signal{std::move(name), 2}
391 template <
typename Func,
typename... BoundArgs>
392 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
406 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
407 unsigned int connect(
const Func& func,
const BoundArgs&... args)
421 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&..., T1, T2)>>::value>* =
nullptr>
422 unsigned int connect(
const Func& func,
const BoundArgs&... args)
424 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<T1>(1), dereferenceParam<T2>(2)); });
439 if (m_handlers.empty())
442 m_parameters[1] =
static_cast<const void*
>(¶m1);
443 m_parameters[2] =
static_cast<const void*
>(¶m2);
474 Signal{std::move(name), 1}
487 template <
typename Func,
typename... BoundArgs>
488 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
490 return connect(func, args...);
502 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
503 unsigned int connect(
const Func& func,
const BoundArgs&... args)
505 return Signal::connect(func, args...);
517 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>
518 unsigned int connect(
const Func& func,
const BoundArgs&... args)
520 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceChildWindow()); });
540 static std::shared_ptr<ChildWindow> dereferenceChildWindow();
563 Signal{std::move(name), 3}
576 template <
typename Func,
typename... BoundArgs>
577 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
579 return connect(func, args...);
591 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
592 unsigned int connect(
const Func& func,
const BoundArgs&... args)
594 return Signal::connect(func, args...);
606 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
int)>>::value>* =
nullptr>
607 unsigned int connect(
const Func& func,
const BoundArgs&... args)
609 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<int>(1)); });
621 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
622 unsigned int connect(
const Func& func,
const BoundArgs&... args)
624 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(2)); });
636 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>
637 unsigned int connect(
const Func& func,
const BoundArgs&... args)
639 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(2), dereferenceParam<String>(3)); });
676 Signal{std::move(name), 2}
689 template <
typename Func,
typename... BoundArgs>
690 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
692 return connect(func, args...);
704 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
705 unsigned int connect(
const Func& func,
const BoundArgs&... args)
707 return Signal::connect(func, args...);
719 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
ShowEffectType)>>::value>* =
nullptr>
720 unsigned int connect(
const Func& func,
const BoundArgs&... args)
722 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<ShowEffectType>(1)); });
734 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
bool)>>::value>* =
nullptr>
735 unsigned int connect(
const Func& func,
const BoundArgs&... args)
737 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<bool>(2)); });
749 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
ShowEffectType,
bool)>>::value>* =
nullptr>
750 unsigned int connect(
const Func& func,
const BoundArgs&... args)
752 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<ShowEffectType>(1), dereferenceParam<bool>(2)); });
789 Signal{std::move(name), 2}
802 template <
typename Func,
typename... BoundArgs>
803 unsigned int operator()(
const Func& func,
const BoundArgs&... args)
805 return connect(func, args...);
817 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value>* =
nullptr>
818 unsigned int connect(
const Func& func,
const BoundArgs&... args)
820 return Signal::connect(func, args...);
832 template <
typename Func,
typename... BoundArgs,
typename std::enable_if_t<std::is_convertible<Func, std::function<void(
const BoundArgs&...,
const String&)>>::value>* =
nullptr>
833 unsigned int connect(
const Func& func,
const BoundArgs&... args)
835 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<String>(1)); });
847 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>
848 unsigned int connect(
const Func& func,
const BoundArgs&... args)
850 return Signal::connect([=]{ invokeFunc(func, args..., dereferenceParam<std::vector<String>>(2)); });
Child window widget.
Definition ChildWindow.hpp:44
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:467
SignalChildWindow(String &&name)
Constructor.
Definition Signal.hpp:473
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:518
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:488
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:503
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:782
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:848
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:818
SignalItemHierarchy(String &&name)
Constructor.
Definition Signal.hpp:788
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:803
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:556
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:577
SignalItem(String &&name)
Constructor.
Definition Signal.hpp:562
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:592
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:669
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:705
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:690
bool emit(const Widget *widget, ShowEffectType type, bool visible)
Call all connected signal handlers.
SignalShowEffect(String &&name)
Constructor.
Definition Signal.hpp:675
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:371
bool emit(const Widget *widget, T1 param1, T2 param2)
Call all connected signal handlers.
Definition Signal.hpp:437
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:407
SignalTyped2(String &&name)
Constructor.
Definition Signal.hpp:377
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:392
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:287
SignalTyped(String &&name)
Constructor.
Definition Signal.hpp:293
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:308
bool emit(const Widget *widget, T param)
Call all connected signal handlers.
Definition Signal.hpp:352
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:323
Signal to which the user can subscribe to get callbacks from.
Definition Signal.hpp:58
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:239
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:114
bool isEnabled() const
Returns whether this signal calls the connected functions when triggered.
Definition Signal.hpp:220
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:206
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:192
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:129
virtual ~Signal()=default
Default destructor.
Signal(String &&name, std::size_t extraParameters=0)
Constructor.
Definition Signal.hpp:73
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:154
Wrapper class to store strings.
Definition String.hpp:79
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
ShowEffectType
Type of animation to show/hide widget.
Definition Animation.hpp:44