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