LkEngine 0.1.2
 
Loading...
Searching...
No Matches
Event.h
1#pragma once
2
4
6#include "LkEngine/Core/LObject/ObjectPtr.h"
7
8namespace LkEngine {
9
15 enum class EEventType : uint32_t
16 {
17 None = 0,
18 WindowFocus,
19 WindowLostFocus,
20 WindowMoved,
21 WindowResize,
22 WindowClose,
23 KeyPressed,
24 KeyReleased,
25 MouseButtonPressed,
26 MouseButtonReleased,
27 MouseButtonHeld,
28 MouseMoved,
29 MouseScrolled,
30 COUNT
31 };
32 LK_ENUM_CLASS(EEventType);
33 LK_ENUM_RANGE_BY_COUNT(EEventType, EEventType::COUNT);
34
40 enum class EEventCategory : uint32_t
41 {
42 None = 0,
43 Input,
44 COUNT
45 };
46 LK_ENUM_CLASS(EEventCategory);
47 LK_ENUM_RANGE_BY_COUNT(EEventCategory, EEventCategory::COUNT);
48
49
55 #define LEVENT(EventName, EventType, ...) \
56 public: \
57 using StaticEventClass = EventName; \
58 static ::LkEngine::EEventType GetStaticType() { return EEventType::EventType; } \
59 virtual ::LkEngine::EEventType GetType() const override { return GetStaticType(); } \
60 virtual const char* GetName() const override { return #EventType; } \
61
70 class LEvent
71 {
72 public:
73 virtual ~LEvent() = default;
74
75 virtual EEventType GetType() const = 0;
76 virtual const char* GetName() const = 0;
77 virtual std::string ToString() const { return GetName(); }
78
79 FORCEINLINE bool IsHandled() const { return bHandled; }
80
81 public:
82 bool bHandled = false;
83 };
84
85 inline std::ostream& operator<<(std::ostream& OStream, const LEvent& InEvent)
86 {
87 return OStream << InEvent.ToString();
88 }
89
90 using FEventCallback = std::function<void(LEvent&)>;
91 using FEventHandler = std::function<void(LEvent&)>;
92
97 {
98 template<typename EventType>
99 using EventFn = std::function<bool(EventType&)>;
100 public:
101 LEventDispatcher(LEvent& InEvent)
102 : EventRef(InEvent)
103 {
104 }
105
106 template<typename T>
107 FORCEINLINE bool Dispatch(EventFn<T> InEventFunc)
108 {
109 if (!EventRef.bHandled)
110 {
111 EventRef.bHandled = InEventFunc(*(T*)&EventRef);
112 LK_CORE_TRACE_TAG("EventDispatcher", "Executing: '{}'", EventRef.GetName());
113 return true;
114 }
115 return false;
116 }
117
118 private:
119 LEvent& EventRef;
120 };
121
122}
Core header.
LObject implementation.
Definition Event.h:97
Definition Event.h:71
Definition Asset.h:11
EEventType
Definition Event.h:16
EEventCategory
Definition Event.h:41