TDME2  1.9.200
GUI.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <unordered_set>
6 #include <vector>
7 
8 #include <tdme/tdme.h>
10 #include <tdme/engine/fwd-tdme.h>
15 #include <tdme/gui/fwd-tdme.h>
18 #include <tdme/gui/nodes/GUINode.h>
22 #include <tdme/utilities/Console.h>
24 #include <tdme/utilities/RTTI.h>
25 #include <tdme/utilities/Time.h>
26 
27 using std::string;
28 using std::to_string;
29 using std::unordered_map;
30 using std::unordered_set;
31 using std::vector;
32 
46 
47 template<typename T, typename U>
48 static T required_dynamic_cast(U u)
49 {
50  auto t = dynamic_cast<T>(u);
51  if (t == nullptr) {
52  Console::println("required_dynamic_cast: unable to perform required dynamic cast @\n\n" + RTTI::backtrace());
53  throw ExceptionBase("required_dynamic_cast did fail");
54 
55  }
56  return t;
57 }
58 
59 /**
60  * GUI module class
61  * @author Andreas Drewke
62  */
63 class tdme::gui::GUI final: public virtual InputEventHandler
64 {
66 
67 private:
69 
70  GUIRenderer* guiRenderer { nullptr };
71  Engine* engine { nullptr };
72  unordered_map<string, GUIScreenNode*> screens;
73  vector<GUIElementNode*> focusableNodes;
74  vector<GUIScreenNode*> focusableScreenNodes;
81  vector<GUIMouseEvent> mouseEvents;
82  vector<GUIKeyboardEvent> keyboardEvents;
83  vector<GUIScreenNode*> renderScreens;
84  int width;
85  int height;
87  int64_t lastEventTime { -1LL };
88  bool tooltipShown { false };
90 
91  unordered_map<string, unordered_set<string>> mouseOutCandidateEventNodeIds;
92  unordered_map<string, unordered_set<string>> mouseOutClickCandidateEventNodeIds;
93  unordered_map<string, unordered_set<string>> mousePressedEventNodeIds;
94  unordered_map<string, unordered_set<string>> mouseDraggingEventNodeIds;
95  unordered_map<string, bool> mouseIsDragging;
96 
97  bool altDown { false };
98  bool controlDown { false };
99  bool shiftDown { false };
100 
101  /**
102  * Determine focussed nodes
103  */
104  void determineFocussedNodes();
105 
106  /**
107  * Handle mouse event for given node
108  * @param node node
109  * @param event event
110  * @param mouseOutCandidateEventNodeIds mouse out candidate event node ids
111  * @param mouseOutClickCandidateEventNodeIds mouse out click candidate event node ids
112  * @param mousePressedEventNodeIds mouse pressed event node ids
113  * @param floatingNodes check if to gather floating nodes only
114  */
115  void handleMouseEvent(GUINode* node, GUIMouseEvent* event, const unordered_set<string>& mouseOutCandidateEventNodeIds, const unordered_set<string>& mouseOutClickCandidateEventNodeIds, unordered_set<string>& mousePressedEventNodeIds, bool floatingNodes);
116 
117  /**
118  * Handle mouse event for given node
119  * @param event event
120  */
122 
123  /**
124  * Reshape screen
125  * @param screenNode screen node
126  */
128 
129  /**
130  * Fake a keyboard modifier event
131  */
133 
134  /**
135  * Render screens change
136  */
137  void unsetMouseStates();
138 
139 public:
140  static constexpr int64_t TOOLTIP_TIME { 250LL };
141 
142  /**
143  * @return is focus control by TAB key disabled
144  */
145  inline static bool isDisableTabFocusControl() {
146  return disableTabFocusControl;
147  }
148 
149  /**
150  * Set focus control by TAB key disabled
151  * @param disableTabFocusControl disable TAB focus control
152  */
155  }
156 
157  // forbid class copy
159 
160  /**
161  * Public constructor
162  * @param engine engine
163  * @param guiRenderer gui renderer
164  */
166 
167  /**
168  * Destructor
169  */
170  ~GUI();
171 
172  /**
173  * @return width
174  */
175  inline int getWidth() {
176  return width;
177  }
178 
179  /**
180  * @return height
181  */
182  inline int getHeight() {
183  return height;
184  }
185 
186  /**
187  * Init
188  */
189  void initialize();
190 
191  /**
192  * Reshape
193  * @param width width
194  * @param height height
195  */
196  void reshape(int width, int height);
197 
198  /**
199  * Dispose
200  */
201  void dispose();
202 
203  /**
204  * @return mouse events
205  */
206  inline vector<GUIMouseEvent>& getMouseEvents() {
207  return mouseEvents;
208  }
209 
210  /**
211  * @return keyboard events
212  */
213  inline vector<GUIKeyboardEvent>& getKeyboardEvents() {
214  return keyboardEvents;
215  }
216 
217  /**
218  * Get screen
219  * @param id id
220  * @return screen
221  */
222  inline GUIScreenNode* getScreen(const string& id) {
223  auto screensIt = screens.find(id);
224  if (screensIt == screens.end()) {
225  return nullptr;
226  }
227  return screensIt->second;
228  }
229 
230  /**
231  * Add screen
232  * @param id id
233  * @param screen gui
234  */
235  void addScreen(const string& id, GUIScreenNode* screen);
236 
237  /**
238  * Removes an screen
239  * @param id id
240  */
241  void removeScreen(const string& id);
242 
243  /**
244  * Removes all screens and caches
245  */
246  void reset();
247 
248  /**
249  * Reset render screens
250  */
251  void resetRenderScreens();
252 
253  /**
254  * Returns if given screen is beeing rendered
255  * @param screenId screenId
256  */
257  inline bool hasRenderScreen(const string& screenId) {
258  for (auto renderScreen: renderScreens) {
259  if (renderScreen->getId() == screenId) return true;
260  }
261  return false;
262  }
263 
264  /**
265  * Add render screen
266  * @param screenId screenId
267  * @param screenIdx screen index or -1 to push to the end
268  */
269  void addRenderScreen(const string& screenId, int screenIdx = -1);
270 
271  /**
272  * Remove render screen
273  * @param screenId screenId
274  */
275  void removeRenderScreen(const string& screenId);
276 
277  /**
278  * Invalidate focussed node
279  */
280  void invalidateFocussedNode();
281 
282  /**
283  * @return focussed node
284  */
286 
287  /**
288  * Unfocus current focussed node
289  */
290  void unfocusNode();
291 
292  /**
293  * Focus current focussed node
294  */
295  void focusNode();
296 
297  /**
298  * Set focussed node
299  * @param newFoccussedNode foccussed node
300  */
301  void setFoccussedNode(GUIElementNode* newFoccussedNode);
302 
303  /**
304  * Focus next node
305  */
306  void focusNextNode();
307 
308  /**
309  * Focus next node
310  */
311  void focusPreviousNode();
312 
313  /**
314  * Add node that is a possible mouse out candidate as it received a mouse over
315  * @param node element node
316  */
317  inline void addMouseOutCandidateNode(GUINode* node) {
318  mouseOutCandidateEventNodeIds[node->getScreenNode()->getId()].insert(node->getId());
319  }
320 
321  /**
322  * Add node that is a possible mouse click out candidate as it received a mouse click
323  * @param node element node
324  */
326  mouseOutClickCandidateEventNodeIds[node->getScreenNode()->getId()].insert(node->getId());
327  }
328 
329  /**
330  * Render GUIs
331  */
332  void render();
333 
334  /**
335  * Reports if node has currently mouse interaction like dragging or pressing
336  * @param node node
337  * @return if node has currently mouse interaction
338  */
339  bool isHavingMouseInteraction(GUINode* node);
340 
341  /**
342  * On char
343  * @param key key
344  * @param x x
345  * @param y y
346  */
347 
348  void onChar(int key, int x, int y) override;
349  /**
350  * On key down
351  * @param key key
352  * @param keyCode key code
353  * @param x x
354  * @param y y
355  * @param repeat repeat event
356  * @param modifiers modifiers
357  */
358  void onKeyDown (int key, int keyCode, int x, int y, bool repeat, int modifiers) override;
359 
360  /**
361  * On key up
362  * @param key key
363  * @param keyCode key code
364  * @param x x
365  * @param y y
366  */
367  void onKeyUp(int key, int keyCode, int x, int y) override;
368 
369  /**
370  * On mouse dragged
371  * @param x x
372  * @param y y
373  */
374  void onMouseDragged(int x, int y) override;
375 
376  /**
377  * On mouse moved
378  * @param x x
379  * @param y y
380  */
381  void onMouseMoved(int x, int y) override;
382 
383  /**
384  * On mouse moved
385  * @param button button
386  * @param state state
387  * @param x x
388  * @param y y
389  */
390  void onMouseButton(int button, int state, int x, int y) override;
391 
392  /**
393  * On mouse wheel
394  * @param button button
395  * @param direction direction
396  * @param x x
397  * @param y y
398  */
399  void onMouseWheel(int button, int direction, int x, int y) override;
400 
401  /**
402  * Handle screen events
403  * @param clearEvents clear events
404  */
405  void handleEvents(bool clearEvents = true);
406 
407  /**
408  * Register mouse dragging
409  * @param node node
410  */
412  mouseIsDragging[node->screenNode->getId()] = true;
413  mousePressedEventNodeIds[node->screenNode->getId()].insert(node->getId());
414  mouseDraggingEventNodeIds[node->screenNode->getId()].insert(node->getId());
415  }
416 
417  /**
418  * Get scaled x from unscaled x
419  * @param screenNode screen node
420  * @param x x
421  * @return scaled x
422  */
423  inline int getScaledX(GUIScreenNode* screenNode, int x) {
424  return (float)x * (float)screenNode->getScreenWidth() / (float)width + screenNode->getGUIEffectOffsetX();
425  }
426 
427  /**
428  * Get scaled y from unscaled y
429  * @param screenNode screen node
430  * @param y y
431  * @return scaled y
432  */
433  inline int getScaledY(GUIScreenNode* screenNode, int y) {
434  return (float)y * (float)screenNode->getScreenHeight() / (float)height + screenNode->getGUIEffectOffsetY();
435  }
436 
437 };
static T required_dynamic_cast(U u)
Definition: GUI.h:48
Application input event handler interface.
Engine main class.
Definition: Engine.h:131
GUI module class.
Definition: GUI.h:64
unordered_map< string, unordered_set< string > > mousePressedEventNodeIds
Definition: GUI.h:93
GUIColor unfocussedNodeBorderTopColor
Definition: GUI.h:79
bool hasRenderScreen(const string &screenId)
Returns if given screen is beeing rendered.
Definition: GUI.h:257
void onMouseMoved(int x, int y) override
On mouse moved.
Definition: GUI.cpp:839
Engine * engine
Definition: GUI.h:71
int getHeight()
Definition: GUI.h:182
bool controlDown
Definition: GUI.h:98
static bool isDisableTabFocusControl()
Definition: GUI.h:145
bool shiftDown
Definition: GUI.h:99
bool tooltipShown
Definition: GUI.h:88
GUIElementNode * getFocussedNode()
Definition: GUI.cpp:224
vector< GUIScreenNode * > renderScreens
Definition: GUI.h:83
string focussedNodeScreenId
Definition: GUI.h:75
static void setDisableTabFocusControl(bool disableTabFocusControl)
Set focus control by TAB key disabled.
Definition: GUI.h:153
bool altDown
Definition: GUI.h:97
unordered_map< string, unordered_set< string > > mouseOutCandidateEventNodeIds
Definition: GUI.h:91
int getScaledY(GUIScreenNode *screenNode, int y)
Get scaled y from unscaled y.
Definition: GUI.h:433
void onMouseDragged(int x, int y) override
On mouse dragged.
Definition: GUI.cpp:820
int width
Definition: GUI.h:84
void initialize()
Init.
Definition: GUI.cpp:80
void focusPreviousNode()
Focus next node.
Definition: GUI.cpp:302
static STATIC_DLL_IMPEXT bool disableTabFocusControl
Definition: GUI.h:68
bool isHavingMouseInteraction(GUINode *node)
Reports if node has currently mouse interaction like dragging or pressing.
Definition: GUI.cpp:357
void removeRenderScreen(const string &screenId)
Remove render screen.
Definition: GUI.cpp:182
~GUI()
Destructor.
Definition: GUI.cpp:77
void handleEvents(bool clearEvents=true)
Handle screen events.
Definition: GUI.cpp:507
vector< GUIScreenNode * > focusableScreenNodes
Definition: GUI.h:74
void addRenderScreen(const string &screenId, int screenIdx=-1)
Add render screen.
Definition: GUI.cpp:155
vector< GUIElementNode * > focusableNodes
Definition: GUI.h:73
void onMouseButton(int button, int state, int x, int y) override
On mouse moved.
Definition: GUI.cpp:858
void handleMouseEvent(GUINode *node, GUIMouseEvent *event, const unordered_set< string > &mouseOutCandidateEventNodeIds, const unordered_set< string > &mouseOutClickCandidateEventNodeIds, unordered_set< string > &mousePressedEventNodeIds, bool floatingNodes)
Handle mouse event for given node.
Definition: GUI.cpp:365
unordered_map< string, bool > mouseIsDragging
Definition: GUI.h:95
void determineFocussedNodes()
Determine focussed nodes.
Definition: GUI.cpp:204
void onMouseWheel(int button, int direction, int x, int y) override
On mouse wheel.
Definition: GUI.cpp:880
GUIScreenNode * getScreen(const string &id)
Get screen.
Definition: GUI.h:222
GUI(Engine *engine, GUIRenderer *guiRenderer)
Public constructor.
Definition: GUI.cpp:68
void addMouseOutClickCandidateNode(GUINode *node)
Add node that is a possible mouse click out candidate as it received a mouse click.
Definition: GUI.h:325
unordered_map< string, GUIScreenNode * > screens
Definition: GUI.h:72
GUIRenderer * guiRenderer
Definition: GUI.h:70
int getWidth()
Definition: GUI.h:175
void reshape(int width, int height)
Reshape.
Definition: GUI.cpp:84
string focussedNodeNodeId
Definition: GUI.h:76
void unfocusNode()
Unfocus current focussed node.
Definition: GUI.cpp:232
void addMouseOutCandidateNode(GUINode *node)
Add node that is a possible mouse out candidate as it received a mouse over.
Definition: GUI.h:317
void unsetMouseStates()
Render screens change.
Definition: GUI.cpp:977
void render()
Render GUIs.
Definition: GUI.cpp:324
void onKeyUp(int key, int keyCode, int x, int y) override
On key up.
Definition: GUI.cpp:804
vector< GUIMouseEvent > & getMouseEvents()
Definition: GUI.h:206
vector< GUIKeyboardEvent > keyboardEvents
Definition: GUI.h:82
static constexpr int64_t TOOLTIP_TIME
Definition: GUI.h:140
void setFoccussedNode(GUIElementNode *newFoccussedNode)
Set focussed node.
Definition: GUI.cpp:267
void onKeyDown(int key, int keyCode, int x, int y, bool repeat, int modifiers) override
On key down.
Definition: GUI.cpp:788
unordered_map< string, unordered_set< string > > mouseOutClickCandidateEventNodeIds
Definition: GUI.h:92
void focusNode()
Focus current focussed node.
Definition: GUI.cpp:248
void fakeKeyboardModifierEvent()
Fake a keyboard modifier event.
Definition: GUI.cpp:902
GUIColor unfocussedNodeBorderRightColor
Definition: GUI.h:78
vector< GUIKeyboardEvent > & getKeyboardEvents()
Definition: GUI.h:213
void removeScreen(const string &id)
Removes an screen.
Definition: GUI.cpp:108
void addScreen(const string &id, GUIScreenNode *screen)
Add screen.
Definition: GUI.cpp:98
void onChar(int key, int x, int y) override
On char.
Definition: GUI.cpp:772
void dispose()
Dispose.
Definition: GUI.cpp:93
void invalidateFocussedNode()
Invalidate focussed node.
Definition: GUI.cpp:197
void reshapeScreen(GUIScreenNode *screenNode)
Reshape screen.
Definition: GUI.cpp:939
unordered_map< string, unordered_set< string > > mouseDraggingEventNodeIds
Definition: GUI.h:94
void focusNextNode()
Focus next node.
Definition: GUI.cpp:281
GUIColor unfocussedNodeBorderLeftColor
Definition: GUI.h:77
int getScaledX(GUIScreenNode *screenNode, int x)
Get scaled x from unscaled x.
Definition: GUI.h:423
int height
Definition: GUI.h:85
void reset()
Removes all screens and caches.
Definition: GUI.cpp:124
GUIMouseEvent lastMouseEvent
Definition: GUI.h:89
int lastMouseButton
Definition: GUI.h:86
void handleKeyboardEvent(GUIKeyboardEvent *event)
Handle mouse event for given node.
Definition: GUI.cpp:454
void resetRenderScreens()
Reset render screens.
Definition: GUI.cpp:136
int64_t lastEventTime
Definition: GUI.h:87
void startMouseDragging(GUINode *node)
Register mouse dragging.
Definition: GUI.h:411
GUIColor unfocussedNodeBorderBottomColor
Definition: GUI.h:80
vector< GUIMouseEvent > mouseEvents
Definition: GUI.h:81
GUI node base class.
Definition: GUINode.h:64
GUIScreenNode * screenNode
Definition: GUINode.h:147
GUIScreenNode * getScreenNode()
Definition: GUINode.h:325
const string & getId()
Definition: GUINode.h:339
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
void clearEvents()
Clear events.
vector< GUINode * > floatingNodes
Definition: GUIScreenNode.h:91
Console class.
Definition: Console.h:29
Exception base class.
Definition: ExceptionBase.h:19
Run time type information utility class.
Definition: RTTI.h:14
Time utility class.
Definition: Time.h:20
std::exception Exception
Exception base class.
Definition: Exception.h:18
#define STATIC_DLL_IMPEXT
Definition: tdme.h:15
#define FORBID_CLASS_COPY(CLASS)
Definition: tdme.h:6