The Battle for Wesnoth  1.17.10+dev
events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <dave@whitevine.net>
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 #pragma once
17 
18 #include <SDL2/SDL_events.h>
19 #include <vector>
20 #include <list>
21 #include <functional>
22 
23 //our user-defined double-click event type
24 #define DOUBLE_CLICK_EVENT SDL_USEREVENT
25 #define TIMER_EVENT (SDL_USEREVENT + 1)
26 #define HOVER_REMOVE_POPUP_EVENT (SDL_USEREVENT + 2)
27 #define DRAW_EVENT (SDL_USEREVENT + 3)
28 #define CLOSE_WINDOW_EVENT (SDL_USEREVENT + 4)
29 #define SHOW_HELPTIP_EVENT (SDL_USEREVENT + 5)
30 #define INVOKE_FUNCTION_EVENT (SDL_USEREVENT + 6)
31 
32 namespace events
33 {
34 
36 
37 typedef std::list<sdl_handler*> handler_list;
38 
39 class context
40 {
41 public:
42  context() :
43  handlers(),
46  {
47  }
48 
49  ~context();
50 
51  context(const context&) = delete;
52 
53  void add_handler(sdl_handler* ptr);
54  /** Returns true if @a ptr is found in either the handlers or staging_handlers lists */
55  bool has_handler(const sdl_handler* ptr) const;
56  bool remove_handler(sdl_handler* ptr);
57  void cycle_focus();
58  void set_focus(const sdl_handler* ptr);
59  void add_staging_handlers();
60 
61  handler_list handlers;
63  std::vector<sdl_handler*> staging_handlers;
64 };
65 
66 //any classes that derive from this class will automatically
67 //receive sdl events through the handle function for their lifetime,
68 //while the event context they were created in is active.
69 //
70 //NOTE: an event_context object must be initialized before a handler object
71 //can be initialized, and the event_context must be destroyed after
72 //the handler is destroyed.
74 {
75 friend class context;
76 public:
77  virtual void handle_event(const SDL_Event& event) = 0;
78  virtual void handle_window_event(const SDL_Event&) {};
79  virtual void process_event() {}
80 
81  virtual bool requires_event_focus(const SDL_Event * = nullptr) const { return false; }
82 
83  virtual void process_help_string(int /*mousex*/, int /*mousey*/) {}
84  virtual void process_tooltip_string(int /*mousex*/, int /*mousey*/) {}
85 
86  virtual void join(); /*joins the current event context*/
87  virtual void join(context &c); /*joins the specified event context*/
88  virtual void join_same(sdl_handler* parent); /*joins the same event context as the parent is already associated with */
89  virtual void leave(); /*leave the event context*/
90 
91  virtual void join_global(); /*join the global event context*/
92  virtual void leave_global(); /*leave the global event context*/
93 
94  virtual bool has_joined() { return has_joined_;}
95  virtual bool has_joined_global() { return has_joined_global_;}
96 
97  /**
98  * Moving would require two instances' context membership to be handled,
99  * it's simpler to delete these and require the two instances to be
100  * separately constructed / destructed.
101  */
102  sdl_handler &operator=(sdl_handler &&) = delete;
103  sdl_handler(sdl_handler &&) = delete;
104 
105 protected:
106  sdl_handler(const bool auto_join=true);
107  sdl_handler(const sdl_handler &);
108  sdl_handler &operator=(const sdl_handler &);
109  virtual ~sdl_handler();
110  virtual std::vector<sdl_handler*> handler_members()
111  {
112  return std::vector<sdl_handler*>();
113  }
114 
115 private:
118 };
119 
120 void focus_handler(const sdl_handler* ptr);
121 
122 bool has_focus(const sdl_handler* ptr, const SDL_Event* event);
123 
124 // whether the currently executing thread is the main thread.
125 bool is_in_main_thread();
126 
127 void call_in_main_thread(const std::function<void (void)>& f);
128 
129 //event_context objects control the handler objects that SDL events are sent
130 //to. When an event_context is created, it will become the current event context.
131 //event_context objects MUST be created in LIFO ordering in relation to each other,
132 //and in relation to handler objects. That is, all event_context objects should be
133 //created as automatic/stack variables.
134 //
135 //handler objects need not be created as automatic variables (e.g. you could put
136 //them in a vector) however you must guarantee that handler objects are destroyed
137 //before their context is destroyed
139 {
140  event_context();
141  ~event_context();
142 };
143 
144 /** Process all events currently in the queue. */
145 void pump();
146 
147 /** Trigger a draw cycle. */
148 void draw();
149 
150 /** pump() then immediately draw() */
151 inline void pump_and_draw() { pump(); draw(); }
152 // TODO: draw_manager - should this also raise_process_event? Some things do some don't
153 
154 struct pump_info {
155  pump_info() : resize_dimensions(), ticks_(0) {}
156  std::pair<int,int> resize_dimensions;
157  int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1);
158 private:
159  int ticks_; //0 if not calculated
160 };
161 
163 //pump_monitors receive notification after an events::pump() occurs
164 public:
165  pump_monitor();
166  virtual ~pump_monitor();
167  virtual void process(pump_info& info) = 0;
168 };
169 
170 void raise_process_event();
171 void raise_resize_event();
172 void raise_help_string_event(int mousex, int mousey);
173 
174 
175 /**
176  * Is the event an input event?
177  *
178  * @returns Whether or not the event is an input event.
179  */
180 bool is_input(const SDL_Event& event);
181 
182 /** Discards all input events. */
183 void discard_input();
184 
185 }
186 
187 typedef std::vector<events::sdl_handler*> sdl_handler_vector;
void raise_resize_event()
Definition: events.cpp:759
void discard_input()
Discards all input events.
Definition: events.cpp:800
void set_focus(const sdl_handler *ptr)
Definition: events.cpp:185
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:187
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
void pump_and_draw()
pump() then immediately draw()
Definition: events.hpp:151
virtual void process_help_string(int, int)
Definition: events.hpp:83
logger & info()
Definition: log.cpp:182
void add_handler(sdl_handler *ptr)
Definition: events.cpp:88
virtual bool has_joined_global()
Definition: events.hpp:95
bool remove_handler(sdl_handler *ptr)
Definition: events.cpp:105
virtual void process_event()
Definition: events.hpp:79
void cycle_focus()
Definition: events.cpp:152
void call_in_main_thread(const std::function< void(void)> &f)
Definition: events.cpp:805
bool is_in_main_thread()
Definition: events.cpp:473
void process(int mousex, int mousey)
Definition: tooltips.cpp:278
void focus_handler(const sdl_handler *ptr)
Definition: events.cpp:404
static events::event_context * event_context
Definition: handler.cpp:65
void add_staging_handlers()
Definition: events.cpp:193
virtual bool requires_event_focus(const SDL_Event *=nullptr) const
Definition: events.hpp:81
std::list< sdl_handler * > handler_list
Definition: events.hpp:35
virtual bool has_joined()
Definition: events.hpp:94
void pump()
Process all events currently in the queue.
Definition: events.cpp:478
virtual void handle_window_event(const SDL_Event &)
Definition: events.hpp:78
void raise_process_event()
Definition: events.cpp:748
bool is_input(const SDL_Event &event)
Is the event an input event?
Definition: events.cpp:795
std::vector< sdl_handler * > staging_handlers
Definition: events.hpp:63
void raise_help_string_event(int mousex, int mousey)
Definition: events.cpp:772
virtual void process_tooltip_string(int, int)
Definition: events.hpp:84
virtual std::vector< sdl_handler * > handler_members()
Definition: events.hpp:110
handler_list::iterator focused_handler
Definition: events.hpp:62
Handling of system events.
Definition: manager.hpp:43
#define f
bool has_focus(const sdl_handler *hand, const SDL_Event *event)
Definition: events.cpp:411
std::pair< int, int > resize_dimensions
Definition: events.hpp:156
handler_list handlers
Definition: events.hpp:61
mock_char c
void draw()
Trigger a draw cycle.
Definition: events.cpp:743
std::string::const_iterator iterator
Definition: tokenizer.hpp:25
bool has_handler(const sdl_handler *ptr) const
Returns true if ptr is found in either the handlers or staging_handlers lists.
Definition: events.cpp:97