Complete input refactor
This commit is contained in:
@@ -55,10 +55,13 @@ void Camera::updatePositionFromKeyboardInput(bool *cameraActionRegister, float d
|
||||
position += deltaPos;
|
||||
}
|
||||
|
||||
void Camera::updateDirectionFromMouseInput(float deltaCursorX, float deltaCursorY) {
|
||||
if(deltaCursorX==0 && deltaCursorY==0) return;
|
||||
yaw += deltaCursorX;
|
||||
pitch += deltaCursorY;
|
||||
void Camera::updateDirectionFromMouseInput(double *cameraMouseActionRegister) {
|
||||
|
||||
if(cameraMouseActionRegister[cameraMouseDeltaX]==0 && cameraMouseActionRegister[cameraMouseDeltaY]==0)
|
||||
return;
|
||||
|
||||
yaw += cameraMouseActionRegister[cameraMouseDeltaX];
|
||||
pitch += cameraMouseActionRegister[cameraMouseDeltaY];
|
||||
|
||||
if(pitch > 89.0f)
|
||||
pitch = 89.0f;
|
||||
|
||||
@@ -12,7 +12,7 @@ public:
|
||||
void updateVPM();
|
||||
void updateAspectRatio(int width, int height);
|
||||
void updatePositionFromKeyboardInput(bool *actionCameraRegister, float deltaTime);
|
||||
void updateDirectionFromMouseInput(float deltaCursorX, float deltaCursorY);
|
||||
void updateDirectionFromMouseInput(double *cameraMouseActionRegister);
|
||||
|
||||
void translate(glm::vec3 translateVector);
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ void Controller::run() {
|
||||
|
||||
camera->updatePositionFromKeyboardInput(gameEventHandler->getCameraActionRegister(), deltaTime);
|
||||
if(gameWindow->getMouseIsCatched())
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDeltaX(), gameEventHandler->getCursorDeltaY());
|
||||
camera->updateDirectionFromMouseInput(gameEventHandler->getCursorDelta());
|
||||
|
||||
gameWindow->handleActionRegister(gameEventHandler->getWindowActionRegister());
|
||||
}
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
#include <iostream>
|
||||
|
||||
bool EventHandler::cameraActionRegister[CAMERA_ACTION_NUM_ITEMS] = {0};
|
||||
double EventHandler::cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS] = {0.0, 0.0};
|
||||
bool EventHandler::windowActionRegister[WINDOW_ACTION_NUM_ITEMS] = {0};
|
||||
|
||||
bool EventHandler::firstMouseInput = 1;
|
||||
float EventHandler::mouseSensitivity = 0.08f;
|
||||
|
||||
|
||||
EventHandler::EventHandler(GLFWwindow *window)
|
||||
@@ -14,42 +16,26 @@ EventHandler::EventHandler(GLFWwindow *window)
|
||||
glfwSetKeyCallback(window, key_callback);
|
||||
|
||||
// Currently disabled because callbacks are shitty
|
||||
//glfwSetCursorPosCallback(window, mouse_callback);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::handleEvents() {
|
||||
|
||||
for(int i = 0; i < CAMERA_MOUSE_ACTION_NUM_ITEMS; i++)
|
||||
cameraMouseActionRegister[i] = 0.0;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
for(int i=0; i<CAMERA_ACTION_NUM_ITEMS; i++) {
|
||||
cameraActionRegister[i] = 0;
|
||||
}
|
||||
|
||||
updateMouseDelta();
|
||||
|
||||
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraForward] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraBackward] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraUp] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraDown] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraLeft] = 1;
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||
cameraActionRegister[cameraRight] = 1;
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
|
||||
// Silence warnings of unused variables.
|
||||
(void)window; (void)scancode; (void)mods; (void)key; (void)action;
|
||||
|
||||
if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
|
||||
if(key==GLFW_KEY_O && action == GLFW_PRESS)
|
||||
windowActionRegister[wireFrameToggle] = 1;
|
||||
if(key==GLFW_KEY_LEFT_CONTROL && action == GLFW_PRESS) {
|
||||
@@ -57,16 +43,48 @@ void EventHandler::key_callback(GLFWwindow *window, int key, int scancode, int a
|
||||
firstMouseInput = 1;
|
||||
}
|
||||
|
||||
// Movement press
|
||||
if(key==GLFW_KEY_W && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraForward] = 1;
|
||||
if(key==GLFW_KEY_S && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraBackward] = 1;
|
||||
if(key==GLFW_KEY_SPACE && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraUp] = 1;
|
||||
if(key==GLFW_KEY_LEFT_SHIFT && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraDown] = 1;
|
||||
if(key==GLFW_KEY_A && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraLeft] = 1;
|
||||
if(key==GLFW_KEY_D && action == GLFW_PRESS)
|
||||
cameraActionRegister[cameraRight] = 1;
|
||||
|
||||
// Movement release
|
||||
if(key==GLFW_KEY_W && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraForward] = 0;
|
||||
if(key==GLFW_KEY_S && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraBackward] = 0;
|
||||
if(key==GLFW_KEY_SPACE && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraUp] = 0;
|
||||
if(key==GLFW_KEY_LEFT_SHIFT && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraDown] = 0;
|
||||
if(key==GLFW_KEY_A && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraLeft] = 0;
|
||||
if(key==GLFW_KEY_D && action == GLFW_RELEASE)
|
||||
cameraActionRegister[cameraRight] = 0;
|
||||
|
||||
}
|
||||
|
||||
void EventHandler::updateMouseDelta() {
|
||||
double xPos, yPos;
|
||||
glfwGetCursorPos(window, &xPos, &yPos);
|
||||
void EventHandler::mouse_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
|
||||
deltaCursorPosX = xPos - lastCursorPosX;
|
||||
deltaCursorPosY = -(yPos - lastCursorPosY);
|
||||
lastCursorPosX = xPos;
|
||||
lastCursorPosY = yPos;
|
||||
(void)window;
|
||||
|
||||
static double lastCursorPosX = 0.0;
|
||||
static double lastCursorPosY = 0.0;
|
||||
|
||||
double deltaCursorPosX = xpos - lastCursorPosX;
|
||||
double deltaCursorPosY = -(ypos - lastCursorPosY);
|
||||
|
||||
lastCursorPosX = xpos;
|
||||
lastCursorPosY = ypos;
|
||||
|
||||
// Check if this is the first VALID mouse event after window being resized
|
||||
if(firstMouseInput && !(deltaCursorPosX == 0 && deltaCursorPosY == 0)) {
|
||||
@@ -77,4 +95,8 @@ void EventHandler::updateMouseDelta() {
|
||||
|
||||
deltaCursorPosX *= mouseSensitivity;
|
||||
deltaCursorPosY *= mouseSensitivity;
|
||||
|
||||
cameraMouseActionRegister[cameraMouseDeltaX] = deltaCursorPosX;
|
||||
cameraMouseActionRegister[cameraMouseDeltaY] = deltaCursorPosY;
|
||||
|
||||
}
|
||||
|
||||
@@ -16,30 +16,22 @@ public:
|
||||
bool * getCameraActionRegister() { return cameraActionRegister; }
|
||||
bool * getWindowActionRegister() { return windowActionRegister; }
|
||||
|
||||
float getCursorDeltaX() { return deltaCursorPosX; }
|
||||
float getCursorDeltaY() { return deltaCursorPosY; }
|
||||
double * getCursorDelta() { return cameraMouseActionRegister; }
|
||||
|
||||
void setFirstMouseInput(bool val) { firstMouseInput = val; }
|
||||
void setLastCursorPos(float posX, float posY) { lastCursorPosX=posX; lastCursorPosY=posY; }
|
||||
|
||||
private:
|
||||
|
||||
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
|
||||
static void mouse_callback(GLFWwindow* window, double xpos, double ypos);
|
||||
|
||||
void updateMouseDelta();
|
||||
|
||||
static bool cameraActionRegister[CAMERA_ACTION_NUM_ITEMS];
|
||||
static double cameraMouseActionRegister[CAMERA_MOUSE_ACTION_NUM_ITEMS];
|
||||
static bool windowActionRegister[WINDOW_ACTION_NUM_ITEMS];
|
||||
|
||||
GLFWwindow *window;
|
||||
|
||||
float lastCursorPosX = 0.0f;
|
||||
float lastCursorPosY = 0.0f;
|
||||
float deltaCursorPosX = 0.0f;
|
||||
float deltaCursorPosY = 0.0f;
|
||||
|
||||
float mouseSensitivity = 0.08f;
|
||||
static float mouseSensitivity;
|
||||
|
||||
static bool firstMouseInput;
|
||||
|
||||
|
||||
@@ -8,6 +8,12 @@ enum cameraActions {
|
||||
CAMERA_ACTION_NUM_ITEMS
|
||||
};
|
||||
|
||||
enum cameraMouseActions {
|
||||
cameraMouseDeltaX,
|
||||
cameraMouseDeltaY,
|
||||
CAMERA_MOUSE_ACTION_NUM_ITEMS
|
||||
};
|
||||
|
||||
enum windowActions {
|
||||
wireFrameToggle,
|
||||
mouseCatchToggle,
|
||||
|
||||
Reference in New Issue
Block a user