The Battle for Wesnoth  1.19.14+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  break;
510 
511  default:
512 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
513  WRN_GUI_E << "Unhandled event " << static_cast<uint32_t>(event.type)
514  << ".";
515 #endif
516  break;
517  }
518 
519  raw_event(event);
520 }
521 
522 void sdl_event_handler::handle_window_event(const SDL_Event& event)
523 {
524  handle_event(event);
525 }
526 
528 {
530  DBG_GUI_E << "adding dispatcher " << static_cast<void*>(dispatcher);
531 
532  if(dispatchers_.empty()) {
533  LOG_GUI_E << "creating new dispatcher event context";
534  event_context = std::make_unique<events::event_context>();
535  join();
536  }
537 
538  dispatchers_.push_back(dispatcher);
539 }
540 
542 {
543  /***** Validate pre conditions. *****/
544  auto itor = std::find(dispatchers_.begin(), dispatchers_.end(), disp);
545  assert(itor != dispatchers_.end());
546 
547  DBG_GUI_E << "removing dispatcher " << static_cast<void*>(disp);
548 
549  /***** Remove dispatcher. *****/
550  dispatchers_.erase(itor);
551 
552  if(disp == mouse_focus) {
553  mouse_focus = nullptr;
554  }
555  if(disp == keyboard_focus_) {
556  keyboard_focus_ = nullptr;
557  }
558 
559  // TODO: draw_manager - Why TF was this "activate"ing on "disconnect"? Seriously WTF?
560  //activate();
561 
562  /***** Validate post conditions. *****/
563  assert(!utils::contains(dispatchers_, disp));
564 
565  if(dispatchers_.empty()) {
566  LOG_GUI_E << "deleting unused dispatcher event context";
567  leave();
568  event_context = nullptr;
569  }
570 }
571 
573 {
574  for(auto dispatcher : dispatchers_)
575  {
576  dispatcher->fire(SDL_ACTIVATE, dynamic_cast<widget&>(*dispatcher), nullptr);
577  }
578 }
579 
581 {
582  DBG_GUI_E << "Firing: " << SDL_VIDEO_RESIZE << ".";
583 
584  for(auto dispatcher : dispatchers_)
585  {
586  dispatcher->fire(SDL_VIDEO_RESIZE, dynamic_cast<widget&>(*dispatcher), new_size);
587  }
588 }
589 
590 void sdl_event_handler::raw_event(const SDL_Event& event) {
591  DBG_GUI_E << "Firing raw event";
592 
593  for(auto dispatcher : dispatchers_)
594  {
595  dispatcher->fire(SDL_RAW_EVENT, dynamic_cast<widget&>(*dispatcher), event);
596  }
597 }
598 
599 void sdl_event_handler::mouse(const ui_event event, const point& position)
600 {
601  DBG_GUI_E << "Firing: " << event << ".";
602 
603  if(mouse_focus) {
604  mouse_focus->fire(event, dynamic_cast<widget&>(*mouse_focus), position);
605  return;
606  }
607 
610  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher), position);
611  break;
612  }
613 
615  continue;
616  }
617 
618  if(dispatcher->is_at(position)) {
619  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher), position);
620  break;
621  }
622  }
623 }
624 
625 void sdl_event_handler::mouse_button_up(const point& position, const uint8_t button)
626 {
627  switch(button) {
628  case SDL_BUTTON_LEFT:
629  mouse(SDL_LEFT_BUTTON_UP, position);
630  break;
631  case SDL_BUTTON_MIDDLE:
632  mouse(SDL_MIDDLE_BUTTON_UP, position);
633  break;
634  case SDL_BUTTON_RIGHT:
635  mouse(SDL_RIGHT_BUTTON_UP, position);
636  break;
637  case SDL_BUTTON_X1:
638  mouse(SDL_BACK_BUTTON_UP, position);
639  break;
640  case SDL_BUTTON_X2:
641  mouse(SDL_FORWARD_BUTTON_UP, position);
642  break;
643  default:
644 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
645  WRN_GUI_E << "Unhandled 'mouse button up' event for button "
646  << static_cast<uint32_t>(button) << ".";
647 #endif
648  break;
649  }
650 }
651 
652 void sdl_event_handler::mouse_button_down(const point& position, const uint8_t button)
653 {
654  switch(button) {
655  case SDL_BUTTON_LEFT:
656  mouse(SDL_LEFT_BUTTON_DOWN, position);
657  break;
658  case SDL_BUTTON_MIDDLE:
659  mouse(SDL_MIDDLE_BUTTON_DOWN, position);
660  break;
661  case SDL_BUTTON_RIGHT:
662  mouse(SDL_RIGHT_BUTTON_DOWN, position);
663  break;
664  case SDL_BUTTON_X1:
665  mouse(SDL_BACK_BUTTON_DOWN, position);
666  break;
667  case SDL_BUTTON_X2:
668  mouse(SDL_FORWARD_BUTTON_DOWN, position);
669  break;
670  default:
671 #ifdef GUI2_SHOW_UNHANDLED_EVENT_WARNINGS
672  WRN_GUI_E << "Unhandled 'mouse button down' event for button "
673  << static_cast<uint32_t>(button) << ".";
674 #endif
675  break;
676  }
677 }
678 
679 void sdl_event_handler::mouse_wheel(const point& position, int x, int y)
680 {
681  if(x > 0) {
682  mouse(SDL_WHEEL_RIGHT, position);
683  } else if(x < 0) {
684  mouse(SDL_WHEEL_LEFT, position);
685  }
686 
687  if(y < 0) {
688  mouse(SDL_WHEEL_DOWN, position);
689  } else if(y > 0) {
690  mouse(SDL_WHEEL_UP, position);
691  }
692 }
693 
695 {
696  if(keyboard_focus_) {
697  return keyboard_focus_;
698  }
699 
702  return dispatcher;
703  }
704  }
705 
706  return nullptr;
707 }
708 
709 void sdl_event_handler::touch_motion(const point& position, const point& distance)
710 {
712  dispatcher->fire(SDL_TOUCH_MOTION , dynamic_cast<widget&>(*dispatcher), position, distance);
713  }
714 }
715 
716 void sdl_event_handler::touch_up(const point& position)
717 {
719  dispatcher->fire(SDL_TOUCH_UP, dynamic_cast<widget&>(*dispatcher), position);
720  }
721 }
722 
724 {
726  dispatcher->fire(SDL_TOUCH_DOWN, dynamic_cast<widget&>(*dispatcher), position);
727  }
728 }
729 
730 void sdl_event_handler::touch_multi_gesture(const point& center, float dTheta, float dDist, uint8_t numFingers)
731 {
733  dispatcher->fire(SDL_TOUCH_MULTI_GESTURE, dynamic_cast<widget&>(*dispatcher), center, dTheta, dDist, numFingers);
734  }
735 }
736 
737 void sdl_event_handler::hat_motion(const SDL_Event& event)
738 {
739  const hotkey::hotkey_ptr& hk = hotkey::get_hotkey(event);
740  bool done = false;
741  if(!hk->null()) {
742  done = hotkey_pressed(hk);
743  }
744  if(!done) {
745  // TODO fendrin think about handling hat motions that are not bound to a
746  // hotkey.
747  }
748 }
749 
750 void sdl_event_handler::button_down(const SDL_Event& event)
751 {
752  const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
753  bool done = false;
754  if(!hk->null()) {
755  done = hotkey_pressed(hk);
756  }
757  if(!done) {
758  // TODO fendrin think about handling button down events that are not
759  // bound to a hotkey.
760  }
761 }
762 
763 void sdl_event_handler::key_down(const SDL_Event& event)
764 {
765  const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
766  bool done = false;
767  if(!hk->null()) {
768  done = hotkey_pressed(hk);
769  }
770  if(!done) {
771  if(event.type == SDL_TEXTINPUT) {
772  text_input(event.text.text);
773  } else {
774  key_down(event.key.keysym.sym, static_cast<SDL_Keymod>(event.key.keysym.mod), "");
775  }
776  }
777 }
778 
779 void sdl_event_handler::text_input(const std::string& unicode)
780 {
781  key_down(SDLK_UNKNOWN, static_cast<SDL_Keymod>(0), unicode);
782 
785  dynamic_cast<widget&>(*dispatcher),
786  unicode, -1, -1);
787  }
788 }
789 
790 void sdl_event_handler::text_editing(const std::string& unicode, int32_t start, int32_t len)
791 {
794  dynamic_cast<widget&>(*dispatcher),
795  unicode, start, len);
796  }
797 }
798 
800 {
802  return dispatcher->execute_hotkey(hotkey::get_hotkey_command(key->get_command()).command);
803  }
804 
805  return false;
806 }
807 
808 void sdl_event_handler::key_down(const SDL_Keycode key,
809  const SDL_Keymod modifier,
810  const std::string& unicode)
811 {
812  DBG_GUI_E << "Firing: " << SDL_KEY_DOWN << ".";
813 
816  dynamic_cast<widget&>(*dispatcher),
817  key,
818  modifier,
819  unicode);
820  }
821 }
822 
824 {
825  DBG_GUI_E << "Firing: " << event << ".";
826 
828  dispatcher->fire(event, dynamic_cast<widget&>(*dispatcher));
829  }
830 }
831 
832 void sdl_event_handler::close_window(const unsigned window_id)
833 {
834  DBG_GUI_E << "Firing " << CLOSE_WINDOW << ".";
835 
836  window* window = window::window_instance(window_id);
837  if(window) {
839  }
840 }
841 
842 /***** manager class. *****/
843 
845 {
846  handler_.reset(new sdl_event_handler());
847 
848 #ifdef MAIN_EVENT_HANDLER
849  draw_interval = 30;
850  SDL_AddTimer(draw_interval, timer_sdl_draw_event, nullptr);
851 
852  event_poll_interval = 10;
853  SDL_AddTimer(event_poll_interval, timer_sdl_poll_events, nullptr);
854 #endif
855 }
856 
858 {
859  handler_.reset(nullptr);
860 
861 #ifdef MAIN_EVENT_HANDLER
862  draw_interval = 0;
863  event_poll_interval = 0;
864 #endif
865 }
866 
867 /***** free functions class. *****/
868 
870 {
871  assert(handler_);
872  assert(dispatcher);
873  handler_->connect(dispatcher);
874 }
875 
877 {
878  assert(handler_);
879  assert(dispatcher);
880  handler_->disconnect(dispatcher);
881 }
882 
883 std::vector<dispatcher*>& get_all_dispatchers()
884 {
885  assert(handler_);
886  return handler_->get_dispatchers();
887 }
888 
890 {
892 
893  SDL_Event event{};
894  event.type = SDL_MOUSEMOTION;
895  event.motion.type = SDL_MOUSEMOTION;
896  event.motion.x = mouse.x;
897  event.motion.y = mouse.y;
898 
899  SDL_PushEvent(&event);
900 }
901 
903 {
904  assert(handler_);
905  assert(dispatcher);
906  handler_->mouse_focus = dispatcher;
907 }
908 
910 {
911  assert(handler_);
912  assert(dispatcher);
913  if(handler_->mouse_focus == dispatcher) {
914  handler_->mouse_focus = nullptr;
915  }
916 }
917 
919 {
920  assert(handler_);
921  assert(dispatcher);
923 
924  handler_->keyboard_focus_ = dispatcher;
925 }
926 
927 std::ostream& operator<<(std::ostream& stream, const ui_event event)
928 {
929  switch(event) {
930  case DRAW:
931  stream << "draw";
932  break;
933  case CLOSE_WINDOW:
934  stream << "close window";
935  break;
936  case SDL_VIDEO_RESIZE:
937  stream << "SDL video resize";
938  break;
939  case SDL_MOUSE_MOTION:
940  stream << "SDL mouse motion";
941  break;
942  case MOUSE_ENTER:
943  stream << "mouse enter";
944  break;
945  case MOUSE_LEAVE:
946  stream << "mouse leave";
947  break;
948  case MOUSE_MOTION:
949  stream << "mouse motion";
950  break;
952  stream << "SDL left button down";
953  break;
954  case SDL_LEFT_BUTTON_UP:
955  stream << "SDL left button up";
956  break;
957  case LEFT_BUTTON_DOWN:
958  stream << "left button down";
959  break;
960  case LEFT_BUTTON_UP:
961  stream << "left button up";
962  break;
963  case LEFT_BUTTON_CLICK:
964  stream << "left button click";
965  break;
967  stream << "left button double click";
968  break;
970  stream << "SDL middle button down";
971  break;
973  stream << "SDL middle button up";
974  break;
975  case MIDDLE_BUTTON_DOWN:
976  stream << "middle button down";
977  break;
978  case MIDDLE_BUTTON_UP:
979  stream << "middle button up";
980  break;
981  case MIDDLE_BUTTON_CLICK:
982  stream << "middle button click";
983  break;
985  stream << "middle button double click";
986  break;
988  stream << "SDL right button down";
989  break;
990  case SDL_RIGHT_BUTTON_UP:
991  stream << "SDL right button up";
992  break;
993  case RIGHT_BUTTON_DOWN:
994  stream << "right button down";
995  break;
996  case RIGHT_BUTTON_UP:
997  stream << "right button up";
998  break;
999  case RIGHT_BUTTON_CLICK:
1000  stream << "right button click";
1001  break;
1003  stream << "right button double click";
1004  break;
1006  stream << "SDL forward/5 button down";
1007  break;
1008  case SDL_FORWARD_BUTTON_UP:
1009  stream << "SDL forward/5 button up";
1010  break;
1011  case FORWARD_BUTTON_DOWN:
1012  stream << "forward/5 button down";
1013  break;
1014  case FORWARD_BUTTON_UP:
1015  stream << "forward/5 button up";
1016  break;
1017  case FORWARD_BUTTON_CLICK:
1018  stream << "forward/5 button click";
1019  break;
1021  stream << "forward/5 button double click";
1022  break;
1023  case SDL_BACK_BUTTON_DOWN:
1024  stream << "SDL back/4 button down";
1025  break;
1026  case SDL_BACK_BUTTON_UP:
1027  stream << "SDL back/4 button up";
1028  break;
1029  case BACK_BUTTON_DOWN:
1030  stream << "back/4 button down";
1031  break;
1032  case BACK_BUTTON_UP:
1033  stream << "back/4 button up";
1034  break;
1035  case BACK_BUTTON_CLICK:
1036  stream << "back/4 button click";
1037  break;
1039  stream << "back/4 button double click";
1040  break;
1041  case SDL_WHEEL_LEFT:
1042  stream << "SDL wheel left";
1043  break;
1044  case SDL_WHEEL_RIGHT:
1045  stream << "SDL wheel right";
1046  break;
1047  case SDL_WHEEL_UP:
1048  stream << "SDL wheel up";
1049  break;
1050  case SDL_WHEEL_DOWN:
1051  stream << "SDL wheel down";
1052  break;
1053  case SDL_KEY_DOWN:
1054  stream << "SDL key down";
1055  break;
1056  case SDL_TEXT_INPUT:
1057  stream << "SDL text input";
1058  break;
1059  case SDL_TEXT_EDITING:
1060  stream << "SDL text editing";
1061  break;
1062 
1063  case NOTIFY_REMOVAL:
1064  stream << "notify removal";
1065  break;
1066  case NOTIFY_MODIFIED:
1067  stream << "notify modified";
1068  break;
1070  stream << "receive keyboard focus";
1071  break;
1072  case LOSE_KEYBOARD_FOCUS:
1073  stream << "lose keyboard focus";
1074  break;
1075  case SHOW_TOOLTIP:
1076  stream << "show tooltip";
1077  break;
1078  case NOTIFY_REMOVE_TOOLTIP:
1079  stream << "notify remove tooltip";
1080  break;
1081  case SDL_ACTIVATE:
1082  stream << "SDL activate";
1083  break;
1084  case MESSAGE_SHOW_TOOLTIP:
1085  stream << "message show tooltip";
1086  break;
1087  case SHOW_HELPTIP:
1088  stream << "show helptip";
1089  break;
1090  case MESSAGE_SHOW_HELPTIP:
1091  stream << "message show helptip";
1092  break;
1093  case REQUEST_PLACEMENT:
1094  stream << "request placement";
1095  break;
1096  case SDL_TOUCH_MOTION:
1097  stream << "SDL touch motion";
1098  break;
1099  case SDL_TOUCH_UP:
1100  stream << "SDL touch up";
1101  break;
1102  case SDL_TOUCH_DOWN:
1103  stream << "SDL touch down";
1104  break;
1106  stream << "SDL multi-touch gesture";
1107  break;
1108  case SDL_RAW_EVENT:
1109  stream << "SDL raw event";
1110  break;
1111  }
1112 
1113  return stream;
1114 }
1115 
1116 } // namespace event
1117 
1119 {
1120  return !event::get_all_dispatchers().empty();
1121 }
1122 
1123 } // namespace gui2
virtual void join()
Definition: events.cpp:305
virtual void leave()
Definition: events.cpp:350
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:723
void keyboard(const ui_event event)
Fires a keyboard event which has no parameters.
Definition: handler.cpp:823
void activate()
Reinitializes the state of all dispatchers.
Definition: handler.cpp:572
void disconnect(dispatcher *dispatcher)
Disconnects a dispatcher.
Definition: handler.cpp:541
void mouse_button_up(const point &position, const uint8_t button)
Fires a mouse button up event.
Definition: handler.cpp:625
void close_window(const unsigned window_id)
Fires a CLOSE_WINDOW event for the window with the given ID.
Definition: handler.cpp:832
void handle_window_event(const SDL_Event &event) override
Inherited from events::sdl_handler.
Definition: handler.cpp:522
void video_resize(const point &new_size)
Fires a video resize event.
Definition: handler.cpp:580
void text_editing(const std::string &unicode, int32_t start, int32_t len)
Fires a text editing event.
Definition: handler.cpp:790
void touch_up(const point &position)
Fires a touch "finger up" event.
Definition: handler.cpp:716
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:694
friend void capture_keyboard(dispatcher *dispatcher)
Captures the keyboard.
Definition: handler.cpp:918
void text_input(const std::string &unicode)
Fires a text input event.
Definition: handler.cpp:779
void touch_motion(const point &position, const point &distance)
Fires a touch-moved event.
Definition: handler.cpp:709
bool hotkey_pressed(const hotkey::hotkey_ptr &key)
Handles the pressing of a hotkey.
Definition: handler.cpp:799
void mouse_button_down(const point &position, const uint8_t button)
Fires a mouse button down event.
Definition: handler.cpp:652
void hat_motion(const SDL_Event &event)
Handles a hat motion event.
Definition: handler.cpp:737
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:527
void key_down(const SDL_Event &event)
Fires a key down event.
Definition: handler.cpp:763
void mouse(const ui_event event, const point &position)
Fires a generic mouse event.
Definition: handler.cpp:599
void touch_multi_gesture(const point &center, float dTheta, float dDist, uint8_t numFingers)
Fires a touch gesture event.
Definition: handler.cpp:730
void mouse_wheel(const point &position, int scrollx, int scrolly)
Fires a mouse wheel event.
Definition: handler.cpp:679
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:750
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:590
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:328
#define DRAW_EVENT
Definition: events.hpp:26
#define CLOSE_WINDOW_EVENT
Definition: events.hpp:27
#define SHOW_HELPTIP_EVENT
Definition: events.hpp:28
#define TIMER_EVENT
Definition: events.hpp:24
#define HOVER_REMOVE_POPUP_EVENT
Definition: events.hpp:25
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:214
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:480
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
@ MIDDLE_BUTTON_DOWN
Definition: handler.hpp:124
@ SDL_FORWARD_BUTTON_DOWN
Definition: handler.hpp:156
@ NOTIFY_REMOVE_TOOLTIP
Definition: handler.hpp:176
@ FORWARD_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:141
@ FORWARD_BUTTON_CLICK
Definition: handler.hpp:140
@ RIGHT_BUTTON_UP
Definition: handler.hpp:129
@ SDL_RIGHT_BUTTON_DOWN
Definition: handler.hpp:150
@ SDL_TOUCH_DOWN
Definition: handler.hpp:166
@ MIDDLE_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:127
@ BACK_BUTTON_DOWN
Definition: handler.hpp:134
@ NOTIFY_MODIFIED
Definition: handler.hpp:175
@ FORWARD_BUTTON_DOWN
Definition: handler.hpp:138
@ BACK_BUTTON_CLICK
Definition: handler.hpp:136
@ MIDDLE_BUTTON_CLICK
Definition: handler.hpp:126
@ BACK_BUTTON_UP
Definition: handler.hpp:135
@ SDL_LEFT_BUTTON_DOWN
Definition: handler.hpp:146
@ SDL_TEXT_INPUT
An SDL text input (commit) event.
Definition: handler.hpp:170
@ SDL_WHEEL_DOWN
Definition: handler.hpp:162
@ 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:178
@ SDL_MOUSE_MOTION
Definition: handler.hpp:144
@ SDL_TEXT_EDITING
An SDL text editing (IME) event.
Definition: handler.hpp:171
@ LEFT_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:123
@ RIGHT_BUTTON_DOWN
Definition: handler.hpp:128
@ NOTIFY_REMOVAL
Definition: handler.hpp:174
@ RECEIVE_KEYBOARD_FOCUS
Definition: handler.hpp:177
@ SDL_TOUCH_MOTION
Definition: handler.hpp:184
@ SDL_WHEEL_RIGHT
Definition: handler.hpp:160
@ SDL_VIDEO_RESIZE
Definition: handler.hpp:143
@ BACK_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:137
@ LEFT_BUTTON_CLICK
Definition: handler.hpp:122
@ SDL_WHEEL_LEFT
Definition: handler.hpp:159
@ SDL_LEFT_BUTTON_UP
Definition: handler.hpp:147
@ SDL_TOUCH_MULTI_GESTURE
Definition: handler.hpp:185
@ REQUEST_PLACEMENT
Definition: handler.hpp:180
@ SDL_MIDDLE_BUTTON_UP
Definition: handler.hpp:149
@ SDL_BACK_BUTTON_DOWN
Definition: handler.hpp:154
@ SDL_BACK_BUTTON_UP
Definition: handler.hpp:155
@ FORWARD_BUTTON_UP
Definition: handler.hpp:139
@ LEFT_BUTTON_DOWN
Definition: handler.hpp:120
@ SDL_MIDDLE_BUTTON_DOWN
Definition: handler.hpp:148
@ SDL_RIGHT_BUTTON_UP
Definition: handler.hpp:151
@ RIGHT_BUTTON_DOUBLE_CLICK
Definition: handler.hpp:131
@ MESSAGE_SHOW_TOOLTIP
Definition: handler.hpp:181
@ MESSAGE_SHOW_HELPTIP
Definition: handler.hpp:182
@ SDL_FORWARD_BUTTON_UP
Definition: handler.hpp:157
void capture_mouse(dispatcher *dispatcher)
Captures the mouse.
Definition: handler.cpp:902
void release_mouse(dispatcher *dispatcher)
Releases a captured mouse.
Definition: handler.cpp:909
void disconnect_dispatcher(dispatcher *dispatcher)
Disconnects a dispatcher to the event handler.
Definition: handler.cpp:876
static std::unique_ptr< class sdl_event_handler > handler_
Definition: handler.cpp:63
void capture_keyboard(dispatcher *dispatcher)
Captures the keyboard.
Definition: handler.cpp:918
@ mouse
Callbacks with a coordinate as extra parameter.
void connect_dispatcher(dispatcher *dispatcher)
Connects a dispatcher to the event handler.
Definition: handler.cpp:869
std::vector< dispatcher * > & get_all_dispatchers()
Gets all event dispatchers in the Z order.
Definition: handler.cpp:883
std::ostream & operator<<(std::ostream &stream, const ui_event event)
Definition: handler.cpp:927
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:889
Generic file dialog.
bool is_in_dialog()
Is a dialog open?
Definition: handler.cpp:1118
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
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...
const hotkey_command & get_hotkey_command(std::string_view command)
Returns the hotkey_command with the given id.
constexpr auto reverse
Definition: ranges.hpp:40
bool contains(const Container &container, const Value &value)
Returns true iff value is found in container.
Definition: general.hpp:87
auto * find(Container &container, const Value &value)
Convenience wrapper for using find on a container without needing to comare to end()
Definition: general.hpp:141
point game_canvas_size()
The size of the game canvas, in drawing coordinates / game pixels.
Definition: video.cpp:449
std::string_view data
Definition: picture.cpp:188
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.