Added event abstraction

dev
anulax1225 ago%!(EXTRA string=1 year)
parent a09f6e5a74
commit 452cc00e57
  1. 20
      bakara/src/bakara/events/app_event.h
  2. 24
      bakara/src/bakara/events/event.cpp
  3. 73
      bakara/src/bakara/events/event.h
  4. 0
      bakara/src/bakara/events/key_event.h
  5. 0
      bakara/src/bakara/events/mouse_event.h
  6. 30
      bakara/src/bakara/events/window_event.h
  7. 3
      bakara/src/bakara/math/base.h

@ -0,0 +1,20 @@
#pragma once
#include "event.h"
namespace Bk {
class AppTickEvent : public Event
{
}
class AppRenderEvent : public Event
{
}
class AppUpdateEvent : public Event
{
}
}

@ -0,0 +1,24 @@
#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();
}
}

@ -0,0 +1,73 @@
#pragma once
#include <bakara/math/base.h>
namespace Bk {
enum class EventType
{
None = 0,
AppTick,
AppUpdate,
AppRender,
WindowClose,
WindowResize,
WindowFocus,
WindowLostFocus,
WindowMove,
KeyPress,
KeyRelease,
KeyTyped,
MouseButtonPress,
MouseButtonRelease,
MouseMove,
MouseScroll
};
enum class EventCategory
{
None = 0,
AppEvent = BIT_SHIFT(0),
InputEvent = BIT_SHIFT(1),
KeyboardEvent = BIT_SHIFT(2),
MouseEvent = BIT_SHIFT(3),
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_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
class Event
{
public:
virtual ~Event() = default;
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(); }
bool IsInCategory(EventCategory category)
{
return GetCategoryFlags() & (int)category == GetCategoryFlags();
}
};
inline std::ostream& operator<<(std::ostream& os, Event& e);
class EventDispatcher
{
public:
EventDispatcher(Event& event);
// F will be deduced by the compiler
template<typename T, typename F>
bool Dispatch(const F& func);
private:
Event& p_event;
};
}

@ -0,0 +1,30 @@
pragma once
#include "event.h"
namespace Bk {
class WindowResizeEvent : public Event
{
}
class WindowCloseEvent : public Event
{
}
class WindowFocusEvent : public Event
{
}
class WindowLostFocusEvent : public Event
{
}
class WindowMoveEvent : public Event
{
}
}

@ -0,0 +1,3 @@
#pragma once
#define BIT_SHIFT(x) (1 << x)
Loading…
Cancel
Save