-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDefaultInspector.cpp
More file actions
470 lines (442 loc) · 13.5 KB
/
Copy pathDefaultInspector.cpp
File metadata and controls
470 lines (442 loc) · 13.5 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "UI/DefaultInspector.h"
#include "UI/Inspector.h"
#include "AssetDatabase.h"
#include "DragDropHelper.hpp"
#include "Game/World.h"
#include "External/imgui/misc/cpp/imgui_stdlib.h"
#include "imgui.h"
#include <cstdint>
#include <algorithm>
namespace sh::editor
{
SH_EDITOR_API void GameObjectInspector::RenderUI(const std::vector<core::SObject*>& objs, int idx)
{
auto mainGameObjPtr = static_cast<game::GameObject*>(objs.back());
auto& gameObjs = reinterpret_cast<const std::vector<game::GameObject*>&>(objs);
if (!core::IsValid(mainGameObjPtr))
return;
bool bActive = mainGameObjPtr->activeSelf;
if (ImGui::Checkbox("##active", &bActive))
{
for (auto gameObjPtr : gameObjs)
if (gameObjPtr != nullptr)
gameObjPtr->SetActive(bActive);
}
ImGui::SameLine();
ImGui::Text("Active");
bool bEditorOnly = mainGameObjPtr->bEditorOnly;
if (ImGui::Checkbox("##editorOnly", &bEditorOnly))
{
for (auto gameObjPtr : gameObjs)
if (gameObjPtr != nullptr)
gameObjPtr->bEditorOnly = bEditorOnly;
}
ImGui::SameLine();
ImGui::Text("EditorOnly");
ImGui::Separator();
Inspector::RenderProperties(mainGameObjPtr->transform->GetType(), *mainGameObjPtr->transform, 0);
ImGui::Separator();
ImGui::Text("Components");
// 드래그 드랍으로 도중에 컴포넌트가 추가 되는 일이 발생한다.
// 그로인해 반복자가 깨지므로 컴포넌트 배열을 복사 해둬야 한다.
std::vector<game::Component*> components = mainGameObjPtr->GetComponents();
int componentsIdx = 1;
for (auto component : components)
{
if (!core::IsValid(component))
continue;
if (component->hideInspector)
continue;
const std::string componentName = component->GetType().name.ToString();
bool bOpenComponent = ImGui::CollapsingHeader((componentName.c_str() + ("##" + std::to_string(componentsIdx))).data());
if (ImGui::BeginPopupContextItem((component->GetUUID().ToString() + "RightClickPopup").c_str()))
{
if (component->GetType() != game::Transform::GetStaticType())
{
if (ImGui::Selectable("Delete"))
component->Destroy();
}
ImGui::EndPopup();
}
if (bOpenComponent && core::IsValid(component))
{
std::vector<core::SObject*> components{ component };
Inspector::RenderPropertiesCustomInspector(component->GetType(), components, componentsIdx);
}
++componentsIdx;
}//for auto& component
ImGui::Separator();
RenderAddComponent(gameObjs);
}
void GameObjectInspector::RenderAddComponent(const std::vector<game::GameObject*>& gameObjs)
{
if (ImGui::Button("Add Component", { -FLT_MIN, 0.0f }))
{
componentItems.clear();
auto& components = gameObjs.back()->world.componentModule.GetComponents();
for (auto& [fullname, _] : components)
{
auto [group, name] = GetComponentGroupAndName(fullname);
auto it = componentItems.find(group);
if (it == componentItems.end())
componentItems.insert({ group, std::vector<std::string>{std::move(name)} });
else
it->second.push_back(std::move(name));
}
bAddComponent = !bAddComponent;
}
if (bAddComponent)
{
ImGui::SetNextItemWidth(-FLT_MIN);
ImGui::BeginChild("ComponentsList", ImVec2(0, 200), ImGuiChildFlags_::ImGuiChildFlags_Border);
for (auto& [group, vector] : componentItems)
{
const char* groupName = group.c_str();
if (group.empty())
groupName = "Default";
if (ImGui::CollapsingHeader(groupName))
{
for (auto& name : vector)
{
if (ImGui::Selectable(name.c_str()))
{
std::string searchName = name;
if (!group.empty())
searchName = group + "/" + name;
for (auto objPtr : gameObjs)
if (objPtr != nullptr)
objPtr->AddComponent(objPtr->world.componentModule.GetComponents().at(searchName)->Create(*objPtr));
bAddComponent = false;
}
}
}
}
ImGui::EndChild();
if (ImGui::Button("Close"))
bAddComponent = false;
}
}
auto GameObjectInspector::GetComponentGroupAndName(std::string_view fullname) -> std::pair<std::string, std::string>
{
auto pos = fullname.find('/');
std::string group{}, name{};
if (pos == fullname.npos)
name = fullname;
else
{
group = fullname.substr(0, pos);
name = fullname.substr(pos + 1);
}
return { std::move(group), std::move(name) };
}
SH_EDITOR_API void MaterialInspector::RenderUI(const std::vector<core::SObject*>& objs, int idx)
{
ImGui::Separator();
render::Material* mat = reinterpret_cast<render::Material*>(objs.back());
const auto& mats = reinterpret_cast<const std::vector<render::Material*>&>(objs);
render::Shader* const shader = mat->GetShader();
bool bSameShaders = true;
bool bChanged = false;
for (auto matPtr : mats)
{
if (matPtr == nullptr)
continue;
if (matPtr->GetShader() != shader)
{
bSameShaders = false;
break;
}
}
{
float iconSize = 20;
float buttonWidth = ImGui::GetContentRegionAvail().x - iconSize;
std::string shaderName = "Empty";
if (core::IsValid(shader))
shaderName = shader->GetName().ToString();
ImGui::Text("Shader");
if (ImGui::Button(shaderName.c_str(), ImVec2{ buttonWidth, iconSize }))
{
}
if (ImGui::BeginDragDropTarget())
{
if (render::Shader* const shaderPtr = dragdrop::AcceptAsset<render::Shader>())
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetShader(shaderPtr);
bChanged = true;
}
ImGui::EndDragDropTarget();
}
}
if (core::IsValid(shader) && bSameShaders)
{
for (auto& [name, propInfo] : shader->GetProperties())
{
ImGui::Text(name.c_str());
if (propInfo.type == core::reflection::GetType<float>())
{
float parameter = mat->GetProperty<float>(name).value_or(0.f);
if (ImGui::InputFloat(("##input_" + name).c_str(), ¶meter))
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetProperty(name, parameter);
bChanged = true;
}
}
else if (propInfo.type == core::reflection::GetType<glm::vec2>())
{
glm::vec2 v = mat->GetProperty<glm::vec2>(name).value_or(glm::vec2{ 0.f, 0.f });
if (ImGui::InputFloat2(("##input_" + name).c_str(), &v[0]))
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetProperty(name, v);
bChanged = true;
}
}
else if (propInfo.type == core::reflection::GetType<glm::vec3>())
{
glm::vec3 v = mat->GetProperty<glm::vec3>(name).value_or(glm::vec3{ 0.f });
if (ImGui::InputFloat3(("##input_" + name).c_str(), &v[0]))
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetProperty(name, v);
bChanged = true;
}
}
else if (propInfo.type == core::reflection::GetType<glm::vec4>())
{
glm::vec4 v = mat->GetProperty<glm::vec4>(name).value_or(glm::vec4{ 0.f });
if (ImGui::InputFloat4(("##input_" + name).c_str(), &v[0]))
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetProperty(name, v);
bChanged = true;
}
}
else if (propInfo.type == core::reflection::GetType<render::Texture>())
{
float iconSize = 20;
float buttonWidth = ImGui::GetContentRegionAvail().x - iconSize;
auto texProp = mat->GetProperty<const render::Texture*>(name);
const render::Texture* const tex = texProp.value();
std::string texName = "Empty";
if (core::IsValid(tex))
texName = tex->GetName().ToString();
if (ImGui::Button(texName.c_str(), ImVec2{buttonWidth, iconSize}))
{
}
if (ImGui::BeginDragDropTarget())
{
if (render::Texture* texturePtr = dragdrop::AcceptAsset<render::Texture>())
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetProperty(name, texturePtr);
bChanged = true;
}
ImGui::EndDragDropTarget();
}
}
}
if (ImGui::TreeNode("Constant value"))
{
for (auto& lightingPass : shader->GetAllShaderPass())
{
for (render::ShaderPass& pass : lightingPass.passes)
{
const auto& data = mat->GetConstantData(pass);
for (auto& [name, info] : pass.GetConstants())
{
const int* dataPtr = reinterpret_cast<const int*>(data->data() + info.offset);
int dataValue = *dataPtr;
ImGui::Text("%s", name.c_str());
if (ImGui::InputInt(("##constant_" + name).c_str(), &dataValue, 0, 0))
{
for (auto matPtr : mats)
if (matPtr != nullptr)
matPtr->SetConstant(name, dataValue);
bChanged = true;
}
}
}
}
ImGui::TreePop();
}
if (bChanged)
{
for (auto matPtr : mats)
if (matPtr != nullptr)
AssetDatabase::GetInstance()->SetDirty(matPtr);
AssetDatabase::GetInstance()->SaveAllAssets();
}
}
ImGui::Separator();
}
TextureInspector::TextureInspector()
{
previewTex = core::SObject::Create<game::GUITexture>();
previewTex->SetName("preview");
core::GarbageCollection::GetInstance()->SetRootSet(previewTex);
}
TextureInspector::~TextureInspector()
{
}
SH_EDITOR_API void TextureInspector::RenderUI(const std::vector<core::SObject*>& objs, int idx)
{
using namespace render;
Texture* const texture = reinterpret_cast<Texture*>(objs.back());
auto& textures = reinterpret_cast<const std::vector<Texture*>&>(objs);
TextureFormat format = texture->GetTextureFormat();
bool bChanged = false;
std::string formatText = "";
switch (format)
{
case TextureFormat::RGB24:
formatText = "RGB24";
break;
case TextureFormat::RGBA32:
formatText = "RGBA32";
break;
case TextureFormat::SRGB24:
formatText = "SRGB24";
break;
case TextureFormat::SRGBA32:
formatText = "SRGBA32";
break;
case TextureFormat::R8:
formatText = "R8";
break;
}
ImGui::Text("format: %s", formatText.c_str());
ImGui::Text("width: %d", texture->GetWidth());
ImGui::Text("height: %d", texture->GetHeight());
ImGui::Text("mipLevel: %d", texture->GetMipLevel());
ImGui::Separator();
ImGui::Text("SRGB");
bool bSRGB = texture->IsSRGB();
if (ImGui::Checkbox("##SRGB", &bSRGB))
{
if (bSRGB)
{
if (format == TextureFormat::RGB24)
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->ChangeTextureFormat(TextureFormat::SRGB24);
}
else if(format == TextureFormat::RGBA32)
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->ChangeTextureFormat(TextureFormat::SRGBA32);
}
}
else
{
if (format == TextureFormat::SRGB24)
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->ChangeTextureFormat(TextureFormat::RGB24);
}
else if (format == TextureFormat::SRGBA32)
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->ChangeTextureFormat(TextureFormat::RGBA32);
}
}
bChanged = true;
}
bool bGenerateMipmap = texture->IsGenerateMipmap();
ImGui::Text("Generate mipmap");
if (ImGui::Checkbox("##mipmap", &bGenerateMipmap))
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->SetGenerateMipmap(bGenerateMipmap);
bChanged = true;
}
int anisoLevel = texture->GetAnisoLevel();
ImGui::Text("Aniso");
if (ImGui::InputInt("##Aniso", &anisoLevel))
{
anisoLevel = std::clamp(anisoLevel, 0, 12);
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->SetAnisoLevel(anisoLevel);
bChanged = true;
}
ImGui::Text("Filtering");
{
const char* filters[] = { "Box", "Linear" };
static int current = 0;
current = static_cast<int>(texture->GetFiltering());
if (ImGui::ListBox(fmt::format("##filtering{}", idx).c_str(), ¤t, filters, IM_ARRAYSIZE(filters), 4))
{
for (auto texPtr : textures)
if (texPtr != nullptr)
texPtr->SetFiltering(static_cast<render::Texture::Filtering>(current));
bChanged = true;
}
}
ImGui::Separator();
if (core::IsValid(texture) && lastTex != texture)
{
previewTex->Create(*texture);
lastTex = texture;
}
const float texAspect = (float)texture->GetWidth() / (float)texture->GetHeight();
const auto area = ImGui::GetContentRegionAvail();
if (area.x > 1.f && texAspect > 0.01f)
{
if (previewTex->IsValid())
previewTex->Draw(ImVec2{ area.x * 0.9f, (area.x * 0.9f) / texAspect });
}
if (bChanged)
{
for (auto texPtr : textures)
if (texPtr != nullptr)
AssetDatabase::GetInstance()->SetDirty(texPtr);
AssetDatabase::GetInstance()->SaveAllAssets();
}
}
SH_EDITOR_API void CameraInspector::RenderUI(const std::vector<core::SObject*>& objs, int idx)
{
game::Component* component = reinterpret_cast<game::Component*>(objs.back());
const core::reflection::STypeInfo* currentType = &component->GetType();
do
{
for (auto& prop : currentType->GetProperties())
{
if (prop->GetName() == core::Util::ConstexprHash("projection"))
{
const char* items[] = { "Perspective", "Orthographic" };
static int current = 0;
int* proj = prop->Get<int>(*component);
current = *proj;
ImGui::Text("Projection");
if (ImGui::ListBox(fmt::format("##Projection{}", idx).c_str(), ¤t, items, IM_ARRAYSIZE(items), 4))
{
*proj = current;
component->OnPropertyChanged(*prop);
}
continue;
}
Inspector::RenderProperty(*prop, *component, idx);
}
currentType = currentType->super;
}
while (currentType != nullptr);
}
SH_EDITOR_API void TextInspector::RenderUI(const std::vector<core::SObject*>& objs, int idx)
{
game::TextObject* textObj = reinterpret_cast<game::TextObject*>(objs.back());
ImGui::InputTextMultiline(fmt::format("##textBox{}", idx).c_str(), &textObj->text);
}
}//namespace