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
|
#include "EditorCore.hpp"
#include "App.hpp"
#include "AppConfig.hpp"
#include "CpuMesh.hpp"
#include "EditorAccessories.hpp"
#include "EditorAttachmentImpl.hpp"
#include "EditorNotification.hpp"
#include "EditorUtils.hpp"
#include "GameObjectTags.hpp"
#include "Level.hpp"
#include "Mesh.hpp"
#include "Player.hpp"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <ImGuizmo.h>
#include <imgui.h>
#include <misc/cpp/imgui_stdlib.h>
#include <functional>
#include <memory>
#include <utility>
namespace ProjectBrussel_UNITY_ID {
void PushKeyCodeRecorder(App* app, int* writeKey, bool* writeKeyStatus) {
app->PushKeyCaptureCallback([=](int key, int action) {
// Allow the user to cancel by pressing Esc
if (key == GLFW_KEY_ESCAPE) {
return true;
}
if (action == GLFW_PRESS) {
*writeKey = key;
*writeKeyStatus = writeKeyStatus;
return true;
}
return false;
});
}
void ShowShaderName(const Shader* shader) {
if (shader) {
auto& name = shader->GetName();
bool isAnnoymous = name.empty();
if (isAnnoymous) {
ImGui::Text("Shader <annoymous at %p>", (void*)shader);
} else {
ImGui::Text("Shader: %s", name.c_str());
}
} else {
ImGui::TextUnformatted("Shader: <null>");
}
}
void ShowMaterialName(const Material* material) {
if (material) {
auto& name = material->GetName();
bool isAnnoymous = name.empty();
if (isAnnoymous) {
ImGui::Text("Material: <annoymous at %p>", (void*)material);
} else {
ImGui::Text("Material: %s", name.c_str());
}
} else {
ImGui::TextUnformatted("Material: <null>");
}
}
} // namespace ProjectBrussel_UNITY_ID
EditorInstance::EditorInstance(App* app, GameWorld* world)
: mApp{ app }
, mWorld{ world }
, mEdContentBrowser(this) {}
EditorInstance::~EditorInstance() {
}
void EditorInstance::Show() {
if (!mWorld) return;
auto& io = ImGui::GetIO();
if (io.KeyCtrl && ImGui::IsKeyPressed(GLFW_KEY_SPACE, false)) {
mEdContentBrowserVisible = !mEdContentBrowserVisible;
}
ImGui::Begin("World properties");
ShowWorldProperties();
ImGui::End();
ImGui::Begin("World structure");
ShowGameObjectInTree(&mWorld->GetRoot());
ImGui::End();
ImGui::Begin("Inspector");
switch (mSelectedItt) {
case ITT_GameObject: ShowInspector(static_cast<GameObject*>(mSelectedItPtr)); break;
case ITT_Shader: ShowInspector(static_cast<Shader*>(mSelectedItPtr)); break;
case ITT_Material: ShowInspector(static_cast<Material*>(mSelectedItPtr)); break;
case ITT_None: break;
}
ImGui::End();
if (mEdContentBrowserVisible) {
mEdContentBrowser.Show(&mEdContentBrowserVisible);
}
}
void EditorInstance::SelectIt(void* ptr, InspectorTargetType itt) {
mSelectedItPtr = ptr;
mSelectedItt = itt;
}
void EditorInstance::ShowWorldProperties() {
}
// TOOD move resource-specific and gameobject-specific inspector code into attachments mechanism
void EditorInstance::ShowInspector(Shader* shader) {
using namespace Tags;
using namespace ProjectBrussel_UNITY_ID;
EaShader* attachment;
if (auto ea = shader->GetEditorAttachment()) {
attachment = static_cast<EaShader*>(ea);
} else {
attachment = new EaShader();
attachment->shader = shader;
shader->SetEditorAttachment(attachment);
}
auto info = shader->GetInfo();
if (!info) {
ImGui::TextUnformatted("No info present for this shader.");
if (ImGui::Button("Create empty info")) {
shader->CreateEmptyInfoIfAbsent();
}
if (ImGui::Button("Gather info")) {
shader->GatherInfoIfAbsent();
}
return;
}
auto& name = shader->GetName();
bool isAnnoymous = name.empty();
ShowShaderName(shader);
if (ImGui::Button("Reimport metadata", isAnnoymous)) {
info->LoadFromFile(shader->GetDesignatedMetadataPath());
}
ImGui::SameLine();
if (ImGui::Button("Export metadata", isAnnoymous)) {
info->SaveToFile(shader->GetDesignatedMetadataPath());
}
auto ShowThing = [&](const std::vector<ShaderInfo::InputOutputThing>& things) {
for (auto& thing : things) {
ImGui::BulletText("Location %d\nName: %s\nSemantic: %s\nType: %s %dx%d",
thing.variable.location,
thing.variable.name.c_str(),
Tags::NameOf(thing.semantic).data(),
Tags::NameOfGLType(thing.variable.scalarType).data(),
thing.variable.width,
thing.variable.height);
}
};
if (ImGui::CollapsingHeader("Inputs")) {
ShowThing(info->inputs);
}
if (ImGui::CollapsingHeader("Outputs")) {
ShowThing(info->outputs);
}
if (ImGui::CollapsingHeader("Uniforms")) {
}
if (ImGui::CollapsingHeader("Uniform blocks")) {
}
}
void EditorInstance::ShowInspector(Material* material) {
using namespace Tags;
using namespace ProjectBrussel_UNITY_ID;
EaMaterial* attachment;
if (auto ea = material->GetEditorAttachment()) {
attachment = static_cast<EaMaterial*>(ea);
} else {
attachment = new EaMaterial();
material->SetEditorAttachment(attachment);
}
auto& name = material->GetName();
bool isAnnoymous = name.empty();
auto shader = material->GetShader();
if (isAnnoymous) {
ImGui::Text("<Annoymous Material at %p>", (void*)(&material));
} else {
if (attachment->isEditingName) {
bool save = false;
save |= ImGui::InputText("##", &attachment->editingScratch, ImGuiInputTextFlags_EnterReturnsTrue);
ImGui::SameLine();
save |= ImGui::Button("Save");
if (save) {
bool success = MaterialManager::instance->RenameMaterial(material, attachment->editingScratch);
if (success) {
attachment->isEditingName = false;
}
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
attachment->isEditingName = false;
}
ImGui::Text("%s", attachment->editingScratch.c_str());
} else {
// NOTE: ReadOnly shouldn't write any data into the buffer
ImGui::InputText("##", material->mName.data(), name.size() + 1, ImGuiInputTextFlags_ReadOnly);
ImGui::SameLine();
if (ImGui::Button("Edit")) {
attachment->editingScratch = name; // Copy
attachment->isEditingName = true;
}
}
}
ShowShaderName(shader);
if (ImGui::BeginDragDropTarget()) {
if (auto payload = ImGui::AcceptDragDropPayload(BRUSSEL_DRAG_DROP_SHADER)) {
auto shader = *static_cast<Shader* const*>(payload->Data);
material->SetShader(shader);
}
ImGui::EndDragDropTarget();
}
ImGui::SameLine();
if (ImGui::Button("GoTo", shader == nullptr)) {
mSelectedItt = ITT_Shader;
mSelectedItPtr = shader;
}
if (ImGui::Button("Reload", isAnnoymous)) {
material->LoadFromFile(material->GetDesignatedPath());
}
ImGui::SameLine();
if (ImGui::Button("Save", isAnnoymous)) {
material->SaveToFile(material->GetDesignatedPath());
}
for (auto& field : material->mBoundScalars) {
// TODO
}
for (auto& field : material->mBoundVectors) {
// TODO
}
for (auto& field : material->mBoundMatrices) {
// TODO
}
for (auto& field : material->mBoundTextures) {
// TODO
}
}
void EditorInstance::ShowInspector(GameObject* object) {
using namespace Tags;
using namespace ProjectBrussel_UNITY_ID;
auto type = object->GetTypeTag();
switch (type) {
case Tags::GOT_Player: {
ShowGameObjecetFields(object);
ImGui::Separator();
auto player = static_cast<Player*>(object);
auto& kb = player->keybinds;
ImGui::Text("Player #%d", player->GetId());
if (ImGui::Button("Load config")) {
bool success = player->LoadFromFile();
if (success) {
ImGui::AddNotification(ImGuiToast(ImGuiToastType_Success, "Successfully loaded player config"));
}
}
ImGui::SameLine();
if (ImGui::Button("Save config")) {
bool success = player->SaveToFile();
if (success) {
ImGui::AddNotification(ImGuiToast(ImGuiToastType_Success, "Successfully saved player config"));
}
}
ImGui::Text("Move left (%s)", ImGui::GetKeyNameGlfw(kb.keyLeft));
ImGui::SameLine();
if (ImGui::Button("Change##Move left")) {
PushKeyCodeRecorder(mApp, &kb.keyLeft, &kb.pressedLeft);
}
ImGui::Text("Move right (%s)", ImGui::GetKeyNameGlfw(kb.keyRight));
ImGui::SameLine();
if (ImGui::Button("Change##Move right")) {
PushKeyCodeRecorder(mApp, &kb.keyRight, &kb.pressedRight);
}
ImGui::Text("Jump (%s)", ImGui::GetKeyNameGlfw(kb.keyJump));
ImGui::SameLine();
if (ImGui::Button("Change##Jump")) {
PushKeyCodeRecorder(mApp, &kb.keyJump, &kb.pressedJump);
}
ImGui::Text("Attack (%s)", ImGui::GetKeyNameGlfw(kb.keyAttack));
ImGui::SameLine();
if (ImGui::Button("Change##Attack")) {
PushKeyCodeRecorder(mApp, &kb.keyAttack, &kb.pressedAttack);
}
} break;
case Tags::GOT_LevelWrapper: {
ShowGameObjecetFields(object);
ImGui::Separator();
auto lwo = static_cast<LevelWrapperObject*>(object);
// TODO
} break;
default:
ShowGameObjecetFields(object);
break;
}
}
void EditorInstance::ShowGameObjecetFields(GameObject* object) {
auto pos = object->GetPos();
if (ImGui::InputFloat3("Position", &pos.x)) {
object->SetPos(pos);
}
auto quat = object->GetRotation();
if (ImGui::InputFloat4("Rotation", &quat.x)) {
object->SetRotation(quat);
}
}
void EditorInstance::ShowGameObjectInTree(GameObject* object) {
auto attachment = object->GetEditorAttachment();
if (!attachment) {
attachment = EaGameObject::Create(object).release();
object->SetEditorAttachment(attachment); // NOTE: takes ownership
}
ImGuiTreeNodeFlags flags = 0;
flags |= ImGuiTreeNodeFlags_DefaultOpen;
flags |= ImGuiTreeNodeFlags_OpenOnDoubleClick;
flags |= ImGuiTreeNodeFlags_OpenOnArrow;
flags |= ImGuiTreeNodeFlags_SpanAvailWidth;
if (mSelectedItPtr == object) {
flags |= ImGuiTreeNodeFlags_Selected;
}
if (ImGui::TreeNodeEx(attachment->name.c_str(), flags)) {
if (ImGui::IsItemClicked() && !ImGui::IsItemToggledOpen()) {
mSelectedItPtr = object;
mSelectedItt = ITT_GameObject;
}
for (auto& child : object->GetChildren()) {
ShowGameObjectInTree(child);
}
ImGui::TreePop();
}
}
|