From 5f53a3037a029021fc4e9fdf1447df08dd83194a Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Wed, 18 Dec 2019 16:21:57 +0100 Subject: [PATCH] Add and implement mouse Events --- inferno/src/inferno/event/event.h | 13 +++ inferno/src/inferno/event/mouseevent.h | 105 +++++++++++++++++++++++++ inferno/src/inferno/window.cpp | 36 +++++++-- inferno/src/inferno/window.h | 2 +- 4 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 inferno/src/inferno/event/mouseevent.h diff --git a/inferno/src/inferno/event/event.h b/inferno/src/inferno/event/event.h index 7168ed6..d4303f2 100644 --- a/inferno/src/inferno/event/event.h +++ b/inferno/src/inferno/event/event.h @@ -9,13 +9,26 @@ namespace Inferno { enum class EventType { None = 0, + WindowClose, WindowResize, + + MouseButtonPress, + MouseButtonRelease, + MousePosition, + MouseScroll, }; enum EventCategory { None = 0, ApplicationEventCategory = BIT(0), + + InputEventCategory = BIT(1), + + KeyEventCategory = BIT(2), + + MouseEventCategory = BIT(3), + MouseButtonEventCategory = BIT(4), }; class Event { diff --git a/inferno/src/inferno/event/mouseevent.h b/inferno/src/inferno/event/mouseevent.h new file mode 100644 index 0000000..31668a5 --- /dev/null +++ b/inferno/src/inferno/event/mouseevent.h @@ -0,0 +1,105 @@ +#ifndef MOUSEEVENT_H +#define MOUSEEVENT_H + +#include "inferno/event/event.h" + +namespace Inferno { + + class MouseButtonEvent : public Event { + public: + MouseButtonEvent(int button) : + m_button(button) {} + +// ----------------------------------------- + + inline int getButton() const { return m_button; } + + EVENT_CLASS_CATEGORY(InputEventCategory | MouseEventCategory | MouseButtonEventCategory) + + protected: + int m_button; + }; + + class MouseButtonPressEvent : public MouseButtonEvent { + public: + MouseButtonPressEvent(int button) : + MouseButtonEvent(button) {} + + virtual std::string toString() const override + { + std::stringstream ss; + ss << "MouseButtonPressed: " << m_button; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseButtonPress) + }; + + class MouseButtonReleaseEvent : public MouseButtonEvent { + public: + MouseButtonReleaseEvent(int button) : + MouseButtonEvent(button) {} + + virtual std::string toString() const override + { + std::stringstream ss; + ss << "MouseButtonReleased: " << m_button; + return ss.str(); + } + + EVENT_CLASS_TYPE(MouseButtonRelease) + }; + + class MousePositionEvent : public Event { + public: + MousePositionEvent(float xPos, float yPos) : + m_xPos(xPos), m_yPos(yPos) {} + + virtual std::string toString() const override + { + std::stringstream ss; + ss << "MousePosition: " << m_xPos << "x" << m_yPos; + return ss.str(); + } + +// ----------------------------------------- + + inline float getXPos() const { return m_xPos; } + inline float getYPos() const { return m_yPos; } + + EVENT_CLASS_TYPE(MousePosition) + EVENT_CLASS_CATEGORY(InputEventCategory | MouseEventCategory) + + private: + float m_xPos; + float m_yPos; + }; + + class MouseScrollEvent : public Event { + public: + MouseScrollEvent(float xOffset, float yOffset) : + m_xOffset(xOffset), m_yOffset(yOffset) {} + + virtual std::string toString() const override + { + std::stringstream ss; + ss << "MouseScroll: " << m_xOffset << ":" << m_yOffset; + return ss.str(); + } + +// ----------------------------------------- + + inline float getXOffset() const { return m_xOffset; } + inline float getYOffset() const { return m_yOffset; } + + EVENT_CLASS_TYPE(MouseScroll) + EVENT_CLASS_CATEGORY(InputEventCategory | MouseEventCategory) + + private: + float m_xOffset; + float m_yOffset; + }; + +} + +#endif // MOUSEEVENT_H diff --git a/inferno/src/inferno/window.cpp b/inferno/src/inferno/window.cpp index bd5626e..789d27d 100644 --- a/inferno/src/inferno/window.cpp +++ b/inferno/src/inferno/window.cpp @@ -3,6 +3,7 @@ #include "inferno/core.h" #include "inferno/event/applicationevent.h" +#include "inferno/event/mouseevent.h" #include "inferno/log.h" #include "inferno/settings.h" #include "inferno/window.h" @@ -91,16 +92,41 @@ namespace Inferno { }); // Keyboard callback - // glfwSetKeyCallback + // glfwSetKeyCallback(GLFWwindow* window, int key, int scanCode, int action, int mods); + + // Mouse button callback + glfwSetMouseButtonCallback(m_window, [](GLFWwindow* window, int button, int action, int mods) { + Window &w = *(Window*)glfwGetWindowUserPointer(window); + + switch (action) { + case GLFW_PRESS: { + MouseButtonPressEvent event(button); + w.m_eventCallback(event); + break; + } + case GLFW_RELEASE: { + MouseButtonReleaseEvent event(button); + w.m_eventCallback(event); + break; + } + } + }); // Mouse position callback - // glfwSetCursorPosCallback + glfwSetCursorPosCallback(m_window, [](GLFWwindow* window, double xPos, double yPos) { + Window &w = *(Window*)glfwGetWindowUserPointer(window); - // Mouse button callback - // glfwSetMouseButtonCallback + MousePositionEvent event(xPos, yPos); + w.m_eventCallback(event); + }); // Mouse scroll callback - // glfwSetScrollCallback + glfwSetScrollCallback(m_window, [](GLFWwindow* window, double xOffset, double yOffset) { + Window &w = *(Window*)glfwGetWindowUserPointer(window); + + MouseScrollEvent event(xOffset, yOffset); + w.m_eventCallback(event); + }); glViewport(0, 0, width, height); diff --git a/inferno/src/inferno/window.h b/inferno/src/inferno/window.h index 446934e..8ff5cde 100644 --- a/inferno/src/inferno/window.h +++ b/inferno/src/inferno/window.h @@ -3,7 +3,7 @@ #include // std::function -class GLFWwindow; +struct GLFWwindow; namespace Inferno {