CG-Project2
Loading...
Searching...
No Matches
Event.hpp
1#pragma once
2
3#include "Utils.hpp"
4
5#include <functional>
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10namespace ogl {
11 using EventType = std::string;
12
13 class Event {
14 public:
15 inline EventType getType() const { return this->m_name; }
16
17 Event() = delete;
18
19 Event(const EventType &name) :
20 m_name(std::move(name)) {}
21
22 ~Event() = default;
23
24 private:
25 EventType m_name{};
26 };
27
28 class EventManager {
29 public:
30 void post(const Event &event) const;
31
32 void subscribe(const Event &event, std::function<void()> &&func);
33
34 void cleanAll();
35
36 EventManager(EventManager &other) = delete;
37
38 void operator=(const EventManager &other) = delete;
39
40 inline static Shared<EventManager> instance() {
41 if (s_pointer == nullptr) {
42 Shared<EventManager> copy(new EventManager());
43 copy.swap(s_pointer);
44 }
45 return s_pointer;
46 }
47
48 ~EventManager() = default;
49
50 private:
51 EventManager() = default;
52
53 inline static Shared<EventManager> s_pointer = nullptr;
54
55 std::unordered_map<EventType, std::vector<std::function<void()>>> m_events{};
56 };
57
58 namespace event {
59 namespace window {
60 const Event WINDOW_CLOSE{"Window Close"};
61 const Event WINDOW_RESIZE{"Window Resize"};
62 const Event WINDOW_MINIMIZED{"Window Minimized"};
63 const Event WINDOW_MAXIMIZED{"Window Maximized"};
64 const Event WINDOW_FULLSCREEN{"Window Fullscreen"};
65 } // namespace window
66
67 namespace loop {
68 const Event LOOP_INPUT{"Loop Input"};
69 const Event LOOP_UPDATE{"Loop Update"};
70 const Event LOOP_BEGIN_RENDER{"Loop Begin Render"};
71 const Event LOOP_RENDER{"Loop Render"};
72 const Event LOOP_END_RENDER{"Loop End Render"};
73 const Event LOOP_RUN{"Loop Run"};
74 const Event LOOP_PAUSE{"Loop Pause"};
75 const Event LOOP_EXIT{"Loop Exit"};
76 } // namespace loop
77
78 namespace shader {
79 const Event SHADER_PROJECTION_CHANGED = Event("Shader Projection Changed");
80 const Event INIT_DEFAULT_SHADER = Event("Initialize Default Shader");
81 } // namespace shader
82
83 namespace render {
84 const Event PREPARE_INSTANCED = Event("Prepare Instanced Mesh");
85 }
86 } // namespace event
87} // namespace ogl
Definition Event.hpp:13