Hana
Loading...
Searching...
No Matches
Event.h
1#pragma once
2
3#include "hnpch.h"
4#include "Hana/Core/Core.h"
5
6namespace Hana
7{
8 // Events in Hana are currently blocking, meaning when an event occurs it
9 // immediately gets dispatched and must be dealt with right then and there.
10 // For the future, a better strategy might be to buffer events in an event
11 // bus and process them during the "event" art of the update stage.
12
13 enum class EventType
14 {
15 None = 0,
16 WindowClose, WindowResize, WindowFocus, WindowLostFocus, WindowMoved,
17 AppTick, AppUpdate, AppRender,
18 KeyPressed, KeyReleased, KeyTyped,
19 MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
20 };
21
22 enum EventCategory
23 {
24 None = 0,
25 EventCategoryApplication = BIT(0),
26 EventCategoryInput = BIT(1),
27 EventCategoryKeyboard = BIT(2),
28 EventCategoryMouse = BIT(3),
29 EventCategoryMouseButton = BIT(4)
30 };
31
32#define EVENT_CLASS_TYPE(type) static EventType GetStaticType() { return EventType::type; }\
33 virtual EventType GetEventType() const override { return GetStaticType(); }\
34 virtual const char* GetName() const override { return #type; }
35
36#define EVENT_CLASS_CATEGORY(category) virtual int GetCategoryFlags() const override { return category; }
37
38 class Event
39 {
40 public:
41 bool Handled = false;
42
43 virtual EventType GetEventType() const = 0;
44 virtual const char* GetName() const = 0;
45 virtual int GetCategoryFlags() const = 0;
46 virtual std::string ToString() const { return GetName(); }
47
48 inline bool IsInCategory(EventCategory category)
49 {
50 return GetCategoryFlags() & category;
51 }
52 };
53
54 class EventDispatcher
55 {
56 public:
57 EventDispatcher(Event& event)
58 : m_Event(event)
59 {
60 }
61
62 template<typename T, typename F>
63 bool Dispatch(const F& func)
64 {
65 if (m_Event.GetEventType() == T::GetStaticType())
66 {
67 m_Event.Handled = func(static_cast<T&>(m_Event));
68 return true;
69 }
70 return false;
71 }
72 private:
73 Event& m_Event;
74 };
75
76 inline std::ostream& operator<<(std::ostream& os, const Event& e)
77 {
78 return os << e.ToString();
79 }
80}
Definition Event.h:39