-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
dispatcher_private.hpp
430 lines (368 loc) · 13.7 KB
/
dispatcher_private.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
Copyright (C) 2009 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "gui/core/event/dispatcher.hpp"
#include "gui/widgets/widget.hpp"
#include <SDL2/SDL_events.h>
#include <boost/range/adaptor/reversed.hpp>
namespace gui2
{
namespace event
{
struct dispatcher_implementation
{
/**
* Helper macro to implement the various event_signal functions.
*
* Implements two helper functions as documented in the macro.
*
* @param SET The set in which the event type needs to be.
* @param FUNCTION The function signature to validate the
* implementation function SFINAE against eg the
* @ref gui2::event::signal_function or another
* one in that header.
* @param QUEUE The queue in which the @p event is slotted.
*/
#define IMPLEMENT_EVENT_SIGNAL(SET, FUNCTION, QUEUE) \
/** \
* Returns the signal structure for a FUNCTION. \
* \
* There are several functions that only overload the return value, in \
* order to do so they use SFINAE. \
* \
* @tparam F signal_function. \
* @param dispatcher The dispatcher whose signal queue is used. \
* @param event The event to get the signal for. \
* \
* @returns The signal of the type \
* dispatcher::signal_type<FUNCTION> \
*/ \
template<typename F> \
static std::enable_if_t<std::is_same_v<F, FUNCTION>, dispatcher::signal_type<FUNCTION>>& \
event_signal(dispatcher& dispatcher, const ui_event event) \
{ \
return dispatcher.QUEUE.queue[event]; \
} \
IMPLEMENT_EVENT_SIGNAL(set_event, signal_function, signal_queue_)
/**
* Small helper macro to wrap @ref IMPLEMENT_EVENT_SIGNAL.
*
* Since the parameters to @ref IMPLEMENT_EVENT_SIGNAL use the same parameters
* with a slight difference per type this macro wraps the function by its type.
*
* @param TYPE The type to wrap for @ref
* IMPLEMENT_EVENT_SIGNAL.
*/
#define IMPLEMENT_EVENT_SIGNAL_WRAPPER(TYPE) \
IMPLEMENT_EVENT_SIGNAL(set_event_##TYPE, \
signal_##TYPE##_function, \
signal_##TYPE##_queue_)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(mouse)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(keyboard)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(touch_motion)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(touch_gesture)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(notification)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(message)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(raw_event)
IMPLEMENT_EVENT_SIGNAL_WRAPPER(text_input)
#undef IMPLEMENT_EVENT_SIGNAL_WRAPPER
#undef IMPLEMENT_EVENT_SIGNAL
#define IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(TYPE) \
else if(is_##TYPE##_event(event)) { \
return queue_check(dispatcher.signal_##TYPE##_queue_); \
}
/**
* A helper to test whether dispatcher has an handler for a certain event.
*
* @param dispatcher The dispatcher whose signal queue is used.
* @param queue_type The type of event to look for.
* @param event The event to get the signal for.
*
* @returns Whether or not the handler is found.
*/
static bool has_handler(dispatcher& dispatcher, const dispatcher::event_queue_type queue_type, ui_event event)
{
const auto queue_check = [&](auto& queue_set) {
return !queue_set.queue[event].empty(queue_type);
};
if(is_general_event(event)) {
return queue_check(dispatcher.signal_queue_);
}
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(mouse)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(keyboard)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(touch_motion)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(touch_gesture)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(notification)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(message)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(raw_event)
IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK(text_input)
return false;
}
#undef IMPLEMENT_RUNTIME_EVENT_SIGNAL_CHECK
};
namespace implementation
{
/*
* Small sample to illustrate the effects of the various build_event_chain
* functions. Assume the widgets are in an window with the following widgets:
*
* -----------------------
* | dispatcher |
* | ------------------- |
* | | container 1 | |
* | | --------------- | |
* | | | container 2 | | |
* | | | ----------- | | |
* | | | | widget | | | |
* | | | ----------- | | |
* | | --------------- | |
* | ------------------- |
* -----------------------
*
* Note that the firing routine fires the events from:
* - pre child for chain.end() - > chain.begin()
* - child for widget
* - post child for chain.begin() -> chain.end()
*/
/**
* Build the event chain.
*
* The event chain is a chain of events starting from the first parent of the
* widget until (and including) the wanted parent. For all these widgets it
* will be tested whether they have either a pre or post handler for the event.
* This ways there will be list of widgets to try to send the events to.
* If there's no line from widget to parent the result is undefined.
* (If widget == dispatcher the result will always be empty.)
*
* @pre dispatcher != nullptr
* @pre widget != nullptr
*
* @param event The event to test.
* @param dispatcher The final widget to test, this is also the
* dispatcher the sends the event.
* @param w The widget should parent(s) to check.
*
* @returns The list of widgets with a handler.
* The order will be (assuming all have a
* handler):
* * container 2
* * container 1
* * dispatcher
*/
template<typename T>
inline std::vector<std::pair<widget*, ui_event>>
build_event_chain(const ui_event event, widget* dispatcher, widget* w)
{
assert(dispatcher);
assert(w);
std::vector<std::pair<widget*, ui_event>> result;
while(true) {
if(w->has_event(event, dispatcher::event_queue_type(dispatcher::pre | dispatcher::post))) {
result.emplace_back(w, event);
}
if(w == dispatcher) {
break;
}
w = w->parent();
assert(w);
}
return result;
}
/**
* Build the event chain for signal_notification_function.
*
* The notification is only send to the receiver it returns an empty chain.
* Since the pre and post queues are unused, it validates whether they are
* empty (using asserts).
*
* @returns An empty vector.
*/
template<>
inline std::vector<std::pair<widget*, ui_event>>
build_event_chain<signal_notification_function>(const ui_event event, widget* dispatcher, widget* w)
{
assert(dispatcher);
assert(w);
assert(!w->has_event(event, dispatcher::event_queue_type(dispatcher::pre | dispatcher::post)));
return std::vector<std::pair<widget*, ui_event>>();
}
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4706)
#endif
/**
* Build the event chain for signal_message_function.
*
* This function expects that the widget sending it is also the receiver. This
* assumption might change, but is valid for now. The function doesn't build an
* event chain from @p dispatcher to @p widget but from @p widget to its
* toplevel item (the first one without a parent) which we call @p window.
*
* @pre dispatcher == widget
*
* @returns The list of widgets with a handler.
* The order will be (assuming all have a
* handler):
* * window
* * container 1
* * container 2
*/
template<>
inline std::vector<std::pair<widget*, ui_event>>
build_event_chain<signal_message_function>(const ui_event event, widget* dispatcher, widget* w)
{
assert(dispatcher);
assert(w);
assert(w == dispatcher);
std::vector<std::pair<widget*, ui_event>> result;
/* We only should add the parents of the widget to the chain. */
while((w = w->parent())) {
assert(w);
if(w->has_event(event, dispatcher::event_queue_type(dispatcher::pre | dispatcher::post))) {
result.emplace(result.begin(), w, event);
}
}
return result;
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
/**
* Helper function for fire_event.
*
* This is called with the same parameters as fire_event except for the
* event_chain, which contains the widgets with the events to call for them.
*/
template<typename T, typename... F>
inline bool fire_event(const ui_event event,
std::vector<std::pair<widget*, ui_event>>& event_chain,
widget* dispatcher,
widget* w,
F&&... params)
{
bool handled = false;
bool halt = false;
/***** ***** ***** Pre ***** ***** *****/
for(auto& ritor_widget : boost::adaptors::reverse(event_chain)) {
auto& signal = dispatcher_implementation::event_signal<T>(*ritor_widget.first, ritor_widget.second);
for(auto& pre_func : signal.pre_child) {
pre_func(*dispatcher, ritor_widget.second, handled, halt, std::forward<F>(params)...);
if(halt) {
assert(handled);
break;
}
}
if(handled) {
return true;
}
}
/***** ***** ***** Child ***** ***** *****/
if(w->has_event(event, dispatcher::child)) {
auto& signal = dispatcher_implementation::event_signal<T>(*w, event);
for(auto& func : signal.child) {
func(*dispatcher, event, handled, halt, std::forward<F>(params)...);
if(halt) {
assert(handled);
break;
}
}
if(handled) {
return true;
}
}
/***** ***** ***** Post ***** ***** *****/
for(auto& itor_widget : event_chain) {
auto& signal = dispatcher_implementation::event_signal<T>(*itor_widget.first, itor_widget.second);
for(auto& post_func : signal.post_child) {
post_func(*dispatcher, itor_widget.second, handled, halt, std::forward<F>(params)...);
if(halt) {
assert(handled);
break;
}
}
if(handled) {
return true;
}
}
/**** ***** ***** Unhandled ***** ***** *****/
assert(handled == false);
return false;
}
} // namespace implementation
/**
* Fires an event.
*
* A helper to allow the common event firing code to be shared between the
* different signal function types.
*
* @pre d != nullptr
* @pre w != nullptr
*
* @tparam T The signal type of the event to handle.
* @tparam F The parameter pack type.
*
*
* @param event The event to fire.
* @param d The dispatcher that handles the event.
* @param w The widget that should receive the event.
* @param params Zero or more additional arguments to pass
* to the signal function when it's executed.
*
* @returns Whether or not the event was handled.
*/
template<typename T, typename... F>
inline bool
fire_event(const ui_event event, dispatcher* d, widget* w, F&&... params)
{
assert(d);
assert(w);
widget* dispatcher_w = dynamic_cast<widget*>(d);
std::vector<std::pair<widget*, ui_event>> event_chain =
implementation::build_event_chain<T>(event, dispatcher_w, w);
return implementation::fire_event<T>(event, event_chain, dispatcher_w, w, std::forward<F>(params)...);
}
template<ui_event click,
ui_event double_click,
bool(event_executor::*wants_double_click)() const,
typename T,
typename... F>
inline bool
fire_event_double_click(dispatcher* dsp, widget* wgt, F&&... params)
{
assert(dsp);
assert(wgt);
std::vector<std::pair<widget*, ui_event>> event_chain;
widget* w = wgt;
widget* d = dynamic_cast<widget*>(dsp);
while(w != d) {
w = w->parent();
assert(w);
if((w->*wants_double_click)()) {
if(w->has_event(double_click, dispatcher::event_queue_type(dispatcher::pre | dispatcher::post))) {
event_chain.emplace_back(w, double_click);
}
} else {
if(w->has_event(click, dispatcher::event_queue_type(dispatcher::pre | dispatcher::post))) {
event_chain.emplace_back(w, click);
}
}
}
if((wgt->*wants_double_click)()) {
return implementation::fire_event<T>(
double_click, event_chain, d, wgt, std::forward<F>(params)...);
} else {
return implementation::fire_event<T>(
click, event_chain, d, wgt, std::forward<F>(params)...);
}
}
} // namespace event
} // namespace gui2