-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputSystem.cpp
101 lines (89 loc) · 2.17 KB
/
InputSystem.cpp
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
#include "InputSystem.h"
InputSystem::InputSystem()
{
}
void InputSystem::Update()
{
POINT current_mouse_pos = {};
GetCursorPos(¤t_mouse_pos);
if (init_move)
{
old_mouse_pos = { static_cast<float>(current_mouse_pos.x), static_cast<float>(current_mouse_pos.y) };
init_move = false;
}
if (current_mouse_pos.x != old_mouse_pos.x || current_mouse_pos.y != old_mouse_pos.y)
{
DirectX::XMFLOAT2 mouse_pos = {static_cast<float> (current_mouse_pos.x) ,static_cast<float>(current_mouse_pos.y)};
for (auto& it : m_listeners)
{
it.second->OnMouseMove(mouse_pos);
}
}
for (int i = 0; i < 256; i++)
{
if (GetAsyncKeyState(i) & 0x8000)
{
for (auto& it : m_listeners)
{
if (i == VK_LBUTTON)
{
it.second->OnLeftMouseDown({ static_cast<float>(current_mouse_pos.x), static_cast<float>(current_mouse_pos.y) });
}
else if (i == VK_RBUTTON)
{
it.second->OnRightMouseDown({ static_cast<float>(current_mouse_pos.x), static_cast<float>(current_mouse_pos.y) });
}
else
{
it.second->OnKeyDown(i);
}
}
}
else
{
for (auto& it : m_listeners)
{
if (i == VK_LBUTTON)
{
it.second->OnLeftMouseUp({ static_cast<float>(current_mouse_pos.x), static_cast<float>(current_mouse_pos.y) });
}
else if (i == VK_RBUTTON)
{
it.second->OnRightMouseUp({ static_cast<float>(current_mouse_pos.x), static_cast<float>(current_mouse_pos.y) });
}
else
{
it.second->OnKeyUp(i);
}
}
}
}
}
void InputSystem::AddListener(IInputListener* listener)
{
m_listeners.insert(std::pair<IInputListener*, IInputListener*>(std::forward<IInputListener*>(listener), std::forward<IInputListener*>(listener)));
}
void InputSystem::SetCursorPosition(const POINT& position)
{
SetCursorPos(position.x, position.y);
}
void InputSystem::ShowCursor(bool show)
{
::ShowCursor(show);
}
void InputSystem::RemoveListener(IInputListener* listener)
{
std::map<IInputListener*, IInputListener*>::iterator it = m_listeners.find(listener);
if (it != m_listeners.end())
{
m_listeners.erase(it);
}
}
InputSystem::~InputSystem()
{
}
InputSystem* InputSystem::Get()
{
static InputSystem s_input_system;
return &s_input_system;
}