-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
handler.cpp
1015 lines (868 loc) · 22.3 KB
/
handler.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2009 - 2016 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/auxiliary/event/handler.hpp"
#include "gui/auxiliary/event/dispatcher.hpp"
#include "gui/auxiliary/timer.hpp"
#include "gui/auxiliary/log.hpp"
#include "gui/widgets/helper.hpp"
#include "gui/widgets/widget.hpp"
#include "gui/widgets/window.hpp"
#include "hotkey/hotkey_item.hpp"
#include "utils/foreach.tpp"
#include "video.hpp"
#include "serialization/unicode_cast.hpp"
#include <cassert>
/**
* @todo The items below are not implemented yet.
*
* - Tooltips have a fixed short time until showing up.
* - Tooltips are shown until the widget is exited.
* - Help messages aren't shown yet.
*
* @note it might be that tooltips will be shown independent of a window and in
* their own window, therefore the code will be cleaned up after that has been
* determined.
*/
/*
* At some point in the future this event handler should become the main event
* handler. This switch controls the experimental switch for that change.
*/
//#define MAIN_EVENT_HANDLER
/* Since this code is still very experimental it's not enabled yet. */
//#define ENABLE
namespace gui2
{
namespace event
{
/***** Static data. *****/
class thandler;
static thandler* handler = NULL;
static events::event_context* event_context = NULL;
#ifdef MAIN_EVENT_HANDLER
static unsigned draw_interval = 0;
static unsigned event_poll_interval = 0;
/***** Static functions. *****/
/**
* SDL_AddTimer() callback for the draw event.
*
* When this callback is called it pushes a new draw event in the event queue.
*
* @returns The new timer interval, 0 to stop.
*/
static Uint32 timer_sdl_draw_event(Uint32, void*)
{
// DBG_GUI_E << "Pushing draw event in queue.\n";
SDL_Event event;
SDL_UserEvent data;
data.type = DRAW_EVENT;
data.code = 0;
data.data1 = NULL;
data.data2 = NULL;
event.type = DRAW_EVENT;
event.user = data;
SDL_PushEvent(&event);
return draw_interval;
}
/**
* SDL_AddTimer() callback for the poll event.
*
* When this callback is called it will run the events in the SDL event queue.
*
* @returns The new timer interval, 0 to stop.
*/
static Uint32 timer_sdl_poll_events(Uint32, void*)
{
try
{
events::pump();
}
catch(CVideo::quit&)
{
return 0;
}
return event_poll_interval;
}
#endif
/***** thandler class. *****/
/**
* This singleton class handles all events.
*
* It's a new experimental class.
*/
class thandler : public events::sdl_handler
{
friend bool gui2::is_in_dialog();
public:
thandler();
~thandler();
/** Inherited from events::sdl_handler. */
void handle_event(const SDL_Event& event);
#if SDL_VERSION_ATLEAST(2, 0, 0)
/** Inherited from events::sdl_handler. */
void handle_window_event(const SDL_Event& event);
#endif
/**
* Connects a dispatcher.
*
* @param dispatcher The dispatcher to connect.
*/
void connect(tdispatcher* dispatcher);
/**
* Disconnects a dispatcher.
*
* @param dispatcher The dispatcher to disconnect.
*/
void disconnect(tdispatcher* dispatcher);
/** The dispatcher that captured the mouse focus. */
tdispatcher* mouse_focus;
private:
/**
* Reinitializes the state of all dispatchers.
*
* This is needed when the application gets activated, to make sure the
* state of mainly the mouse is set properly.
*/
void activate();
/***** Handlers *****/
/** Fires a draw event. */
using events::sdl_handler::draw;
void draw(const bool force);
/**
* Fires a video resize event.
*
* @param new_size The new size of the window.
*/
void video_resize(const tpoint& new_size);
/**
* Fires a generic mouse event.
*
* @param event The event to fire.
* @param position The position of the mouse.
*/
void mouse(const tevent event, const tpoint& position);
/**
* Fires a mouse button up event.
*
* @param position The position of the mouse.
* @param button The SDL id of the button that caused the
* event.
*/
void mouse_button_up(const tpoint& position, const Uint8 button);
/**
* Fires a mouse button down event.
*
* @param position The position of the mouse.
* @param button The SDL id of the button that caused the
* event.
*/
void mouse_button_down(const tpoint& position, const Uint8 button);
/**
* Fires a mouse wheel event.
*
* @param position The position of the mouse.
* @param scrollx The amount of horizontal scrolling.
* @param scrolly The amount of vertical scrolling.
*/
void mouse_wheel(const tpoint& position, int scrollx, int scrolly);
/**
* Gets the dispatcher that wants to receive the keyboard input.
*
* @returns The dispatcher.
* @retval NULL No dispatcher found.
*/
tdispatcher* keyboard_dispatcher();
/**
* Handles a hat motion event.
*
* @param event The SDL joystick hat event triggered.
*/
void hat_motion(const SDL_Event& event);
/**
* Handles a joystick button down event.
*
* @param event The SDL joystick button event triggered.
*/
void button_down(const SDL_Event& event);
/**
* Fires a key down event.
*
* @param event The SDL keyboard event triggered.
*/
void key_down(const SDL_Event& event);
/**
* Handles the pressing of a hotkey.
*
* @param key The hotkey item pressed.
*
* @returns True if the hotkey is handled false otherwise.
*/
bool hotkey_pressed(const hotkey::hotkey_ptr key);
/**
* Fires a key down event.
*
* @param key The SDL key code of the key pressed.
* @param modifier The SDL key modifiers used.
* @param unicode The unicode value for the key pressed.
*/
void key_down(const SDLKey key,
const SDLMod modifier,
const utf8::string& unicode);
/**
* Fires a text input event.
*
* @param unicode The unicode value for the text entered.
*/
void text_input(const std::string& unicode);
/**
* Fires a keyboard event which has no parameters.
*
* This can happen for example when the mouse wheel is used.
*
* @param event The event to fire.
*/
void keyboard(const tevent event);
/**
* The dispatchers.
*
* The order of the items in the list is also the z-order the front item
* being the one completely in the background and the back item the one
* completely in the foreground.
*/
std::vector<tdispatcher*> dispatchers_;
/**
* Needed to determine which dispatcher gets the keyboard events.
*
* NOTE the keyboard events aren't really wired in yet so doesn't do much.
*/
tdispatcher* keyboard_focus_;
friend void capture_keyboard(tdispatcher*);
};
thandler::thandler()
: events::sdl_handler(false)
, mouse_focus(NULL)
, dispatchers_()
, keyboard_focus_(NULL)
{
if(SDL_WasInit(SDL_INIT_TIMER) == 0) {
if(SDL_InitSubSystem(SDL_INIT_TIMER) == -1) {
assert(false);
}
}
// The event context is created now we join it.
#ifdef ENABLE
join();
#endif
}
thandler::~thandler()
{
#ifdef ENABLE
leave();
#endif
}
void thandler::handle_event(const SDL_Event& event)
{
/** No dispatchers drop the event. */
if(dispatchers_.empty()) {
return;
}
switch(event.type) {
case SDL_MOUSEMOTION:
mouse(SDL_MOUSE_MOTION, tpoint(event.motion.x, event.motion.y));
break;
case SDL_MOUSEBUTTONDOWN:
mouse_button_down(tpoint(event.button.x, event.button.y),
event.button.button);
break;
case SDL_MOUSEBUTTONUP:
mouse_button_up(tpoint(event.button.x, event.button.y),
event.button.button);
break;
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_MOUSEWHEEL:
mouse_wheel(get_mouse_position(), event.wheel.x, event.wheel.y);
break;
#endif
case SHOW_HELPTIP_EVENT:
mouse(SHOW_HELPTIP, get_mouse_position());
break;
case HOVER_REMOVE_POPUP_EVENT:
// remove_popup();
break;
case DRAW_EVENT:
draw(false);
break;
case DRAW_ALL_EVENT:
draw(true);
break;
case TIMER_EVENT:
execute_timer(reinterpret_cast<size_t>(event.user.data1));
break;
case CLOSE_WINDOW_EVENT: {
/** @todo Convert this to a proper new style event. */
DBG_GUI_E << "Firing " << CLOSE_WINDOW << ".\n";
twindow* window = twindow::window_instance(event.user.code);
if(window) {
window->set_retval(twindow::AUTO_CLOSE);
}
} break;
case SDL_JOYBUTTONDOWN:
button_down(event);
break;
case SDL_JOYBUTTONUP:
break;
case SDL_JOYAXISMOTION:
break;
case SDL_JOYHATMOTION:
hat_motion(event);
break;
case SDL_KEYDOWN:
key_down(event);
break;
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_WINDOWEVENT:
switch(event.window.event) {
case SDL_WINDOWEVENT_EXPOSED:
draw(true);
break;
case SDL_WINDOWEVENT_RESIZED:
video_resize(
tpoint(event.window.data1, event.window.data2));
break;
case SDL_WINDOWEVENT_ENTER:
case SDL_WINDOWEVENT_FOCUS_GAINED:
activate();
break;
}
break;
case SDL_TEXTINPUT:
text_input(event.text.text);
break;
#else
case SDL_VIDEOEXPOSE:
draw(true);
break;
case SDL_VIDEORESIZE:
video_resize(tpoint(event.resize.w, event.resize.h));
break;
case SDL_ACTIVEEVENT:
activate();
break;
#endif
#if(defined(_X11) && !defined(__APPLE__)) || defined(_WIN32)
case SDL_SYSWMEVENT:
/* DO NOTHING */
break;
#endif
// Silently ignored events.
case SDL_KEYUP:
case DOUBLE_CLICK_EVENT:
break;
default:
WRN_GUI_E << "Unhandled event " << static_cast<Uint32>(event.type)
<< ".\n";
break;
}
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
void thandler::handle_window_event(const SDL_Event& event)
{
handle_event(event);
}
#endif
void thandler::connect(tdispatcher* dispatcher)
{
assert(std::find(dispatchers_.begin(), dispatchers_.end(), dispatcher)
== dispatchers_.end());
if(dispatchers_.empty()) {
event_context = new events::event_context();
join();
}
dispatchers_.push_back(dispatcher);
}
void thandler::disconnect(tdispatcher* dispatcher)
{
/***** Validate pre conditions. *****/
std::vector<tdispatcher*>::iterator itor
= std::find(dispatchers_.begin(), dispatchers_.end(), dispatcher);
assert(itor != dispatchers_.end());
/***** Remove dispatcher. *****/
dispatchers_.erase(itor);
if(dispatcher == mouse_focus) {
mouse_focus = NULL;
}
if(dispatcher == keyboard_focus_) {
keyboard_focus_ = NULL;
}
/***** Set proper state for the other dispatchers. *****/
FOREACH(AUTO dispatcher, dispatchers_)
{
dynamic_cast<twidget&>(*dispatcher).set_is_dirty(true);
}
activate();
/***** Validate post conditions. *****/
assert(std::find(dispatchers_.begin(), dispatchers_.end(), dispatcher)
== dispatchers_.end());
if(dispatchers_.empty()) {
leave();
delete event_context;
event_context = NULL;
}
}
void thandler::activate()
{
FOREACH(AUTO dispatcher, dispatchers_)
{
dispatcher->fire(
SDL_ACTIVATE, dynamic_cast<twidget&>(*dispatcher), NULL);
}
}
void thandler::draw(const bool force)
{
// Don't display this event since it floods the screen
// DBG_GUI_E << "Firing " << DRAW << ".\n";
/*
* In normal draw mode the first window in not forced to be drawn the
* others are. So for forced mode we only need to force the first window to
* be drawn the others are already handled.
*/
bool first = !force;
/**
* @todo Need to evaluate which windows really to redraw.
*
* For now we use a hack, but would be nice to rewrite it for 1.9/1.11.
*/
FOREACH(AUTO dispatcher, dispatchers_)
{
if(!first) {
/*
* This leaves glitches on window borders if the window beneath it
* has changed, on the other hand invalidating twindown::restorer_
* causes black borders around the window. So there's the choice
* between two evils.
*/
dynamic_cast<twidget&>(*dispatcher).set_is_dirty(true);
} else {
first = false;
}
dispatcher->fire(DRAW, dynamic_cast<twidget&>(*dispatcher));
}
if(!dispatchers_.empty()) {
CVideo& video = dynamic_cast<twindow&>(*dispatchers_.back()).video();
#if !SDL_VERSION_ATLEAST(2,0,0)
surface& frame_buffer = video.getSurface();
cursor::draw(frame_buffer);
#endif
video.flip();
#if !SDL_VERSION_ATLEAST(2,0,0)
cursor::undraw(frame_buffer);
#endif
}
}
void thandler::video_resize(const tpoint& new_size)
{
DBG_GUI_E << "Firing: " << SDL_VIDEO_RESIZE << ".\n";
FOREACH(AUTO dispatcher, dispatchers_)
{
dispatcher->fire(SDL_VIDEO_RESIZE,
dynamic_cast<twidget&>(*dispatcher),
new_size);
}
}
void thandler::mouse(const tevent event, const tpoint& position)
{
DBG_GUI_E << "Firing: " << event << ".\n";
if(mouse_focus) {
mouse_focus->fire(
event, dynamic_cast<twidget&>(*mouse_focus), position);
} else {
for(std::vector<tdispatcher*>::reverse_iterator ritor
= dispatchers_.rbegin();
ritor != dispatchers_.rend();
++ritor) {
if((**ritor).get_mouse_behavior() == tdispatcher::all) {
(**ritor)
.fire(event, dynamic_cast<twidget&>(**ritor), position);
break;
}
if((**ritor).get_mouse_behavior() == tdispatcher::none) {
continue;
}
if((**ritor).is_at(position)) {
(**ritor)
.fire(event, dynamic_cast<twidget&>(**ritor), position);
break;
}
}
}
}
void thandler::mouse_button_up(const tpoint& position, const Uint8 button)
{
switch(button) {
case SDL_BUTTON_LEFT:
mouse(SDL_LEFT_BUTTON_UP, position);
break;
case SDL_BUTTON_MIDDLE:
mouse(SDL_MIDDLE_BUTTON_UP, position);
break;
case SDL_BUTTON_RIGHT:
mouse(SDL_RIGHT_BUTTON_UP, position);
break;
#if !SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_BUTTON_WHEELLEFT:
mouse(SDL_WHEEL_LEFT, get_mouse_position());
break;
case SDL_BUTTON_WHEELRIGHT:
mouse(SDL_WHEEL_RIGHT, get_mouse_position());
break;
case SDL_BUTTON_WHEELUP:
mouse(SDL_WHEEL_UP, get_mouse_position());
break;
case SDL_BUTTON_WHEELDOWN:
mouse(SDL_WHEEL_DOWN, get_mouse_position());
break;
#endif
default:
WRN_GUI_E << "Unhandled 'mouse button up' event for button "
<< static_cast<Uint32>(button) << ".\n";
break;
}
}
void thandler::mouse_button_down(const tpoint& position, const Uint8 button)
{
// The wheel buttons generate and up and down event we handle the
// up event so ignore the mouse if it's a down event. Handle it
// here to avoid a warning.
if(button == SDL_BUTTON_WHEELUP || button == SDL_BUTTON_WHEELDOWN
|| button == SDL_BUTTON_WHEELLEFT || button == SDL_BUTTON_WHEELRIGHT) {
return;
}
switch(button) {
case SDL_BUTTON_LEFT:
mouse(SDL_LEFT_BUTTON_DOWN, position);
break;
case SDL_BUTTON_MIDDLE:
mouse(SDL_MIDDLE_BUTTON_DOWN, position);
break;
case SDL_BUTTON_RIGHT:
mouse(SDL_RIGHT_BUTTON_DOWN, position);
break;
default:
WRN_GUI_E << "Unhandled 'mouse button down' event for button "
<< static_cast<Uint32>(button) << ".\n";
break;
}
}
#if SDL_VERSION_ATLEAST(2, 0, 0)
void thandler::mouse_wheel(const tpoint& position, int x, int y)
{
if(x > 0) {
mouse(SDL_WHEEL_RIGHT, position);
} else if(x < 0) {
mouse(SDL_WHEEL_LEFT, position);
}
if(y < 0) {
mouse(SDL_WHEEL_DOWN, position);
} else if(y > 0) {
mouse(SDL_WHEEL_UP, position);
}
}
#endif
tdispatcher* thandler::keyboard_dispatcher()
{
if(keyboard_focus_) {
return keyboard_focus_;
}
for(std::vector<tdispatcher*>::reverse_iterator ritor
= dispatchers_.rbegin();
ritor != dispatchers_.rend();
++ritor) {
if((**ritor).get_want_keyboard_input()) {
return *ritor;
}
}
return NULL;
}
void thandler::hat_motion(const SDL_Event& event)
{
const hotkey::hotkey_ptr& hk = hotkey::get_hotkey(event);
bool done = false;
if(!hk->null()) {
done = hotkey_pressed(hk);
}
if(!done) {
// TODO fendrin think about handling hat motions that are not bound to a
// hotkey.
}
}
void thandler::button_down(const SDL_Event& event)
{
const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
bool done = false;
if(!hk->null()) {
done = hotkey_pressed(hk);
}
if(!done) {
// TODO fendrin think about handling button down events that are not
// bound to a hotkey.
}
}
void thandler::key_down(const SDL_Event& event)
{
const hotkey::hotkey_ptr hk = hotkey::get_hotkey(event);
bool done = false;
if(!hk->null()) {
done = hotkey_pressed(hk);
}
if(!done) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
key_down(event.key.keysym.sym,
static_cast<const SDL_Keymod>(event.key.keysym.mod),
"");
#else
key_down(event.key.keysym.sym,
event.key.keysym.mod,
unicode_cast<std::string>(static_cast<ucs4::char_t>(event.key.keysym.unicode)));
#endif
}
}
void thandler::text_input(const std::string& unicode)
{
key_down(static_cast<SDLKey>(0), static_cast<SDLMod>(0), unicode);
}
bool thandler::hotkey_pressed(const hotkey::hotkey_ptr key)
{
tdispatcher* dispatcher = keyboard_dispatcher();
if(!dispatcher) {
return false;
}
return dispatcher->execute_hotkey(hotkey::get_id(key->get_command()));
}
void thandler::key_down(const SDLKey key,
const SDLMod modifier,
const utf8::string& unicode)
{
DBG_GUI_E << "Firing: " << SDL_KEY_DOWN << ".\n";
if(tdispatcher* dispatcher = keyboard_dispatcher()) {
dispatcher->fire(SDL_KEY_DOWN,
dynamic_cast<twidget&>(*dispatcher),
key,
modifier,
unicode);
}
}
void thandler::keyboard(const tevent event)
{
DBG_GUI_E << "Firing: " << event << ".\n";
if(tdispatcher* dispatcher = keyboard_dispatcher()) {
dispatcher->fire(event, dynamic_cast<twidget&>(*dispatcher));
}
}
/***** tmanager class. *****/
tmanager::tmanager()
{
handler = new thandler();
#ifdef MAIN_EVENT_HANDLER
draw_interval = 30;
SDL_AddTimer(draw_interval, timer_sdl_draw_event, NULL);
event_poll_interval = 10;
SDL_AddTimer(event_poll_interval, timer_sdl_poll_events, NULL);
#endif
}
tmanager::~tmanager()
{
delete handler;
handler = NULL;
#ifdef MAIN_EVENT_HANDLER
draw_interval = 0;
event_poll_interval = 0;
#endif
}
/***** free functions class. *****/
void connect_dispatcher(tdispatcher* dispatcher)
{
assert(handler);
assert(dispatcher);
handler->connect(dispatcher);
}
void disconnect_dispatcher(tdispatcher* dispatcher)
{
assert(handler);
assert(dispatcher);
handler->disconnect(dispatcher);
}
void init_mouse_location()
{
tpoint mouse = get_mouse_position();
SDL_Event event;
event.type = SDL_MOUSEMOTION;
event.motion.type = SDL_MOUSEMOTION;
event.motion.x = mouse.x;
event.motion.y = mouse.y;
SDL_PushEvent(&event);
}
void capture_mouse(tdispatcher* dispatcher)
{
assert(handler);
assert(dispatcher);
handler->mouse_focus = dispatcher;
}
void release_mouse(tdispatcher* dispatcher)
{
assert(handler);
assert(dispatcher);
if(handler->mouse_focus == dispatcher) {
handler->mouse_focus = NULL;
}
}
void capture_keyboard(tdispatcher* dispatcher)
{
assert(handler);
assert(dispatcher);
assert(dispatcher->get_want_keyboard_input());
handler->keyboard_focus_ = dispatcher;
}
std::ostream& operator<<(std::ostream& stream, const tevent event)
{
switch(event) {
case DRAW:
stream << "draw";
break;
case CLOSE_WINDOW:
stream << "close window";
break;
case SDL_VIDEO_RESIZE:
stream << "SDL video resize";
break;
case SDL_MOUSE_MOTION:
stream << "SDL mouse motion";
break;
case MOUSE_ENTER:
stream << "mouse enter";
break;
case MOUSE_LEAVE:
stream << "mouse leave";
break;
case MOUSE_MOTION:
stream << "mouse motion";
break;
case SDL_LEFT_BUTTON_DOWN:
stream << "SDL left button down";
break;
case SDL_LEFT_BUTTON_UP:
stream << "SDL left button up";
break;
case LEFT_BUTTON_DOWN:
stream << "left button down";
break;
case LEFT_BUTTON_UP:
stream << "left button up";
break;
case LEFT_BUTTON_CLICK:
stream << "left button click";
break;
case LEFT_BUTTON_DOUBLE_CLICK:
stream << "left button double click";
break;
case SDL_MIDDLE_BUTTON_DOWN:
stream << "SDL middle button down";
break;
case SDL_MIDDLE_BUTTON_UP:
stream << "SDL middle button up";
break;
case MIDDLE_BUTTON_DOWN:
stream << "middle button down";
break;
case MIDDLE_BUTTON_UP:
stream << "middle button up";
break;
case MIDDLE_BUTTON_CLICK:
stream << "middle button click";
break;
case MIDDLE_BUTTON_DOUBLE_CLICK:
stream << "middle button double click";
break;
case SDL_RIGHT_BUTTON_DOWN:
stream << "SDL right button down";
break;
case SDL_RIGHT_BUTTON_UP:
stream << "SDL right button up";
break;
case RIGHT_BUTTON_DOWN:
stream << "right button down";
break;
case RIGHT_BUTTON_UP:
stream << "right button up";
break;
case RIGHT_BUTTON_CLICK:
stream << "right button click";
break;
case RIGHT_BUTTON_DOUBLE_CLICK:
stream << "right button double click";
break;
case SDL_WHEEL_LEFT:
stream << "SDL wheel left";
break;
case SDL_WHEEL_RIGHT:
stream << "SDL wheel right";
break;
case SDL_WHEEL_UP:
stream << "SDL wheel up";
break;
case SDL_WHEEL_DOWN:
stream << "SDL wheel down";
break;
case SDL_KEY_DOWN:
stream << "SDL key down";
break;
case NOTIFY_REMOVAL:
stream << "notify removal";
break;
case NOTIFY_MODIFIED:
stream << "notify modified";
break;
case RECEIVE_KEYBOARD_FOCUS:
stream << "receive keyboard focus";
break;
case LOSE_KEYBOARD_FOCUS:
stream << "lose keyboard focus";
break;
case SHOW_TOOLTIP:
stream << "show tooltip";
break;
case NOTIFY_REMOVE_TOOLTIP:
stream << "notify remove tooltip";
break;
case SDL_ACTIVATE:
stream << "SDL activate";
break;
case MESSAGE_SHOW_TOOLTIP:
stream << "message show tooltip";
break;
case SHOW_HELPTIP:
stream << "show helptip";
break;
case MESSAGE_SHOW_HELPTIP:
stream << "message show helptip";
break;
case REQUEST_PLACEMENT: