Started events abstraction and classes

dev
anulax1225 ago%!(EXTRA string=1 year)
parent f346726644
commit ee4df2592a
  1. 17
      bakara/src/bakara/events/app_event.h
  2. 24
      bakara/src/bakara/events/event.cpp
  3. 45
      bakara/src/bakara/events/event.h
  4. 59
      bakara/src/bakara/events/key_event.h
  5. 36
      bakara/src/bakara/events/window_event.h
  6. 4
      sandbox/src/sandbox.cpp

@ -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)
};
}

@ -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<T&>(p_event));
return true;
}
return false;
}
inline std::ostream& operator<<(std::ostream& os, Event& e)
{
return os << e.ToString();
}
}

@ -1,6 +1,8 @@
#pragma once
#include <bakara/math/base.h>
#include <bakara/core/base.h>
#include <bkpch.h>
#include <bakara/core/string_fmt.h>
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<typename T, typename F>
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<T&>(p_event));
return true;
}
return false;
}
private:
Event& p_event;
};

@ -0,0 +1,59 @@
#pragma once
#include <bakara/core/key_codes.h>
#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)
};
}

@ -1,30 +1,34 @@
pragma once
#pragma once
#include <bkpch.h>
#include <format>
#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)
};
}

@ -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()

Loading…
Cancel
Save