From 58a0d460094f26e1873767f033ace08e4e1eb70a Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Sun, 19 Dec 2021 15:11:24 -0500 Subject: [PATCH] Added Event system basics --- Navigator/src/Navigator/Application.cpp | 16 ++- Navigator/src/Navigator/Application.h | 8 +- Navigator/src/Navigator/Events/AppEvent.h | 52 ++++++++++ Navigator/src/Navigator/Events/Event.h | 82 +++++++++++++++ Navigator/src/Navigator/Events/KeyEvent.h | 88 +++++++++++++++++ Navigator/src/Navigator/Events/MouseEvent.h | 104 ++++++++++++++++++++ Navigator/src/Navigator/NavCore.h | 9 ++ navigator.sublime-project | 2 +- premake5.lua | 9 +- 9 files changed, 362 insertions(+), 8 deletions(-) create mode 100644 Navigator/src/Navigator/Events/AppEvent.h create mode 100644 Navigator/src/Navigator/Events/Event.h create mode 100644 Navigator/src/Navigator/Events/KeyEvent.h create mode 100644 Navigator/src/Navigator/Events/MouseEvent.h create mode 100644 Navigator/src/Navigator/NavCore.h diff --git a/Navigator/src/Navigator/Application.cpp b/Navigator/src/Navigator/Application.cpp index f170f71..07adf43 100644 --- a/Navigator/src/Navigator/Application.cpp +++ b/Navigator/src/Navigator/Application.cpp @@ -13,7 +13,18 @@ namespace Navigator { Application::~Application() {} - void Application::OnEvent() {} + void Application::OnEvent(Event& event) + { + EventDispatcher dispatch(event); + dispatch.Dispatch(BIND_EVENT_FUNCTION(Application::OnWindowCloseEvent)); + } + + bool Application::OnWindowCloseEvent(WindowCloseEvent& event) + { + m_runFlag = false; + NAV_WARN("Closing the window!"); + return true; + } void Application::PushLayer() {} @@ -24,7 +35,8 @@ namespace Navigator { while(m_runFlag) { NAV_TRACE("Doing a run."); - m_runFlag = false; + WindowCloseEvent event; + OnEvent(event); } } } \ No newline at end of file diff --git a/Navigator/src/Navigator/Application.h b/Navigator/src/Navigator/Application.h index a3cff1a..ab56843 100644 --- a/Navigator/src/Navigator/Application.h +++ b/Navigator/src/Navigator/Application.h @@ -1,6 +1,9 @@ #ifndef APPLICATION_H #define APPLICATION_H +#include "NavCore.h" +#include "Events/Event.h" +#include "Events/AppEvent.h" namespace Navigator { @@ -12,7 +15,7 @@ namespace Navigator { void Run(); - void OnEvent(); + void OnEvent(Event& event); void PushLayer(); void PushOverlay(); @@ -21,7 +24,8 @@ namespace Navigator { inline void GetWindow() { return; } private: - + bool OnWindowCloseEvent(WindowCloseEvent& event); + bool m_runFlag; static Application* s_instance; diff --git a/Navigator/src/Navigator/Events/AppEvent.h b/Navigator/src/Navigator/Events/AppEvent.h new file mode 100644 index 0000000..443e600 --- /dev/null +++ b/Navigator/src/Navigator/Events/AppEvent.h @@ -0,0 +1,52 @@ +#ifndef APP_EVENT_H +#define APP_EVENT_H + +#include "Event.h" + +namespace Navigator { + + class WindowCloseEvent : public Event + { + public: + WindowCloseEvent() {}; + + EVENT_CATEGORY_SETUP(EventCategoryApp) + EVENT_TYPE_SETUP(WindowClose) + }; + + class WindowResizeEvent : public Event + { + public: + WindowResizeEvent(float x, float y) : + m_xSize(x), m_ySize(y) + { + } + + inline float GetXSize() { return m_xSize; } + inline float GetYSize() { return m_ySize; } + std::string ToString() const override + { + std::stringstream ss; + ss << GetName() << " to size: (" << m_xSize << ", " << m_ySize << ")"; + return ss.str(); + } + + EVENT_CATEGORY_SETUP(EventCategoryApp) + EVENT_TYPE_SETUP(WindowResize) + + private: + float m_xSize, m_ySize; + }; + + class AppUpdateEvent : public Event + { + public: + AppUpdateEvent() = default; + + EVENT_CATEGORY_SETUP(EventCategoryApp) + EVENT_TYPE_SETUP(AppUpdate) + }; + +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Events/Event.h b/Navigator/src/Navigator/Events/Event.h new file mode 100644 index 0000000..3b0796b --- /dev/null +++ b/Navigator/src/Navigator/Events/Event.h @@ -0,0 +1,82 @@ +#ifndef EVENT_H +#define EVENT_H + +#include "NavCore.h" +#include + +namespace Navigator { + + enum class EventType + { + None=0, + WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved, + KeyPressed, KeyReleased, KeyTyped, + MouseButtonPressed, MouseButtonReleased, MouseScrolled, MouseMoved, + AppUpdate + }; + + enum EventCategory + { + EventCategoryNone=0, + EventCategoryApp=BIT(0), + EventCategoryInput=BIT(1), + EventCategoryKey=BIT(2), + EventCategoryMouse=BIT(3), + EventCategoryWindow=BIT(4) + }; + + //Some function generation automation to reduce code written for all events + #define EVENT_CATEGORY_SETUP(cat) virtual int GetCategoryFlags() const override { return cat; } + + #define EVENT_TYPE_SETUP(type) static EventType GetStaticType() { return EventType::type; } \ + virtual EventType GetEventType() const override { return GetStaticType(); } \ + virtual const char* GetName() const override { return #type; } + + class Event + { + friend class EventDispatcher; + public: + virtual EventType GetEventType() const = 0; + virtual const char* GetName() const = 0; + virtual int GetCategoryFlags() const = 0; + virtual std::string ToString() const { return GetName(); } + inline bool IsCategory(EventCategory cat) const { return GetCategoryFlags() & cat; } + bool handledFlag = false; + }; + + class EventDispatcher + { + template + using EventFunc = std::function; + + public: + EventDispatcher(Event& e) : + m_event(e) + { + } + + template + bool Dispatch(EventFunc function) + { + if(m_event.GetEventType() == T::GetStaticType()) + { + m_event.handledFlag = function(*(T*)&m_event); + return true; + } + else + { + return false; + } + } + + private: + Event& m_event; + }; + + inline std::ostream& operator<<(std::ostream& os, const Event& e) + { + return os << e.ToString(); + } +} + +#endif \ No newline at end of file diff --git a/Navigator/src/Navigator/Events/KeyEvent.h b/Navigator/src/Navigator/Events/KeyEvent.h new file mode 100644 index 0000000..921b53f --- /dev/null +++ b/Navigator/src/Navigator/Events/KeyEvent.h @@ -0,0 +1,88 @@ +#ifndef KEY_EVENT_H +#define KEY_EVENT_H + +#include "Event.h" + +namespace Navigator { + + class KeyEvent : public Event + { + public: + inline int GetKeycode() const { return m_keycode; } + EVENT_CATEGORY_SETUP(EventCategoryKey | EventCategoryInput) + protected: + KeyEvent(int code) : + m_keycode(code) + { + } + + int m_keycode; + + }; + + class KeyPressedEvent : public KeyEvent + { + public: + KeyPressedEvent(int code, int count) : + KeyEvent(code), m_repeatCount(count) + { + } + + EVENT_TYPE_SETUP(KeyPressed) + + inline int GetRepeatCount() const { return m_repeatCount; } + + std::string ToString() const override + { + std::stringstream stream; + stream << GetName() << " with code "<