The Battle for Wesnoth  1.19.11+dev
handler.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2025
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "events.hpp"
22 #include "gui/core/timer.hpp"
23 #include "gui/core/log.hpp"
24 #include "gui/widgets/helper.hpp"
25 #include "gui/widgets/widget.hpp"
26 #include "gui/widgets/window.hpp"
27 #include "hotkey/hotkey_item.hpp"
28 #include "video.hpp"
29 #include "utils/ranges.hpp"
30 
31 #include <SDL2/SDL.h>
32 
33 #include <cassert>
34 
35 /**
36  * @todo The items below are not implemented yet.
37  *
38  * - Tooltips have a fixed short time until showing up.
39  * - Tooltips are shown until the widget is exited.
40  * - Help messages aren't shown yet.
41  *
42  * @note it might be that tooltips will be shown independent of a window and in
43  * their own window, therefore the code will be cleaned up after that has been
44  * determined.
45  */
46 
47 /*
48  * At some point in the future this event handler should become the main event
49  * handler. This switch controls the experimental switch for that change.
50  */
51 //#define MAIN_EVENT_HANDLER
52 
53 /* Since this code is still very experimental it's not enabled yet. */
54 //#define ENABLE
55 
56 namespace gui2
57 {
58 
59 namespace event
60 {
61 
62 /***** Static data. *****/
63 static std::unique_ptr<class sdl_event_handler> handler_ = nullptr;
64 static std::unique_ptr<events::event_context> event_context = nullptr;
65 
66 #ifdef MAIN_EVENT_HANDLER
67 static unsigned draw_interval = 0;
68 static unsigned event_poll_interval = 0;
69 
70 /***** Static functions. *****/
71 
72 /**
73  * SDL_AddTimer() callback for the draw event.
74  *
75  * When this callback is called it pushes a new draw event in the event queue.
76  *
77  * @returns The new timer interval, 0 to stop.
78  */
79 static uint32_t timer_sdl_draw_event(uint32_t, void*)
80 {
81  // DBG_GUI_E << "Pushing draw event in queue.";
82 
83  SDL_Event event;
85 
86  event.type = DRAW_EVENT;
87  event.user = data;
88 
89  SDL_PushEvent(&event);
90  return draw_interval;
91 }
92 
93 /**
94  * SDL_AddTimer() callback for the poll event.
95  *
96  * When this callback is called it will run the events in the SDL event queue.
97  *
98  * @returns The new timer interval, 0 to stop.
99  */
100 static uint32_t timer_sdl_poll_events(uint32_t, void*)
101 {
102  try
103  {
104  events::pump();
105  }
106  catch(video::quit&)
107  {
108  return 0;
109  }
110  return event_poll_interval;
111 }
112 #endif
113 
114 /***** handler class. *****/
115 
116 /**
117  * This singleton class handles all events.
118  *
119  * It's a new experimental class.
120  */
122 {
123  friend bool gui2::is_in_dialog();
124 
125 public:
127 
129 
130  /** Inherited from events::sdl_handler. */
131  void handle_event(const SDL_Event& event) override;
132 
133  /** Inherited from events::sdl_handler. */
134  void handle_window_event(const SDL_Event& event) override;
135 
136  /**
137  * Connects a dispatcher.
138  *
139  * @param dispatcher The dispatcher to connect.
140  */
142 
143  /**
144  * Disconnects a dispatcher.
145  *
146  * @param dispatcher The dispatcher to disconnect.
147  */
149 
150  /**
151  * Returns all dispatchers in the Z order.
152  */
153  std::vector<dispatcher*>& get_dispatchers() { return dispatchers_; }
154 
155  /** The dispatcher that captured the mouse focus. */
157 
158 private:
159  /**
160  * Reinitializes the state of all dispatchers.
161  *
162  * This is needed when the application gets activated, to make sure the
163  * state of mainly the mouse is set properly.
164  */
165  void activate();
166 
167  /***** Handlers *****/
168 
169  /** Fires a raw SDL event. */
170  void raw_event(const SDL_Event &event);
171 
172  /**
173  * Fires a video resize event.
174  *
175  * @param new_size The new size of the window.
176  */
177  void video_resize(const point& new_size);
178 
179  /**
180  * Fires a generic mouse event.
181  *
182  * @param event The event to fire.
183  * @param position The position of the mouse.
184  */
185  void mouse(const ui_event event, const point& position);
186 
187  /**
188  * Fires a mouse button up event.
189  *
190  * @param position The position of the mouse.
191  * @param button The SDL id of the button that caused the
192  * event.
193  */
194  void mouse_button_up(const point& position, const uint8_t button);
195 
196  /**
197  * Fires a mouse button down event.
198  *
199  * @param position The position of the mouse.
200  * @param button The SDL id of the button that caused the
201  * event.
202  */
203  void mouse_button_down(const point& position, const uint8_t button);
204 
205  /**
206  * Fires a mouse wheel event.
207  *
208  * @param position The position of the mouse.
209  * @param scrollx The amount of horizontal scrolling.
210  * @param scrolly The amount of vertical scrolling.
211  */
212  void mouse_wheel(const point& position, int scrollx, int scrolly);
213 
214  /**
215  * Gets the dispatcher that wants to receive the keyboard input.
216  *
217  * @returns The dispatcher.
218  * @retval nullptr No dispatcher found.
219  */
221 
222  /**
223  * Fires a touch-moved event.
224  *
225  * @param position The position touched.
226  * @param distance The distance moved.
227  */
228  void touch_motion(const point& position, const point& distance);
229 
230  /**
231  * Fires a touch "finger down" event.
232  *
233  * @param position The position touched.
234  */
235  void touch_down(const point& position);
236 
237  /**
238  * Fires a touch "finger up" event.
239  *
240  * @param position The position touched.
241  */
242  void touch_up(const point& position);
243 
244  /**
245  * Fires a touch gesture event.
246  * @param center the center of gesture
247  * @param dTheta the amount that the fingers rotated during this motion
248  * @param dDist the amount that the fingers pinched during this motion
249  * @param numFingers the number of fingers used in the gesture
250  */
251  void touch_multi_gesture(const point& center, float dTheta, float dDist, uint8_t numFingers);
252 
253  /**
254  * Handles a hat motion event.
255  *
256  * @param event The SDL joystick hat event triggered.
257  */
258  void hat_motion(const SDL_Event& event);
259 
260  /**
261  * Handles a joystick button down event.
262  *
263  * @param event The SDL joystick button event triggered.
264  */
265  void button_down(const SDL_Event& event);
266 
267  /**
268  * Fires a key down event.
269  *
270  * @param event The SDL keyboard event triggered.
271  */
272  void key_down(const SDL_Event& event);
273 
274  /**
275  * Handles the pressing of a hotkey.
276  *
277  * @param key The hotkey item pressed.
278  *
279  * @returns True if there was a valid dispatcher with
280  * which to execute the hotkey callback, false
281  * otherwise.
282  */
283  bool hotkey_pressed(const hotkey::hotkey_ptr& key);
284 
285  /**
286  * Fires a key down event.
287  *
288  * @param key The SDL key code of the key pressed.
289  * @param modifier The SDL key modifiers used.
290  * @param unicode The unicode value for the key pressed.
291  */
292  void key_down(const SDL_Keycode key,
293  const SDL_Keymod modifier,
294  const std::string& unicode);
295 
296  /**
297  * Fires a text input event.
298  *
299  * @param unicode The unicode value for the text entered.
300  */
301  void text_input(const std::string& unicode);
302 
303  /**
304  * Fires a text editing event.
305  *
306  * @param unicode The unicode value for the text being edited.
307  * @param start The start position for the text being edited.
308  * @param len The selection length for the text being edited.
309  */
310  void text_editing(const std::string& unicode, int32_t start, int32_t len);
311 
312  /**
313  * Fires a keyboard event which has no parameters.
314  *
315  * This can happen for example when the mouse wheel is used.
316  *
317  * @param event The event to fire.
318  */
319  void keyboard(const ui_event event);
320 
321  /**
322  * Fires a CLOSE_WINDOW event for the window with the given ID.
323  *
324  * @param window_id The ID of the window to close.
325  */
326  void close_window(const unsigned window_id);
327 
328  /**
329  * The dispatchers.
330  *
331  * The order of the items in the list is also the z-order the front item
332  * being the one completely in the background and the back item the one
333  * completely in the foreground.
334  */
335  std::vector<dispatcher*> dispatchers_;
336 
337  /**
338  * Needed to determine which dispatcher gets the keyboard events.
339  *
340  * NOTE the keyboard events aren't really wired in yet so doesn't do much.
341  */
343  friend void capture_keyboard(dispatcher* dispatcher);
344 };
345 
347  : events::sdl_handler(false)
348  , mouse_focus(nullptr)
349  , dispatchers_()
350  , keyboard_focus_(nullptr)
351 {
352  if(SDL_WasInit(SDL_INIT_TIMER) == 0) {
353  if(SDL_InitSubSystem(SDL_INIT_TIMER) == -1) {
354  assert(false);
355  }
356  }
357 
358 // The event context is created now we join it.
359 #ifdef ENABLE
360  join();
361 #endif
362 }
363 
365 {
366 #ifdef ENABLE
367  leave();
368 #endif
369 }
370 
371 void sdl_event_handler::handle_event(const SDL_Event& event)
372 {
373  /** No dispatchers drop the event. */
374  if(dispatchers_.empty()) {
375  return;
376  }
377 
378  uint8_t button = event.button.button;
379 
380  switch(event.type) {
381  case SDL_MOUSEMOTION:
382 #ifdef MOUSE_TOUCH_EMULATION
383  // There's no finger motion when it's not down.
384  if (event.motion.state != 0)
385 #endif
386  {
387  mouse(SDL_MOUSE_MOTION, {event.motion.x, event.motion.y});
388  }
389  break;
390 
391  case SDL_MOUSEBUTTONDOWN:
392  {
393  mouse_button_down({event.button.x, event.button.y}, button);
394  }
395  break;
396 
397  case SDL_MOUSEBUTTONUP:
398  {
399  mouse_button_up({event.button.x, event.button.y}, button);
400  }
401  break;
402 
403  case SDL_MOUSEWHEEL:
404  mouse_wheel(get_mouse_position(), event.wheel.x, event.wheel.y);
405  break;
406 
407  case SHOW_HELPTIP_EVENT:
409  break;
410 
412  // remove_popup();
413  break;
414 
415  case TIMER_EVENT:
416  execute_timer(reinterpret_cast<std::size_t>(event.user.data1));
417  break;
418 
419  case CLOSE_WINDOW_EVENT:
420  close_window(event.user.code);
421  break;
422 
423  case SDL_JOYBUTTONDOWN:
424  button_down(event);
425  break;
426 
427  case SDL_JOYBUTTONUP:
428  break;
429 
430  case SDL_JOYAXISMOTION:
431  break;
432 
433  case SDL_JOYHATMOTION:
434  hat_motion(event);
435  break;
436 
437  case SDL_KEYDOWN:
438  key_down(event);
439  break;
440 
441  case SDL_WINDOWEVENT:
442  switch(event.window.event) {
443  // Always precedes SDL_WINDOWEVENT_RESIZED, but the latter does not always
444  // happen; in particular when we change the game resolution via
445  // SDL_SetWindowSize() <https://github.com/wesnoth/wesnoth/issues/7436>
446  case SDL_WINDOWEVENT_SIZE_CHANGED:
448  break;
449 
450  case SDL_WINDOWEVENT_ENTER:
451  case SDL_WINDOWEVENT_FOCUS_GAINED:
452  activate();
453  break;
454  }
455 
456  break;
457 
458  case SDL_TEXTINPUT:
459  key_down(event);
460  break;
461 
462  case SDL_TEXTEDITING:
463  text_editing(event.edit.text, event.edit.start, event.edit.length);
464  break;
465 
466  case SDL_FINGERMOTION:
467  {
469  touch_motion(
470  point(event.tfinger.x * c.x, event.tfinger.y * c.y),
471  point(event.tfinger.dx * c.x, event.tfinger.dy * c.y)
472  );
473  }
474  break;
475 
476  case SDL_FINGERUP:
477  {
479  touch_up(point(event.tfinger.x * c.x, event.tfinger.y * c.y));
480  }
481  break;
482 
483  case SDL_FINGERDOWN:
484  {
486  touch_down(point(event.tfinger.x * c.x, event.tfinger.y * c.y));
487  }
488  break;
489 
490  case SDL_MULTIGESTURE:
491  {
494  point(event.mgesture.x * c.x, event.mgesture.y * c.y),
495  event.mgesture.dTheta, event.mgesture.dDist,
496  event.mgesture.numFingers
497  );
498  }
499  break;
500 
501 #if(defined(_X11) && !defined(__APPLE__)) || defined(_WIN32)
502  case SDL_SYSWMEVENT:
503  /* DO NOTHING */
504  break;
505 #endif
506 
507  // Silently ignored events.
508  case SDL_KEYUP:
509  case DOUBLE_CLICK_EVENT:
510  break;
511 
512  default:
513 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
514  WRN_GUI_E << "Unhandled event " << static_cast<uint32_t>(event.type)
515  << ".";
516 #endif
517  break;
518  }
519 
520  raw_event(event);
521 }
522 
523 void sdl_event_handler::handle_window_event(const SDL_Event& event)
524 {
525  handle_event(event);
526 }
527 
529 {
530  assert(std::find(dispatchers_.begin(), dispatchers_.end(), dispatcher)
531  == dispatchers_.end());
532 
533  DBG_GUI_E << "adding dispatcher " << static_cast<void*>(dispatcher);
534 
535  if(dispatchers_.empty()) {
536  LOG_GUI_E << "creating new dispatcher event context";
537  event_context = std::make_unique<events::event_context>();
538  join();
539  }
540 
541  dispatchers_.push_back(dispatcher);
542 }
543 
545 {
546  /***** Validate pre conditions. *****/
547  auto itor = std::find(dispatchers_.begin(), dispatchers_.end(), disp);
548  assert(itor != dispatchers_.end());
549 
550  DBG_GUI_E << "removing dispatcher " << static_cast<void*>(disp);
551 
552  /***** Remove dispatcher. *****/
553  dispatchers_.erase(itor);
554 
555  if(disp == mouse_focus) {
556  mouse_focus = nullptr;
557  }
558  if(disp == keyboard_focus_) {
559  keyboard_focus_ = nullptr;
560  }
561 
562  // TODO: draw_manager - Why TF was this "activate"ing on "disconnect"? Seriously WTF?
563  //activate();
564 
565  /***** Validate post conditions. *****/
566  assert(std::find(dispatchers_.begin(), dispatchers_.end(), disp)
567  == dispatchers_.end());
568 
569  if(dispatchers_.empty()) {
570  LOG_GUI_E << "deleting unused dispatcher event context";
571  leave();
572  event_context = nullptr;
573  }
574 }
575 
577 {
578  for(auto dispatcher : dispatchers_)
579  {
580  dispatcher->fire(SDL_ACTIVATE, dynamic_cast<widget&>(*dispatcher), nullptr);
581  }
582 }
583 
585 {
586  DBG_GUI_E << "Firing: " << SDL_VIDEO_RESIZE << ".";
587 
588  for(auto dispatcher : dispatchers_)
589  {
590  dispatcher->fire(SDL_VIDEO_RESIZE, dynamic_cast<widget&>(*dispatcher), new_size);
591  }
592 }
593 
594 void sdl_event_handler::raw_event(const SDL_Event& event) {
595  DBG_GUI_E << "Firing raw event";
596 
597  for(auto dispatcher : dispatchers_)
598  {
599  dispatcher->fire(SDL_RAW_EVENT, dynamic_cast<widget&>(*dispatcher), event);
600  }
601 }
602 
603 void sdl_event_handler::mouse(const ui_event event, const point& position)
604 {
605  DBG_GUI_E << "Firing: " << event << ".";
606 
607  if(mouse_focus) {
608  mouse_focus->fire(event, dynamic_cast<widget&>(*mouse_focus), position);
609  return;
610  }
611 
614  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher), position);
615  break;
616  }
617 
619  continue;
620  }
621 
622  if(dispatcher->is_at(position)) {
623  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher), position);
624  break;
625  }
626  }
627 }
628 
629 void sdl_event_handler::mouse_button_up(const point& position, const uint8_t button)
630 {
631  switch(button) {
632  case SDL_BUTTON_LEFT:
633  mouse(SDL_LEFT_BUTTON_UP, position);
634  break;
635  case SDL_BUTTON_MIDDLE:
636  mouse(SDL_MIDDLE_BUTTON_UP, position);
637  break;
638  case SDL_BUTTON_RIGHT:
639  mouse(SDL_RIGHT_BUTTON_UP, position);
640  break;
641  default:
642 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
643  WRN_GUI_E << "Unhandled 'mouse button up' event for button "
644  << static_cast<uint32_t>(button) << ".";
645 #endif
646  break;
647  }
648 }
649 
650 void sdl_event_handler::mouse_button_down(const point& position, const uint8_t button)
651 {
652  switch(button) {
653  case SDL_BUTTON_LEFT:
654  mouse(SDL_LEFT_BUTTON_DOWN, position);
655  break;
656  case SDL_BUTTON_MIDDLE:
657  mouse(SDL_MIDDLE_BUTTON_DOWN, position);
658  break;
659  case SDL_BUTTON_RIGHT:
660  mouse(SDL_RIGHT_BUTTON_DOWN, position);
661  break;
662  default:
663 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
664  WRN_GUI_E << "Unhandled 'mouse button down' event for button "
665  << static_cast<uint32_t>(button) << ".";
666 #endif
667  break;
668  }
669 }
670 
671 void sdl_event_handler::mouse_wheel(const point& position, int x, int y)
672 {
673  if(x > 0) {
674  mouse(SDL_WHEEL_RIGHT, position);
675  } else if(x < 0) {
676  mouse(SDL_WHEEL_LEFT, position);
677  }
678 
679  if(y < 0) {
680  mouse(SDL_WHEEL_DOWN, position);
681  } else if(y > 0) {
682  mouse(SDL_WHEEL_UP, position);
683  }
684 }
685 
687 {
688  if(keyboard_focus_) {
689  return keyboard_focus_;
690  }
691 
694  return dispatcher;
695  }
696  }
697 
698  return nullptr;
699 }
700 
701 void sdl_event_handler::touch_motion(const point& position, const point& distance)
702 {
704  dispatcher->fire(SDL_TOUCH_MOTION , dynamic_cast<widget&>(*dispatcher), position, distance);
705  }
706 }
707 
708 void sdl_event_handler::touch_up(const point& position)
709 {
711  dispatcher->fire(SDL_TOUCH_UP, dynamic_cast<widget&>(*dispatcher), position);
712  }
713 }
714 
716 {
718  dispatcher->fire(SDL_TOUCH_DOWN, dynamic_cast<widget&>(*dispatcher), position);
719  }
720 }
721 
722 void sdl_event_handler::touch_multi_gesture(const point& center, float dTheta, float dDist, uint8_t numFingers)
723 {
725  dispatcher->fire(SDL_TOUCH_MULTI_GESTURE, dynamic_cast<widget&>(*dispatcher), center, dTheta, dDist, numFingers);
726  }
727 }
728 
729 void sdl_event_handler::hat_motion(const SDL_Event& event)
730 {
731  const hotkey::hotkey_ptr& hk = hotkey::get_hotkey(event);
732  bool done = false;
733  if(!hk->null()) {
734  done = hotkey_pressed(hk);
735  }
736  if(!done) {
737  // TODO fendrin think about handling hat motions that are not bound to a
738  // hotkey.
739  }
740 }
741 
742 void sdl_event_handler::button_down(const SDL_Event& event)
743 {
744  const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
745  bool done = false;
746  if(!hk->null()) {
747  done = hotkey_pressed(hk);
748  }
749  if(!done) {
750  // TODO fendrin think about handling button down events that are not
751  // bound to a hotkey.
752  }
753 }
754 
755 void sdl_event_handler::key_down(const SDL_Event& event)
756 {
757  const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
758  bool done = false;
759  if(!hk->null()) {
760  done = hotkey_pressed(hk);
761  }
762  if(!done) {
763  if(event.type == SDL_TEXTINPUT) {
764  text_input(event.text.text);
765  } else {
766  key_down(event.key.keysym.sym, static_cast<SDL_Keymod>(event.key.keysym.mod), "");
767  }
768  }
769 }
770 
771 void sdl_event_handler::text_input(const std::string& unicode)
772 {
773  key_down(SDLK_UNKNOWN, static_cast<SDL_Keymod>(0), unicode);
774 
777  dynamic_cast<widget&>(*dispatcher),
778  unicode, -1, -1);
779  }
780 }
781 
782 void sdl_event_handler::text_editing(const std::string& unicode, int32_t start, int32_t len)
783 {
786  dynamic_cast<widget&>(*dispatcher),
787  unicode, start, len);
788  }
789 }
790 
792 {
794  return dispatcher->execute_hotkey(hotkey::get_hotkey_command(key->get_command()).command);
795  }
796 
797  return false;
798 }
799 
800 void sdl_event_handler::key_down(const SDL_Keycode key,
801  const SDL_Keymod modifier,
802  const std::string& unicode)
803 {
804  DBG_GUI_E << "Firing: " << SDL_KEY_DOWN << ".";
805 
808  dynamic_cast<widget&>(*dispatcher),
809  key,
810  modifier,
811  unicode);
812  }
813 }
814 
816 {
817  DBG_GUI_E << "Firing: " << event << ".";
818 
820  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher));
821  }
822 }
823 
824 void sdl_event_handler::close_window(const unsigned window_id)
825 {
826  DBG_GUI_E << "Firing " << CLOSE_WINDOW << ".";
827 
828  window* window = window::window_instance(window_id);
829  if(window) {
831  }
832 }
833 
834 /***** manager class. *****/
835 
837 {
838  handler_.reset(new sdl_event_handler());
839 
840 #ifdef MAIN_EVENT_HANDLER
841  draw_interval = 30;
842  SDL_AddTimer(draw_interval, timer_sdl_draw_event, nullptr);
843 
844  event_poll_interval = 10;
845  SDL_AddTimer(event_poll_interval, timer_sdl_poll_events, nullptr);
846 #endif
847 }
848 
850 {
851  handler_.reset(nullptr);
852 
853 #ifdef MAIN_EVENT_HANDLER
854  draw_interval = 0;
855  event_poll_interval = 0;
856 #endif
857 }
858 
859 /***** free functions class. *****/
860 
862 {
863  assert(handler_);
864  assert(dispatcher);
865  handler_->connect(dispatcher);
866 }
867 
869 {
870  assert(handler_);
871  assert(dispatcher);
872  handler_->disconnect(dispatcher);
873 }
874 
875 std::vector<dispatcher*>& get_all_dispatchers()
876 {
877  assert(handler_);
878  return handler_->get_dispatchers();
879 }
880 
882 {
884 
885  SDL_Event event{};
886  event.type = SDL_MOUSEMOTION;
887  event.motion.type = SDL_MOUSEMOTION;
888  event.motion.x = mouse.x;
889  event.motion.y = mouse.y;
890 
891  SDL_PushEvent(&event);
892 }
893 
895 {
896  assert(handler_);
897  assert(dispatcher);
898  handler_->mouse_focus = dispatcher;
899 }
900 
902 {
903  assert(handler_);
904  assert(dispatcher);
905  if(handler_->mouse_focus == dispatcher) {
906  handler_->mouse_focus = nullptr;
907  }
908 }
909 
911 {
912  assert(handler_);
913  assert(dispatcher);
915 
916  handler_->keyboard_focus_ = dispatcher;
917 }
918 
919 std::ostream& operator<<(std::ostream& stream, const ui_event event)
920 {
921  switch(event) {
922  case DRAW:
923  stream << "draw";
924  break;
925  case CLOSE_WINDOW:
926  stream << "close window";
927  break;
928  case SDL_VIDEO_RESIZE:
929  stream << "SDL video resize";
930  break;
931  case SDL_MOUSE_MOTION:
932  stream << "SDL mouse motion";
933  break;
934  case MOUSE_ENTER:
935  stream << "mouse enter";
936  break;
937  case MOUSE_LEAVE:
938  stream << "mouse leave";
939  break;
940  case MOUSE_MOTION:
941  stream << "mouse motion";
942  break;
944  stream << "SDL left button down";
945  break;
946  case SDL_LEFT_BUTTON_UP:
947  stream << "SDL left button up";
948  break;
949  case LEFT_BUTTON_DOWN:
950  stream << "left button down";
951  break;
952  case LEFT_BUTTON_UP:
953  stream << "left button up";
954  break;
955  case LEFT_BUTTON_CLICK:
956  stream << "left button click";
957  break;
959  stream << "left button double click";
960  break;
962  stream << "SDL middle button down";
963  break;
965  stream << "SDL middle button up";
966  break;
967  case MIDDLE_BUTTON_DOWN:
968  stream << "middle button down";
969  break;
970  case MIDDLE_BUTTON_UP:
971  stream << "middle button up";
972  break;
973  case MIDDLE_BUTTON_CLICK:
974  stream << "middle button click";
975  break;
977  stream << "middle button double click";
978  break;
980  stream << "SDL right button down";
981  break;
982  case SDL_RIGHT_BUTTON_UP:
983  stream << "SDL right button up";
984  break;
985  case RIGHT_BUTTON_DOWN:
986  stream << "right button down";
987  break;
988  case RIGHT_BUTTON_UP:
989  stream << "right button up";
990  break;
991  case RIGHT_BUTTON_CLICK:
992  stream << "right button click";
993  break;
995  stream << "right button double click";
996  break;
997  case SDL_WHEEL_LEFT:
998  stream << "SDL wheel left";
999  break;
1000  case SDL_WHEEL_RIGHT:
1001  stream << "SDL wheel right";
1002  break;
1003  case SDL_WHEEL_UP:
1004  stream << "SDL wheel up";
1005  break;
1006  case SDL_WHEEL_DOWN:
1007  stream << "SDL wheel down";
1008  break;
1009  case SDL_KEY_DOWN:
1010  stream << "SDL key down";
1011  break;
1012  case SDL_TEXT_INPUT:
1013  stream << "SDL text input";
1014  break;
1015  case SDL_TEXT_EDITING:
1016  stream << "SDL text editing";
1017  break;
1018 
1019  case NOTIFY_REMOVAL:
1020  stream << "notify removal";
1021  break;
1022  case NOTIFY_MODIFIED:
1023  stream << "notify modified";
1024  break;
1026  stream << "receive keyboard focus";
1027  break;
1028  case LOSE_KEYBOARD_FOCUS:
1029  stream << "lose keyboard focus";
1030  break;
1031  case SHOW_TOOLTIP:
1032  stream << "show tooltip";
1033  break;
1034  case NOTIFY_REMOVE_TOOLTIP:
1035  stream << "notify remove tooltip";
1036  break;
1037  case SDL_ACTIVATE:
1038  stream << "SDL activate";
1039  break;
1040  case MESSAGE_SHOW_TOOLTIP:
1041  stream << "message show tooltip";
1042  break;
1043  case SHOW_HELPTIP:
1044  stream << "show helptip";
1045  break;
1046  case MESSAGE_SHOW_HELPTIP:
1047  stream << "message show helptip";
1048  break;
1049  case REQUEST_PLACEMENT:
1050  stream << "request placement";
1051  break;
1052  case SDL_TOUCH_MOTION:
1053  stream << "SDL touch motion";
1054  break;
1055  case SDL_TOUCH_UP:
1056  stream << "SDL touch up";
1057  break;
1058  case SDL_TOUCH_DOWN:
1059  stream << "SDL touch down";
1060  break;
1062  stream << "SDL multi-touch gesture";
1063  break;
1064  case SDL_RAW_EVENT:
1065  stream << "SDL raw event";
1066  break;
1067  }
1068 
1069  return stream;
1070 }
1071 
1072 } // namespace event
1073 
1075 {
1076  return !event::get_all_dispatchers().empty();
1077 }
1078 
1079 } // namespace gui2
virtual void join()
Definition: events.cpp:308
virtual void leave()
Definition: events.cpp:353
Simple push button.
Definition: button.hpp:36
button(const implementation::builder_button &builder)
Definition: button.cpp:44
Base class for event handling.
Definition: dispatcher.hpp:150
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:74
virtual bool is_at(const point &coordinate) const =0
Determines whether the location is inside an active widget.
bool get_want_keyboard_input() const
Definition: dispatcher.hpp:427
bool execute_hotkey(const hotkey::HOTKEY_COMMAND id)
Executes a hotkey.
Definition: dispatcher.cpp:147
mouse_behavior get_mouse_behavior() const
Definition: dispatcher.hpp:417
This singleton class handles all events.
Definition: handler.cpp:122
void touch_down(const point &position)
Fires a touch "finger down" event.
Definition: handler.cpp:715
void keyboard(const ui_event event)
Fires a keyboard event which has no parameters.
Definition: handler.cpp:815
void activate()
Reinitializes the state of all dispatchers.
Definition: handler.cpp:576
void disconnect(dispatcher *dispatcher)
Disconnects a dispatcher.
Definition: handler.cpp:544
void mouse_button_up(const point &position, const uint8_t button)
Fires a mouse button up event.
Definition: handler.cpp:629
void close_window(const unsigned window_id)
Fires a CLOSE_WINDOW event for the window with the given ID.
Definition: handler.cpp:824
void handle_window_event(const SDL_Event &event) override
Inherited from events::sdl_handler.
Definition: handler.cpp:523
void video_resize(const point &new_size)
Fires a video resize event.
Definition: handler.cpp:584
void text_editing(const std::string &unicode, int32_t start, int32_t len)
Fires a text editing event.
Definition: handler.cpp:782
void touch_up(const point &position)
Fires a touch "finger up" event.
Definition: handler.cpp:708
std::vector< dispatcher * > & get_dispatchers()
Returns all dispatchers in the Z order.
Definition: handler.cpp:153
dispatcher * keyboard_dispatcher()
Gets the dispatcher that wants to receive the keyboard input.
Definition: handler.cpp:686
friend void capture_keyboard(dispatcher *dispatcher)
Captures the keyboard.
Definition: handler.cpp:910
void text_input(const std::string &unicode)
Fires a text input event.
Definition: handler.cpp:771
void touch_motion(const point &position, const point &distance)
Fires a touch-moved event.
Definition: handler.cpp:701
bool hotkey_pressed(const hotkey::hotkey_ptr &key)
Handles the pressing of a hotkey.
Definition: handler.cpp:791
void mouse_button_down(const point &position, const uint8_t button)
Fires a mouse button down event.
Definition: handler.cpp:650
void hat_motion(const SDL_Event &event)
Handles a hat motion event.
Definition: handler.cpp:729
dispatcher * keyboard_focus_
Needed to determine which dispatcher gets the keyboard events.
Definition: handler.cpp:342
void connect(dispatcher *dispatcher)
Connects a dispatcher.
Definition: handler.cpp:528
void key_down(const SDL_Event &event)
Fires a key down event.
Definition: handler.cpp:755
void mouse(const ui_event event, const point &position)
Fires a generic mouse event.
Definition: handler.cpp:603
void touch_multi_gesture(const point &center, float dTheta, float dDist, uint8_t numFingers)
Fires a touch gesture event.
Definition: handler.cpp:722
void mouse_wheel(const point &position, int scrollx, int scrolly)
Fires a mouse wheel event.
Definition: handler.cpp:671
std::vector< dispatcher * > dispatchers_
The dispatchers.
Definition: handler.cpp:335
void button_down(const SDL_Event &event)
Handles a joystick button down event.
Definition: handler.cpp:742
dispatcher * mouse_focus
The dispatcher that captured the mouse focus.
Definition: handler.cpp:156
void raw_event(const SDL_Event &event)
Fires a raw SDL event.
Definition: handler.cpp:594
void handle_event(const SDL_Event &event) override
Inherited from events::sdl_handler.
Definition: handler.cpp:371
Base class for all widgets.
Definition: widget.hpp:55
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:61
static window * window_instance(const unsigned handle)
Returns the instance of a window.
Definition: window.cpp:408
Type that can be thrown as an exception to quit to desktop.
Definition: video.hpp:323
#define DRAW_EVENT
Definition: events.hpp:27
#define CLOSE_WINDOW_EVENT
Definition: events.hpp:28
#define SHOW_HELPTIP_EVENT
Definition: events.hpp:29
#define DOUBLE_CLICK_EVENT
Definition: events.hpp:24
#define TIMER_EVENT
Definition: events.hpp:25
#define HOVER_REMOVE_POPUP_EVENT
Definition: events.hpp:26
Define the common log macros for the gui toolkit.
#define WRN_GUI_E
Definition: log.hpp:37
#define LOG_GUI_E
Definition: log.hpp:36
#define DBG_GUI_E
Definition: log.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:211
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Handling of system events.
void pump()
Process all events currently in the queue.
Definition: events.cpp:483
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
@ MIDDLE_BUTTON_DOWN
Definition: handler.hpp:124
@ NOTIFY_REMOVE_TOOLTIP
Definition: handler.hpp:159
@ RIGHT_BUTTON_UP
Definition: handler.hpp:129
@ SDL_RIGHT_BUTTON_DOWN
Definition: handler.hpp:140
@ SDL_TOUCH_DOWN
Definition: handler.hpp:149
@ MIDDLE_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:127
@ NOTIFY_MODIFIED
Definition: handler.hpp:158
@ MIDDLE_BUTTON_CLICK
Definition: handler.hpp:126
@ SDL_LEFT_BUTTON_DOWN
Definition: handler.hpp:136
@ SDL_TEXT_INPUT
An SDL text input (commit) event.
Definition: handler.hpp:153
@ SDL_WHEEL_DOWN
Definition: handler.hpp:145
@ LEFT_BUTTON_UP
Definition: handler.hpp:121
@ MIDDLE_BUTTON_UP
Definition: handler.hpp:125
@ RIGHT_BUTTON_CLICK
Definition: handler.hpp:130
@ LOSE_KEYBOARD_FOCUS
Definition: handler.hpp:161
@ SDL_MOUSE_MOTION
Definition: handler.hpp:134
@ SDL_TEXT_EDITING
An SDL text editing (IME) event.
Definition: handler.hpp:154
@ LEFT_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:123
@ RIGHT_BUTTON_DOWN
Definition: handler.hpp:128
@ NOTIFY_REMOVAL
Definition: handler.hpp:157
@ RECEIVE_KEYBOARD_FOCUS
Definition: handler.hpp:160
@ SDL_TOUCH_MOTION
Definition: handler.hpp:167
@ SDL_WHEEL_RIGHT
Definition: handler.hpp:143
@ SDL_VIDEO_RESIZE
Definition: handler.hpp:133
@ LEFT_BUTTON_CLICK
Definition: handler.hpp:122
@ SDL_WHEEL_LEFT
Definition: handler.hpp:142
@ SDL_LEFT_BUTTON_UP
Definition: handler.hpp:137
@ SDL_TOUCH_MULTI_GESTURE
Definition: handler.hpp:168
@ REQUEST_PLACEMENT
Definition: handler.hpp:163
@ SDL_MIDDLE_BUTTON_UP
Definition: handler.hpp:139
@ LEFT_BUTTON_DOWN
Definition: handler.hpp:120
@ SDL_MIDDLE_BUTTON_DOWN
Definition: handler.hpp:138
@ SDL_RIGHT_BUTTON_UP
Definition: handler.hpp:141
@ RIGHT_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:131
@ MESSAGE_SHOW_TOOLTIP
Definition: handler.hpp:164
@ MESSAGE_SHOW_HELPTIP
Definition: handler.hpp:165
void capture_mouse(dispatcher *dispatcher)
Captures the mouse.
Definition: handler.cpp:894
void release_mouse(dispatcher *dispatcher)
Releases a captured mouse.
Definition: handler.cpp:901
void disconnect_dispatcher(dispatcher *dispatcher)
Disconnects a dispatcher to the event handler.
Definition: handler.cpp:868
static std::unique_ptr< class sdl_event_handler > handler_
Definition: handler.cpp:63
void capture_keyboard(dispatcher *dispatcher)
Captures the keyboard.
Definition: handler.cpp:910
@ mouse
Callbacks with a coordinate as extra parameter.
void connect_dispatcher(dispatcher *dispatcher)
Connects a dispatcher to the event handler.
Definition: handler.cpp:861
std::vector< dispatcher * > & get_all_dispatchers()
Gets all event dispatchers in the Z order.
Definition: handler.cpp:875
std::ostream & operator<<(std::ostream &stream, const ui_event event)
Definition: handler.cpp:919
static std::unique_ptr< events::event_context > event_context
Definition: handler.cpp:64
void init_mouse_location()
Initializes the location of the mouse.
Definition: handler.cpp:881
Generic file dialog.
bool is_in_dialog()
Is a dialog open?
Definition: handler.cpp:1074
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:168
bool execute_timer(const std::size_t id)
Executes a timer.
Definition: timer.cpp:197
const hotkey_command & get_hotkey_command(const std::string &command)
returns the hotkey_command with the given name
std::shared_ptr< class hotkey_base > hotkey_ptr
Definition: hotkey_item.hpp:27
const hotkey_ptr get_hotkey(const SDL_Event &event)
Iterate through the list of hotkeys and return a hotkey that matches the SDL_Event and the current ke...
constexpr auto reverse
Definition: ranges.hpp:40
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:140
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
Definition: video.cpp:432
std::string_view data
Definition: picture.cpp:178
HOTKEY_COMMAND command
The command associated with this hotkey.
Holds a 2D point.
Definition: point.hpp:25
mock_char c
Contains the gui2 timer routines.