-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGamepad.cpp
More file actions
150 lines (150 loc) · 5.09 KB
/
Gamepad.cpp
File metadata and controls
150 lines (150 loc) · 5.09 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
#include "Gamepad.h"
#include "Settings.h"
bool MHGamepad::enabled = false;
DWORD MHGamepad::prevButtons[4] = {0};
int MHGamepad::deadzoneX = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
int MHGamepad::deadzoneY = XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE;
int MHGamepad::sensitivity = 3;
bool MHGamepad::Initialize()
{
if (!MHSettings::flag_gamepad_enabled) return false;
sensitivity = MHSettings::gamepad_sensitivity;
for (DWORD i = 0; i < 4; i++)
{
XINPUT_STATE state;
if (XInputGetState(i, &state) == ERROR_SUCCESS)
{
enabled = true;
return true;
}
}
return false;
}
void MHGamepad::Update()
{
if (!enabled) return;
sensitivity = MHSettings::gamepad_sensitivity;
for (DWORD i = 0; i < 4; i++)
{
XINPUT_STATE state;
if (XInputGetState(i, &state) == ERROR_SUCCESS)
{
HandleGamepad(i, &state);
}
}
}
void MHGamepad::Shutdown()
{
enabled = false;
}
bool MHGamepad::IsConnected(DWORD userIndex)
{
if (userIndex >= 4) return false;
XINPUT_STATE state;
return XInputGetState(userIndex, &state) == ERROR_SUCCESS;
}
void MHGamepad::SetEnabled(bool en)
{
enabled = en;
}
bool MHGamepad::IsEnabled()
{
return enabled;
}
void MHGamepad::HandleGamepad(DWORD userIndex, XINPUT_STATE* state)
{
short stickX = state->Gamepad.sThumbLX;
short stickY = state->Gamepad.sThumbLY;
if (stickX > deadzoneX || stickX < -deadzoneX ||
stickY > deadzoneY || stickY < -deadzoneY)
{
SimulateMouseMove(stickX, stickY);
}
WORD buttons = state->Gamepad.wButtons;
HandleButton(userIndex, XINPUT_GAMEPAD_A, (buttons & XINPUT_GAMEPAD_A) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_B, (buttons & XINPUT_GAMEPAD_B) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_X, (buttons & XINPUT_GAMEPAD_X) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_Y, (buttons & XINPUT_GAMEPAD_Y) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_LEFT_SHOULDER, (buttons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_RIGHT_SHOULDER, (buttons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_LEFT_THUMB, (buttons & XINPUT_GAMEPAD_LEFT_THUMB) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_RIGHT_THUMB, (buttons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_START, (buttons & XINPUT_GAMEPAD_START) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_BACK, (buttons & XINPUT_GAMEPAD_BACK) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_DPAD_UP, (buttons & XINPUT_GAMEPAD_DPAD_UP) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_DPAD_DOWN, (buttons & XINPUT_GAMEPAD_DPAD_DOWN) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_DPAD_LEFT, (buttons & XINPUT_GAMEPAD_DPAD_LEFT) != 0);
HandleButton(userIndex, XINPUT_GAMEPAD_DPAD_RIGHT, (buttons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0);
prevButtons[userIndex] = buttons;
}
void MHGamepad::SimulateMouseMove(short stickX, short stickY)
{
double moveX = (stickX / 32767.0) * sensitivity;
double moveY = (stickY / 32767.0) * sensitivity;
INPUT input = {0};
input.type = INPUT_MOUSE;
input.mi.dwFlags = MOUSEEVENTF_MOVE;
input.mi.dx = (LONG)moveX;
input.mi.dy = (LONG)moveY;
SendInput(1, &input, sizeof(INPUT));
}
void MHGamepad::HandleButton(DWORD userIndex, WORD button, bool pressed)
{
bool wasPressed = (prevButtons[userIndex] & button) != 0;
if (pressed && !wasPressed)
{
DWORD mouseButton = 0;
switch (button)
{
case XINPUT_GAMEPAD_A:
case XINPUT_GAMEPAD_DPAD_DOWN:
mouseButton = MOUSEEVENTF_LEFTDOWN;
break;
case XINPUT_GAMEPAD_B:
case XINPUT_GAMEPAD_DPAD_RIGHT:
mouseButton = MOUSEEVENTF_RIGHTDOWN;
break;
case XINPUT_GAMEPAD_X:
case XINPUT_GAMEPAD_DPAD_LEFT:
mouseButton = MOUSEEVENTF_MIDDLEDOWN;
break;
case XINPUT_GAMEPAD_Y:
case XINPUT_GAMEPAD_DPAD_UP:
mouseButton = MOUSEEVENTF_WHEEL;
break;
}
if (mouseButton)
{
INPUT input = {0};
input.type = INPUT_MOUSE;
input.mi.dwFlags = mouseButton;
SendInput(1, &input, sizeof(INPUT));
}
}
else if (!pressed && wasPressed)
{
DWORD mouseButton = 0;
switch (button)
{
case XINPUT_GAMEPAD_A:
case XINPUT_GAMEPAD_DPAD_DOWN:
mouseButton = MOUSEEVENTF_LEFTUP;
break;
case XINPUT_GAMEPAD_B:
case XINPUT_GAMEPAD_DPAD_RIGHT:
mouseButton = MOUSEEVENTF_RIGHTUP;
break;
case XINPUT_GAMEPAD_X:
case XINPUT_GAMEPAD_DPAD_LEFT:
mouseButton = MOUSEEVENTF_MIDDLEUP;
break;
}
if (mouseButton)
{
INPUT input = {0};
input.type = INPUT_MOUSE;
input.mi.dwFlags = mouseButton;
SendInput(1, &input, sizeof(INPUT));
}
}
}