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