Repository navigation
Expand file tree
/
Copy pathcontroller.cpp
More file actions
160 lines (125 loc) · 3.22 KB
/
Copy pathcontroller.cpp
File metadata and controls
160 lines (125 loc) · 3.22 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
#include<GLFW/glfw3.h>
#include "controller.h"
#include "camera.h"
struct MOUSE_DRAG_CONTROL {
bool is_active;
double dx;
double dy;
double _last_x;
double _last_y;
};
struct MOUSE_DRAG_CONTROL drag_control;
void drag_init(GLFWwindow* window) {
drag_control.is_active = true;
drag_control.dx = 0;
drag_control.dy = 0;
glfwGetCursorPos(window, &drag_control._last_x, &drag_control._last_y);
}
void drag_stop() {
drag_control.is_active = false;
drag_control.dx = 0;
drag_control.dy = 0;
}
void drag_update(GLFWwindow* window) {
if (!drag_control.is_active) {
return;
}
double x, y;
glfwGetCursorPos(window, &x, &y);
drag_control.dx = x - drag_control._last_x;
drag_control.dy = y - drag_control._last_y;
drag_control._last_x = x;
drag_control._last_y = y;
}
void drag_get_vector(double *dx, double *dy) {
*dx = drag_control.dx;
*dy = drag_control.dy;
}
//std::shared_ptr<Controller> Controller::getptr()
//{
// return shared_from_this();
//}
void FrameEventHandler::FrameUpdate()
{
}
void CFpsCameraController::FrameUpdate()
{
double dx, dy;
drag_get_vector(&dx, &dy);
camera->yall -= dx / camera->sensitivity;
camera->pitch -= dy / camera->sensitivity;
float ryall = glm::radians(camera->yall);
float rpitch = glm::radians(camera->pitch);
glm::vec3 forward(
-sin(ryall)*cos(rpitch),
sin(rpitch),
-cos(ryall)*cos(rpitch));
glm::vec3 right(
cos(ryall),
0,
-sin(ryall));
camera->position += forward * axis.GetY() + right * axis.GetX();
}
void InputAxis::TestKey(int key, int action)
{
for (int i = 0; i < 4; i++) {
auto iter = std::find(stroke_keys[i].begin(), stroke_keys[i].end(), key);
if (iter != stroke_keys[i].end()) {
stroke_strength[i] = action;
break;
}
}
}
void InputAxis::OnKeyEvent(int key, int scancode, int action, int mods)
{
TestKey(key, action);
}
InputAxis::InputAxis()
{
stroke_keys[UP].push_back(GLFW_KEY_W);
stroke_keys[UP].push_back(GLFW_KEY_UP);
stroke_keys[DOWN].push_back(GLFW_KEY_S);
stroke_keys[DOWN].push_back(GLFW_KEY_DOWN);
stroke_keys[LEFT].push_back(GLFW_KEY_A);
stroke_keys[LEFT].push_back(GLFW_KEY_LEFT);
stroke_keys[RIGHT].push_back(GLFW_KEY_D);
stroke_keys[RIGHT].push_back(GLFW_KEY_RIGHT);
}
float InputAxis::GetX()
{
auto v= stroke_strength[RIGHT] - stroke_strength[LEFT];
if (v > 1) { v = 1; }
if (v < -1) { v = -1; }
return v;
}
float InputAxis::GetY()
{
auto v = stroke_strength[UP] - stroke_strength[DOWN];
if (v > 1) { v = 1; }
if (v < -1) { v = -1; }
return v;
}
#include<list>
std::list<KeyEventHandler*> handlers;
void _handler_call(int key, int scancode, int action, int mods) {
for (auto i = handlers.begin(); i != handlers.end(); i++) {
(*i)->OnKeyEvent(key, scancode, action, mods);
}
}
void _key_callback(GLFWwindow*, int key, int scancode, int action, int mods) {
_handler_call(key, scancode, action, mods);
}
//void KeyEventHandler::Enable()
//{
// /*GLFWkeyfun(keyfunc);
// std::cout << "???"<<std::endl;*/
// //glfwSetKeyCallback()
//}
KeyEventHandler::KeyEventHandler()
{
handlers.push_back(this);
}
KeyEventHandler::~KeyEventHandler()
{
handlers.remove(this);
}