-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputManager.cpp
More file actions
432 lines (358 loc) · 13.3 KB
/
Copy pathInputManager.cpp
File metadata and controls
432 lines (358 loc) · 13.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
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
#include "InputManager.h"
#include <wrl.h>
#include <iostream>
#pragma comment(lib, "GameInput.lib")
using namespace Microsoft::WRL;
bool InputManager::Initialize(HWND _hwnd)
{
if (FAILED(GameInputCreate(&gameInput))) {
return false;
}
else
{
hwnd = _hwnd;
std::cout << "GameInput System NewCreateSceneInitialize succeed" << std::endl;
return true;
}
SetControllerVibrationTime(0,2.0f);
return false;
}
void InputManager::Update(float deltaTime)
{
// ★ Initialize 가 실패하면 gameInput 은 널로 남는다. 이 아래 넷이 전부
// 그것을 역참조하므로 여기서 막는다.
//
// 실제로 그런 기계가 있다: GameInput 헤더가 API v3 를 요구하는데
// 시스템의 gameinput.dll 이 구버전이면(GameInputInitialize 진입점이
// 없다) 생성이 실패한다. 그때 입력만 죽어야지 엔진이 죽으면 안 된다.
if (nullptr == gameInput)
{
return;
}
PadUpdate();
KeyBoardUpdate();
MouseUpdate();
GamePadUpdate();
UpdateControllerVibration(deltaTime);
}
void InputManager::KeyBoardUpdate()
{
m_curKeyStates.Reset();
ComPtr<IGameInputReading> reading;
HRESULT hr = gameInput->GetCurrentReading(GameInputKindKeyboard, nullptr, &reading);
if (FAILED(hr) || !reading)
return;
// 현재 눌러진 키만 가져오기
uint32_t keyCount = reading->GetKeyCount();
if (keyCount > 0)
{
m_GameInputKeyStates.clear();
m_GameInputKeyStates.resize(keyCount);
reading->GetKeyState(keyCount, m_GameInputKeyStates.data());
for (uint32_t i = 0; i < keyCount; ++i)
{
uint8_t virtualKey = m_GameInputKeyStates[i].virtualKey;
if (virtualKey < KEYBOARD_COUNT)
m_curKeyStates.Set(virtualKey);
}
}
m_keyboardState.Update();
}
bool InputManager::IsAnyKeyPressed()
{
if (m_GameInputKeyStates.size() != 0)
{
// std::cout << "키가 눌렸습니다 " << std::endl;
return true;
}
return false;
}
void InputManager::MouseUpdate()
{
ComPtr<IGameInputReading> reading;
//memset(curmouseState, 0, sizeof(bool) * MOUSE_COUNT);
m_curMouseState.Reset();
ResetMouseDelta();
m_prevMouseWheelDelta = m_mouseWheelDelta;
// 🔹 현재 마우스 입력 읽기
HRESULT hr = gameInput->GetCurrentReading(GameInputKindMouse, nullptr, &reading);
if (FAILED(hr))
{
std::cout << "마우스 GetCurrentReading 실패!" << std::endl;
return;
}
if (reading->GetMouseState(&m_GameInputMouseState))
{
m_mousePos.x = m_GameInputMouseState.positionX;
m_mousePos.y = m_GameInputMouseState.positionY;
if (m_GameInputMouseState.buttons & GameInputMouseLeftButton)
{
m_curMouseState.Set((uint8)MouseKey::LEFT);
}
if (m_GameInputMouseState.buttons & GameInputMouseRightButton)
{
m_curMouseState.Set((uint8)MouseKey::RIGHT);
}
if (m_GameInputMouseState.buttons & GameInputMouseMiddleButton)
{
m_curMouseState.Set((uint8)MouseKey::MIDDLE);
}
/* curmouseState[1] = (m_GameInputMouseState.buttons & GameInputMouseRightButton) != 0;
curmouseState[2] = (m_GameInputMouseState.buttons & GameInputMouseMiddleButton) != 0;*/
//curmouseState[3] = (mouseState.buttons & GameInputMouseXButton1) != 0; 추가버튼 쓸거면 추가필요
//curmouseState[4] = (mouseState.buttons & GameInputMouseXButton2) != 0;
}
m_mouseWheelDelta = m_GameInputMouseState.wheelY;
m_mouseDelta.x = (m_mousePos.x - m_prevMousePos.x) * 0.5f;
m_mouseDelta.y = (m_mousePos.y - m_prevMousePos.y) * 0.5f;
m_mouseState.Update();
}
void InputManager::SetMousePos(POINT pos)
{
m_mousePos.x = pos.x;
m_mousePos.y = pos.y;
}
math::vector2 InputManager::GetMousePos()
{
POINT cursorPos;
GetCursorPos(&cursorPos);
ScreenToClient(hwnd, &cursorPos);
m_mousePos.x = cursorPos.x;
m_mousePos.y = cursorPos.y;
return m_mousePos;
}
math::vector2 InputManager::GetMouseDelta() const
{
return m_mouseDelta;
}
short InputManager::GetWheelDelta() const
{
return m_mouseWheelDelta;
}
bool InputManager::IsWheelUp()
{
return m_prevMouseWheelDelta < m_mouseWheelDelta;
}
bool InputManager::IsWheelDown()
{
return m_prevMouseWheelDelta > m_mouseWheelDelta;
}
bool InputManager::IsMouseButtonDown(MouseKey button)
{
return m_mouseState.GetKeyState(static_cast<size_t>(button)) == KeyState::Down;
}
bool InputManager::IsMouseButtonPressed(MouseKey button)
{
return m_mouseState.GetKeyState(static_cast<size_t>(button)) == KeyState::Pressed;
}
bool InputManager::IsMouseButtonReleased(MouseKey button)
{
return m_mouseState.GetKeyState(static_cast<size_t>(button)) == KeyState::Released;
}
void InputManager::HideCursor()
{
if (!m_isCursorHidden)
{
while (::ShowCursor(FALSE) >= 0); // Keep hiding cursor until it's fully hidden
m_isCursorHidden = true;
}
}
void InputManager::ShowCursor()
{
if (m_isCursorHidden)
{
while (::ShowCursor(TRUE) < 0); // Keep showing cursor until it's fully shown
m_isCursorHidden = false;
}
}
void InputManager::ResetMouseDelta()
{
m_prevMousePos = m_mousePos;
m_mouseDelta = { 0, 0 };
//m_mouseWheelDelta = 0;
}
void InputManager::PadUpdate()
{
ComPtr<IGameInputReading> reading;
HRESULT hr = gameInput->GetCurrentReading(GameInputKindGamepad, nullptr, &reading);
if (FAILED(hr))
{
return;
}
ComPtr<IGameInputDevice> tempDevice;
if (reading.Get() == nullptr)
return;
reading->GetDevice(&tempDevice);
if (FAILED(hr) || tempDevice == nullptr)
{
return;
}
bool found = false;
for (int i = 0; i < MAX_CONTROLLER; i++)
{
if (device[i] != nullptr && device[i] == tempDevice.Get())
{
found = true;
break;
}
}
if (!found)
{
for (int i = 0; i < MAX_CONTROLLER; i++)
{
if (device[i] == nullptr)
{
device[i] = tempDevice.Get();
break;
}
}
}
}
void InputManager::GamePadUpdate()
{
for (int i = 0; i < MAX_CONTROLLER; ++i)
{
m_curPadState[i].Reset();
if (!device[i])
continue;
ComPtr<IGameInputReading> reading;
HRESULT hr = gameInput->GetCurrentReading(GameInputKindGamepad, device[i], &reading);
if (FAILED(hr) || !reading)
continue;
reading->GetGamepadState(&m_GameInputPadState[i]);
const auto& buttons = m_GameInputPadState[i].buttons;
if (buttons & GameInputGamepadA) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::A));
if (buttons & GameInputGamepadB) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::B));
if (buttons & GameInputGamepadX) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::X));
if (buttons & GameInputGamepadY) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::Y));
if (buttons & GameInputGamepadDPadUp) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::DPAD_UP));
if (buttons & GameInputGamepadDPadDown) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::DPAD_DOWN));
if (buttons & GameInputGamepadDPadLeft) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::DPAD_LEFT));
if (buttons & GameInputGamepadDPadRight) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::DPAD_RIGHT));
if (buttons & GameInputGamepadMenu) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::START_BUTTON));
if (buttons & GameInputGamepadView) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::BACK_BUTTON));
if (buttons & GameInputGamepadLeftShoulder) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::LEFT_SHOULDER));
if (buttons & GameInputGamepadRightShoulder) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::RIGHT_SHOULDER));
if (buttons & GameInputGamepadLeftThumbstick) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::LEFT_THUMB));
if (buttons & GameInputGamepadRightThumbstick) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::RIGHT_THUMB));
if (buttons & GameInputGamepadNone) m_curPadState[i].Set(static_cast<size_t>(ControllerButton::None));
m_controllerThumbL[i].x = m_GameInputPadState[i].leftThumbstickX;
m_controllerThumbL[i].y = m_GameInputPadState[i].leftThumbstickY;
m_controllerThumbR[i].x = m_GameInputPadState[i].rightThumbstickX;
m_controllerThumbR[i].y = m_GameInputPadState[i].rightThumbstickY;
m_controllerTriggerL[i] = m_GameInputPadState[i].leftTrigger;
m_controllerTriggerR[i] = m_GameInputPadState[i].rightTrigger;
}
m_padState.Update();
}
bool InputManager::IsControllerConnected(DWORD Index)
{
return device[Index] != nullptr;
}
bool InputManager::IsControllerButtonDown(DWORD index, ControllerButton btn) const
{
return m_padState.GetKeyState(index, static_cast<size_t>(btn)) == KeyState::Down;
}
bool InputManager::IsControllerButtonPressed(DWORD index, ControllerButton btn) const
{
return m_padState.GetKeyState(index, static_cast<size_t>(btn)) == KeyState::Pressed;
}
bool InputManager::IsControllerButtonReleased(DWORD index, ControllerButton btn) const
{
return m_padState.GetKeyState(index, static_cast<size_t>(btn)) == KeyState::Released;
}
bool InputManager::IsControllerTriggerL(DWORD index) const
{
return m_controllerTriggerL[index] > triggerdeadZone;
}
bool InputManager::IsControllerTriggerR(DWORD index) const
{
return m_controllerTriggerR[index] > triggerdeadZone;
}
math::vector2 InputManager::GetControllerThumbL(DWORD index) const
{
math::vector2 stick(m_controllerThumbL[index].x, m_controllerThumbL[index].y);
if (std::abs(stick.x) < deadZone) stick.x = 0.0f;
if (std::abs(stick.y) < deadZone) stick.y = 0.0f;
return stick;
}
math::vector2 InputManager::GetControllerThumbR(DWORD index) const
{
math::vector2 stick(m_controllerThumbR[index].x, m_controllerThumbR[index].y);
if (std::abs(stick.x) < deadZone) stick.x = 0.0f;
if (std::abs(stick.y) < deadZone) stick.y = 0.0f;
return stick;
}
void InputManager::SetControllerVibration(DWORD Index, float leftMotorSpeed, float rightMotorSpeed, float lowFre, float highFre,float time)
{
GameInputRumbleParams vibration = {};
if (m_controllerVibrationTime[Index] > 0.f) //현재진행중인 바이브레이션이있으면 더 강한진동일떄만 적용
{
if (leftMotorSpeed >= vibrations[Index].z)
{
vibration.lowFrequency = lowFre; // 저주파 모터 진동 강도
vibration.highFrequency = highFre; // 고주파 모터 진동 강도
vibration.leftTrigger = leftMotorSpeed; // 왼쪽 트리거 진동 강도
vibration.rightTrigger = rightMotorSpeed;
vibrations[Index].x = lowFre;
vibrations[Index].y = highFre;
vibrations[Index].z = leftMotorSpeed;
vibrations[Index].w = rightMotorSpeed;
if (device[Index] == nullptr) return;
device[Index]->SetRumbleState(&vibration);
m_controllerVibrationTime[Index] = time;
}
}
else //없으면 그냥적용
{
vibration.lowFrequency = lowFre; // 저주파 모터 진동 강도
vibration.highFrequency = highFre; // 고주파 모터 진동 강도
vibration.leftTrigger = leftMotorSpeed; // 왼쪽 트리거 진동 강도
vibration.rightTrigger = rightMotorSpeed;
vibrations->x = lowFre;
vibrations->y = highFre;
vibrations->z = leftMotorSpeed;
vibrations->w = rightMotorSpeed;
if (device[Index] == nullptr) return;
device[Index]->SetRumbleState(&vibration);
m_controllerVibrationTime[Index] = time;
}
}
void InputManager::SetControllerVibration(DWORD Index, float leftMotorSpeed, float rightMotorSpeed, float lowFre, float highFre)
{
GameInputRumbleParams vibration = {};
vibration.lowFrequency = lowFre; // 저주파 모터 진동 강도
vibration.highFrequency = highFre; // 고주파 모터 진동 강도
vibration.leftTrigger = leftMotorSpeed; // 왼쪽 트리거 진동 강도
vibration.rightTrigger = rightMotorSpeed;
vibrations->x = lowFre;
vibrations->y = highFre;
vibrations->z = leftMotorSpeed;
vibrations->w = rightMotorSpeed;
if (device[Index] == nullptr) return;
device[Index]->SetRumbleState(&vibration);
}
void InputManager::UpdateControllerVibration(float tick)
{
for (DWORD i = 0; i < MAX_CONTROLLER; ++i)
{
if (device[i])
{
if (device[i] == nullptr) continue;
if (m_controllerVibrationTime[i] > 0.0f)
{
m_controllerVibrationTime[i] -= tick;
}
else
{
m_controllerVibrationTime[i] = 0.f;
GameInputRumbleParams vibration = {};
device[i]->SetRumbleState(&vibration);
}
}
}
}
void InputManager::SetControllerVibrationTime(DWORD Index, float time)
{
m_controllerVibrationTime[Index] = time;
}