From e7c973385e85ff628c5e0e2559626fe926eedaf6 Mon Sep 17 00:00:00 2001 From: Rick van Vonderen <0945444@hr.nl> Date: Mon, 16 Dec 2019 17:30:20 +0100 Subject: [PATCH] Add event class --- inferno/src/inferno/core.h | 7 ++- inferno/src/inferno/event/event.h | 89 +++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 inferno/src/inferno/event/event.h diff --git a/inferno/src/inferno/core.h b/inferno/src/inferno/core.h index a7ff7ed..f7b1cd2 100644 --- a/inferno/src/inferno/core.h +++ b/inferno/src/inferno/core.h @@ -1,7 +1,12 @@ #ifndef CORE_H #define CORE_H -#include +#include // raise +#include // bind + +#define BIT(x) (1 << x) + +#define NF_BIND_EVENT(f) std::bind(&f, this, std::placeholders::_1) // Debugging defines #ifndef NDEBUG diff --git a/inferno/src/inferno/event/event.h b/inferno/src/inferno/event/event.h new file mode 100644 index 0000000..dbc76d7 --- /dev/null +++ b/inferno/src/inferno/event/event.h @@ -0,0 +1,89 @@ +#ifndef EVENT_H +#define EVENT_H + +#include // ostream + +#include "inferno/core.h" + +namespace Inferno { + + enum class EventType { + None = 0, + WindowClose, + WindowResize, + }; + + enum EventCategory { + None = 0, + ApplicationEventCategory = BIT(0), + }; + + class Event { + // EventDispatcher is allowed to access Event private/protected members + friend class EventDispatcher; + + public: + virtual ~Event() {} + + virtual std::string toString() const { return getName(); } + + inline bool inCategory(EventCategory category) + { + return getCategoryFlags() & category; + } + +// ----------------------------------------- + + // Getter function templates + virtual char getCategoryFlags() const = 0; + virtual const char* getName() const = 0; + virtual EventType getType() const = 0; + + protected: + bool handled = false; + }; + + class EventDispatcher { + public: + EventDispatcher(Event &e) : m_event(e) {} + + // Easily dispatch all type of Events, call with: + // dispatch(std::bind(&F, this, std::placeholders::_1)); + // T is the type of Event + // F is the function to call, signature: bool name(T &e); + template + bool dispatch(const F &function) + { + // If type is same as member variable type + if (T::getTypeStatic() == m_event.getType()) { + // Call the function + m_event.handled = function(static_cast(m_event)); + return true; + } + + return false; + } + + private: + Event &m_event; + }; + + // Add class type functions macro +#define EVENT_CLASS_TYPE(type) \ + virtual const char* getName() const override { return #type; } \ + virtual EventType getType() const override { return getTypeStatic(); } \ + static inline EventType getTypeStatic() { return EventType::type; } + + // Add class catergory function macro +#define EVENT_CLASS_CATEGORY(category) \ + virtual char getCategoryFlags() const override { return category; } + + // Make Events easily printable + inline std::ostream& operator<<(std::ostream &os, const Event &e) + { + return os << e.toString(); + } + +} + +#endif // EVENT_H