Browse Source

Add and implement mouse Events

master
Rick van Vonderen 5 years ago
parent
commit
5f53a3037a
  1. 13
      inferno/src/inferno/event/event.h
  2. 105
      inferno/src/inferno/event/mouseevent.h
  3. 36
      inferno/src/inferno/window.cpp
  4. 2
      inferno/src/inferno/window.h

13
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 {

105
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

36
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);

2
inferno/src/inferno/window.h

@ -3,7 +3,7 @@
#include <functional> // std::function
class GLFWwindow;
struct GLFWwindow;
namespace Inferno {

Loading…
Cancel
Save