Add and implement mouse Events
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
// Mouse position callback
|
||||
// glfwSetCursorPosCallback
|
||||
// glfwSetKeyCallback(GLFWwindow* window, int key, int scanCode, int action, int mods);
|
||||
|
||||
// Mouse button callback
|
||||
// glfwSetMouseButtonCallback
|
||||
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(m_window, [](GLFWwindow* window, double xPos, double yPos) {
|
||||
Window &w = *(Window*)glfwGetWindowUserPointer(window);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <functional> // std::function
|
||||
|
||||
class GLFWwindow;
|
||||
struct GLFWwindow;
|
||||
|
||||
namespace Inferno {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user