forked from Green-Sky/imgui_entt_entity_editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimgui_entt_entity_editor.hpp
223 lines (183 loc) · 7.01 KB
/
imgui_entt_entity_editor.hpp
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
214
215
216
217
218
219
220
221
222
// for the license, see the end of the file
#pragma once
#include <set>
#include <map>
#include <entt/entt.hpp>
#include <imgui.h>
// if you have font awesome or something comparable you can set this to a wastebin
#ifndef ESS_IMGUI_ENTT_E_E_DELETE_COMP_STR
#define ESS_IMGUI_ENTT_E_E_DELETE_COMP_STR "-"
#endif
namespace MM {
template<typename Registry>
class ImGuiEntityEditor {
private:
using component_type = entt::component;
std::set<component_type> _component_types;
std::map<component_type, std::string> _component_names;
std::map<component_type, void(*)(Registry&, typename Registry::entity_type)> _component_widget;
std::map<component_type, void(*)(Registry&, typename Registry::entity_type)> _component_create;
std::map<component_type, void(*)(Registry&, typename Registry::entity_type)> _component_destroy;
public:
bool show_window = true;
private:
inline bool entity_has_component(Registry& ecs, typename Registry::entity_type& e, component_type ct) {
component_type type[] = { ct };
auto rv = ecs.runtime_view(std::cbegin(type), std::cend(type));
return rv.contains(e);
}
public:
// calls all the ImGui functions
// call this every frame
void renderImGui(Registry& ecs, typename Registry::entity_type& e) {
if (show_window) {
if(ImGui::Begin("Entity Editor", &show_window)) {
ImGui::TextUnformatted("editing:");
ImGui::SameLine();
//ImGuiWidgets::Entity(e, ecs, true);
if (ecs.valid(e)) {
ImGui::Text("id: %d, v: %d", ecs.entity(e), ecs.version(e));
} else {
ImGui::Text("INVALID ENTITY");
}
// TODO: investigate
if (ImGui::Button("New Entity")) {
e = ecs.create();
}
// TODO: implemnt cloning by ether forking entt or implementing function lists...
//ImGui::SameLine();
//ImGui::TextUnformatted(ICON_II_ARCHIVE " drop to clone Entity");
//if (ImGui::BeginDragDropTarget()) {
//if (auto* payload = ImGui::AcceptDragDropPayload(IMGUI_PAYLOAD_TYPE_MM_ENTITY)) {
//auto clone_e = *(MM::FrameworkConfig::Entity*)payload->Data;
//e = ecs.clone(clone_e);
//}
//ImGui::EndDragDropTarget();
//}
ImGui::Separator();
// TODO: needed?
if (!ecs.valid(e)) {
e = entt::null;
}
if (e != entt::null) {
std::vector<component_type> has_not;
for (auto ct : _component_types) {
if (entity_has_component(ecs, e, ct)) {
// delete component button
if (_component_destroy.count(ct)) {
std::string button_label = ESS_IMGUI_ENTT_E_E_DELETE_COMP_STR "##";
button_label += entt::to_integer(ct);
if (ImGui::Button(button_label.c_str())) {
_component_destroy[ct](ecs, e);
continue; // early out to prevent access to deleted data
} else {
ImGui::SameLine();
}
}
std::string label;
if (_component_names.count(ct)) {
label = _component_names[ct];
} else {
label = "unnamed component (";
label += entt::to_integer(ct);
label += ")";
}
if (ImGui::CollapsingHeader(label.c_str())) {
ImGui::Indent(30.f);
if (_component_widget.count(ct)) {
_component_widget[ct](ecs, e);
} else {
ImGui::TextDisabled("missing widget to display component!");
}
ImGui::Unindent(30.f);
}
} else {
has_not.push_back(ct);
}
}
if (!has_not.empty()) {
if (ImGui::Button("+ Add Component")) {
ImGui::OpenPopup("add component");
}
if (ImGui::BeginPopup("add component")) {
ImGui::TextUnformatted("available:");
ImGui::Separator();
for (auto ct : has_not) {
if (_component_create.count(ct)) {
std::string label;
if (_component_names.count(ct)) {
label = _component_names[ct];
} else {
label = "unnamed component (";
label += entt::to_integer(ct);
label += ")";
}
label += "##"; label += entt::to_integer(ct); // better but optional
if (ImGui::Selectable(label.c_str())) {
_component_create[ct](ecs, e);
}
}
}
ImGui::EndPopup();
}
}
}
}
ImGui::End();
}
}
// call this (or registerTrivial) before any of the other register functions
void registerComponentType(component_type ct) {
if (!_component_types.count(ct)) {
_component_types.emplace(ct);
}
}
// register a name to be displayed for the component
void registerComponentName(component_type ct, const std::string& name) {
_component_names[ct] = name;
}
// register a callback to a function displaying a component. using imgui
void registerComponentWidgetFn(component_type ct, void(*fn)(Registry&, typename Registry::entity_type)) {
_component_widget[ct] = fn;
}
// register a callback to create a component, if none, you wont be able to create it in the editor
void registerComponentCreateFn(component_type ct, void(*fn)(Registry&, typename Registry::entity_type)) {
_component_create[ct] = fn;
}
// register a callback to delete a component, if none, you wont be able to delete it in the editor
void registerComponentDestroyFn(component_type ct, void(*fn)(Registry&, typename Registry::entity_type)) {
_component_destroy[ct] = fn;
}
// registers the component_type, name, create and destroy for rather trivial types
template<typename T>
void registerTrivial(Registry& ecs, const std::string& name) {
registerComponentType(ecs.template type<T>());
registerComponentName(ecs.template type<T>(), name);
registerComponentCreateFn(ecs.template type<T>(),
[](Registry& ecs, typename Registry::entity_type e) {
ecs.template assign<T>(e);
});
registerComponentDestroyFn(ecs.template type<T>(),
[](Registry& ecs, typename Registry::entity_type e) {
ecs.template remove<T>(e);
});
}
};
} // MM
// MIT License
// Copyright (c) 2019 Erik Scholz
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.