-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathinput_system.cpp
More file actions
170 lines (152 loc) · 6.18 KB
/
Copy pathinput_system.cpp
File metadata and controls
170 lines (152 loc) · 6.18 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
#include "runtime/function/input/input_system.h"
#include "core/base/macro.h"
#include "runtime/engine.h"
#include "runtime/function/global/global_context.h"
#include "runtime/function/render/render_camera.h"
#include "runtime/function/render/render_system.h"
#include "runtime/function/render/window_system.h"
#include <GLFW/glfw3.h>
namespace Piccolo
{
unsigned int k_complement_control_command = 0xFFFFFFFF;
void InputSystem::onKey(int key, int scancode, int action, int mods)
{
if (!g_is_editor_mode)
{
onKeyInGameMode(key, scancode, action, mods);
}
}
void InputSystem::onKeyInGameMode(int key, int scancode, int action, int mods)
{
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::jump);
if (action == GLFW_PRESS)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
// close();
break;
case GLFW_KEY_R:
break;
case GLFW_KEY_A:
m_game_command |= (unsigned int)GameCommand::left;
break;
case GLFW_KEY_S:
m_game_command |= (unsigned int)GameCommand::backward;
break;
case GLFW_KEY_W:
m_game_command |= (unsigned int)GameCommand::forward;
break;
case GLFW_KEY_D:
m_game_command |= (unsigned int)GameCommand::right;
break;
case GLFW_KEY_SPACE:
m_game_command |= (unsigned int)GameCommand::jump;
break;
case GLFW_KEY_LEFT_CONTROL:
m_game_command |= (unsigned int)GameCommand::squat;
break;
case GLFW_KEY_LEFT_ALT: {
std::shared_ptr<WindowSystem> window_system = g_runtime_global_context.m_window_system;
window_system->setFocusMode(!window_system->getFocusMode());
}
break;
case GLFW_KEY_LEFT_SHIFT:
m_game_command |= (unsigned int)GameCommand::sprint;
break;
case GLFW_KEY_F:
m_game_command ^= (unsigned int)GameCommand::free_camera;
break;
default:
break;
}
}
else if (action == GLFW_RELEASE)
{
switch (key)
{
case GLFW_KEY_ESCAPE:
// close();
break;
case GLFW_KEY_R:
break;
case GLFW_KEY_W:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::forward);
break;
case GLFW_KEY_S:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::backward);
break;
case GLFW_KEY_A:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::left);
break;
case GLFW_KEY_D:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::right);
break;
case GLFW_KEY_LEFT_CONTROL:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::squat);
break;
case GLFW_KEY_LEFT_SHIFT:
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::sprint);
break;
default:
break;
}
}
}
void InputSystem::onCursorPos(double current_cursor_x, double current_cursor_y)
{
if (g_runtime_global_context.m_window_system->getFocusMode())
{
m_cursor_delta_x = m_last_cursor_x - current_cursor_x;
m_cursor_delta_y = m_last_cursor_y - current_cursor_y;
}
m_last_cursor_x = current_cursor_x;
m_last_cursor_y = current_cursor_y;
}
void InputSystem::clear()
{
m_cursor_delta_x = 0;
m_cursor_delta_y = 0;
}
void InputSystem::calculateCursorDeltaAngles()
{
std::array<int, 2> window_size = g_runtime_global_context.m_window_system->getWindowSize();
if (window_size[0] < 1 || window_size[1] < 1)
{
return;
}
std::shared_ptr<RenderCamera> render_camera = g_runtime_global_context.m_render_system->getRenderCamera();
const Vector2& fov = render_camera->getFOV();
Radian cursor_delta_x(Math::degreesToRadians(m_cursor_delta_x));
Radian cursor_delta_y(Math::degreesToRadians(m_cursor_delta_y));
m_cursor_delta_yaw = (cursor_delta_x / (float)window_size[0]) * fov.x;
m_cursor_delta_pitch = -(cursor_delta_y / (float)window_size[1]) * fov.y;
}
void InputSystem::initialize()
{
std::shared_ptr<WindowSystem> window_system = g_runtime_global_context.m_window_system;
ASSERT(window_system);
window_system->registerOnKeyFunc(std::bind(&InputSystem::onKey,
this,
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3,
std::placeholders::_4));
window_system->registerOnCursorPosFunc(
std::bind(&InputSystem::onCursorPos, this, std::placeholders::_1, std::placeholders::_2));
}
void InputSystem::tick()
{
calculateCursorDeltaAngles();
clear();
std::shared_ptr<WindowSystem> window_system = g_runtime_global_context.m_window_system;
if (window_system->getFocusMode())
{
m_game_command &= (k_complement_control_command ^ (unsigned int)GameCommand::invalid);
}
else
{
m_game_command |= (unsigned int)GameCommand::invalid;
}
}
} // namespace Piccolo