The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
events.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef EVENTS_HPP_INCLUDED
16 #define EVENTS_HPP_INCLUDED
17 
18 #include <SDL_events.h>
19 #include <SDL_version.h>
20 #include <vector>
21 
22 //our user-defined double-click event type
23 #define DOUBLE_CLICK_EVENT SDL_USEREVENT
24 #define TIMER_EVENT (SDL_USEREVENT + 1)
25 #define HOVER_REMOVE_POPUP_EVENT (SDL_USEREVENT + 2)
26 #define DRAW_EVENT (SDL_USEREVENT + 3)
27 #define CLOSE_WINDOW_EVENT (SDL_USEREVENT + 4)
28 #define SHOW_HELPTIP_EVENT (SDL_USEREVENT + 5)
29 #define DRAW_ALL_EVENT (SDL_USEREVENT + 6)
30 
31 namespace events
32 {
33 
34 class sdl_handler;
35 
36 struct context
37 {
38  context() :
39  handlers(),
40  focused_handler(-1)
41  {
42  }
43 
44  void add_handler(sdl_handler* ptr);
45  bool remove_handler(sdl_handler* ptr);
46  int cycle_focus();
47  void set_focus(const sdl_handler* ptr);
48 
49  std::vector<sdl_handler*> handlers;
51 
52  void delete_handler_index(size_t handler);
53 };
54 
55 //any classes that derive from this class will automatically
56 //receive sdl events through the handle function for their lifetime,
57 //while the event context they were created in is active.
58 //
59 //NOTE: an event_context object must be initialized before a handler object
60 //can be initialized, and the event_context must be destroyed after
61 //the handler is destroyed.
63 {
64 public:
65  virtual void handle_event(const SDL_Event& event) = 0;
66  virtual void handle_window_event(const SDL_Event& event) = 0;
67  virtual void process_event() {}
68  virtual void draw() {}
69 
70  virtual void volatile_draw() {}
71  virtual void volatile_undraw() {}
72 
73  virtual bool requires_event_focus(const SDL_Event * = nullptr) const { return false; }
74 
75  virtual void process_help_string(int /*mousex*/, int /*mousey*/) {}
76  virtual void process_tooltip_string(int /*mousex*/, int /*mousey*/) {}
77 
78  virtual void join(); /*joins the current event context*/
79  virtual void join(context &c); /*joins the specified event context*/
80  virtual void join_same(sdl_handler* parent); /*joins the same event context as the parent is already associated with */
81  virtual void leave(); /*leave the event context*/
82 
83  virtual void join_global(); /*join the global event context*/
84  virtual void leave_global(); /*leave the global event context*/
85 
86 protected:
87  sdl_handler(const bool auto_join=true);
88  virtual ~sdl_handler();
89  virtual std::vector<sdl_handler*> handler_members()
90  {
91  return std::vector<sdl_handler*>();
92  }
93 
94 private:
97 };
98 
99 void focus_handler(const sdl_handler* ptr);
100 void cycle_focus();
101 
102 bool has_focus(const sdl_handler* ptr, const SDL_Event* event);
103 
104 //event_context objects control the handler objects that SDL events are sent
105 //to. When an event_context is created, it will become the current event context.
106 //event_context objects MUST be created in LIFO ordering in relation to each other,
107 //and in relation to handler objects. That is, all event_context objects should be
108 //created as automatic/stack variables.
109 //
110 //handler objects need not be created as automatic variables (e.g. you could put
111 //them in a vector) however you must guarantee that handler objects are destroyed
112 //before their context is destroyed
114 {
115  event_context();
116  ~event_context();
117 };
118 
119 //causes events to be dispatched to all handler objects.
120 void pump();
121 
122 //look for resize events and update references to the screen area
123 void peek_for_resize();
124 
125 struct pump_info {
127  std::pair<int,int> resize_dimensions;
128  int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1);
129 private:
130  int ticks_; //0 if not calculated
131 };
132 
134 //pump_monitors receive notification after an events::pump() occurs
135 public:
136  pump_monitor();
137  virtual ~pump_monitor();
138  virtual void process(pump_info& info) = 0;
139 };
140 
141 void raise_process_event();
142 void raise_resize_event();
143 void raise_draw_event();
144 void raise_draw_all_event();
148 void raise_help_string_event(int mousex, int mousey);
149 
150 
151 /**
152  * Is the event an input event?
153  *
154  * @returns Whether or not the event is an input event.
155  */
156 bool is_input(const SDL_Event& event);
157 
158 /** Discards all input events. */
159 void discard_input();
160 
161 }
162 
163 typedef std::vector<events::sdl_handler*> sdl_handler_vector;
164 
165 
166 #endif
void raise_resize_event()
Definition: events.cpp:553
void raise_volatile_undraw_event()
Definition: events.cpp:613
virtual void handle_window_event(const SDL_Event &event)=0
void discard_input()
Discards all input events.
Definition: events.cpp:657
static thandler * handler
Definition: handler.cpp:60
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
const GLfloat * c
Definition: glew.h:12741
bool has_joined_global_
Definition: events.hpp:96
virtual void process_help_string(int, int)
Definition: events.hpp:75
logger & info()
Definition: log.cpp:91
virtual void volatile_undraw()
Definition: events.hpp:71
int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1)
Definition: events.cpp:640
virtual void process(pump_info &info)=0
virtual void leave_global()
Definition: events.cpp:248
int cycle_focus()
Definition: events.cpp:93
virtual void draw()
Definition: events.hpp:68
virtual void process_event()
Definition: events.hpp:67
virtual void volatile_draw()
Definition: events.hpp:70
virtual void join_same(sdl_handler *parent)
Definition: events.cpp:195
void raise_draw_all_event()
Definition: events.cpp:579
void focus_handler(const sdl_handler *ptr)
Definition: events.cpp:262
virtual void handle_event(const SDL_Event &event)=0
std::vector< sdl_handler * > handlers
Definition: events.hpp:49
void raise_volatile_draw_all_event()
Definition: events.cpp:603
void set_focus(const sdl_handler *ptr)
Definition: events.cpp:110
virtual void join()
Definition: events.cpp:173
void peek_for_resize()
Definition: events.cpp:662
int focused_handler
Definition: events.hpp:50
void raise_draw_event()
Definition: events.cpp:565
void pump()
Definition: events.cpp:336
void delete_handler_index(size_t handler)
Definition: events.cpp:49
virtual ~sdl_handler()
Definition: events.cpp:163
void raise_process_event()
Definition: events.cpp:539
bool is_input(const SDL_Event &event)
Is the event an input event?
Definition: events.cpp:652
void raise_help_string_event(int mousex, int mousey)
Definition: events.cpp:627
virtual bool requires_event_focus(const SDL_Event *=nullptr) const
Definition: events.hpp:73
virtual void process_tooltip_string(int, int)
Definition: events.hpp:76
void raise_volatile_draw_event()
Definition: events.cpp:589
virtual std::vector< sdl_handler * > handler_members()
Definition: events.hpp:89
Handling of system events.
Definition: manager.hpp:42
sdl_handler(const bool auto_join=true)
Definition: events.cpp:151
cl_event event
Definition: glew.h:3070
void add_handler(sdl_handler *ptr)
Definition: events.cpp:44
bool has_focus(const sdl_handler *hand, const SDL_Event *event)
Definition: events.cpp:269
std::pair< int, int > resize_dimensions
Definition: events.hpp:127
virtual void leave()
Definition: events.cpp:212
bool remove_handler(sdl_handler *ptr)
Definition: events.cpp:60
void cycle_focus()
virtual ~pump_monitor()
Definition: events.cpp:134
virtual void join_global()
Definition: events.cpp:230