From 2d648c6cee55db3bf2f29d866303eb339571895a Mon Sep 17 00:00:00 2001 From: Riyyi Date: Tue, 24 Dec 2019 23:19:10 +0100 Subject: [PATCH] Add mouse offset to Input --- inferno/src/inferno/input.cpp | 31 +++++++++++++++++++++++++++++++ inferno/src/inferno/input.h | 19 +++++++++++++++++-- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/inferno/src/inferno/input.cpp b/inferno/src/inferno/input.cpp index e07ee8f..23ff6ab 100644 --- a/inferno/src/inferno/input.cpp +++ b/inferno/src/inferno/input.cpp @@ -1,11 +1,42 @@ #include #include "inferno/application.h" +#include "inferno/event/mouseevent.h" #include "inferno/input.h" #include "inferno/window.h" namespace Inferno { + bool Input::m_firstMousePos = true; + float Input::m_xPosLast = 0.0f; + float Input::m_yPosLast = 0.0f; + float Input::m_xOffset = 0.0f; + float Input::m_yOffset = 0.0f; + + void Input::update() + { + m_xOffset = 0.0f; + m_yOffset = 0.0f; + } + + bool Input::onMousePosition(MousePositionEvent &e) + { + // Prevent weird jump on first cursor window enter + if(m_firstMousePos) { + m_firstMousePos = false; + m_xPosLast = e.getXPos(); + m_yPosLast = e.getYPos(); + } + + m_xOffset = e.getXPos() - m_xPosLast; + // Reversed since y-coordinates range from bottom to top + m_yOffset = m_yPosLast - e.getYPos(); + m_xPosLast = e.getXPos(); + m_yPosLast = e.getYPos(); + + return true; + } + bool Input::isKeyPressed(int key) { GLFWwindow* w = Application::get().getWindow().getWindow(); diff --git a/inferno/src/inferno/input.h b/inferno/src/inferno/input.h index 2a132b1..bd5fb41 100644 --- a/inferno/src/inferno/input.h +++ b/inferno/src/inferno/input.h @@ -1,20 +1,35 @@ #ifndef INPUT_H #define INPUT_H -#include +#include // std::pair namespace Inferno { + class MousePositionEvent; + class Input { public: + static void update(); + + static bool onMousePosition(MousePositionEvent &e); + static bool isKeyPressed(int key); static bool isMouseButtonPressed(int button); static std::pair getMousePosition(); static float getMouseX(); static float getMouseY(); + + static inline float getXOffset() { return m_xOffset; } + static inline float getYOffset() { return m_yOffset; } + + private: + static bool m_firstMousePos; + static float m_xPosLast; + static float m_yPosLast; + static float m_xOffset; + static float m_yOffset; }; } - #endif // INPUT_H