TGUI  v0.6.10
Callback.hpp
1 //
3 // TGUI - Texus's Graphical User Interface
4 // Copyright (C) 2012-2015 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_CALLBACK_HPP
27 #define TGUI_CALLBACK_HPP
28 
29 #include <map>
30 #include <list>
31 #include <functional>
32 
33 #include <TGUI/Global.hpp>
34 #include <TGUI/SharedWidgetPtr.hpp>
35 
37 
38 namespace tgui
39 {
40  class Widget;
41 
43  // Struct that tells more about the callback that happened.
45  struct TGUI_API Callback
46  {
47  Callback() : widget(nullptr) {};
48 
49  // The callback id that was passed to the widget. It is used to identify from what widget the callback came from.
50  unsigned int id;
51 
52  // How did the callbak occur?
53  unsigned int trigger;
54 
55  // Pointer to the widget
56  Widget* widget;
57 
58  // The type of the widget
59  WidgetTypes widgetType;
60 
61  // When the mouse has something to do with the callback then this data will be filled
62  sf::Vector2i mouse;
63 
64  // This text is only used by some widgets, but it can be set together with some other member
65  sf::String text;
66 
67  // Only one of these things can be filled at a given time
68  bool checked;
69  int value;
70  sf::Vector2f value2d;
71  sf::Vector2f position;
72  sf::Vector2f size;
73  unsigned int index;
74  };
75 
76 
78  // Used internally by all widgets to handle callbacks.
80  class TGUI_API CallbackManager
81  {
82  public:
83 
98  void bindCallback(std::function<void()> func, unsigned int trigger);
99 
100 
117  template <typename T>
118  void bindCallback(void (T::*func)(), T* const classPtr, unsigned int trigger)
119  {
120  mapCallback(std::bind(func, classPtr), trigger);
121  }
122 
123 
137  void bindCallbackEx(std::function<void(const Callback&)> func, unsigned int trigger);
138 
139 
156  template <typename T>
157  void bindCallbackEx(void (T::*func)(const Callback&), T* const classPtr, unsigned int trigger)
158  {
159  mapCallback(std::bind(func, classPtr, std::ref(m_Callback)), trigger);
160  }
161 
162 
177  void bindCallback(unsigned int trigger);
178 
179 
186  void unbindCallback(unsigned int trigger);
187 
188 
195  void unbindAllCallback();
196 
197 
199  protected:
200 
202  // Map the callback function to the needed trigger(s).
204  void mapCallback(const std::function<void()>& function, unsigned int trigger);
205 
206 
208  protected:
209 
210  std::map<unsigned int, std::list<std::function<void()>>> m_CallbackFunctions;
211 
212  Callback m_Callback;
213 
214 
215  template <class T>
216  friend class SharedWidgetPtr;
217 
219  };
220 
221 
223 }
224 
226 
227 #endif // TGUI_CALLBACK_HPP
228 
Namespace that contains all TGUI functions and classes.
Definition: AnimatedPicture.hpp:33
WidgetTypes
A list of all widget types.
Definition: Global.hpp:80
void bindCallbackEx(void(T::*func)(const Callback &), T *const classPtr, unsigned int trigger)
Bind a function to one or more specific callback trigger(s).
Definition: Callback.hpp:157
The parent class for every widget.
Definition: Widget.hpp:45
Definition: Callback.hpp:45
Definition: Callback.hpp:80
void bindCallback(void(T::*func)(), T *const classPtr, unsigned int trigger)
Bind a function to one or more specific callback trigger(s).
Definition: Callback.hpp:118
Definition: SharedWidgetPtr.hpp:44