-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppClassControls.cpp
More file actions
319 lines (291 loc) · 10.3 KB
/
Copy pathAppClassControls.cpp
File metadata and controls
319 lines (291 loc) · 10.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
#include "AppClass.h"
using namespace Simplex;
//Mouse
void Application::ProcessMouseMovement(sf::Event a_event)
{
//get global mouse position
sf::Vector2i mouse = sf::Mouse::getPosition();
sf::Vector2i window = m_pWindow->getPosition();
m_v3Mouse.x = static_cast<float>(mouse.x - window.x);
m_v3Mouse.y = static_cast<float>(mouse.y - window.y);
if(!m_pSystem->IsWindowFullscreen() && !m_pSystem->IsWindowBorderless())
m_v3Mouse += vector3(-8.0f, -32.0f, 0.0f);
gui.io.MousePos = ImVec2(m_v3Mouse.x, m_v3Mouse.y);
}
void Application::ProcessMouseScroll(sf::Event a_event)
{
gui.io.MouseWheel = a_event.mouseWheelScroll.delta;
float fSpeed = a_event.mouseWheelScroll.delta;
float fMultiplier = sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) ||
sf::Keyboard::isKeyPressed(sf::Keyboard::RShift);
if (fMultiplier)
fSpeed *= 2.0f;
m_pCameraMngr->MoveForward(-fSpeed);
}
//Joystick
void Application::ProcessJoystickConnected(uint nController)
{
// if the index of the controller is larger than 7
if (nController > 7)
return;
bool bConnected = sf::Joystick::isConnected(nController);
m_bGUI_Controller = bConnected;
if (bConnected)
{
SafeDelete(m_pController[nController]);
sf::Joystick::Identification joyID = sf::Joystick::getIdentification(nController);
String productName = joyID.name.toAnsiString();
int nVendorID = joyID.vendorId;
int nProductID = joyID.productId;
m_pController[nController] = new ControllerInput(nVendorID, nProductID);
//m_pWindow->setJoystickThreshold(7);
++m_uControllerCount;
}
}
void Application::ProcessJoystickPressed(sf::Event a_event)
{
//Check the ID of the controller
int nID = a_event.joystickButton.joystickId;
//Identify the button pressed
int nButton = a_event.joystickButton.button;
//map the value to our control value
m_pController[nID]->button[m_pController[nID]->mapButton[nButton]] = true;
//Process...
//If we press L3 + R3 on the active controller we quit the application
if (m_pController[m_uActCont]->button[SimplexKey_L3] && m_pController[m_uActCont]->button[SimplexKey_R3])
m_bRunning = false;
}
void Application::ProcessJoystickReleased(sf::Event a_event)
{
//Check the ID of the controller
int nID = a_event.joystickButton.joystickId;
//Identify the button pressed
int nButton = a_event.joystickButton.button;
//map the value to our control value
m_pController[nID]->button[m_pController[nID]->mapButton[nButton]] = false;
//Process...
//if we released the Pad key on the active controller quit the application
if (m_pController[m_uActCont]->mapButton[nButton] == SimplexKey_Pad)
m_bRunning = false;
}
void Application::ProcessJoystickMoved(sf::Event a_event)
{
int nID = a_event.joystickMove.joystickId;
float fPosition = a_event.joystickMove.position;
int nAxis = a_event.joystickMove.axis;
//invert vertical axis for sticks
if (nAxis == sf::Joystick::Axis::Y || nAxis == sf::Joystick::Axis::R)
{
fPosition *= -1.0f;
}
//Adjust with threshold
float fThreshold = 10.0f;
if (nAxis != sf::Joystick::Axis::PovX || nAxis != sf::Joystick::Axis::PovY)
{
if (fPosition > -fThreshold && fPosition < fThreshold)
{
fPosition = 0.0f;
}
}
/*
SFML does not use XInput so XBOX one controller is a special snowflake,
LTrigger and RTrigger share the same axis (sf::Joystick::Axis::Z)
instead of separate ones as in other controllers:
LTrigger reports from 0 to 100 and RTrigger reports from 0 to -100
I mapped the values from 0 to 100 in both cases to keep consistent
with other controllers, but sharing the axis means that triggers cannot
be independent from each other
*/
//PS4 Controller
if (m_pController[nID]->uModel == SimplexController_DualShock4)
{
//For Axis U or V
if (nAxis == sf::Joystick::Axis::U || nAxis == sf::Joystick::Axis::V)
{
fPosition = MapValue(fPosition, -100.0f, 100.0f, 0.0f, 100.0f);
}
m_pController[nID]->axis[m_pController[nID]->mapAxis[nAxis]] = fPosition;
}
//Nintendo Switch Pro
else if (m_pController[nID]->uModel == SimplexController_SwitchPro)
{
if (nAxis != sf::Joystick::Axis::PovX && nAxis != sf::Joystick::Axis::PovY)
{
fPosition = MapValue(fPosition, -75.0f, 75.0f, -100.0f, 100.0f);
if (fPosition > 100) fPosition = 100;
if (fPosition < -100) fPosition = -100;
if (fPosition > -10 && fPosition < 10) fPosition = 0;
}
m_pController[nID]->axis[m_pController[nID]->mapAxis[nAxis]] = fPosition;
}
//Microsoft controllers
else if (m_pController[nID]->uModel == SimplexController_XBONE || m_pController[nID]->uModel == SimplexController_360)
{
//Axis different than Z
if (nAxis != sf::Joystick::Axis::Z)
{
m_pController[nID]->axis[m_pController[nID]->mapAxis[nAxis]] = fPosition;
}
else //Z axis
{
//get the uncleaned value
fPosition = a_event.joystickMove.position;
//positive values go on the left trigger
if (fPosition >= 0)
{
if (fPosition < 25.0f)
fPosition = 0.0f;
m_pController[nID]->axis[SimplexAxis_L] = fPosition;
}
//negative values go on the right trigger
else
{
if (fPosition > -25.0f)
fPosition = 0.0f;
m_pController[nID]->axis[SimplexAxis_R] = -fPosition;
}
}
}
//8Bitdo NES30 PRO
else if (m_pController[nID]->uModel == SimplexController_NES30PRO)
{
m_pController[nID]->axis[m_pController[nID]->mapAxis[nAxis]] = fPosition;
}
//Other controllers
else
{
m_pController[nID]->axis[m_pController[nID]->mapAxis[nAxis]] = fPosition;
}
}
/*
Continuous update (once per frame) for discreet input use
process events.
*/
//Mouse
void Application::ArcBall(float a_fSensitivity)
{
//If the arcball is not enabled return
if (!m_bArcBall)
return;
//static quaternion qArcBall;
UINT MouseX, MouseY; // Coordinates for the mouse
UINT CenterX, CenterY; // Coordinates for the center of the screen.
//Initialize the position of the pointer to the middle of the screen
CenterX = m_pSystem->GetWindowX() + m_pSystem->GetWindowWidth() / 2;
CenterY = m_pSystem->GetWindowY() + m_pSystem->GetWindowHeight() / 2;
//Calculate the position of the mouse and store it
POINT pt;
GetCursorPos(&pt);
MouseX = pt.x;
MouseY = pt.y;
//Calculate the difference in position and update the quaternion orientation based on it
float DeltaMouse;
if (MouseX < CenterX)
{
DeltaMouse = static_cast<float>(CenterX - MouseX);
m_qArcBall = quaternion(vector3(0.0f, glm::radians(a_fSensitivity * DeltaMouse), 0.0f)) * m_qArcBall;
}
else if (MouseX > CenterX)
{
DeltaMouse = static_cast<float>(MouseX - CenterX);
m_qArcBall = quaternion(vector3(0.0f, glm::radians(-a_fSensitivity * DeltaMouse), 0.0f)) * m_qArcBall;
}
if (MouseY < CenterY)
{
DeltaMouse = static_cast<float>(CenterY - MouseY);
m_qArcBall = quaternion(vector3(glm::radians(-a_fSensitivity * DeltaMouse), 0.0f, 0.0f)) * m_qArcBall;
}
else if (MouseY > CenterY)
{
DeltaMouse = static_cast<float>(MouseY - CenterY);
m_qArcBall = quaternion(vector3(glm::radians(a_fSensitivity * DeltaMouse), 0.0f, 0.0f)) * m_qArcBall;
}
SetCursorPos(CenterX, CenterY);//Position the mouse in the center
//return qArcBall; // return the new quaternion orientation
}
void Application::CameraRotation(float a_fSpeed)
{
if (m_bFPC == false)
return;
UINT MouseX, MouseY; // Coordinates for the mouse
UINT CenterX, CenterY; // Coordinates for the center of the screen.
//Initialize the position of the pointer to the middle of the screen
CenterX = m_pSystem->GetWindowX() + m_pSystem->GetWindowWidth() / 2;
CenterY = m_pSystem->GetWindowY() + m_pSystem->GetWindowHeight() / 2;
//Calculate the position of the mouse and store it
POINT pt;
GetCursorPos(&pt);
MouseX = pt.x;
MouseY = pt.y;
//Calculate the difference in view with the angle
float fAngleX = 0.0f;
float fAngleY = 0.0f;
float fDeltaMouse = 0.0f;
if (MouseX < CenterX)
{
fDeltaMouse = static_cast<float>(CenterX - MouseX);
fAngleY += fDeltaMouse * a_fSpeed;
}
else if (MouseX > CenterX)
{
fDeltaMouse = static_cast<float>(MouseX - CenterX);
fAngleY -= fDeltaMouse * a_fSpeed;
}
if (MouseY < CenterY)
{
fDeltaMouse = static_cast<float>(CenterY - MouseY);
fAngleX -= fDeltaMouse * a_fSpeed;
}
else if (MouseY > CenterY)
{
fDeltaMouse = static_cast<float>(MouseY - CenterY);
fAngleX += fDeltaMouse * a_fSpeed;
}
//Change the Yaw and the Pitch of the camera
m_pCameraMngr->ChangeYaw(fAngleY * 3.0f);
m_pCameraMngr->ChangePitch(-fAngleX * 3.0f);
SetCursorPos(CenterX, CenterY);//Position the mouse in the center
}
//Joystick
void Application::ProcessJoystick(void)
{
if (!m_bFocused)
return;
/*
This is used for things that are continuously happening,
for discreet on/off use ProcessJoystickPressed/Released
*/
#pragma region Camera Position
float fForwardSpeed = m_pController[m_uActCont]->axis[SimplexAxis_Y] / 150.0f;
float fHorizontalSpeed = m_pController[m_uActCont]->axis[SimplexAxis_X] / 150.0f;
float fVerticalSpeed = m_pController[m_uActCont]->axis[SimplexAxis_R] / 150.0f -
m_pController[m_uActCont]->axis[SimplexAxis_L] / 150.0f;
bool fMultiplier = m_pController[m_uActCont]->button[SimplexKey_L1] ||
m_pController[m_uActCont]->button[SimplexKey_R1];
if (fMultiplier)
{
fForwardSpeed *= 3.0f;
fHorizontalSpeed *= 3.0f;
fVerticalSpeed *= 3.0f;
}
m_pCameraMngr->MoveForward(fForwardSpeed);
m_pCameraMngr->MoveSideways(fHorizontalSpeed);
m_pCameraMngr->MoveVertical(fVerticalSpeed);
#pragma endregion
#pragma region Camera Orientation
//Change the Yaw and the Pitch of the camera
m_pCameraMngr->ChangeYaw(-m_pController[m_uActCont]->axis[SimplexAxis_U] / 150.0f);
m_pCameraMngr->ChangePitch(m_pController[m_uActCont]->axis[SimplexAxis_V] / 150.0f);
#pragma endregion
#pragma region ModelOrientation Orientation
m_qArcBall = quaternion(vector3(glm::radians(m_pController[m_uActCont]->axis[SimplexAxis_POVY] / 20.0f), 0.0f, 0.0f)) * m_qArcBall;
if (fMultiplier)
{
m_qArcBall = quaternion(vector3(0.0f, 0.0f, glm::radians(m_pController[m_uActCont]->axis[SimplexAxis_POVX] / 20.0f))) * m_qArcBall;
}
else
{
m_qArcBall = quaternion(vector3(0.0f, glm::radians(m_pController[m_uActCont]->axis[SimplexAxis_POVX] / 20.0f), 0.0f)) * m_qArcBall;
}
#pragma endregion
}