processKeyInput(dtime);
}
-void InputHandler::processMouseInput(double dtime)
+void InputHandler::listenFor(int key)
{
- vec2 cursor_delta = vec2(mouse_sensitivity * dtime) * (vec2)window->getCursorDelta();
- onMouseMove(dtime, cursor_delta.x, cursor_delta.y);
+ listened_keys.insert(key);
}
-void InputHandler::processKeyInput(double dtime)
+void InputHandler::dontListenFor(int key)
{
- set<int> keysDown;
- for (auto it = listened_keys.begin(); it != listened_keys.end(); it++) {
- int key = *it;
- if (window->wasKeyDown(key)) {
- keysDown.insert(key);
- }
- }
- onKeyPress(dtime, keysDown);
+ listened_keys.erase(key);
}
-void InputHandler::onMouseMove(double dtime, double x, double y)
+void InputHandler::addMouseHandler(void (*callback)(double, double, double))
{
- yaw += x;
- pitch -= y;
- pitch = clamp(pitch, -89.0, 89.0);
- camera->update(yaw, pitch);
+ mouse_handlers.insert(callback);
}
-void InputHandler::onKeyPress(double dtime, set<int> keys)
+void InputHandler::removeMouseHandler(void (*callback)(double, double, double))
{
- vec3 vel = vec3(speed * dtime);
- vec3 front = camera->front(), right = camera->right(), up = camera->up();
- if (! pitch_move) {
- front = normalize(vec3(front.x, 0, front.z));
- up = normalize(vec3(0, up.y, 0));
- }
- if (keys.find(GLFW_KEY_W) != keys.end()) {
- camera->pos += vel * front;
- } else if (keys.find(GLFW_KEY_S) != keys.end()) {
- camera->pos -= vel * front;
- }
- if (keys.find(GLFW_KEY_D) != keys.end()) {
- camera->pos += vel * right;
- } else if (keys.find(GLFW_KEY_A) != keys.end()) {
- camera->pos -= vel * right;
- }
- if (keys.find(GLFW_KEY_SPACE) != keys.end()) {
- camera->pos += vel * up;
- } else if (keys.find(GLFW_KEY_LEFT_SHIFT) != keys.end()) {
- camera->pos -= vel * up;
- }
+ mouse_handlers.erase(callback);
}
-void InputHandler::listenFor(int key)
+void InputHandler::addKeyHandler(void (*callback)(double, set<int>))
{
- listened_keys.insert(key);
+ key_handlers.insert(callback);
}
-void InputHandler::dontListenFor(int key)
+void InputHandler::removeKeyHandler(void (*callback)(double, set<int>))
{
- listened_keys.erase(key);
+ key_handlers.erase(callback);
}
-InputHandler::InputHandler(Camera *c, Window *w) : camera(c), window(w)
+InputHandler::InputHandler(Window *w) : window(w)
+{
+}
+
+void InputHandler::processMouseInput(double dtime)
{
- listenFor(GLFW_KEY_W);
- listenFor(GLFW_KEY_A);
- listenFor(GLFW_KEY_S);
- listenFor(GLFW_KEY_D);
- listenFor(GLFW_KEY_SPACE);
- listenFor(GLFW_KEY_LEFT_SHIFT);
+ vec2 cursor_delta = vec2(mouse_sensitivity * dtime) * (vec2)window->getCursorDelta();
+ double x, y;
+ x = cursor_delta.x;
+ y = cursor_delta.y;
+ if (x != 0 && y != 0) {
+ for (auto it = mouse_handlers.begin(); it != mouse_handlers.end(); it++) {
+ (**it)(dtime, x, y);
+ }
+ }
}
+void InputHandler::processKeyInput(double dtime)
+{
+ set<int> keysDown;
+ for (auto it = listened_keys.begin(); it != listened_keys.end(); it++) {
+ int key = *it;
+ if (window->wasKeyDown(key)) {
+ keysDown.insert(key);
+ }
+ }
+ if (keysDown.begin() != keysDown.end()) {
+ for (auto it = key_handlers.begin(); it != key_handlers.end(); it++) {
+ (**it)(dtime, keysDown);
+ }
+ }
+}