Repository navigation
Expand file tree
/
Copy pathinput_manager.cpp
More file actions
1477 lines (1341 loc) · 54.1 KB
/
Copy pathinput_manager.cpp
File metadata and controls
1477 lines (1341 loc) · 54.1 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) 2012-2015 SuperTuxKart-Team
//
// 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 "input/input_manager.hpp"
#include "config/user_config.hpp"
#include "graphics/camera/camera_fps.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/shader_based_renderer.hpp"
#include "graphics/sp/sp_base.hpp"
#include "guiengine/engine.hpp"
#include "guiengine/event_handler.hpp"
#include "guiengine/modaldialog.hpp"
#include "guiengine/screen.hpp"
#include "guiengine/screen_keyboard.hpp"
#include "input/device_manager.hpp"
#include "input/gamepad_device.hpp"
#include "input/input.hpp"
#include "input/keyboard_device.hpp"
#include "input/multitouch_device.hpp"
#include "input/sdl_controller.hpp"
#include "input/wiimote_manager.hpp"
#include "karts/abstract_kart.hpp"
#include "karts/controller/controller.hpp"
#include "modes/demo_world.hpp"
#include "modes/world.hpp"
#include "network/network_config.hpp"
#include "network/protocols/client_lobby.hpp"
#include "network/rewind_manager.hpp"
#include "physics/physics.hpp"
#include "race/history.hpp"
#include "replay/replay_recorder.hpp"
#include "states_screens/kart_selection.hpp"
#include "states_screens/main_menu_screen.hpp"
#include "states_screens/online/networking_lobby.hpp"
#include "states_screens/options/options_screen_device.hpp"
#ifdef MOBILE_STK
#include "states_screens/race_gui_multitouch.hpp"
#endif
#include "states_screens/state_manager.hpp"
#include "utils/debug.hpp"
#include "utils/string_utils.hpp"
#include "utils/translation.hpp"
#include <IrrlichtDevice.h>
#include <ISceneManager.h>
#include <ICameraSceneNode.h>
#include <ISceneNode.h>
#include <ITexture.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#ifndef SERVER_ONLY
#include <SDL.h>
#endif
#ifdef __SWITCH__
extern "C" {
#define Event libnx_Event
#define u64 libnx_u64
#define u32 libnx_u32
#define s64 libnx_s64
#define s32 libnx_s32
#include <switch/runtime/pad.h>
#undef u64
#undef u32
#undef s64
#undef s32
#undef Event
}
#endif
InputManager *input_manager;
using GUIEngine::EventPropagation;
using GUIEngine::EVENT_LET;
using GUIEngine::EVENT_BLOCK;
#define INPUT_MODE_DEBUG 0
//-----------------------------------------------------------------------------
/** Initialise input
*/
InputManager::InputManager() : m_mode(BOOTSTRAP),
m_mouse_val_x(-1), m_mouse_val_y(-1)
{
Log::debug("InputManager", "Initialising InputManager!");
m_device_manager = new DeviceManager();
m_device_manager->initialize();
m_master_player_only = false;
#ifndef SERVER_ONLY
#ifdef __SWITCH__
padConfigureInput(8, HidNpadStyleSet_NpadStandard);
// Otherwise we report 'B' as 'A' (like Xbox controller)
SDL_SetHint(
SDL_HINT_GAMECONTROLLERCONFIG,
"53776974636820436F6E74726F6C6C65,Switch Controller,a:b0,b:b1,back:b11,dpdown:b15,dpleft:b12,dpright:b14,dpup:b13,leftshoulder:b6,leftstick:b4,lefttrigger:b8,leftx:a0,lefty:a1,rightshoulder:b7,rightstick:b5,righttrigger:b9,rightx:a2,righty:a3,start:b10,x:b2,y:b3,\n"
);
#endif // __SWITCH__
if (SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER) != 0)
{
Log::error("InputManager", "Failed to init SDL game controller: %s",
SDL_GetError());
}
if (SDL_InitSubSystem(SDL_INIT_HAPTIC) != 0)
{
Log::error("InputManager", "Failed to init SDL haptics: %s",
SDL_GetError());
}
#endif // SERVER_ONLY
}
// -----------------------------------------------------------------------------
void InputManager::addJoystick()
{
#ifndef SERVER_ONLY
// When irrlicht device is reinitialized the joystick added event may be
// lost, we look for them and add it back
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
try
{
SDL_Joystick* joystick = SDL_JoystickOpen(i);
if (!joystick)
continue;
SDL_JoystickID id = SDL_JoystickInstanceID(joystick);
if (m_sdl_controller.find(id) != m_sdl_controller.end())
continue;
std::unique_ptr<SDLController> c(
new SDLController(i));
id = c->getInstanceID();
m_sdl_controller[id] = std::move(c);
}
catch (std::exception& e)
{
Log::error("SDLController", "Error in addJoystick %s", e.what());
}
}
#endif
}
// -----------------------------------------------------------------------------
#ifndef SERVER_ONLY
// For CIrrDeviceSDL
extern "C" void handle_joystick(SDL_Event& event)
{
if (input_manager)
input_manager->handleJoystick(event);
} // handle_joystick
// -----------------------------------------------------------------------------
void InputManager::handleJoystick(SDL_Event& event)
{
try
{
switch (event.type)
{
case SDL_JOYDEVICEADDED:
{
std::unique_ptr<SDLController> c(
new SDLController(event.jdevice.which));
SDL_JoystickID id = c->getInstanceID();
m_sdl_controller[id] = std::move(c);
break;
}
case SDL_JOYDEVICEREMOVED:
{
int deviceID = m_sdl_controller.at(event.jdevice.which)->getInstanceID();
m_gamepads_timer.erase(deviceID);
m_gamepads_held_input_id.erase(deviceID);
m_gamepads_held_value.erase(deviceID);
m_gamepads_held_type.erase(deviceID);
m_sdl_controller.erase(event.jdevice.which);
break;
}
case SDL_JOYAXISMOTION:
{
auto& controller = m_sdl_controller.at(event.jaxis.which);
if (m_mode == INPUT_SENSE_GAMEPAD)
controller->handleAxisInputSense(event);
if (controller->handleAxis(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
case SDL_JOYHATMOTION:
{
auto& controller = m_sdl_controller.at(event.jhat.which);
if (controller->handleHat(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
{
auto& controller = m_sdl_controller.at(event.jbutton.which);
if (controller->handleButton(event) &&
!UserConfigParams::m_gamepad_visualisation)
input(controller->getEvent());
break;
}
default:
break;
}
}
catch (std::exception& e)
{
Log::error("SDLController", "Error in handleJoystick() %s", e.what());
}
} // handleJoystick
SDLController* InputManager::getSDLController(unsigned i) {
assert(i < m_sdl_controller.size());
auto it = m_sdl_controller.begin();
for(unsigned j = 0; j < i; ++j)
it++;
return it->second.get();
}
#endif
// -----------------------------------------------------------------------------
void InputManager::update(float dt)
{
#ifdef ENABLE_WIIUSE
if (wiimote_manager)
wiimote_manager->update();
#endif
#ifdef __SWITCH__
hidSetNpadJoyHoldType(HidNpadJoyHoldType_Horizontal);
#endif
for (auto it = m_gamepads_timer.begin(); it != m_gamepads_timer.end();)
{
it->second -= dt;
if (it->second < 0)
{
// If holding a stick active in a menu, we generate new inputs
dispatchInput(m_gamepads_held_type[it->first], it->first /* device ID */,
m_gamepads_held_input_id[it->first], Input::AD_NEUTRAL,
m_gamepads_held_value[it->first]);
m_gamepads_timer[it->first] = 0.1f;
}
else
{
it++;
}
}
#ifndef SERVER_ONLY
for (auto& controller : m_sdl_controller)
controller.second->checkPowerLevel();
#endif
}
//-----------------------------------------------------------------------------
#ifndef SERVER_ONLY
const irr::SEvent& InputManager::getEventForGamePad(unsigned i) const
{
auto it = m_sdl_controller.begin();
return std::next(it, i)->second->getEvent();
}
#endif
//-----------------------------------------------------------------------------
/** Destructor. Frees all data structures.
*/
InputManager::~InputManager()
{
#ifndef SERVER_ONLY
m_sdl_controller.clear();
#endif
delete m_device_manager;
} // ~InputManager
//-----------------------------------------------------------------------------
void InputManager::handleStaticAction(int key, int value)
{
static bool control_is_pressed = false;
static bool shift_is_pressed = false;
World *world = World::getWorld();
// When no players... a cutscene
if (RaceManager::get() &&
RaceManager::get()->getNumPlayers() == 0 && world != NULL && value > 0 &&
(key == IRR_KEY_SPACE || key == IRR_KEY_RETURN ||
key == IRR_KEY_BUTTON_A))
{
world->onFirePressed(NULL);
}
if (world != NULL && UserConfigParams::m_artist_debug_mode)
{
Debug::handleStaticAction(key, value, control_is_pressed, shift_is_pressed);
}
switch (key)
{
#ifdef DEBUG
// Special debug options for profile mode: switch the
// camera to show a different kart.
case IRR_KEY_1:
case IRR_KEY_2:
case IRR_KEY_3:
case IRR_KEY_4:
case IRR_KEY_5:
case IRR_KEY_6:
case IRR_KEY_7:
case IRR_KEY_8:
case IRR_KEY_9:
{
if(!ProfileWorld::isProfileMode() || !world) break;
int kart_id = key - IRR_KEY_1;
if(kart_id < 0 || kart_id >= (int)world->getNumKarts()) break;
Camera::getCamera(0)->setKart(world->getKart(kart_id));
break;
}
#endif
case IRR_KEY_CONTROL:
case IRR_KEY_RCONTROL:
case IRR_KEY_LCONTROL:
case IRR_KEY_RMENU:
case IRR_KEY_LMENU:
case IRR_KEY_LWIN:
{
control_is_pressed = value != 0;
break;
}
case IRR_KEY_LSHIFT:
case IRR_KEY_RSHIFT:
case IRR_KEY_SHIFT:
{
shift_is_pressed = value != 0;
break;
}
case IRR_KEY_SNAPSHOT:
case IRR_KEY_PRINT:
{
// on windows we don't get a press event, only release. So
// save on release only (to avoid saving twice on other platforms)
if (value == 0)
{
if (control_is_pressed)
{
const bool is_recording = irr_driver->isRecording();
irr_driver->setRecording(!is_recording);
}
else
{
irr_driver->requestScreenshot();
}
}
break;
}
case IRR_KEY_F11:
{
if (value)
{
if (control_is_pressed)
{
history->Save();
}
else if (shift_is_pressed)
{
#ifndef SERVER_ONLY
ShaderBasedRenderer* sbr = SP::getRenderer();
if (sbr)
{
sbr->dumpRTT();
}
#endif
}
else
{
ReplayRecorder::get()->save();
}
}
break;
}
case IRR_KEY_F12:
{
if (value)
{
if (control_is_pressed)
{
UserConfigParams::m_karts_powerup_gui =
!UserConfigParams::m_karts_powerup_gui;
}
else if (shift_is_pressed)
{
UserConfigParams::m_soccer_player_list =
!UserConfigParams::m_soccer_player_list;
}
else
{
UserConfigParams::m_display_fps =
!UserConfigParams::m_display_fps;
}
}
break;
}
default : break;
}
} // handleStaticAction
//-----------------------------------------------------------------------------
/**
* Handles input when an input sensing mode (when configuring input)
*/
void InputManager::inputSensing(Input::InputType type, int deviceID,
int button, Input::AxisDirection axisDirection,
int value)
{
#ifndef SERVER_ONLY
#if INPUT_MODE_DEBUG
Log::info("InputManager::inputSensing", "Start sensing input");
#endif
// don't store if we're trying to do something like bindings keyboard
// keys on a gamepad
if (m_mode == INPUT_SENSE_KEYBOARD && type != Input::IT_KEYBOARD)
return;
if (m_mode == INPUT_SENSE_GAMEPAD && type != Input::IT_STICKMOTION &&
type != Input::IT_STICKBUTTON)
return;
#if INPUT_MODE_DEBUG
Log::info("InputManager::inputSensing", store_new ? "storing it" : "ignoring it");
#endif
switch(type)
{
case Input::IT_KEYBOARD:
if (value > Input::MAX_VALUE/2)
{
m_sensed_input_high_kbd.insert(button);
break;
}
if (value != 0) break; // That shouldn't happen
// only notify on key release
if (m_sensed_input_high_kbd.find(button)
!= m_sensed_input_high_kbd.end())
{
Input sensed_input;
sensed_input.m_type = Input::IT_KEYBOARD;
sensed_input.m_device_id = deviceID;
sensed_input.m_button_id = button;
sensed_input.m_character = deviceID;
OptionsScreenDevice::getInstance()->gotSensedInput(sensed_input);
return;
}
break;
case Input::IT_STICKBUTTON:
if (abs(value) > Input::MAX_VALUE/2.0f)
{
Input sensed_input;
sensed_input.m_type = Input::IT_STICKBUTTON;
sensed_input.m_device_id = deviceID;
sensed_input.m_button_id = button;
sensed_input.m_character = deviceID;
sensed_input.m_axis_direction = Input::AD_NEUTRAL;
OptionsScreenDevice::getInstance()->gotSensedInput(sensed_input);
return;
}
break;
case Input::IT_STICKMOTION:
{
// We have to save the direction in which the axis was moved.
// This is done by storing it as a sign (and since button can
// be zero, we add one before changing the sign).
int input_button_id = value>=0 ? 1+button : -(1+button);
std::tuple<int, int> input_id(deviceID, input_button_id);
std::tuple<int, int> input_id_inv(deviceID, -input_button_id);
bool id_was_high = m_sensed_input_high_gamepad.find(input_id)
!= m_sensed_input_high_gamepad.end();
bool inverse_id_was_high = m_sensed_input_high_gamepad.find(input_id_inv)
!= m_sensed_input_high_gamepad.end();
bool id_was_zero = m_sensed_input_zero_gamepad.find(button)
!= m_sensed_input_zero_gamepad.end();
// A stick was pushed far enough (for the first time) to count as
// 'triggered' - save the axis (coded with direction in the button
// value) for later, so that it can be registered when the stick is
// released again.
// This is mostly legacy behaviour, it is probably good enough
// to register this as soon as the value is high enough.
if (!id_was_high && abs(value) > Input::MAX_VALUE*6.0f/7.0f)
{
if(inverse_id_was_high && !id_was_zero) {
Input sensed_input;
sensed_input.m_type = type;
sensed_input.m_device_id = deviceID;
sensed_input.m_button_id = button;
sensed_input.m_axis_direction = (value>=0) ? Input::AD_POSITIVE
: Input::AD_NEGATIVE;
sensed_input.m_axis_range = Input::AR_FULL;
sensed_input.m_character = deviceID;
OptionsScreenDevice::getInstance()->gotSensedInput(sensed_input);
}
else m_sensed_input_high_gamepad.insert(input_id);
}
// At least with xbox controller they can come to a 'rest' with a value of
// around 6000! So in order to detect that an axis was released, we need to
// test with a rather high deadzone value
else if ( abs(value) < Input::MAX_VALUE/3.0f )
{
if( id_was_high )
{
Input sensed_input;
sensed_input.m_type = type;
sensed_input.m_device_id = deviceID;
sensed_input.m_button_id = button;
sensed_input.m_axis_direction = (value>=0) == id_was_zero
? Input::AD_POSITIVE
: Input::AD_NEGATIVE;
sensed_input.m_axis_range = id_was_zero ? Input::AR_HALF
: Input::AR_FULL;
sensed_input.m_character = deviceID;
OptionsScreenDevice::getInstance()->gotSensedInput(sensed_input);
}
else if( inverse_id_was_high )
{
Input sensed_input;
sensed_input.m_type = type;
sensed_input.m_device_id = deviceID;
sensed_input.m_button_id = button;
// Since the inverse direction was high (i.e. stick went from
// +30000 to -100), we have to inverse the sign
sensed_input.m_axis_direction = (value>=0) == id_was_zero
? Input::AD_NEGATIVE
: Input::AD_POSITIVE;
sensed_input.m_axis_range = id_was_zero ? Input::AR_HALF
: Input::AR_FULL;
sensed_input.m_character = deviceID;
OptionsScreenDevice::getInstance()->gotSensedInput(sensed_input);
}
else
{
m_sensed_input_zero_gamepad.insert(button);
}
}
break;
}
case Input::IT_NONE:
case Input::IT_MOUSEMOTION:
case Input::IT_MOUSEBUTTON:
// uninteresting (but we keep them here to explicitely state we do
// nothing with them, and thus to fix warnings)
break;
} // switch
#endif // SERVER_ONLY
} // inputSensing
//-----------------------------------------------------------------------------
int InputManager::getPlayerKeyboardID() const
{
// In no-assign mode, just return the GUI player ID (devices not
// assigned yet)
if (m_device_manager->getAssignMode() == NO_ASSIGN)
return PLAYER_ID_GAME_MASTER;
// Otherwise, after devices are assigned, we can check in more depth
// Return the first keyboard that is actually being used
const int amount = m_device_manager->getKeyboardAmount();
for (int k=0; k<amount; k++)
{
if (m_device_manager->getKeyboard(k) != NULL &&
m_device_manager->getKeyboard(k)->getPlayer() != NULL)
{
return m_device_manager->getKeyboard(k)->getPlayer()->getID();
}
}
return -1;
}
//-----------------------------------------------------------------------------
/** Handles the conversion from some input to a GameAction and its distribution
* to the currently active menu.
* It also handles whether the game is currently sensing input. It does so by
* suppressing the distribution of the input as a GameAction. Instead the
* input is stored in 'm_sensed_input' and GA_SENSE_COMPLETE is distributed. If
* however the input in question has resolved to GA_LEAVE this is treated as
* an attempt of the user to cancel the sensing. In that case GA_SENSE_CANCEL
* is distributed.
*
* Note: It is the obligation of the called menu to switch of the sense mode.
*
*/
void InputManager::dispatchInput(Input::InputType type, int deviceID,
int button,
Input::AxisDirection axisDirection, int value,
bool shift_mask)
{
// Updated in rendering from main loop
if (!irr_driver->getDevice()->getEventReceiver())
return;
// Reset the timer when the axis which triggered the timer is released
if (abs(value) < Input::MAX_VALUE * 2 / 3 &&
m_gamepads_timer.find(deviceID) != m_gamepads_timer.end() &&
type == m_gamepads_held_type[deviceID] &&
button == m_gamepads_held_input_id[deviceID])
{
m_gamepads_timer.erase(deviceID);
m_gamepads_held_input_id.erase(deviceID);
m_gamepads_held_value.erase(deviceID);
m_gamepads_held_type.erase(deviceID);
}
// Act different in input sensing mode.
if (m_mode == INPUT_SENSE_KEYBOARD ||
m_mode == INPUT_SENSE_GAMEPAD)
{
// Do not pick disabled gamepads for input sensing
if (type == Input::IT_STICKBUTTON || type == Input::IT_STICKMOTION)
{
GamePadDevice *gPad = m_device_manager->getGamePadFromIrrID(deviceID);
// This can happen in case of automatically ignored accelerator
// devices, which are not part of stk's gamepad mapping.
if (!gPad) return;
DeviceConfig *conf = gPad->getConfiguration();
if (!conf->isEnabled())
return;
}
inputSensing(type, deviceID, button, axisDirection, value);
return;
}
// Abort demo mode if a key is pressed during the race in demo mode,
// if a dialog is not active
if(dynamic_cast<DemoWorld*>(World::getWorld()) &&
!GUIEngine::ModalDialog::isADialogActive() && value)
{
RaceManager::get()->exitRace();
StateManager::get()->resetAndGoToScreen(MainMenuScreen::getInstance());
return;
}
StateManager::ActivePlayer* player = NULL;
PlayerAction action;
bool action_found = m_device_manager->translateInput(type, deviceID,
button, axisDirection,
&value, m_mode,
&player, &action);
// in menus, some keyboard keys are standard (before each player selected
// their device). So if a key could not be mapped to any known binding,
// fall back to check the defaults.
if (!action_found &&
StateManager::get()->getGameState() != GUIEngine::GAME &&
type == Input::IT_KEYBOARD && m_mode == MENU &&
m_device_manager->getAssignMode() != DETECT_NEW)
{
action = PA_BEFORE_FIRST;
bool always_valid = false;
// Listen for page-up, page-down, home and end even in assign-only mode
if (button == IRR_KEY_PRIOR) action = PA_MENU_PAGE_UP;
else if (button == IRR_KEY_NEXT) action = PA_MENU_PAGE_DOWN;
else if (button == IRR_KEY_END) action = PA_MENU_END;
else if (button == IRR_KEY_HOME) action = PA_MENU_START;
if (action != PA_BEFORE_FIRST)
always_valid = true;
if (m_device_manager->getAssignMode() == NO_ASSIGN)
{
if (button == IRR_KEY_UP) action = PA_MENU_UP;
else if (button == IRR_KEY_DOWN) action = PA_MENU_DOWN;
else if (button == IRR_KEY_LEFT) action = PA_MENU_LEFT;
else if (button == IRR_KEY_RIGHT) action = PA_MENU_RIGHT;
else if (button == IRR_KEY_SPACE) action = PA_MENU_SELECT;
else if (button == IRR_KEY_RETURN) action = PA_MENU_SELECT;
else if (button == IRR_KEY_BUTTON_UP) action = PA_MENU_DOWN;
else if (button == IRR_KEY_BUTTON_DOWN) action = PA_MENU_UP;
else if (button == IRR_KEY_BUTTON_LEFT) action = PA_MENU_LEFT;
else if (button == IRR_KEY_BUTTON_RIGHT) action = PA_MENU_RIGHT;
else if (button == IRR_KEY_BUTTON_A) action = PA_MENU_SELECT;
else if (button == IRR_KEY_TAB)
{
if (shift_mask)
action = PA_MENU_UP;
else
action = PA_MENU_DOWN;
}
if (button == IRR_KEY_RETURN || button == IRR_KEY_BUTTON_A)
{
if (GUIEngine::ModalDialog::isADialogActive() &&
!GUIEngine::ScreenKeyboard::isActive())
{
GUIEngine::ModalDialog::onEnterPressed();
}
}
} // NO_ASSIGN
if (action != PA_BEFORE_FIRST)
{
action_found = true;
player = nullptr;
if (always_valid && m_device_manager->getAssignMode() == ASSIGN)
player = StateManager::get()->getActivePlayer(PLAYER_ID_GAME_MASTER);
}
}
// do something with the key if it matches a binding
if (action_found)
{
// If we're in the kart menu awaiting new players, do special things
// when a device presses fire or rescue
if (m_device_manager->getAssignMode() == DETECT_NEW)
{
if (NetworkConfig::get()->isNetworking() &&
NetworkConfig::get()->isAddingNetworkPlayers())
{
// Ignore release event
if (value == 0)
return;
InputDevice *device = NULL;
if (type == Input::IT_KEYBOARD)
{
//Log::info("InputManager", "New Player Joining with Key %d", button);
device = m_device_manager->getKeyboardFromBtnID(button);
}
else if (type == Input::IT_STICKBUTTON ||
type == Input::IT_STICKMOTION )
{
device = m_device_manager->getGamePadFromIrrID(deviceID);
}
if (device && (action == PA_FIRE || action == PA_MENU_SELECT) &&
!GUIEngine::ModalDialog::isADialogActive())
{
GUIEngine::Screen* screen = GUIEngine::getCurrentScreen();
NetworkingLobby* lobby = dynamic_cast<NetworkingLobby*>(screen);
if (lobby!=NULL)
{
lobby->openSplitscreenDialog(device);
}
return;
}
}
// Player is unjoining
if ((player != NULL) && (action == PA_RESCUE ||
action == PA_MENU_CANCEL ) )
{
// returns true if the event was handled
// Ignore release event (otherwise it exit the kart selection screen
// when back from race setup screen)
if (value != 0 &&
KartSelectionScreen::getRunningInstance()->playerQuit( player ))
{
return; // we're done here
}
}
/* The way this is currently structured, any time an event is
received from an input device that is not associated with a
player and the device manager is in DETECT_NEW mode, the event
is ignored, unless it is a PA_FIRE event (a player is joining)
perhaps it will be good to let unassigned devices back out
of the kart selection menu?
*/
else if (player == NULL)
{
// New player is joining
if (value != 0 && (action == PA_FIRE || action == PA_MENU_SELECT))
{
InputDevice *device = NULL;
if (type == Input::IT_KEYBOARD)
{
//Log::info("InputManager", "New Player Joining with Key %d", button);
device = m_device_manager->getKeyboardFromBtnID(button);
}
else if (type == Input::IT_STICKBUTTON ||
type == Input::IT_STICKMOTION )
{
device = m_device_manager->getGamePadFromIrrID(deviceID);
}
if (device != NULL)
{
KartSelectionScreen::getRunningInstance()->joinPlayer(device, NULL/*player profile*/);
}
}
else if (value != 0 && (action == PA_MENU_CANCEL))
{
StateManager::get()->escapePressed();
}
return; // we're done here, ignore devices that aren't
// associated with players
}
} // DETECT_NEW
auto cl = LobbyProtocol::get<ClientLobby>();
bool is_nw_spectator = cl && cl->isSpectator() &&
StateManager::get()->getGameState() == GUIEngine::GAME &&
!GUIEngine::ModalDialog::isADialogActive();
// ... when in-game
if (StateManager::get()->getGameState() == GUIEngine::GAME &&
!GUIEngine::ModalDialog::isADialogActive() &&
!GUIEngine::ScreenKeyboard::isActive() &&
!RaceManager::get()->isWatchingReplay() && !is_nw_spectator)
{
if (player == NULL)
{
// Prevent null pointer crash
return;
}
// Find the corresponding PlayerKart from our ActivePlayer instance
AbstractKart* pk = player->getKart();
if (pk == NULL)
{
Log::error("InputManager::dispatchInput", "Trying to process "
"action for an unknown player");
return;
}
Controller* controller = pk->getController();
if (controller != NULL) controller->action(action, abs(value));
#ifdef MOBILE_STK
if (type == Input::IT_STICKBUTTON || type == Input::IT_STICKMOTION)
{
if (UserConfigParams::m_multitouch_draw_gui &&
irr_driver->getDevice()->isAccelerometerAvailable() &&
World::getWorld() && World::getWorld()->getRaceGUI() &&
World::getWorld()->getRaceGUI()->getMultitouchGUI() &&
!World::getWorld()->getRaceGUI()->getMultitouchGUI()->isSpectatorMode() &&
UserConfigParams::m_multitouch_controls != MULTITOUCH_CONTROLS_STEERING_WHEEL)
{
// Disable accelerometer or gyroscope control if gamepad events trigger, see #4705
UserConfigParams::m_multitouch_controls = MULTITOUCH_CONTROLS_STEERING_WHEEL;
World::getWorld()->getRaceGUI()->recreateGUI();
}
}
#endif
}
else if (RaceManager::get() &&
RaceManager::get()->isWatchingReplay() && !GUIEngine::ModalDialog::isADialogActive() &&
StateManager::get()->getGameState() == GUIEngine::GAME)
{
// Get the first ghost kart
World::getWorld()->getKart(0)
->getController()->action(action, abs(value));
}
// ... when in menus
else
{
// When in master-only mode, we can safely assume that players
// are set up, contrarly to early menus where we accept every
// input because players are not set-up yet
if (m_master_player_only && player == NULL && !is_nw_spectator)
{
if (type == Input::IT_STICKMOTION ||
type == Input::IT_STICKBUTTON)
{
GamePadDevice* gp =
getDeviceManager()->getGamePadFromIrrID(deviceID);
// Check for deadzone
if (gp != NULL && gp->moved(value))
{
//I18N: message shown when an input device is used but
// is not associated to any player
GUIEngine::showMessage(
_("Ignoring '%s'. You needed to join earlier to play!",
core::stringw(gp->getName().c_str())));
}
}
return;
}
// menu input
// Throttle gamepad stick inputs
if (type == Input::IT_STICKMOTION &&
m_gamepads_timer.find(deviceID) != m_gamepads_timer.end() &&
m_gamepads_timer[deviceID] > 0.0f)
{
return;
}
bool is_nav_action = action == PA_MENU_DOWN || action == PA_MENU_UP ||
action == PA_MENU_LEFT || action == PA_MENU_RIGHT;
if ((type == Input::IT_STICKMOTION && abs(value) > Input::MAX_VALUE * 2 / 3) ||
(type == Input::IT_STICKBUTTON && is_nav_action))
{
m_gamepads_timer[deviceID] = 0.2f;
m_gamepads_held_input_id[deviceID] = button;
m_gamepads_held_value[deviceID] = value;
m_gamepads_held_type[deviceID] = type;
}
if (is_nw_spectator)
{
cl->changeSpectateTarget(action, abs(value), type);
return;
}
// player may be NULL in early menus, before player setup has
// been performed
int playerID = (player == NULL ? 0 : player->getID());
// If only the master player can act, and this player is not
// the master, ignore his input
if (m_device_manager->getAssignMode() == ASSIGN &&
m_master_player_only && playerID != PLAYER_ID_GAME_MASTER)
{
//I18N: message shown when a player that isn't game master
//I18N: tries to modify options that only the game master
//I18N: is allowed to
GUIEngine::showMessage(
_("Only the Game Master may act at this point!"));
return;
}
// all is good, pass the translated input event on to the
// event handler
GUIEngine::EventHandler::get()
->processGUIAction(action, deviceID, abs(value), type, playerID);
} // When in menus
}
else if (type == Input::IT_KEYBOARD)
{
// keyboard press not handled by device manager / bindings.
// Check static bindings...
handleStaticAction( button, value );
}
} // input
//-----------------------------------------------------------------------------
void InputManager::setMasterPlayerOnly(bool enabled)
{
#if INPUT_MODE_DEBUG
Log::info("InputManager::setMasterPlayerOnly", enabled ? "enabled" : "disabled");
#endif
m_master_player_only = enabled;
}
//-----------------------------------------------------------------------------
/** Returns whether only the master player should be allowed to perform changes
* in menus */
bool InputManager::masterPlayerOnly() const
{
return m_device_manager->getAssignMode() == ASSIGN && m_master_player_only;
}
//-----------------------------------------------------------------------------
/**
* Called on keyboard events [indirectly] by irrLicht
*
* Analog axes can have any value from [-32768, 32767].
*
* There are no negative values. Instead this is reported as an axis with a
* negative direction. This simplifies input configuration and allows greater
* flexibility (= treat 4 directions as four buttons).
*
* Returns whether to halt the event's propagation here
*/
EventPropagation InputManager::input(const SEvent& event)
{