From ee4df2592a584e4dd2a8eeab2ac3c2282affe111 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Thu, 18 Jan 2024 19:54:21 +0100 Subject: [PATCH] Started events abstraction and classes --- bakara/src/bakara/events/app_event.h | 17 +++++-- bakara/src/bakara/events/event.cpp | 24 ---------- bakara/src/bakara/events/event.h | 45 +++++++++++++------ bakara/src/bakara/events/key_event.h | 59 +++++++++++++++++++++++++ bakara/src/bakara/events/window_event.h | 36 ++++++++------- sandbox/src/sandbox.cpp | 4 +- 6 files changed, 126 insertions(+), 59 deletions(-) delete mode 100644 bakara/src/bakara/events/event.cpp diff --git a/bakara/src/bakara/events/app_event.h b/bakara/src/bakara/events/app_event.h index daf784d..c9d29ed 100644 --- a/bakara/src/bakara/events/app_event.h +++ b/bakara/src/bakara/events/app_event.h @@ -5,16 +5,25 @@ namespace Bk { class AppTickEvent : public Event { + AppTickEvent() = default; - } + EVENT_CLASS_TYPE(AppTick) + EVENT_CLASS_CATEGORY(AppEvent) + }; class AppRenderEvent : public Event { + AppRenderEvent() = default; - } + EVENT_CLASS_TYPE(AppRender) + EVENT_CLASS_CATEGORY(AppEvent) + }; class AppUpdateEvent : public Event { - - } + AppUpdateEvent() = default; + + EVENT_CLASS_TYPE(AppUpdate) + EVENT_CLASS_CATEGORY(AppEvent) + }; } \ No newline at end of file diff --git a/bakara/src/bakara/events/event.cpp b/bakara/src/bakara/events/event.cpp deleted file mode 100644 index f35e5cd..0000000 --- a/bakara/src/bakara/events/event.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "event.h" - -namespace Bk { - EventDispatcher::EventDispatcher(Event& event) - { - p_event = event; - } - - bool ventDispatcher::Dispatch(const F& func) - { - if (p_event.GetEventType() == T::GetStaticType()) - { - p_event.handled |= func(static_cast(p_event)); - return true; - } - return false; - } - - inline std::ostream& operator<<(std::ostream& os, Event& e) - { - return os << e.ToString(); - } - -} \ No newline at end of file diff --git a/bakara/src/bakara/events/event.h b/bakara/src/bakara/events/event.h index 8dafdce..bf704f5 100644 --- a/bakara/src/bakara/events/event.h +++ b/bakara/src/bakara/events/event.h @@ -1,6 +1,8 @@ #pragma once -#include +#include +#include +#include namespace Bk { enum class EventType @@ -33,11 +35,13 @@ namespace Bk { MouseButtonEvent = BIT_SHIFT(4) }; - #define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\ - virtual EventType GetEventType() const override { return GetStaticType(); }\ - virtual const char* GetName() const override { return #type; } + #define EVENT_CLASS_TYPE(type) static EventType get_static_type() { return EventType::type; }\ + virtual EventType get_event_type() const override { return get_static_type(); }\ + virtual const char* get_name() const override { return #type; } - #define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; } + #define EVENT_CLASS_CATEGORY(category) virtual int get_category_flags() const override { return (int)EventCategory::category; } + + #define EVENT_STRINGIFY(str, ...) std::string to_string() const override { return format(str, __VA_ARGS__); } class Event { @@ -46,27 +50,40 @@ namespace Bk { bool handled = false; - virtual EventType GetEventType() const = 0; - virtual const char* GetName() const = 0; - virtual int GetCategoryFlags() const = 0; - virtual std::string ToString() const { return GetName(); } + virtual EventType get_event_type() const = 0; + virtual const char* get_name() const = 0; + virtual int get_category_flags() const = 0; + + virtual std::string to_string() const { return get_name(); } - bool IsInCategory(EventCategory category) + bool is_in_category(EventCategory category) { - return GetCategoryFlags() & (int)category == GetCategoryFlags(); + return get_category_flags() & (int)category == get_category_flags(); } }; - inline std::ostream& operator<<(std::ostream& os, Event& e); + inline std::ostream& operator<<(std::ostream& os, Event& e) + { + return os << e.to_string(); + } class EventDispatcher { public: - EventDispatcher(Event& event); + EventDispatcher(Event& event) + : p_event(event) {} // F will be deduced by the compiler template - bool Dispatch(const F& func); + bool dispatch(const F& func) + { + if (p_event.get_event_type() == T::get_static_type()) + { + p_event.handled |= func(static_cast(p_event)); + return true; + } + return false; + } private: Event& p_event; }; diff --git a/bakara/src/bakara/events/key_event.h b/bakara/src/bakara/events/key_event.h index e69de29..996956d 100644 --- a/bakara/src/bakara/events/key_event.h +++ b/bakara/src/bakara/events/key_event.h @@ -0,0 +1,59 @@ +#pragma once + + +#include +#include "event.h" + +namespace Bk { + class KeyEvent : public Event + { + public: + KeyCode get_key() const { return m_key; } + + EVENT_CLASS_CATEGORY(KeyboardEvent) + + protected: + KeyEvent(const KeyCode key) + : m_key(key) {} + + KeyCode m_key; + }; + + class KeyPressEvent : public KeyEvent + { + public: + KeyPressEvent(KeyCode key, bool is_repeated = false) + : KeyEvent(key), p_is_repeated(is_repeated) {} + + EVENT_CLASS_TYPE(KeyPress) + + EVENT_STRINGIFY("KeyPressEvent: %d repeat : %b", m_key, p_is_repeated) + + bool is_repeated() { return p_is_repeated; } + + private: + bool p_is_repeated; + }; + + class KeyReleaseEvent : public KeyEvent + { + public: + KeyReleaseEvent(KeyCode key) + : KeyEvent(key) {} + + EVENT_CLASS_TYPE(KeyRelease) + + EVENT_STRINGIFY("KeyReleaseEvent: %d", m_key) + }; + + class KeyTypedEvent : public KeyEvent + { + public: + KeyTypedEvent(const KeyCode keycode) + : KeyEvent(keycode) {} + + EVENT_CLASS_TYPE(KeyTyped) + + EVENT_STRINGIFY("KeyTypedEvent: %d", m_key) + }; +} \ No newline at end of file diff --git a/bakara/src/bakara/events/window_event.h b/bakara/src/bakara/events/window_event.h index b180cc2..b8e5741 100644 --- a/bakara/src/bakara/events/window_event.h +++ b/bakara/src/bakara/events/window_event.h @@ -1,30 +1,34 @@ -pragma once +#pragma once +#include +#include #include "event.h" namespace Bk { class WindowResizeEvent : public Event { + public: + WindowResizeEvent(uint width, uint height) + : p_width(width), p_height(height) {}; - } + uint get_width() const { return p_width; } + uint get_height() const { return p_height; } - class WindowCloseEvent : public Event - { + EVENT_STRINGIFY("WindowResizeEvent : %d %d", p_width, p_height) - } + EVENT_CLASS_TYPE(WindowResize) + EVENT_CLASS_CATEGORY(AppEvent) - class WindowFocusEvent : public Event - { - - } + private: + uint p_width; + uint p_height; + }; - class WindowLostFocusEvent : public Event + class WindowCloseEvent : public Event { + WindowCloseEvent() = default; - } - - class WindowMoveEvent : public Event - { - - } + EVENT_CLASS_TYPE(WindowClose) + EVENT_CLASS_CATEGORY(AppEvent) + }; } \ No newline at end of file diff --git a/sandbox/src/sandbox.cpp b/sandbox/src/sandbox.cpp index 2c22ab7..fc62e8f 100644 --- a/sandbox/src/sandbox.cpp +++ b/sandbox/src/sandbox.cpp @@ -8,7 +8,9 @@ class Sandbox : public Bk::Application public: Sandbox() { - + auto event = Bk::WindowResizeEvent(120, 120); + auto key_event = Bk::KeyPressEvent(Bk::Key::A); + std::cout << event.to_string() << "\n" << key_event.to_string() << "\n"; } ~Sandbox()