@@ -32,14 +32,9 @@ UInputMan::~UInputMan() {
32
32
void UInputMan::Clear () {
33
33
m_SkipHandlingSpecialInput = false ;
34
34
m_TextInput.clear ();
35
- ;
36
35
m_NumJoysticks = 0 ;
37
36
m_OverrideInput = false ;
38
- m_AbsoluteMousePos.Reset ();
39
- m_RawMouseMovement.Reset ();
40
- m_AnalogMouseData.Reset ();
41
37
m_MouseSensitivity = 0 .6F ;
42
- m_MouseWheelChange = 0 ;
43
38
m_TrapMousePos = false ;
44
39
m_PlayerScreenMouseBounds = {0 , 0 , 0 , 0 };
45
40
m_MouseTrapRadius = 350 ;
@@ -89,9 +84,9 @@ int UInputMan::Initialize() {
89
84
g_ConsoleMan.PrintString (" ERROR: Failed to connect gamepad " + std::to_string (index) + " " + std::string (SDL_GetError ()));
90
85
continue ;
91
86
}
92
- SDL_SetGamepadPlayerIndex (controller, index );
93
- s_PrevJoystickStates[controllerIndex] = Gamepad (index , joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
94
- s_ChangedJoystickStates[controllerIndex] = Gamepad (index , joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
87
+ SDL_SetGamepadPlayerIndex (controller, controllerIndex );
88
+ s_PrevJoystickStates[controllerIndex] = Gamepad (controllerIndex , joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
89
+ s_ChangedJoystickStates[controllerIndex] = Gamepad (controllerIndex , joysticks[index], SDL_GAMEPAD_AXIS_COUNT, SDL_GAMEPAD_BUTTON_COUNT);
95
90
auto playerScheme = std::find_if (m_ControlScheme.begin (), m_ControlScheme.end (), [controllerIndex](auto & scheme) { return scheme.GetDevice () == controllerIndex + InputDevice::DEVICE_GAMEPAD_1; });
96
91
playerScheme->SetDeviceID ({.gamepad = joysticks[index]});
97
92
controllerIndex++;
@@ -102,8 +97,8 @@ int UInputMan::Initialize() {
102
97
g_ConsoleMan.PrintString (" ERROR: Failed to connect joystick." );
103
98
continue ;
104
99
}
105
- s_PrevJoystickStates[controllerIndex] = Gamepad (index , joysticks[index], SDL_GetNumJoystickAxes (joy), SDL_GetNumJoystickButtons (joy));
106
- s_ChangedJoystickStates[controllerIndex] = Gamepad (index , joysticks[index], SDL_GetNumJoystickAxes (joy), SDL_GetNumJoystickButtons (joy));
100
+ s_PrevJoystickStates[controllerIndex] = Gamepad (controllerIndex , joysticks[index], SDL_GetNumJoystickAxes (joy), SDL_GetNumJoystickButtons (joy));
101
+ s_ChangedJoystickStates[controllerIndex] = Gamepad (controllerIndex , joysticks[index], SDL_GetNumJoystickAxes (joy), SDL_GetNumJoystickButtons (joy));
107
102
auto playerScheme = std::find_if (m_ControlScheme.begin (), m_ControlScheme.end (), [controllerIndex](auto & scheme) { return scheme.GetDevice () == controllerIndex + InputDevice::DEVICE_GAMEPAD_1; });
108
103
playerScheme->SetDeviceID ({.gamepad = joysticks[index]});
109
104
controllerIndex++;
@@ -163,7 +158,7 @@ Vector UInputMan::AnalogAimValues(int whichPlayer) {
163
158
Vector aimValues (0 , 0 );
164
159
if (device == InputDevice::DEVICE_MOUSE_KEYB) {
165
160
if (!m_EnableMultiMouseKeyboard) {
166
- aimValues = (m_AnalogMouseData / m_MouseTrapRadius);
161
+ aimValues = (m_MouseStates. at ( 0 ). analogAim / m_MouseTrapRadius);
167
162
} else {
168
163
if (auto mouse = m_MouseStates.find (m_ControlScheme.at (whichPlayer).GetDeviceID ().mouseKeyboard .mouse ); mouse != m_MouseStates.end ()) {
169
164
aimValues = mouse->second .analogAim / m_MouseTrapRadius;
@@ -367,7 +362,7 @@ Vector UInputMan::GetAbsoluteMousePosition(int whichPlayer) const {
367
362
368
363
void UInputMan::SetAbsoluteMousePosition (const Vector& pos, int whichPlayer) {
369
364
if (whichPlayer == Players::NoPlayer || !m_EnableMultiMouseKeyboard) {
370
- m_AbsoluteMousePos = pos;
365
+ m_MouseStates. at ( 0 ). position = pos;
371
366
} else if (m_ControlScheme.at (whichPlayer).GetDevice () == InputDevice::DEVICE_MOUSE_KEYB) {
372
367
if (auto mouse = m_MouseStates.find (m_ControlScheme.at (whichPlayer).GetDeviceID ().mouseKeyboard .mouse ); mouse != m_MouseStates.end ()) {
373
368
mouse->second .position = pos;
@@ -453,6 +448,20 @@ void UInputMan::ClearMouseButtons() {
453
448
}
454
449
}
455
450
451
+ int UInputMan::MouseWheelMovedByPlayer (int player) const {
452
+ if (player == Players::NoPlayer || player < Players::PlayerOne || player >= Players::MaxPlayerCount || !m_EnableMultiMouseKeyboard) {
453
+ return m_MouseStates.at (0 ).wheelChange ;
454
+ }
455
+ InputDevice playerInput = m_ControlScheme.at (player).GetDevice ();
456
+ if (playerInput == InputDevice::DEVICE_MOUSE_KEYB) {
457
+ DeviceID playerDevice = m_ControlScheme.at (player).GetDeviceID ();
458
+ if (auto mouse = m_MouseStates.find (playerDevice.mouseKeyboard .mouse ); mouse != m_MouseStates.end ()) {
459
+ return mouse->second .wheelChange ;
460
+ }
461
+ }
462
+ return m_MouseStates.at (0 ).wheelChange ;
463
+ }
464
+
456
465
bool UInputMan::AnyMouseButtonPress (SDL_MouseID mouseID) const {
457
466
for (int button = MouseButtons::MOUSE_LEFT; button < MouseButtons::MAX_MOUSE_BUTTONS; ++button) {
458
467
if (MouseButtonPressed (button, NoPlayer, mouseID)) {
@@ -490,9 +499,9 @@ void UInputMan::ForceMouseWithinBox(int x, int y, int width, int height, int whi
490
499
rightMostPos = std::clamp (leftPos + width, leftPos, rightMostPos - 1 );
491
500
bottomMostPos = std::clamp (topPos + height, topPos, bottomMostPos - 1 );
492
501
493
- if (!WithinBox (m_AbsoluteMousePos , static_cast <float >(leftPos), static_cast <float >(topPos), static_cast <float >(rightMostPos), static_cast <float >(bottomMostPos))) {
494
- int limitX = std::clamp (m_AbsoluteMousePos .GetFloorIntX (), leftPos, rightMostPos);
495
- int limitY = std::clamp (m_AbsoluteMousePos .GetFloorIntY (), topPos, bottomMostPos);
502
+ if (!WithinBox (m_MouseStates[ 0 ]. position , static_cast <float >(leftPos), static_cast <float >(topPos), static_cast <float >(rightMostPos), static_cast <float >(bottomMostPos))) {
503
+ int limitX = std::clamp (m_MouseStates[ 0 ]. position .GetFloorIntX (), leftPos, rightMostPos);
504
+ int limitY = std::clamp (m_MouseStates[ 0 ]. position .GetFloorIntY (), topPos, bottomMostPos);
496
505
SDL_WarpMouseInWindow (g_WindowMan.GetWindow (), limitX, limitY);
497
506
}
498
507
} else {
@@ -852,8 +861,6 @@ int UInputMan::Update() {
852
861
}
853
862
854
863
m_TextInput.clear ();
855
- m_MouseWheelChange = 0 ;
856
- m_RawMouseMovement.Reset ();
857
864
for (auto & [mouseID, mouse]: m_MouseStates) {
858
865
mouse.wheelChange = 0 ;
859
866
mouse.relativeMotion .Reset ();
@@ -931,15 +938,13 @@ int UInputMan::Update() {
931
938
m_MouseStates[0 ].position = {inputEvent.motion .x , inputEvent.motion .y };
932
939
m_MouseStates[0 ].relativeMotion += {inputEvent.motion .xrel , inputEvent.motion .yrel };
933
940
934
- m_RawMouseMovement += Vector (static_cast <float >(inputEvent.motion .xrel ), static_cast <float >(inputEvent.motion .yrel ));
935
- m_AbsoluteMousePos.SetXY (static_cast <float >(inputEvent.motion .x ), static_cast <float >(inputEvent.motion .y ));
936
-
937
941
if (g_WindowMan.FullyCoversAllDisplays ()) {
938
942
int windowPosX = 0 ;
939
943
int windowPosY = 0 ;
940
944
SDL_GetWindowPosition (SDL_GetWindowFromID (inputEvent.motion .windowID ), &windowPosX, &windowPosY);
941
945
Vector windowCoord (static_cast <float >(g_WindowMan.GetDisplayArrangementAbsOffsetX () + windowPosX), static_cast <float >(g_WindowMan.GetDisplayArrangementAbsOffsetY () + windowPosY));
942
- m_AbsoluteMousePos += windowCoord;
946
+ mouse.position += windowCoord;
947
+ m_MouseStates[0 ].position += windowCoord;
943
948
}
944
949
break ;
945
950
}
@@ -959,7 +964,6 @@ int UInputMan::Update() {
959
964
break ;
960
965
}
961
966
case SDL_EVENT_MOUSE_WHEEL: {
962
- m_MouseWheelChange += inputEvent.wheel .direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel .y : -inputEvent.wheel .y ;
963
967
m_MouseStates[inputEvent.wheel .which ].wheelChange += inputEvent.wheel .direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel .y : -inputEvent.wheel .y ;
964
968
m_MouseStates[0 ].wheelChange += inputEvent.wheel .direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel .y : -inputEvent.wheel .y ;
965
969
break ;
@@ -988,9 +992,9 @@ int UInputMan::Update() {
988
992
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
989
993
case SDL_EVENT_JOYSTICK_AXIS_MOTION:
990
994
if (std::vector<Gamepad>::iterator device = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), inputEvent.type == SDL_EVENT_GAMEPAD_AXIS_MOTION ? inputEvent.gaxis .which : inputEvent.jaxis .which ); device != s_PrevJoystickStates.end ()) {
991
- if (SDL_IsGamepad (device->m_DeviceIndex ) && inputEvent.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
995
+ if (SDL_IsGamepad (device->m_JoystickID ) && inputEvent.type == SDL_EVENT_GAMEPAD_AXIS_MOTION) {
992
996
UpdateJoystickAxis (device, inputEvent.gaxis .axis , inputEvent.gaxis .value );
993
- } else if (!SDL_IsGamepad (device->m_DeviceIndex )) {
997
+ } else if (!SDL_IsGamepad (device->m_JoystickID )) {
994
998
UpdateJoystickAxis (device, inputEvent.jaxis .axis , inputEvent.jaxis .value );
995
999
}
996
1000
}
@@ -1003,7 +1007,7 @@ int UInputMan::Update() {
1003
1007
if (std::vector<Gamepad>::iterator device = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), joystickEvent ? inputEvent.jbutton .which : inputEvent.gbutton .which ); device != s_PrevJoystickStates.end ()) {
1004
1008
int button = -1 ;
1005
1009
int down = false ;
1006
- if (SDL_IsGamepad (device->m_DeviceIndex )) {
1010
+ if (SDL_IsGamepad (device->m_JoystickID )) {
1007
1011
if (inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_UP || inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
1008
1012
button = inputEvent.gbutton .button ;
1009
1013
down = inputEvent.gbutton .down ;
@@ -1028,7 +1032,7 @@ int UInputMan::Update() {
1028
1032
case SDL_EVENT_JOYSTICK_REMOVED:
1029
1033
case SDL_EVENT_GAMEPAD_REMOVED:
1030
1034
if (std::vector<Gamepad>::iterator prevDevice = std::find (s_PrevJoystickStates.begin (), s_PrevJoystickStates.end (), inputEvent.jdevice .which ); prevDevice != s_PrevJoystickStates.end ()) {
1031
- g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (prevDevice-> m_DeviceIndex + 1 ) + " disconnected!" );
1035
+ g_ConsoleMan.PrintString (" INFO: Gamepad " + std::to_string (prevDevice - s_PrevJoystickStates. begin () + 1 ) + " disconnected!" );
1032
1036
SDL_CloseGamepad (SDL_GetGamepadFromID (prevDevice->m_JoystickID ));
1033
1037
prevDevice->m_JoystickID = -1 ;
1034
1038
std::fill (prevDevice->m_Axis .begin (), prevDevice->m_Axis .end (), 0 );
@@ -1046,7 +1050,6 @@ int UInputMan::Update() {
1046
1050
}
1047
1051
}
1048
1052
m_EventQueue.clear ();
1049
- m_RawMouseMovement *= m_MouseSensitivity;
1050
1053
for (auto & [mouseID, mouse]: m_MouseStates) {
1051
1054
mouse.relativeMotion *= m_MouseSensitivity;
1052
1055
}
@@ -1170,9 +1173,6 @@ void UInputMan::HandleSpecialInput() {
1170
1173
void UInputMan::UpdateMouseInput () {
1171
1174
// Detect and store mouse movement input, translated to analog stick emulation
1172
1175
// TODO: Figure out a less shit solution to updating the mouse in GUIs when there are no mouse players configured, i.e. no player input scheme is using mouse+keyboard. For not just check if we're out of Activity.
1173
- m_AnalogMouseData.m_X += m_RawMouseMovement.m_X * 3 ;
1174
- m_AnalogMouseData.m_Y += m_RawMouseMovement.m_Y * 3 ;
1175
- m_AnalogMouseData.CapMagnitude (m_MouseTrapRadius);
1176
1176
for (auto & [mouseID, mouse]: m_MouseStates) {
1177
1177
mouse.analogAim += mouse.relativeMotion * 3 ;
1178
1178
mouse.analogAim .CapMagnitude (m_MouseTrapRadius);
@@ -1190,8 +1190,8 @@ void UInputMan::UpdateMouseInput() {
1190
1190
1191
1191
// Enable the mouse cursor positioning again after having been disabled. Only do this when the mouse is within the drawing area so it
1192
1192
// won't cause the whole window to move if the user clicks the title bar and unintentionally drags it due to programmatic positioning.
1193
- int mousePosX = m_AbsoluteMousePos .m_X / g_WindowMan.GetResMultiplier ();
1194
- int mousePosY = m_AbsoluteMousePos .m_Y / g_WindowMan.GetResMultiplier ();
1193
+ int mousePosX = m_MouseStates. at ( 0 ). position .m_X / g_WindowMan.GetResMultiplier ();
1194
+ int mousePosY = m_MouseStates. at ( 0 ). position .m_Y / g_WindowMan.GetResMultiplier ();
1195
1195
if (m_DisableMouseMoving && m_PrepareToEnableMouseMoving && (mousePosX >= 0 && mousePosX < g_WindowMan.GetResX () && mousePosY >= 0 && mousePosY < g_WindowMan.GetResY ())) {
1196
1196
m_DisableMouseMoving = m_PrepareToEnableMouseMoving = false ;
1197
1197
}
@@ -1284,25 +1284,25 @@ void UInputMan::UpdateJoystickDigitalAxis() {
1284
1284
}
1285
1285
}
1286
1286
1287
- void UInputMan::HandleGamepadHotPlug (SDL_JoystickID deviceIndex ) {
1287
+ void UInputMan::HandleGamepadHotPlug (SDL_JoystickID joystickID ) {
1288
1288
SDL_Joystick* controller = nullptr ;
1289
1289
int controllerIndex = 0 ;
1290
1290
1291
1291
for (controllerIndex = 0 ; controllerIndex < s_PrevJoystickStates.size (); ++controllerIndex) {
1292
- if (s_PrevJoystickStates[controllerIndex].m_JoystickID == deviceIndex ) {
1292
+ if (s_PrevJoystickStates[controllerIndex].m_JoystickID == joystickID ) {
1293
1293
return ;
1294
1294
}
1295
1295
if (s_PrevJoystickStates[controllerIndex].m_JoystickID == -1 ) {
1296
- if (SDL_IsGamepad (deviceIndex )) {
1297
- SDL_Gamepad* gameController = SDL_OpenGamepad (deviceIndex );
1296
+ if (SDL_IsGamepad (joystickID )) {
1297
+ SDL_Gamepad* gameController = SDL_OpenGamepad (joystickID );
1298
1298
if (!gameController) {
1299
1299
g_ConsoleMan.PrintString (" ERROR: Failed to connect Gamepad!" );
1300
1300
break ;
1301
1301
}
1302
1302
controller = SDL_GetGamepadJoystick (gameController);
1303
1303
SDL_SetGamepadPlayerIndex (gameController, controllerIndex);
1304
1304
} else {
1305
- controller = SDL_OpenJoystick (deviceIndex );
1305
+ controller = SDL_OpenJoystick (joystickID );
1306
1306
}
1307
1307
if (!controller) {
1308
1308
g_ConsoleMan.PrintString (" ERROR: Failed to connect Gamepad!" );
@@ -1320,15 +1320,15 @@ void UInputMan::HandleGamepadHotPlug(SDL_JoystickID deviceIndex) {
1320
1320
if (controller) {
1321
1321
int numAxis = 0 ;
1322
1322
int numButtons = 0 ;
1323
- if (SDL_IsGamepad (deviceIndex )) {
1323
+ if (SDL_IsGamepad (joystickID )) {
1324
1324
numAxis = SDL_GAMEPAD_AXIS_COUNT;
1325
1325
numButtons = SDL_GAMEPAD_BUTTON_COUNT;
1326
1326
} else {
1327
1327
numAxis = SDL_GetNumJoystickAxes (controller);
1328
1328
numButtons = SDL_GetNumJoystickButtons (controller);
1329
1329
}
1330
- s_PrevJoystickStates[controllerIndex] = Gamepad (controllerIndex, deviceIndex , numAxis, numButtons);
1331
- s_ChangedJoystickStates[controllerIndex] = Gamepad (controllerIndex, deviceIndex , numAxis, numButtons);
1330
+ s_PrevJoystickStates[controllerIndex] = Gamepad (controllerIndex, joystickID , numAxis, numButtons);
1331
+ s_ChangedJoystickStates[controllerIndex] = Gamepad (controllerIndex, joystickID , numAxis, numButtons);
1332
1332
m_NumJoysticks++;
1333
1333
}
1334
1334
}
0 commit comments