CG-Project2
Loading...
Searching...
No Matches
ImGui.hpp
1#pragma once
2
3#include "Layer.hpp"
4#include "Utils.hpp"
5#include "Window.hpp"
6
7#include <algorithm>
8#include <functional>
9#include <map>
10#include <string>
11#include <utility>
12#include <vector>
13
14namespace ogl {
15 const ImGuiConfigFlags DEFAULT_IMGUI_CONFIGS{ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_DockingEnable | ImGuiConfigFlags_ViewportsEnable};
16
17 class ImGuiPanel {
18 public:
19 inline unsigned short getPriority() const { return this->m_priority; }
20
21 inline void setPriority(const unsigned short &priority) {
22 // EVENT: ig_layer_priority_changed
23 this->m_priority = priority;
24 }
25
26 inline virtual void onAttach() {
27 if (this->m_attached)
28 return;
29 this->m_attached = true;
30 }
31
32 inline void setRenderFunc(std::function<void()> &&func) { this->m_renderFunc = std::move(func); }
33
34 inline virtual void onRender() { this->m_renderFunc(); }
35
36 ImGuiPanel(const std::string &name = "Panel", const unsigned short &priority = 0) :
37 m_name(std::move(name)), m_priority(priority) {}
38
39 virtual ~ImGuiPanel() = default;
40
41 private:
42 std::string m_name = "Panel";
43 unsigned short m_priority = 0;
44 bool m_attached = false;
45
46 std::function<void()> m_renderFunc = []() {};
47 };
48
49 class ImGuiManager : public Layer {
50 public:
51 ImGuiManager() = delete;
52
53 ImGuiManager(const std::string &layerName, Window *window, const ImGuiConfigFlags &flags = 0) :
54 Layer(std::move(layerName)), m_window(window), m_flags(flags) {}
55
56 ImGuiManager(Window *window, const ImGuiConfigFlags &flags) :
57 Layer("ImGui Manager"), m_flags(flags), m_window(window) {}
58
59 template <typename T, typename... Args>
60 inline Shared<T> addPanel(Args &&...args) {
61 if (!this->m_attached)
62 return nullptr;
63
64 Shared<T> elem = CreateShared<T>(std::forward<Args>(args)...);
65 elem->onAttach();
66
67 if (this->m_panels.find(elem->getPriority()) == this->m_panels.end()) {
68 this->m_panels.insert_or_assign(elem->getPriority(), std::vector<Shared<ImGuiPanel>>());
69 }
70
71 this->m_panels.at(elem->getPriority()).push_back(elem);
72 return elem;
73 }
74
75 template <typename T>
76 inline bool removePanel(const Shared<ImGuiPanel> &panel) {
77 auto prio = panel->getPriority();
78 auto elem = std::find(ALL(this->m_panels.at(prio)), panel);
79 if (elem != this->m_panels.at(prio).end()) {
80 this->m_panels.at(prio).erase(elem);
81 return true;
82 }
83 return false;
84 }
85
86 inline void addPanel(const Shared<ImGuiPanel> &panel) {
87 if (this->m_panels.find(panel->getPriority()) == this->m_panels.end()) {
88 this->m_panels.insert_or_assign(panel->getPriority(), std::vector<Shared<ImGuiPanel>>());
89 }
90
91 this->m_panels.at(panel->getPriority()).push_back(panel);
92 }
93
94 std::vector<Shared<ImGuiPanel>> getPanels(const unsigned short &priority) { return {this->m_panels.at(priority)}; }
95
96 virtual void onAttach() override;
97 virtual void onDetach() override;
98
99 virtual void onUpdate() override;
100 virtual void onRender() override;
101
102 virtual void begin() override;
103 virtual void end() override;
104
105 private:
106 ImGuiConfigFlags m_flags = DEFAULT_IMGUI_CONFIGS;
107 Window *m_window;
108
109 std::map<unsigned short, std::vector<Shared<ImGuiPanel>>> m_panels{};
110 };
111
112} // namespace ogl
Definition Window.hpp:36