Skip to content

Commit 8e21f7c

Browse files
committed
fix menu cursor control; fix gamepad axis not working correctly
1 parent 80d89c3 commit 8e21f7c

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

Source/Managers/UInputMan.cpp

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,9 @@ UInputMan::~UInputMan() {
3232
void UInputMan::Clear() {
3333
m_SkipHandlingSpecialInput = false;
3434
m_TextInput.clear();
35-
;
3635
m_NumJoysticks = 0;
3736
m_OverrideInput = false;
38-
m_AbsoluteMousePos.Reset();
39-
m_RawMouseMovement.Reset();
40-
m_AnalogMouseData.Reset();
4137
m_MouseSensitivity = 0.6F;
42-
m_MouseWheelChange = 0;
4338
m_TrapMousePos = false;
4439
m_PlayerScreenMouseBounds = {0, 0, 0, 0};
4540
m_MouseTrapRadius = 350;
@@ -163,7 +158,7 @@ Vector UInputMan::AnalogAimValues(int whichPlayer) {
163158
Vector aimValues(0, 0);
164159
if (device == InputDevice::DEVICE_MOUSE_KEYB) {
165160
if (!m_EnableMultiMouseKeyboard) {
166-
aimValues = (m_AnalogMouseData / m_MouseTrapRadius);
161+
aimValues = (m_MouseStates.at(0).analogAim / m_MouseTrapRadius);
167162
} else {
168163
if (auto mouse = m_MouseStates.find(m_ControlScheme.at(whichPlayer).GetDeviceID().mouseKeyboard.mouse); mouse != m_MouseStates.end()) {
169164
aimValues = mouse->second.analogAim / m_MouseTrapRadius;
@@ -367,7 +362,7 @@ Vector UInputMan::GetAbsoluteMousePosition(int whichPlayer) const {
367362

368363
void UInputMan::SetAbsoluteMousePosition(const Vector& pos, int whichPlayer) {
369364
if (whichPlayer == Players::NoPlayer || !m_EnableMultiMouseKeyboard) {
370-
m_AbsoluteMousePos = pos;
365+
m_MouseStates.at(0).position = pos;
371366
} else if (m_ControlScheme.at(whichPlayer).GetDevice() == InputDevice::DEVICE_MOUSE_KEYB) {
372367
if (auto mouse = m_MouseStates.find(m_ControlScheme.at(whichPlayer).GetDeviceID().mouseKeyboard.mouse); mouse != m_MouseStates.end()) {
373368
mouse->second.position = pos;
@@ -453,6 +448,20 @@ void UInputMan::ClearMouseButtons() {
453448
}
454449
}
455450

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+
456465
bool UInputMan::AnyMouseButtonPress(SDL_MouseID mouseID) const {
457466
for (int button = MouseButtons::MOUSE_LEFT; button < MouseButtons::MAX_MOUSE_BUTTONS; ++button) {
458467
if (MouseButtonPressed(button, NoPlayer, mouseID)) {
@@ -490,9 +499,9 @@ void UInputMan::ForceMouseWithinBox(int x, int y, int width, int height, int whi
490499
rightMostPos = std::clamp(leftPos + width, leftPos, rightMostPos - 1);
491500
bottomMostPos = std::clamp(topPos + height, topPos, bottomMostPos - 1);
492501

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);
496505
SDL_WarpMouseInWindow(g_WindowMan.GetWindow(), limitX, limitY);
497506
}
498507
} else {
@@ -852,8 +861,6 @@ int UInputMan::Update() {
852861
}
853862

854863
m_TextInput.clear();
855-
m_MouseWheelChange = 0;
856-
m_RawMouseMovement.Reset();
857864
for (auto& [mouseID, mouse]: m_MouseStates) {
858865
mouse.wheelChange = 0;
859866
mouse.relativeMotion.Reset();
@@ -931,15 +938,13 @@ int UInputMan::Update() {
931938
m_MouseStates[0].position = {inputEvent.motion.x, inputEvent.motion.y};
932939
m_MouseStates[0].relativeMotion += {inputEvent.motion.xrel, inputEvent.motion.yrel};
933940

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-
937941
if (g_WindowMan.FullyCoversAllDisplays()) {
938942
int windowPosX = 0;
939943
int windowPosY = 0;
940944
SDL_GetWindowPosition(SDL_GetWindowFromID(inputEvent.motion.windowID), &windowPosX, &windowPosY);
941945
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;
943948
}
944949
break;
945950
}
@@ -959,7 +964,6 @@ int UInputMan::Update() {
959964
break;
960965
}
961966
case SDL_EVENT_MOUSE_WHEEL: {
962-
m_MouseWheelChange += inputEvent.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel.y : -inputEvent.wheel.y;
963967
m_MouseStates[inputEvent.wheel.which].wheelChange += inputEvent.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel.y : -inputEvent.wheel.y;
964968
m_MouseStates[0].wheelChange += inputEvent.wheel.direction == SDL_MOUSEWHEEL_NORMAL ? inputEvent.wheel.y : -inputEvent.wheel.y;
965969
break;
@@ -988,9 +992,9 @@ int UInputMan::Update() {
988992
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
989993
case SDL_EVENT_JOYSTICK_AXIS_MOTION:
990994
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) {
992996
UpdateJoystickAxis(device, inputEvent.gaxis.axis, inputEvent.gaxis.value);
993-
} else if (!SDL_IsGamepad(device->m_DeviceIndex)) {
997+
} else if (!SDL_IsGamepad(device->m_JoystickID)) {
994998
UpdateJoystickAxis(device, inputEvent.jaxis.axis, inputEvent.jaxis.value);
995999
}
9961000
}
@@ -1003,7 +1007,7 @@ int UInputMan::Update() {
10031007
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()) {
10041008
int button = -1;
10051009
int down = false;
1006-
if (SDL_IsGamepad(device->m_DeviceIndex)) {
1010+
if (SDL_IsGamepad(device->m_JoystickID)) {
10071011
if (inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_UP || inputEvent.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
10081012
button = inputEvent.gbutton.button;
10091013
down = inputEvent.gbutton.down;
@@ -1028,7 +1032,7 @@ int UInputMan::Update() {
10281032
case SDL_EVENT_JOYSTICK_REMOVED:
10291033
case SDL_EVENT_GAMEPAD_REMOVED:
10301034
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!");
10321036
SDL_CloseGamepad(SDL_GetGamepadFromID(prevDevice->m_JoystickID));
10331037
prevDevice->m_JoystickID = -1;
10341038
std::fill(prevDevice->m_Axis.begin(), prevDevice->m_Axis.end(), 0);
@@ -1046,7 +1050,6 @@ int UInputMan::Update() {
10461050
}
10471051
}
10481052
m_EventQueue.clear();
1049-
m_RawMouseMovement *= m_MouseSensitivity;
10501053
for (auto& [mouseID, mouse]: m_MouseStates) {
10511054
mouse.relativeMotion *= m_MouseSensitivity;
10521055
}
@@ -1170,9 +1173,6 @@ void UInputMan::HandleSpecialInput() {
11701173
void UInputMan::UpdateMouseInput() {
11711174
// Detect and store mouse movement input, translated to analog stick emulation
11721175
// 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);
11761176
for (auto& [mouseID, mouse]: m_MouseStates) {
11771177
mouse.analogAim += mouse.relativeMotion * 3;
11781178
mouse.analogAim.CapMagnitude(m_MouseTrapRadius);
@@ -1190,8 +1190,8 @@ void UInputMan::UpdateMouseInput() {
11901190

11911191
// Enable the mouse cursor positioning again after having been disabled. Only do this when the mouse is within the drawing area so it
11921192
// 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();
11951195
if (m_DisableMouseMoving && m_PrepareToEnableMouseMoving && (mousePosX >= 0 && mousePosX < g_WindowMan.GetResX() && mousePosY >= 0 && mousePosY < g_WindowMan.GetResY())) {
11961196
m_DisableMouseMoving = m_PrepareToEnableMouseMoving = false;
11971197
}

0 commit comments

Comments
 (0)