-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInspectorWindow.cpp
More file actions
319 lines (281 loc) · 11.8 KB
/
Copy pathInspectorWindow.cpp
File metadata and controls
319 lines (281 loc) · 11.8 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
//
// Created by Alex on 12/30/2020.
//
#include "InspectorWindow.hpp"
#include "EditorStats.hpp"
#include "../engine/application/ApplicationStats.hpp"
#include "../engine/core/Log.hpp"
#include "../engine/core/Property.hpp"
#include "../engine/core/GLMWrapper.hpp"
#include "../engine/core/components/CameraComponent.hpp"
#include "../engine/core/components/LuaScriptComponent.hpp"
#include "../engine/core/components/MeshComponent.hpp"
#include "../engine/core/components/SpriteComponent.hpp"
#include "../engine/core/components/BoxColliderComponent.hpp"
#include "../engine/core/components/PhysicsComponent.hpp"
#include "../engine/core/Action.hpp"
#include "../engine/core/AssetInventory.hpp"
#include "../engine/core/Serializer.hpp"
#include "../engine/core/SceneManager.hpp"
#include "payloads/ModelPayload.hpp"
#include "payloads/GameObjectPayload.hpp"
#include "payloads/GeneralPayload.hpp"
#include "../engine/core/Singleton.hpp"
#include "../engine/core/Texture.hpp"
#include "../engine/core/Content.hpp"
#include <regex>
using RPG::InspectorWindow;
struct InspectorWindow::Internal {
bool isOpened;
std::shared_ptr<RPG::GameObject> selectedGameObject;
Internal() : isOpened(true),
selectedGameObject(nullptr) {
BuildDefaultPropertyLayouts();
}
void Render(ImGuiID dockID) {
if (!isOpened) return;
ImGui::SetNextWindowDockID(dockID, ImGuiCond_FirstUseEver);
ImGui::Begin("Inspector", &isOpened);
if (selectedGameObject == nullptr) { ImGui::End(); return; }
std::string name = selectedGameObject->GetName();
if (ImGui::InputText("Name", &name)) {
selectedGameObject->SetName(name);
}
ImGui::TextDisabled(selectedGameObject->GetGuid().c_str());
ImGui::SameLine();
ImGui::Text("Guid");
if (ImGui::Button("Add Component")) {
ImGui::OpenPopup("Add Component");
}
ImGui::SameLine();
bool isDeleted = ImGui::Button("Delete GameObject");
//Render Current Components
{
std::string removedComponent = "";
for (auto component : selectedGameObject->GetComponents()) {
ImGui::Separator();
ImGui::BeginGroup();
std::string name = SplitName(component->Name()) + "##" + component->Guid();
if (ImGui::CollapsingHeader(name.c_str())) {
ImGui::TextDisabled(component->Guid().c_str());
ImGui::SameLine();
ImGui::Text("Guid");
if (component->Name() != "TransformComponent") {
if (ImGui::Button("Remove")) {
removedComponent = component->Guid();
}
}
//Render Component Internals
for (auto property : component->GetProperties()) {
RenderProperty(property);
}
}
ImGui::EndGroup();
}
if (removedComponent != "") {
selectedGameObject->RemoveComponent(removedComponent);
}
}
//Add Component
{
//TODO: This will need to be dynamic some how in the future for project to add c++ level components if they cant rely on lua everytime
if (ImGui::BeginPopup("Add Component")) {
ImGui::Text("Components");
ImGui::Separator();
if (ImGui::Selectable("Camera Component")) {
auto size = RPG::ApplicationStats::GetInstance().GetWindowSize();
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::CameraComponent>(RPG::CameraComponent(size.x, size.y, selectedGameObject->GetTransform())));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (CameraComponent) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
if (ImGui::Selectable("Lua Script Component")) {
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::LuaScriptComponent>(RPG::LuaScriptComponent("", selectedGameObject)));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (LuaScriptComponent) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
if (ImGui::Selectable("Mesh Component")) {
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::MeshComponent>(RPG::MeshComponent("", "")));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (MeshComponent) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
if (ImGui::Selectable("Sprite Component")) {
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::SpriteComponent>(RPG::SpriteComponent("")));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (SpriteComponent) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
if (ImGui::Selectable("Box Collider Component")) {
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::BoxColliderComponent>(RPG::BoxColliderComponent(glm::vec3{1, 1, 1}, false)));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (Box Collider) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
if (ImGui::Selectable("Physics Component")) {
std::shared_ptr<RPG::IComponent> component = selectedGameObject->AddComponent(std::make_unique<RPG::PhysicsComponent>(RPG::PhysicsComponent(selectedGameObject->GetTransform(), [this]() -> std::vector<std::shared_ptr<RPG::IComponent>> {
return selectedGameObject->GetLuaScripts();
})));
if (component == nullptr) {
RPG::Log("SceneMain", "Failed to add (Physics) component to GameObject");
} else {
RPG::Log("SceneMain", "Added (" + component->Name() + ") component to GameObject");
}
}
ImGui::EndPopup();
}
}
//Deleting GameObject and preforming cleanup
if (isDeleted) {
if (!selectedGameObject->HasParent()) {
//Was part of our main hierarchy
auto hierarchy = RPG::SceneManager::GetInstance().GetCurrentScene()->GetHierarchy();
hierarchy->Remove(selectedGameObject);
}
selectedGameObject->SetParent(selectedGameObject, nullptr); //Clear my parent relation ship; deleting children as well
selectedGameObject = nullptr;
//TODO: Currently if anyone has a reference to the pointer it doesnt get shut down right away
//Deleting a child for example with release at the end of the application and not right away
//If all children are removed then the main parent will be cleaned up at the end of application
}
ImGui::End();
}
void RenderProperty(std::shared_ptr<RPG::Property> property) {
//Perhaps have a list that we can add new types to that contain an Action<> this makes it easier to add new types in the future
for (auto const& [key, value] : RPG::Serializer::GetInstance().GetPropertyLayouts()) {
if (key == property->GetType()) {
value(property);
return;
}
}
RPG::Log("Inspector", "Unable to render property with type (" + property->GetType() + ")");
}
void BuildDefaultPropertyLayouts() {
RPG::Serializer::GetInstance().AddPropertyLayout({"std::string", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
std::string v = std::any_cast<std::string>(prop);
if (ImGui::InputText(property->GetEditorName().c_str(), &v)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"RPG::Resource::String", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
std::string v = std::any_cast<std::string>(prop);
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.75, 0.75, 0.75, 1));
std::string id = "##" + property->GetGuid();
ImGui::InputText(id.c_str(), &v, ImGuiInputTextFlags_ReadOnly);
ImGui::PopStyleColor();
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text(v.c_str());
ImGui::EndTooltip();
}
//Dropable Payloads
if (property->AllowsDragAndDrop()) {
//Drop - payload
if (ImGui::BeginDragDropTarget()) {
if (ImGui::AcceptDragDropPayload(property->DragAndDropTag().c_str())) {
RPG::GeneralPayload p = RPG::EditorStats::GetInstance().GetPayload();
RPG::Log("Payload", p.path);
property->SetProperty(p.path);
}
ImGui::EndDragDropTarget();
}
}
ImGui::SameLine();
ImGui::Text(property->GetName().c_str(), property->GetEditorName().c_str());
if (property->DragAndDropTag() == "Texture") {
std::string texturePath = std::any_cast<std::string>(property->GetProperty());
uint32_t id = RPG::Content::GetInstance().GetTextureID(texturePath);
if (id != -1) {
ImGui::Image((void *) (intptr_t) id, ImVec2{50,50}, ImVec2{0, 1}, ImVec2{1, 0}, ImVec4{1,1,1,1}, ImVec4(1,1,1,1));
}
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"int", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
int v = std::any_cast<int>(prop);
if (ImGui::DragInt(property->GetEditorName().c_str(), (int*)&v)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"float", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
float v = std::any_cast<float>(prop);
if (ImGui::DragFloat(property->GetEditorName().c_str(), (float*)&v, 0.05f)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"bool", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
bool v = std::any_cast<bool>(prop);
if (ImGui::Checkbox(property->GetEditorName().c_str(), (bool*)&v)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"glm::vec2", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
glm::vec2 v = std::any_cast<glm::vec2>(prop);
if (ImGui::DragFloat2(property->GetEditorName().c_str(), (float*)&v, 0.05f)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"glm::vec3", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
glm::vec3 v = std::any_cast<glm::vec3>(prop);
if (ImGui::DragFloat3(property->GetEditorName().c_str(), (float*)&v, 0.05f)) {
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"RPG::CameraType", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
RPG::CameraType v = std::any_cast<RPG::CameraType>(prop);
int num = static_cast<int>(v);
if (ImGui::Combo(property->GetEditorName().c_str(), &num, "Perspective\0Orthographic\0\0")) {
v = static_cast<RPG::CameraType>(num);
property->SetProperty(v);
}
}});
RPG::Serializer::GetInstance().AddPropertyLayout({"RPG::PhysicsShape", [](std::shared_ptr<RPG::Property> property) {
std::any prop = property->GetProperty();
RPG::PhysicsShape v = std::any_cast<RPG::PhysicsShape>(prop);
int num = static_cast<int>(v);
if (ImGui::Combo(property->GetEditorName().c_str(), &num, "Circle\0Capsule\0\0")) {
v = static_cast<RPG::PhysicsShape>(num);
property->SetProperty(v);
}
}});
}
std::string SplitName(std::string name) {
std::regex regex("(?=[^a-z])");
return std::regex_replace(name, regex, " ");
}
};
InspectorWindow::InspectorWindow() : internal(RPG::MakeInternalPointer<Internal>()) {}
void InspectorWindow::Render(ImGuiID dockID) {
internal->Render(dockID);
}
RPG::Action<>::Callback InspectorWindow::ToggleIsOpen() {
return [this] () {
internal->isOpened = !internal->isOpened;
};
}
RPG::Action<>::Func<bool> InspectorWindow::IsOpen() {
return [this] () -> bool {
return internal->isOpened;
};
}
void InspectorWindow::SetSelectedGameObject(std::shared_ptr<RPG::GameObject> gameObject) {
internal->selectedGameObject = gameObject;
}