The Battle for Wesnoth  1.17.0-dev
events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 DRAW_ALL_EVENT (SDL_USEREVENT + 6)
31 #define INVOKE_FUNCTION_EVENT (SDL_USEREVENT + 7)
32 
33 namespace events
34 {
35 
37 
38 typedef std::list<sdl_handler*> handler_list;
39 
40 class context
41 {
42 public:
43  context() :
44  handlers(),
47  {
48  }
49 
50  ~context();
51 
52  context(const context&) = delete;
53 
54  void add_handler(sdl_handler* ptr);
55  /** Returns true if @a ptr is found in either the handlers or staging_handlers lists */
56  bool has_handler(const sdl_handler* ptr) const;
57  bool remove_handler(sdl_handler* ptr);
58  void cycle_focus();
59  void set_focus(const sdl_handler* ptr);
60  void add_staging_handlers();
61 
62  handler_list handlers;
64  std::vector<sdl_handler*> staging_handlers;
65 };
66 
67 //any classes that derive from this class will automatically
68 //receive sdl events through the handle function for their lifetime,
69 //while the event context they were created in is active.
70 //
71 //NOTE: an event_context object must be initialized before a handler object
72 //can be initialized, and the event_context must be destroyed after
73 //the handler is destroyed.
75 {
76 friend class context;
77 public:
78  virtual void handle_event(const SDL_Event& event) = 0;
79  virtual void handle_window_event(const SDL_Event& event) = 0;
80  virtual void process_event() {}
81  virtual void draw() {}
82 
83  virtual void volatile_draw() {}
84  virtual void volatile_undraw() {}
85 
86  virtual bool requires_event_focus(const SDL_Event * = nullptr) const { return false; }
87 
88  virtual void process_help_string(int /*mousex*/, int /*mousey*/) {}
89  virtual void process_tooltip_string(int /*mousex*/, int /*mousey*/) {}
90 
91  virtual void join(); /*joins the current event context*/
92  virtual void join(context &c); /*joins the specified event context*/
93  virtual void join_same(sdl_handler* parent); /*joins the same event context as the parent is already associated with */
94  virtual void leave(); /*leave the event context*/
95 
96  virtual void join_global(); /*join the global event context*/
97  virtual void leave_global(); /*leave the global event context*/
98 
99  virtual bool has_joined() { return has_joined_;}
100  virtual bool has_joined_global() { return has_joined_global_;}
101 
102  /**
103  * Moving would require two instances' context membership to be handled,
104  * it's simpler to delete these and require the two instances to be
105  * separately constructed / destructed.
106  */
107  sdl_handler &operator=(sdl_handler &&) = delete;
108  sdl_handler(sdl_handler &&) = delete;
109 
110 protected:
111  sdl_handler(const bool auto_join=true);
112  sdl_handler(const sdl_handler &);
113  sdl_handler &operator=(const sdl_handler &);
114  virtual ~sdl_handler();
115  virtual std::vector<sdl_handler*> handler_members()
116  {
117  return std::vector<sdl_handler*>();
118  }
119 
120 private:
123 };
124 
125 void focus_handler(const sdl_handler* ptr);
126 
127 bool has_focus(const sdl_handler* ptr, const SDL_Event* event);
128 
129 void call_in_main_thread(const std::function<void (void)>& f);
130 
131 //event_context objects control the handler objects that SDL events are sent
132 //to. When an event_context is created, it will become the current event context.
133 //event_context objects MUST be created in LIFO ordering in relation to each other,
134 //and in relation to handler objects. That is, all event_context objects should be
135 //created as automatic/stack variables.
136 //
137 //handler objects need not be created as automatic variables (e.g. you could put
138 //them in a vector) however you must guarantee that handler objects are destroyed
139 //before their context is destroyed
141 {
142  event_context();
143  ~event_context();
144 };
145 
146 //causes events to be dispatched to all handler objects.
147 void pump();
148 
149 //look for resize events and update references to the screen area
150 void peek_for_resize();
151 
152 struct pump_info {
153  pump_info() : resize_dimensions(), ticks_(0) {}
154  std::pair<int,int> resize_dimensions;
155  int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1);
156 private:
157  int ticks_; //0 if not calculated
158 };
159 
161 //pump_monitors receive notification after an events::pump() occurs
162 public:
163  pump_monitor();
164  virtual ~pump_monitor();
165  virtual void process(pump_info& info) = 0;
166 };
167 
168 void raise_process_event();
169 void raise_resize_event();
170 void raise_draw_event();
171 void raise_draw_all_event();
175 void raise_help_string_event(int mousex, int mousey);
176 
177 
178 /**
179  * Is the event an input event?
180  *
181  * @returns Whether or not the event is an input event.
182  */
183 bool is_input(const SDL_Event& event);
184 
185 /** Discards all input events. */
186 void discard_input();
187 
188 }
189 
190 typedef std::vector<events::sdl_handler*> sdl_handler_vector;
void raise_resize_event()
Definition: events.cpp:762
void raise_volatile_undraw_event()
Definition: events.cpp:814
void discard_input()
Discards all input events.
Definition: events.cpp:851
void set_focus(const sdl_handler *ptr)
Definition: events.cpp:175
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:190
std::string join(const T &v, const std::string &s=",")
Generates a new string joining container items in a list.
virtual void process_help_string(int, int)
Definition: events.hpp:88
logger & info()
Definition: log.cpp:89
virtual void volatile_undraw()
Definition: events.hpp:84
void add_handler(sdl_handler *ptr)
Definition: events.cpp:78
virtual bool has_joined_global()
Definition: events.hpp:100
virtual void draw()
Definition: events.hpp:81
bool remove_handler(sdl_handler *ptr)
Definition: events.cpp:95
virtual void process_event()
Definition: events.hpp:80
virtual void volatile_draw()
Definition: events.hpp:83
void cycle_focus()
Definition: events.cpp:142
void call_in_main_thread(const std::function< void(void)> &f)
Definition: events.cpp:867
void raise_draw_all_event()
Definition: events.cpp:787
void process(int mousex, int mousey)
Definition: tooltips.cpp:194
void focus_handler(const sdl_handler *ptr)
Definition: events.cpp:394
static events::event_context * event_context
Definition: handler.cpp:63
void add_staging_handlers()
Definition: events.cpp:183
void raise_volatile_draw_all_event()
Definition: events.cpp:805
virtual bool requires_event_focus(const SDL_Event *=nullptr) const
Definition: events.hpp:86
std::list< sdl_handler * > handler_list
Definition: events.hpp:36
void peek_for_resize()
Definition: events.cpp:856
virtual bool has_joined()
Definition: events.hpp:99
void raise_draw_event()
Definition: events.cpp:774
void pump()
Definition: events.cpp:473
void raise_process_event()
Definition: events.cpp:751
bool is_input(const SDL_Event &event)
Is the event an input event?
Definition: events.cpp:846
std::vector< sdl_handler * > staging_handlers
Definition: events.hpp:64
void raise_help_string_event(int mousex, int mousey)
Definition: events.cpp:823
virtual void process_tooltip_string(int, int)
Definition: events.hpp:89
void raise_volatile_draw_event()
Definition: events.cpp:796
virtual std::vector< sdl_handler * > handler_members()
Definition: events.hpp:115
handler_list::iterator focused_handler
Definition: events.hpp:63
Handling of system events.
Definition: manager.hpp:43
#define f
bool has_focus(const sdl_handler *hand, const SDL_Event *event)
Definition: events.cpp:401
std::pair< int, int > resize_dimensions
Definition: events.hpp:154
handler_list handlers
Definition: events.hpp:62
mock_char c
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:87