-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleManager.hpp
More file actions
213 lines (173 loc) · 3.71 KB
/
ModuleManager.hpp
File metadata and controls
213 lines (173 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#ifndef MODULE_MANAGER_H
#define MODULE_MANAGER_H
#include <string>
#include <json.hpp>
#include "Global.hpp"
class Identifyable
{
protected:
int id;
bool changed = false;
public:
inline Identifyable(int id) : id{ id } {}
virtual ~Identifyable() = default;
inline const int& GetID() const
{
return id;
}
inline const bool& HasChanged() const
{
return changed;
}
inline void SetChanged()
{
changed = true;
}
inline void ResetChanged()
{
changed = false;
}
virtual void UpdateFromJson(const nlohmann::json&) = 0;
};
class ModuleManager;
struct Cycle
{
ModuleManager& modules;
nlohmann::json& changes;
float inputWidth;
int theme;
bool search = false;
bool highlightMatches;
bool hideNonMatches;
uint16_t enableUiHotkey;
int currentID = -1;
inline int NextID()
{
return ++currentID;
}
inline int CurrentId() const
{
return currentID;
}
};
class Module // abstract base
{
protected:
std::string name;
public:
bool enabled;
private:
ImVec4 changeBounds{ -1.0f, -1.0f, -1.0f, -1.0f };
public:
inline Module(std::string_view name) :
name{ name },
enabled{ true } {}
inline const std::string& GetName() const
{
return name;
}
inline void ChangeBounds(ImVec4 bounds)
{
changeBounds = bounds;
}
protected:
inline void UpdateBounds()
{
if (changeBounds.w != -1.0f)
{
ImGui::SetNextWindowPos(ImVec2{ changeBounds.x, changeBounds.y });
ImGui::SetNextWindowSize(ImVec2{ changeBounds.z, changeBounds.w });
changeBounds = { -1.0f, -1.0f, -1.0f, -1.0f };
}
}
public:
inline virtual bool Visible() { return true; }
inline virtual void Init(Cycle&) {};
inline virtual void RenderImGui(Cycle&) = 0;
inline virtual void Update(Cycle&) {}; // called after RenderImGui
inline virtual void UpdateSearch(std::string_view, bool) = 0;
inline virtual void ContextMenu(Identifyable&, Cycle&) {}
inline virtual void Visit(const std::function<void(Identifyable&)>&) = 0;
};
std::unique_ptr<Module> JsonToModule(const nlohmann::json&);
class ModuleManager
{
std::vector<std::unique_ptr<Module>> modules;
std::unordered_map<int, Identifyable*> idToIdent;
bool initCalled = false;
public:
inline decltype(auto) begin()
{
return modules.begin();
}
inline decltype(auto) end()
{
return modules.end();
}
inline decltype(auto) begin() const
{
return modules.begin();
}
inline decltype(auto) end() const
{
return modules.end();
}
inline decltype(auto) VisibleModules() const
{
return modules | std::views::filter([](const auto& m) { return m->Visible(); });
}
inline void Push(std::unique_ptr<Module>&& module)
{
modules.push_back(std::move(module));
}
inline Identifyable* GetIdent(int id) const
{
auto it = idToIdent.find(id);
if (it != idToIdent.end())
return it->second;
return nullptr;
}
inline void DoCycle(Cycle& cycle)
{
if (!initCalled)
{
for (const auto& m : modules)
m->Init(cycle);
initCalled = true;
}
for (const auto& m : modules)
if (m->enabled)
m->RenderImGui(cycle);
for (const auto& m : modules)
m->Update(cycle);
for (auto& [id, ident] : idToIdent)
ident->ResetChanged();
}
inline void UpdateFromJson(const nlohmann::json& json)
{
for (const auto& data : json["Changes"])
{
int id = data["ID"];
auto it = idToIdent.find(id);
if (it != idToIdent.end())
it->second->UpdateFromJson(data);
}
}
inline void LoadFromJson(const nlohmann::json& data)
{
for (const auto& m : data["Modules"])
{
modules.push_back(JsonToModule(m));
modules.back()->Visit([&](Identifyable& ident)
{
idToIdent.emplace(ident.GetID(), &ident);
});
}
}
inline void ContextMenu(Identifyable& ident, Cycle& cycle)
{
for (const auto& m : modules)
m->ContextMenu(ident, cycle);
}
};
#endif