Repository navigation
Expand file tree
/
Copy pathevent_handler.cpp
More file actions
1001 lines (867 loc) · 36.3 KB
/
Copy pathevent_handler.cpp
File metadata and controls
1001 lines (867 loc) · 36.3 KB
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
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2010-2015 Marianne Gagnon
//
// 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 3
// 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; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "guiengine/event_handler.hpp"
#include "audio/music_manager.hpp"
#include "audio/sfx_manager.hpp"
#include "config/player_manager.hpp"
#include "config/user_config.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/stk_tex_manager.hpp"
#include "guiengine/abstract_state_manager.hpp"
#include "guiengine/engine.hpp"
#include "guiengine/modaldialog.hpp"
#include "guiengine/screen.hpp"
#include "guiengine/screen_keyboard.hpp"
#include "guiengine/widget.hpp"
#include "guiengine/widgets/list_widget.hpp"
#include "guiengine/widgets/ribbon_widget.hpp"
#include "guiengine/widgets/spinner_widget.hpp"
#include "input/input_manager.hpp"
#include "modes/demo_world.hpp"
#include "modes/world.hpp"
#include "network/rewind_manager.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/debug.hpp"
#include "utils/profiler.hpp"
#include <IGUIEnvironment.h>
#include <IGUIListBox.h>
#include <iostream>
using GUIEngine::EventHandler;
using GUIEngine::EventPropagation;
using GUIEngine::NavigationDirection;
using namespace irr::gui;
// -----------------------------------------------------------------------------
EventHandler::EventHandler()
{
m_accept_events = false;
}
// -----------------------------------------------------------------------------
EventHandler::~EventHandler()
{
}
// -----------------------------------------------------------------------------
bool EventHandler::OnEvent (const SEvent &event)
{
if (!m_accept_events && event.EventType != EET_LOG_TEXT_EVENT) return true;
if(!Debug::onEvent(event))
return false;
if (ScreenKeyboard::isActive())
{
if (ScreenKeyboard::getCurrent()->onEvent(event))
return true; // EVENT_BLOCK
}
// TO DEBUG HATS (when you don't actually have a hat)
/*
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if (event.KeyInput.Key == 'W')
{
printf("Sending hat up event %i\n", event.KeyInput.PressedDown);
SEvent evt2;
evt2.EventType = EET_JOYSTICK_INPUT_EVENT;
for (int n=0; n<SEvent::SJoystickEvent::NUMBER_OF_AXES; n++)
{
evt2.JoystickEvent.Axis[n] = 0.0f;
}
evt2.JoystickEvent.ButtonStates = 0;
evt2.JoystickEvent.Joystick = 0;
evt2.JoystickEvent.POV = (event.KeyInput.PressedDown ? 0 : 65535); // 0 degrees
OnEvent(evt2);
return false;
}
else if (event.KeyInput.Key == 'D')
{
printf("Sending hat right event %i\n", event.KeyInput.PressedDown);
SEvent evt2;
evt2.EventType = EET_JOYSTICK_INPUT_EVENT;
for (int n=0; n<SEvent::SJoystickEvent::NUMBER_OF_AXES; n++)
{
evt2.JoystickEvent.Axis[n] = 0.0f;
}
evt2.JoystickEvent.ButtonStates = 0;
evt2.JoystickEvent.Joystick = 0;
evt2.JoystickEvent.POV = (event.KeyInput.PressedDown ? 9000 : 65535); // 90 degrees
OnEvent(evt2);
return false;
}
else if (event.KeyInput.Key == 'S')
{
printf("Sending hat down event %i\n", event.KeyInput.PressedDown);
SEvent evt2;
evt2.EventType = EET_JOYSTICK_INPUT_EVENT;
for (int n=0; n<SEvent::SJoystickEvent::NUMBER_OF_AXES; n++)
{
evt2.JoystickEvent.Axis[n] = 0.0f;
}
evt2.JoystickEvent.ButtonStates = 0;
evt2.JoystickEvent.Joystick = 0;
evt2.JoystickEvent.POV = (event.KeyInput.PressedDown ? 18000 : 65535); // 180 degrees
OnEvent(evt2);
return false;
}
else if (event.KeyInput.Key == 'A')
{
printf("Sending hat left event %i\n", event.KeyInput.PressedDown);
SEvent evt2;
evt2.EventType = EET_JOYSTICK_INPUT_EVENT;
for (int n=0; n<SEvent::SJoystickEvent::NUMBER_OF_AXES; n++)
{
evt2.JoystickEvent.Axis[n] = 0.0f;
}
evt2.JoystickEvent.ButtonStates = 0;
evt2.JoystickEvent.Joystick = 0;
evt2.JoystickEvent.POV = (event.KeyInput.PressedDown ? 27000 : 65535); // 270 degrees
OnEvent(evt2);
return false;
}
}
*/
// We do this (seemingly) overzealously to make sure that:
// 1. It resets on any GUI events
// 2. It resets on any mouse/joystick movement
// 3. It resets on any keyboard presses
if ((StateManager::get()->getGameState() == MENU)
&& (event.EventType != EET_LOG_TEXT_EVENT )
&& (event.EventType != EET_USER_EVENT ))
{
DemoWorld::resetIdleTime();
}
if (event.EventType == EET_GUI_EVENT)
{
return onGUIEvent(event) == EVENT_BLOCK;
}
else if (GUIEngine::getStateManager()->getGameState() != GUIEngine::GAME &&
event.EventType != EET_KEY_INPUT_EVENT && event.EventType != EET_JOYSTICK_INPUT_EVENT &&
event.EventType != EET_LOG_TEXT_EVENT)
{
return false; // EVENT_LET
}
else if (event.EventType == EET_MOUSE_INPUT_EVENT ||
event.EventType == EET_TOUCH_INPUT_EVENT ||
event.EventType == EET_KEY_INPUT_EVENT ||
event.EventType == EET_JOYSTICK_INPUT_EVENT ||
event.EventType == EET_ACCELEROMETER_EVENT ||
event.EventType == EET_GYROSCOPE_EVENT)
{
// Remember the mouse position
if (event.EventType == EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_MOUSE_MOVED)
{
m_mouse_pos.X = event.MouseInput.X;
m_mouse_pos.Y = event.MouseInput.Y;
}
// Notify the profiler of mouse events
if(UserConfigParams::m_profiler_enabled &&
event.EventType == EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
{
profiler.onClick(m_mouse_pos);
}
if (event.EventType == EET_MOUSE_INPUT_EVENT &&
event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN &&
StateManager::get()->getGameState() == GAME)
{
World::getWorld()->onMouseClick(event.MouseInput.X, event.MouseInput.Y);
}
if (UserConfigParams::m_keyboard_debug)
{
Log::verbose("keyboard", "char %d key %d ctrl %d down %d shift %d",
event.KeyInput.Char, event.KeyInput.Key,
event.KeyInput.Control, event.KeyInput.PressedDown,
event.KeyInput.Shift);
}
// FIXME? it may be a bit unclean that all input events go trough
// the gui module
const EventPropagation blockPropagation = input_manager->input(event);
if (event.EventType == EET_KEY_INPUT_EVENT &&
event.KeyInput.Key == irr::IRR_KEY_TAB)
{
// block all tab events, if we let them go, irrlicht will try
// to apply its own focus code
return true; // EVENT_BLOCK
}
return blockPropagation == EVENT_BLOCK;
}
else if (event.EventType == EET_LOG_TEXT_EVENT)
{
// Ignore 'normal' messages
if (event.LogEvent.Level > irr:: ELL_INFORMATION)
{
// Unfortunatly irrlicht produces some internal error/warnings
// messages that can't be avoided (see COpenGLTexture where
// the constructor of COpenGLFBOTexture is used without
// specifying a color format, so the detault format (ECF_UNKOWNO)
// is used, which produces this error message). In non-debug
// mode ignore this error message, but leave it in for debugging.
if(std::string(event.LogEvent.Text)=="Unsupported texture format")
#ifdef DEBUG
Log::info("EventHandler", "The following message will not be printed in release mode");
#else
return true; // EVENT_BLOCK
#endif
const std::string &error_info = STKTexManager::getInstance()->getTextureErrorMessage();
if (event.LogEvent.Level == irr::ELL_WARNING)
{
if(error_info.size()>0)
Log::warn("EventHandler", error_info.c_str());
Log::warn("Irrlicht", event.LogEvent.Text);
}
else if (event.LogEvent.Level == irr::ELL_ERROR)
{
if(error_info.size()>0)
Log::error("EventHandler", error_info.c_str());
Log::error("Irrlicht", event.LogEvent.Text);
}
}
return true;
}
// nothing to do with other events
return false;
}
// -----------------------------------------------------------------------------
void EventHandler::processGUIAction(const PlayerAction action,
int deviceID,
const unsigned int value,
Input::InputType type,
const int playerID)
{
Screen* screen = GUIEngine::getCurrentScreen();
if (screen != NULL)
{
EventPropagation propg = screen->filterActions(action, deviceID, value,
type, playerID);
if (propg == EVENT_BLOCK) return;
}
const bool pressedDown = value > Input::MAX_VALUE*2/3;
bool page_up = false;
if (!pressedDown) return;
switch (action)
{
case PA_STEER_LEFT:
case PA_MENU_LEFT:
{
sendNavigationEvent(NAV_LEFT, playerID);
break;
}
case PA_STEER_RIGHT:
case PA_MENU_RIGHT:
{
sendNavigationEvent(NAV_RIGHT, playerID);
break;
}
case PA_ACCEL:
case PA_MENU_UP:
{
sendNavigationEvent(NAV_UP, playerID);
break;
}
case PA_BRAKE:
case PA_MENU_DOWN:
{
sendNavigationEvent(NAV_DOWN, playerID);
break;
}
case PA_RESCUE:
case PA_MENU_CANCEL:
if (!isWithinATextBox())
{
GUIEngine::getStateManager()->escapePressed();
}
break;
case PA_FIRE:
case PA_MENU_SELECT:
{
Widget* w = GUIEngine::getFocusForPlayer(playerID);
if (w == NULL) break;
// FIXME : consider returned value?
onWidgetActivated(w, playerID, type);
break;
}
case PA_MENU_PAGE_UP:
page_up = true;
// Fall-through
case PA_MENU_PAGE_DOWN:
{
Widget* w = GUIEngine::getFocusForPlayer(playerID);
if (w != nullptr && w->getType() == WTYPE_LIST)
{
ListWidget* list = dynamic_cast<ListWidget*>(w);
list->pageMove(page_up);
}
else
{
screen->handlePaging(page_up);
}
break;
}
case PA_MENU_END:
{
Widget* w = GUIEngine::getFocusForPlayer(playerID);
if (w != nullptr && w->getType() == WTYPE_LIST)
{
ListWidget* list = dynamic_cast<ListWidget*>(w);
list->listEnd();
}
break;
}
case PA_MENU_START:
{
Widget* w = GUIEngine::getFocusForPlayer(playerID);
if (w != nullptr && w->getType() == WTYPE_LIST)
{
ListWidget* list = dynamic_cast<ListWidget*>(w);
list->listStart();
}
break;
}
default:
return;
}
} // processGUIAction
// -----------------------------------------------------------------------------
static EventHandler* event_handler_singleton = NULL;
EventHandler* EventHandler::get()
{
if (event_handler_singleton == NULL)
event_handler_singleton = new EventHandler();
return event_handler_singleton;
}
void EventHandler::deallocate()
{
delete event_handler_singleton;
event_handler_singleton = NULL;
} // deallocate
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
#if 0
#pragma mark -
#pragma mark Private methods
#endif
void EventHandler::sendNavigationEvent(const NavigationDirection nav, const int playerID)
{
Widget* w = GUIEngine::getFocusForPlayer(playerID);
if (w != NULL)
{
// Screen keyboard and modal dialogs override focus.
if (ScreenKeyboard::isActive() &&
!ScreenKeyboard::getCurrent()->isMyIrrChild(w->getIrrlichtElement()))
{
w = NULL;
}
else if (ModalDialog::isADialogActive() &&
!ModalDialog::getCurrent()->isMyIrrChild(w->getIrrlichtElement()))
{
w = NULL;
}
}
if (w == NULL)
{
Widget* defaultWidget = NULL;
if (ScreenKeyboard::isActive())
defaultWidget = ScreenKeyboard::getCurrent()->getFirstWidget();
else if (ModalDialog::isADialogActive())
defaultWidget = ModalDialog::getCurrent()->getFirstWidget();
else if (GUIEngine::getCurrentScreen() != NULL)
defaultWidget = GUIEngine::getCurrentScreen()->getFirstWidget();
if (defaultWidget != NULL)
{
if (playerID != PLAYER_ID_GAME_MASTER && !defaultWidget->m_supports_multiplayer)
return;
defaultWidget->setFocusForPlayer(playerID);
}
return;
}
Widget* widget_to_call = w;
bool handled_by_widget = false;
EventPropagation propagation_state = EVENT_BLOCK;
/* Find topmost parent. Stop looping if a widget event handler's is itself, to not fall
in an infinite loop (this can happen e.g. in checkboxes, where they need to be
notified of clicks onto themselves so they can toggle their state. )
On the way, also notify everyone in the chain of the press. */
do
{
if (nav == NAV_LEFT)
propagation_state = widget_to_call->leftPressed(playerID);
else if (nav == NAV_RIGHT)
propagation_state = widget_to_call->rightPressed(playerID);
else if (nav == NAV_UP)
propagation_state = widget_to_call->upPressed(playerID);
else if (nav == NAV_DOWN)
propagation_state = widget_to_call->downPressed(playerID);
if (propagation_state == EVENT_LET || propagation_state == EVENT_BLOCK_BUT_HANDLED)
handled_by_widget = true;
if (propagation_state == EVENT_LET)
sendEventToUser(widget_to_call, widget_to_call->m_properties[PROP_ID], playerID);
if (widget_to_call->m_event_handler == NULL)
break;
widget_to_call = widget_to_call->m_event_handler;
} while (widget_to_call->m_event_handler != widget_to_call);
if (!handled_by_widget)
{
navigate(nav, GUIEngine::getFocusForPlayer(playerID), playerID);
}
} // sendNavigationEvent
/**
* Focus the next widget downards, upwards, leftwards or rightwards.
*
* \param nav Determine in which direction to navigate
*/
void EventHandler::navigate(const NavigationDirection nav, Widget* starting_widget, const int playerID)
{
int next_id = findIDClosestWidget(nav, playerID, starting_widget, false);
if (next_id != -1)
{
Widget* closest_widget = GUIEngine::getWidget(next_id);
closest_widget->setFocusForPlayer(playerID);
// A list exception : when entering a list by going down/left/right, select the first item
// when focusing a list by going up, select the last item of the list
if (closest_widget->m_type == WTYPE_LIST)
{
ListWidget* list = (ListWidget*) closest_widget;
assert(list != NULL);
list->focusHeader(nav);
}
// Similar exception for vertical tabs, only apply when entering with down/up
if (closest_widget->m_type == GUIEngine::WTYPE_RIBBON && (nav == NAV_UP || nav == NAV_DOWN))
{
RibbonWidget* ribbon = dynamic_cast<RibbonWidget*>(closest_widget);
assert(ribbon != NULL);
if (ribbon->getRibbonType() == GUIEngine::RibbonType::RIBBON_VERTICAL_TABS)
{
// Some of the tabs may be disabled. So we navigate until an active tab is found
(nav == NAV_UP) ? ribbon->setLastSelection(playerID)
: ribbon->setFirstSelection(playerID);
// The tab selection triggers an action
sendEventToUser(ribbon, ribbon->m_properties[PROP_ID], playerID);
}
}
// For spinners, select the most intuitive button
// based on where the navigation came from
// Right if coming from right by a left press
// Left for all other directions
if (closest_widget->getType() == WTYPE_SPINNER)
{
SpinnerWidget* spinner = dynamic_cast<SpinnerWidget*>(closest_widget);
spinner->setSelectedButton(nav == NAV_LEFT);
}
}
return;
} // navigate
/**
* This function use simple heuristics to find the closest widget
* in the requested direction.
* It prioritize widgets close vertically to widget close horizontally,
* as it is expected behavior in any direction.
* Several hardcoded values are used, having been found to work well
* experimentally while keeping simple heuristics.
*/
int EventHandler::findIDClosestWidget(const NavigationDirection nav, const int playerID,
Widget* w, bool ignore_disabled, int recursion_counter)
{
int closest_widget_id = -1;
int distance = 0;
// So that the UI behavior doesn't change when it is upscaled
const int BIG_DISTANCE = irr_driver->getActualScreenSize().Width*500;
int smallest_distance = BIG_DISTANCE;
// Used when there is no suitable widget in the requested direction
int closest_wrapping_widget_id = -1;
int wrapping_distance = 0;
int smallest_wrapping_distance = BIG_DISTANCE;
// In theory, it's better to look recursively in m_widgets
// In practice, this is much much simpler and works equally well
// Usual widget IDs begin at 100 and there is rarely more
// than a few dozen of them - but it's very cheap to have some margin,
for (int i=0;i<1000;i++)
{
Widget* w_test = GUIEngine::getWidget(i);
// The widget id is invalid if :
// - it doesn't match a widget
// - it doesn't match a focusable widget
// - it corresponds to the current widget
// - it corresponds to an invisible or disabled widget (while ignore_disabled is true)
// - the player is not allowed to select it
// - Its base coordinates are negative (such as buttons within ribbons)
if (w_test == NULL || !Widget::isFocusableId(i) || w == w_test ||
(!w_test->isVisible() && ignore_disabled) ||
(!w_test->isActivated() && ignore_disabled) ||
(playerID != PLAYER_ID_GAME_MASTER && !w_test->m_supports_multiplayer) ||
(w_test->m_x < 0) || (w_test->m_y < 0))
continue;
// Ignore empty ribbon widgets and lists
if (w_test->m_type == GUIEngine::WTYPE_RIBBON)
{
RibbonWidget* ribbon = dynamic_cast<RibbonWidget*>(w_test);
assert(ribbon != NULL);
if (ribbon->getActiveChildrenNumber(playerID) == 0)
continue;
}
else if (w_test->m_type == WTYPE_LIST)
{
ListWidget* list = (ListWidget*) w_test;
assert(list != NULL);
if (list->getItemCount() == 0)
continue;
}
// if a dialog is shown, restrict to items in the dialog
if (ScreenKeyboard::isActive())
{
if (!ScreenKeyboard::getCurrent()->isMyChild(w_test))
continue;
}
else if (ModalDialog::isADialogActive())
{
if (!ModalDialog::getCurrent()->isMyChild(w_test))
continue;
}
int offset = 0;
int rightmost = w_test->m_x + w_test->m_w;
if (nav == NAV_UP || nav == NAV_DOWN)
{
// Compare current top point with other widget lowest point
if (nav == NAV_UP)
distance = w->m_y - (w_test->m_y + w_test->m_h);
// compare current lowest point with other widget top point
else
distance = w_test->m_y - (w->m_y + w->m_h);
// Better select an item on the side that one much higher,
// so make the vertical distance matter much more
// than the horizontal offset.
// The multiplicator of 500 is meant so that the offset will matter
// only if there are two or more widget with a (nearly) equal vertical height.
distance *= 500;
wrapping_distance = distance;
// if the two widgets are not well aligned, consider them farther
// If w's left/right-mosts points are between w_test's,
// right_offset and left_offset will be 0.
// If w_test's are between w's,
// we subtract the smaller from the bigger
// else, the smaller is 0 and we keep the bigger
int left_offset = 10 * std::max(0, w_test->m_x - w->m_x);
int right_offset = std::max(0, (w->m_x + w->m_w) - rightmost);
offset = std::max (right_offset - left_offset, left_offset - right_offset);
}
else if (nav == NAV_LEFT || nav == NAV_RIGHT)
{
// compare current leftmost point with other widget rightmost
if (nav == NAV_LEFT)
distance = w->m_x - rightmost;
// compare current rightmost point with other widget leftmost
else
distance = w_test->m_x - (w->m_x + w->m_w);
wrapping_distance = distance;
int down_offset = std::max(0, w_test->m_y - w->m_y);
int up_offset = std::max(0, (w->m_y + w->m_h) - (w_test->m_y + w_test->m_h));
offset = std::max (down_offset - up_offset, up_offset - down_offset);
// Special case for vertical tabs : select the top element of the body
if (w->m_type == GUIEngine::WTYPE_RIBBON)
{
RibbonWidget* ribbon = dynamic_cast<RibbonWidget*>(w);
if (ribbon->getRibbonType() == GUIEngine::RibbonType::RIBBON_VERTICAL_TABS)
{
offset = w_test->m_y;
offset *= 100;
// Don't count elements above the tabs or too high as valid
if (!(w_test->m_x > (w->m_x + w->m_w) || rightmost < w->m_x) ||
(w_test->m_y + w_test->m_h) < w->m_y)
{
distance = BIG_DISTANCE;
wrapping_distance = BIG_DISTANCE;
}
}
}
// No lateral selection if there is not at least partial alignement
// >= because we don't want it to trigger if two widgets touch each other
else if (offset >= w->m_h)
{
distance = BIG_DISTANCE;
wrapping_distance = BIG_DISTANCE;
}
}
// This happens if the tested widget is in the opposite direction
if (distance < 0)
distance = BIG_DISTANCE;
distance += offset;
wrapping_distance += offset;
if (distance < smallest_distance)
{
smallest_distance = distance;
closest_widget_id = i;
}
if (wrapping_distance < smallest_wrapping_distance)
{
smallest_wrapping_distance = wrapping_distance;
closest_wrapping_widget_id = i;
}
} // for i < 1000
int closest_id = (smallest_distance < BIG_DISTANCE) ? closest_widget_id :
closest_wrapping_widget_id;
Widget* w_test = GUIEngine::getWidget(closest_id);
if (w_test == NULL)
return -1;
// If the newly found focus target is invisible, or not activated,
// it is not a good target, search again
// This allows to skip over disabled/invisible widgets in a grid
if (!w_test->isVisible() || !w_test->isActivated())
{
// Can skip over at most 3 consecutive disabled/invisible widgets
if (recursion_counter <=2)
closest_id = findIDClosestWidget(nav, playerID, w_test, /*ignore disabled*/ false, recursion_counter + 1);
// We have only found disabled/invisible widgets on our path
else if (recursion_counter == 3)
closest_id = -1;
}
// If nothing has been found or if we looped back to the starting widget (see issue #5283),
// do a search ignoring disabled/invisible widgets, restarting from the initial widget
if (recursion_counter == 0 && (closest_id == -1 || GUIEngine::getWidget(closest_id) == w))
closest_id = findIDClosestWidget(nav, playerID, w, /*ignore disabled*/ true, 1 /* avoid infinite loops */);
return closest_id;
} // findIDClosestWidget
// -----------------------------------------------------------------------------
void EventHandler::sendEventToUser(GUIEngine::Widget* widget, std::string& name, const int playerID)
{
if (ScreenKeyboard::isActive() &&
ScreenKeyboard::getCurrent()->processEvent(widget->m_properties[PROP_ID]) != EVENT_LET)
{
return;
}
if (ModalDialog::isADialogActive() &&
ModalDialog::getCurrent()->processEvent(widget->m_properties[PROP_ID]) != EVENT_LET)
{
return;
}
if (getCurrentScreen() != NULL)
getCurrentScreen()->eventCallback(widget, name, playerID);
}
// -----------------------------------------------------------------------------
EventPropagation EventHandler::onWidgetActivated(GUIEngine::Widget* w, const int playerID, Input::InputType type)
{
if (w->onActivationInput(playerID) == EVENT_BLOCK)
return EVENT_BLOCK;
Widget* parent = w->m_event_handler;
//FIXME : sendEventToUser do the same screen keyboard and modal dialog checks, so they are done twice
if (ScreenKeyboard::isActive())
{
if (ScreenKeyboard::getCurrent()->processEvent(w->m_properties[PROP_ID]) == EVENT_BLOCK)
{
return EVENT_BLOCK;
}
}
if (ModalDialog::isADialogActive() && (parent == NULL || parent->m_type != GUIEngine::WTYPE_RIBBON))
{
if (ModalDialog::getCurrent()->processEvent(w->m_properties[PROP_ID]) == EVENT_BLOCK)
{
return EVENT_BLOCK;
}
}
//Log::info("EventHandler", "Widget activated: %s", w->m_properties[PROP_ID].c_str());
// For spinners, also trigger activation
if (w->getType() == WTYPE_SPINNER)
{
SpinnerWidget* spinner = dynamic_cast<SpinnerWidget*>(w);
spinner->activateSelectedButton();
}
if (w->m_event_handler != NULL)
{
/* Find all parents. Stop looping if a widget event handler's is itself, to not fall
in an infinite loop (this can happen e.g. in checkboxes, where they need to be
notified of clicks onto themselves so they can toggle their state. ) */
while (parent->m_event_handler != NULL && parent->m_event_handler != parent)
{
parent->transmitEvent(w, w->m_properties[PROP_ID], playerID);
parent = parent->m_event_handler;
}
if (!parent->isActivated()) return EVENT_BLOCK;
/* notify the found event event handler, and also notify the main callback if the
parent event handler says so */
if (parent->transmitEvent(w, w->m_properties[PROP_ID], playerID) == EVENT_LET)
{
if (parent->isEventCallbackActive(type))
sendEventToUser(parent, parent->m_properties[PROP_ID], playerID);
}
}
else
{
std::string id = w->m_properties[PROP_ID];
assert(w->ok());
if (w->transmitEvent(w, id, playerID) == EVENT_LET)
{
assert(w->ok());
if (w->isEventCallbackActive(type))
sendEventToUser(w, id, playerID);
}
}
return EVENT_BLOCK;
}
// -----------------------------------------------------------------------------
EventPropagation EventHandler::onGUIEvent(const SEvent& event)
{
if (event.EventType == EET_GUI_EVENT)
{
if (event.GUIEvent.Caller == NULL) return EVENT_LET;
const s32 id = event.GUIEvent.Caller->getID();
switch (event.GUIEvent.EventType)
{
case EGET_BUTTON_CLICKED:
case EGET_SCROLL_BAR_CHANGED:
case EGET_CHECKBOX_CHANGED:
case EGET_LISTBOX_SELECTED_AGAIN:
{
Widget* w = GUIEngine::getWidget(id);
if (w == NULL) break;
if (!w->isActivated())
{
// Some dialog in overworld could have deactivated widget, and no current screen in overworld
if (GUIEngine::getCurrentScreen())
GUIEngine::getCurrentScreen()->onDisabledItemClicked(w->m_properties[PROP_ID].c_str());
return EVENT_BLOCK;
}
EventPropagation result = w->onClick();
if (result == EVENT_BLOCK)
return result;
// These events are only triggered by mouse (or so I hope)
// The player that owns the mouser receives "game master" priviledges
return onWidgetActivated(w, PLAYER_ID_GAME_MASTER, Input::IT_MOUSEBUTTON);
// These events are only triggered by keyboard/mouse (or so I hope...)
//const int playerID = input_manager->getPlayerKeyboardID();
//if (input_manager->masterPlayerOnly() && playerID != PLAYER_ID_GAME_MASTER) break;
//else if (playerID != -1) return onWidgetActivated(w, playerID);
//else break;
}
case EGET_ELEMENT_HOVERED:
{
Widget* w = GUIEngine::getWidget(id);
if (w == NULL) break;
if (!w->isFocusable() || !w->isActivated()) return GUIEngine::EVENT_BLOCK;
// When a modal dialog is shown, don't select widgets out of the dialog
if (ScreenKeyboard::isActive())
{
// check for parents too before discarding event
if (!ScreenKeyboard::getCurrent()->isMyChild(w) &&
w->m_event_handler != NULL)
{
if (!ScreenKeyboard::getCurrent()->isMyChild(w->m_event_handler))
{
break;
}
}
}
else if (ModalDialog::isADialogActive())
{
// check for parents too before discarding event
if (!ModalDialog::getCurrent()->isMyChild(w) &&
w->m_event_handler != NULL)
{
if (!ModalDialog::getCurrent()->isMyChild(w->m_event_handler))
{
break;
}
}
}
// select ribbons on hover
if (w->m_event_handler != NULL && w->m_event_handler->m_type == WTYPE_RIBBON)
{
// FIXME: don't make a special case for ribbon here, there should be a generic callback
// that all widgets may hook onto
RibbonWidget* ribbon = (RibbonWidget*)(w->m_event_handler);
if (ribbon == NULL) break;
// give the mouse "game master" priviledges
const int playerID = PLAYER_ID_GAME_MASTER;
ribbon->mouseHovered(w, playerID);
if (ribbon->m_event_handler != NULL) ribbon->m_event_handler->mouseHovered(w, playerID);
ribbon->setFocusForPlayer(playerID);
}
else
{
// focus on hover for other widgets
// give the mouse "game master" priviledges
const int playerID = PLAYER_ID_GAME_MASTER;
{
// lists don't like that combined with scrollbars
// (FIXME: find why instead of working around)
if (w->getType() != WTYPE_LIST)
{
w->setFocusForPlayer(playerID);
}
}
}
break;
}
/*
case EGET_ELEMENT_LEFT:
{
Widget* el = getWidget(id);
if(el == NULL) break;
break;
}
*/
case EGET_ELEMENT_FOCUSED:
{
Widget* w = GUIEngine::getWidget(id);
if (w == NULL) break;
// forbid list for gaining "irrLicht focus", then they will process key events and
// we don't want that since we do our own custom processing for keys
if (w->m_type == WTYPE_LIST)
{
// FIXME: fix that better
// cheap way to remove the focus from the element (nope, IGUIEnv::removeFocus doesn't work)
// Obviously will not work if the list is the first item of the screen.
IGUIElement* elem = getCurrentScreen()->getFirstWidget()->getIrrlichtElement();
if (elem->getType() == EGUIET_LIST_BOX)
{
elem = getCurrentScreen()->getLastWidget()->getIrrlichtElement();
assert(elem->getType() != EGUIET_LIST_BOX);
}
GUIEngine::getGUIEnv()->setFocus( elem );
return EVENT_BLOCK; // confirms to irrLicht that we processed it
}
break;
}
case EGET_LISTBOX_CHANGED:
{
Widget* w = GUIEngine::getWidget(id);
if (w == NULL) break;
assert(w->getType() == WTYPE_LIST);
const int playerID = input_manager->getPlayerKeyboardID();
if (input_manager->masterPlayerOnly() && playerID != PLAYER_ID_GAME_MASTER) break;
if (playerID != -1 && !w->isFocusedForPlayer(playerID)) w->setFocusForPlayer(playerID);
break;
}
case EGET_EDITBOX_ENTER:
{
// currently, enter pressed in text ctrl events can only happen in dialogs.
// FIXME : find a cleaner way to route the event to its proper location
if (!ScreenKeyboard::isActive() && ModalDialog::isADialogActive())
ModalDialog::onEnterPressed();
break;
}
default:
break;
} // end switch
}
/*
EGET_BUTTON_CLICKED, EGET_SCROLL_BAR_CHANGED, EGET_CHECKBOX_CHANGED, EGET_TAB_CHANGED,
EGET_MENU_ITEM_SELECTED, EGET_COMBO_BOX_CHANGED, EGET_SPINBOX_CHANGED, EGET_EDITBOX_ENTER,
EGET_LISTBOX_CHANGED, EGET_LISTBOX_SELECTED_AGAIN,
EGET_FILE_SELECTED, EGET_FILE_CHOOSE_DIALOG_CANCELLED,
EGET_MESSAGEBOX_YES, EGET_MESSAGEBOX_NO, EGET_MESSAGEBOX_OK, EGET_MESSAGEBOX_CANCEL,
EGET_TABLE_CHANGED, EGET_TABLE_HEADER_CHANGED, EGET_TABLE_SELECTED_AGAIN
EGET_ELEMENT_FOCUS_LOST, EGET_ELEMENT_FOCUSED, EGET_ELEMENT_HOVERED, EGET_ELEMENT_LEFT,
EGET_ELEMENT_CLOSED,
*/
return EVENT_LET;
}