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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
|
#include "editor.hpp"
#include "thread_pool.hpp"
#include "game.hpp"
#include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
#include "math-helpers.hpp"
#include "camera.hpp"
#include "entities.hpp"
#include "game-settings.hpp"
#include "player-input.hpp"
#include "window.hpp"
#include "vendor/glm_include.hpp"
#include "vendor/tinyfiledialogs//tinyfiledialogs.h"
#include "imgui.h"
#include <future>
const char *dbgCtrl_CameraLeft = "debug-camera-left";
const char *dbgCtrl_CameraRight = "debug-camera-right";
const char *dbgCtrl_CameraForward = "debug-camera-forward";
const char *dbgCtrl_CameraBack = "debug-camera-back";
const char *dbgCtrl_CameraUp = "debug-camera-up";
const char *dbgCtrl_CameraDown = "debug-camera-down";
const char *dbgCtrl_CameraRotCC = "debug-camera-rot-counter-clockwise";
const char *dbgCtrl_CameraRotC = "debug-camera-rot-clockwise";
const char *dbgCtrl_CameraRot = "debug-camera-rot";
const char *dbgCtrl_UnlockCamera = "debug-camera-unlock";
const char *dbgCtrl_SelectHovered = "debug-select-hovered";
const char *dbgCtrl_ClearSelection = "debug-clear-selection";
ThreadPoolHandle threadPoolHandle = ThreadPoolHandle_MAX;
PkeCamera cameraDefault {
.pos = glm::vec3(-40.f, -40.f, -40.f),
.rot = glm::quat(1.f, 0.f, 0.f, 0.f),
.target = glm::vec3(0.f),
.type = PKE_CAMERA_TYPE_PERSPECTIVE,
.orientation = PKE_CAMERA_ORIENTATION_TARGET,
.stale = PKE_CAMERA_STALE_ALL,
};
PkeCamera cameraDbg {
.pos = glm::vec3(4.f, 4.f, 4.f),
.rot = glm::quat(1.f, 0.f, 0.f, 0.f),
.target = glm::vec3(0.f),
.type = PKE_CAMERA_TYPE_PERSPECTIVE,
.orientation = PKE_CAMERA_ORIENTATION_FREE,
.stale = PKE_CAMERA_STALE_ALL,
};
InputActionSetHandle debugControlsHandle = InputActionSetHandle_MAX;
bool shouldSetupEditor = true;
bool shouldDisableEditor = false;
bool shouldLockCamera = false;
bool shouldUnlockCamera = false;
struct EntityTypeInstanceCreateInfo {
EntityHandle entityTypeEntityHandle;
};
DynArray<EntityTypeInstanceCreateInfo> entityInstancesToCreate{16};
EntityHandle selectedEntity = EntityHandle_MAX;
EntityHandle hoveredEntity = EntityHandle_MAX;
bool shouldCreateEntityType = false;
EntityType entityTypeToCreate{};
char *sceneName = nullptr;
bool shouldOpenLoadSceneDialog = false;
bool shouldOpenSaveSceneDialog = false;
bool shouldLoadScene = false;
bool shouldSaveScene = false;
glm::vec3 unproject(glm::vec3 windowCoords) {
double xDevNorm = (2.0f * windowCoords.x) / Extent.width - 1.0f;
double yDevNorm = 1.0f - (2.0f * windowCoords.y) / Extent.height;
yDevNorm *= -1;
glm::vec4 clipCoords(xDevNorm, yDevNorm, windowCoords.z, 1.f);
glm::vec4 rayEye = glm::inverse(UBO.proj) * clipCoords;
glm::vec4 worldCoords = glm::inverse(UBO.model) * glm::inverse(UBO.view) * rayEye;
if (std::abs(0.0f - worldCoords.w) > 0.00001f) {
worldCoords *= 1.0f / worldCoords.w;
}
return glm::vec3(worldCoords);
}
void PkeEditor_Tick(double delta) {
if (shouldSetupEditor) {
PkeEditor_Setup();
}
if (shouldDisableEditor) {
PkeEditor_Teardown();
}
if (shouldOpenLoadSceneDialog) {
shouldOpenLoadSceneDialog = false;
PkeThreads_Enqueue(threadPoolHandle, std::move(std::packaged_task<void()>( [] {
const char * patterns[1] = {"*.pstf"};
char *selectedScene = tinyfd_openFileDialog(nullptr, nullptr, 1, patterns, "Pke Scene Text File", 0);
if (selectedScene != nullptr) {
sceneName = selectedScene;
shouldLoadScene = true;
}
})));
}
if (shouldLoadScene && sceneName) {
shouldLoadScene = false;
Game_LoadSceneFile(sceneName);
}
if (shouldUnlockCamera) {
shouldUnlockCamera = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
cameraDbg.pos = ActiveCamera->pos;
if (ActiveCamera->orientation == cameraDbg.orientation) {
cameraDbg.rot = ActiveCamera->rot;
} else {
cameraDbg.rot = glm::quat(UBO.view);
}
cameraDbg.stale = PKE_CAMERA_STALE_ALL;
ActiveCamera = &cameraDbg;
}
if (EntitiesToBeRemoved.Has(selectedEntity)) {
selectedEntity = EntityHandle_MAX;
}
if (EntitiesToBeRemoved.Has(hoveredEntity)) {
hoveredEntity = EntityHandle_MAX;
}
bool imGuiHovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_AnyWindow);
if (selectedEntity != EntityHandle_MAX && !imGuiHovered) {
auto holder = PkeInput_Query(dbgCtrl_ClearSelection);
if (holder.type != InputEventHash{0}) {
const PkeMouseButtonEvent *mbEvent = static_cast<PkeMouseButtonEvent *>(holder.ptr);
if (mbEvent->isPressed) {
selectedEntity = EntityHandle_MAX;
}
}
}
// raycast for hovering
hoveredEntity = EntityHandle_MAX;
if (!imGuiHovered) {
double xMousePos, yMousePos;
if (pkeSettings.editorSettings.isUsingDebugCamera) {
xMousePos = Extent.width / 2.0;
yMousePos = Extent.height / 2.0;
} else {
glfwGetCursorPos(window, &xMousePos, &yMousePos);
}
glm::vec3 fromCoords{unproject(glm::vec3(xMousePos, yMousePos, -1.f))};
glm::vec3 toCoords{unproject(glm::vec3(xMousePos, yMousePos, 1.f))};
btVector3 rayOrigin{};
btVector3 rayDestination{};
GlmToBullet(fromCoords, rayOrigin);
GlmToBullet(toCoords, rayDestination);
btCollisionWorld::ClosestRayResultCallback rayResult{rayOrigin, rayDestination};
rayResult.m_flags |= btTriangleRaycastCallback::kF_FilterBackfaces;
BtDynamicsWorld->rayTest(rayOrigin, rayDestination, rayResult);
if (rayResult.hasHit()) {
EntityHandle_T hoveredEntity_T{reinterpret_cast<EntityHandle_T>(rayResult.m_collisionObject->getUserPointer())};
hoveredEntity = EntityHandle{hoveredEntity_T};
}
}
if (hoveredEntity != EntityHandle_MAX) {
auto holder = PkeInput_Query(dbgCtrl_SelectHovered);
if (holder.type != InputEventHash{0}) {
const PkeMouseButtonEvent *mbEvent = static_cast<PkeMouseButtonEvent *>(holder.ptr);
if (mbEvent->isPressed) {
selectedEntity = hoveredEntity;
}
}
}
EntityHandle focusedHandle = selectedEntity != EntityHandle_MAX ? selectedEntity : hoveredEntity;
if (focusedHandle != EntityHandle_MAX) {
const auto *inst = ECS_GetInstance(focusedHandle);
const auto *grBinds = ECS_GetGrBinds(inst->grBindsHandle);
pkeDebugHitbox.instanceBuffer = grBinds->instanceBuffer;
pkeDebugHitbox.instanceStartingIndex = inst->index;
} else {
pkeDebugHitbox.instanceBuffer = VK_NULL_HANDLE;
pkeDebugHitbox.instanceStartingIndex = 0;
}
if (shouldCreateEntityType) {
assert(entityTypeToCreate.entityHandle == EntityHandle_MAX);
// TODO this needs to be more elegant
int64_t existingEntityTypeIndex = EntityType_FindByTypeCode(entityTypeToCreate.entityTypeCode);
if (existingEntityTypeIndex == -1) {
entityTypeToCreate.entityHandle = ECS_CreateEntity();
EntityType_Load(entityTypeToCreate);
GlobalEntityTypes.Push(entityTypeToCreate);
}
entityTypeToCreate = EntityType{};
shouldCreateEntityType = false;
}
while (entityInstancesToCreate.Count() > 0) {
auto createInfo = entityInstancesToCreate.Pop();
// TODO needs to be more elegant
int64_t etIndex = EntityType_FindByEntityHandle(createInfo.entityTypeEntityHandle);
auto &et = GlobalEntityTypes[etIndex];
EntityHandle newEntity = ECS_CreateEntity();
auto &compInst = ECS_CreateInstance(newEntity, createInfo.entityTypeEntityHandle);
compInst.physicsLayer = et.bt.startingCollisionLayer;
compInst.physicsMask = et.bt.startingCollisionMask;
btVector3 localInertia(0, 0, 0);
et.bt.shape->calculateLocalInertia(et.bt.startingMass, localInertia);
btTransform posRot{};
posRot.setIdentity();
compInst.bt.motionState = Pke_New<btDefaultMotionState>(MemBkt_Bullet);
new (compInst.bt.motionState) btDefaultMotionState(posRot);
compInst.bt.rigidBody = Pke_New<btRigidBody>(MemBkt_Bullet);
new (compInst.bt.rigidBody) btRigidBody(et.bt.startingMass, compInst.bt.motionState, et.bt.shape, localInertia);
compInst.bt.rigidBody->setLinearVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->setAngularVelocity(btVector3(0,0,0));
compInst.bt.rigidBody->getCollisionShape()->setLocalScaling(btVector3(1, 1, 1));
BtDynamicsWorld->addRigidBody(compInst.bt.rigidBody);
compInst.bt.rigidBody->getBroadphaseProxy()->m_collisionFilterGroup = static_cast<PhysicsCollision_T>(et.bt.startingCollisionLayer);
compInst.bt.rigidBody->getBroadphaseProxy()->m_collisionFilterMask = static_cast<PhysicsCollision_T>(et.bt.startingCollisionMask);
compInst.bt.rigidBody->setUserPointer(reinterpret_cast<void *>(compInst.entHandle));
}
PkeInputEventHolder holder = PkeInput_Query(dbgCtrl_UnlockCamera);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyEsc;
keyEsc = static_cast<PkeKeyEvent *>(holder.ptr);
if (keyEsc->isPressed || keyEsc->pressCount > 0) {
pkeSettings.editorSettings.isUsingDebugCamera = false;
shouldLockCamera = true;
}
}
if (shouldLockCamera) {
shouldLockCamera = false;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
cameraDefault.stale = PKE_CAMERA_STALE_ALL;
ActiveCamera = &cameraDefault;
}
if (pkeSettings.editorSettings.isUsingDebugCamera) {
holder = PkeInput_Query(dbgCtrl_CameraRot);
if (holder.type != InputEventHash{0}) {
const PkeCursorPosEvent *posEvent = static_cast<PkeCursorPosEvent *>(holder.ptr);
if (posEvent->xMotion || posEvent->yMotion) {
glm::vec3 axis1Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(1.f, 0.f, 0.f);
glm::vec3 axis2Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, 1.f, 0.f);
glm::quat pitch = glm::angleAxis(float(posEvent->yMotion) * 0.01f, glm::normalize(axis1Heading));
glm::quat yaw = glm::angleAxis(float(posEvent->xMotion) * 0.01f, glm::normalize(axis2Heading));
glm::quat rot = cameraDbg.rot * pitch * yaw;
auto eul = glm::eulerAngles(rot);
cameraDbg.rot = glm::quat(eul);
cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_ROT;
}
}
double leftCount = 0;
double rightCount = 0;
double forwardCount = 0;
double backCount = 0;
double upCount = 0;
double downCount = 0;
double rotCCCount = 0;
double rotCCount = 0;
holder = PkeInput_Query(dbgCtrl_CameraLeft);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyLeft;
keyLeft = static_cast<PkeKeyEvent *>(holder.ptr);
leftCount = keyLeft->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraRight);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyRight;
keyRight = static_cast<PkeKeyEvent *>(holder.ptr);
rightCount = keyRight->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraForward);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyForward;
keyForward = static_cast<PkeKeyEvent *>(holder.ptr);
forwardCount = keyForward->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraBack);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyBack;
keyBack = static_cast<PkeKeyEvent *>(holder.ptr);
backCount = keyBack->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraUp);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyUp;
keyUp = static_cast<PkeKeyEvent *>(holder.ptr);
upCount = keyUp->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraDown);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyDown;
keyDown = static_cast<PkeKeyEvent *>(holder.ptr);
downCount = keyDown->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraRotCC);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyRotCC;
keyRotCC = static_cast<PkeKeyEvent *>(holder.ptr);
rotCCCount = keyRotCC->isPressed ? 1 : 0;
}
holder = PkeInput_Query(dbgCtrl_CameraRotC);
if (holder.type != InputEventHash{0}) {
PkeKeyEvent *keyRotC;
keyRotC = static_cast<PkeKeyEvent *>(holder.ptr);
rotCCount = keyRotC->isPressed ? 1 : 0;
}
double accelerated = delta * 10.f;
double axis1 = -(leftCount * accelerated) + (rightCount * accelerated);
double axis2 = (forwardCount * accelerated) + -(backCount * accelerated);
double axis3 = -(upCount * accelerated) + (downCount * accelerated);
if (axis1 != 0 || axis2 != 0 || axis3 != 0) {
glm::vec3 axis1Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(-axis1, 0.f, 0.f);
glm::vec3 axis2Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, 0.f, axis2);
glm::vec3 axis3Heading = glm::conjugate(cameraDbg.rot) * glm::vec3(0.f, axis3, 0.f);
cameraDbg.pos += glm::vec3(axis1Heading + axis2Heading + axis3Heading);
cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_POS;
}
double axis4 = -(rotCCCount * delta) + (rotCCount * delta);
if (axis4 != 0.0) {
cameraDbg.rot = glm::quat(glm::vec3(0.f, 0.f, axis4)) * cameraDbg.rot;
cameraDbg.stale = cameraDbg.stale | PKE_CAMERA_STALE_ROT;
}
}
}
void RecordImGui_GLM(const char *label, glm::mat4 &mat) {
float min = -1;
float max = 1;
for (long i = 0; i < 4; ++i) {
ImGui::PushID(i);
ImGui::SliderFloat4(label, &mat[i][0], min, max, "%.3f", ImGuiSliderFlags_NoRoundToFormat);
ImGui::PopID();
}
}
void RecordImGuiEditorWrapper() {
ImGui::DockSpaceOverViewport(nullptr, ImGuiDockNodeFlags_PassthruCentralNode);
ImGui::BeginMainMenuBar();
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Exit")) {
glfwSetWindowShouldClose(window, true);
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Debug")) {
ImGui::Checkbox("Show Debug Hitboxes", &pkeSettings.isRenderingDebug);
ImGui::BeginDisabled();
ImGui::Checkbox("Pause Physics Simulation", &pkeSettings.isSimulationPaused);
ImGui::EndDisabled();
if (ImGui::Checkbox("Uncap Framerate", &pkeSettings.graphicsSettings.isFramerateUnlocked)) {
shouldRecreateSwapchain = true;
}
if (ImGui::Checkbox("wait for v-sync", &pkeSettings.graphicsSettings.isWaitingForVsync)) {
shouldRecreateSwapchain = true;
}
// ImGui::Checkbox("Uncap Tickrate", &pkeSettings.isTickrateUnlocked);
if (ImGui::Checkbox("Use Debug Camera", &pkeSettings.editorSettings.isUsingDebugCamera)) {
if (pkeSettings.editorSettings.isUsingDebugCamera) {
shouldUnlockCamera = true;
ImGui::CloseCurrentPopup();
} else {
shouldLockCamera = true;
}
}
ImGuiIO &io = ImGui::GetIO();
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
void RecordImGuiEntityList() {
if (!ImGui::Begin("EntityList", &pkeSettings.editorSettings.isShowingEntityList)) {
ImGui::End();
return;
}
static ImGuiTableFlags tableFlags{
ImGuiTableFlags_Borders |
ImGuiTableFlags_RowBg
};
if (ImGui::BeginTable("Entities", 7, tableFlags)) {
ImGui::TableSetupColumn("Select");
ImGui::TableSetupColumn("EntityHandle");
ImGui::TableSetupColumn("ParentEntityHandle");
ImGui::TableSetupColumn("GrBindsHandle");
ImGui::TableSetupColumn("InstanceHandle");
ImGui::TableSetupColumn("IsMarkedForRemoval");
ImGui::TableSetupColumn("Delete");
ImGui::TableHeadersRow();
uint64_t bucketCount = ECS_GetEntities_BucketCount();
for (long bucket = 0; bucket < bucketCount; ++bucket) {
uint64_t itemCount = 0;
auto *entities = ECS_GetEntities(bucket, itemCount);
ImGui::PushID(bucket);
for (long row = 0; row < itemCount; row++) {
auto *entity = &entities[row];
ImGui::PushID(row);
ImGui::TableNextRow();
ImGui::TableSetColumnIndex(0);
ImGui::BeginDisabled(selectedEntity == entity->handle);
if (ImGui::Button("Select"))
selectedEntity = entity->handle;
ImGui::EndDisabled();
ImGui::TableSetColumnIndex(1);
ImGui::Text("0x%016lX", static_cast<EntityHandle_T>(entity->handle));
ImGui::TableSetColumnIndex(2);
ImGui::Text("0x%016lX", static_cast<EntityHandle_T>(entity->parentHandle));
ImGui::TableSetColumnIndex(3);
ImGui::Text("0x%016lX", static_cast<GrBindsHandle_T>(entity->grBindsHandle));
ImGui::TableSetColumnIndex(4);
ImGui::Text("0x%016lX", static_cast<InstanceHandle_T>(entity->instanceHandle));
ImGui::TableSetColumnIndex(5);
ImGui::Text("%u", entity->isMarkedForRemoval);
ImGui::TableSetColumnIndex(6);
ImGui::BeginDisabled(selectedEntity != entity->handle);
if (ImGui::Button("Delete"))
ECS_MarkForRemoval(entity->handle);
ImGui::EndDisabled();
ImGui::PopID();
}
ImGui::PopID();
}
ImGui::EndTable();
}
ImGui::End();
}
void RecordImGuiUBO() {
if (!ImGui::Begin("UBO", &pkeSettings.editorSettings.isShowingUBO)) {
ImGui::End();
return;
}
ImGui::Text("Model");
RecordImGui_GLM("UBO-model", UBO.model);
ImGui::Text("View");
RecordImGui_GLM("UBO-view", UBO.view);
ImGui::Text("Proj");
RecordImGui_GLM("UBO-proj", UBO.proj);
ImGui::End();
}
void RecordImGuiModalCreateEntityType() {
if (ImGui::BeginPopupModal("CreateEntityType", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
static char modelsDir[64];
static char modelFile[64];
static char entityTypeCode[32];
ImGui::InputText("Models Dir", modelsDir, 63);
ImGui::InputText("Model File", modelFile, 63);
ImGui::InputText("Entity Type Code", entityTypeCode, 31);
ImGui::InputScalar("Starting Instance Count", ImGuiDataType_U32, &entityTypeToCreate.startingInstanceCount);
ImGui::InputScalar("GLTF Import Index - Vertex", ImGuiDataType_S16, &entityTypeToCreate.Importer_GLTF.AccessorIndexVertex);
ImGui::InputScalar("GLTF Import Index - Normal", ImGuiDataType_S16, &entityTypeToCreate.Importer_GLTF.AccessorIndexNormal);
ImGui::InputScalar("GLTF Import Index - UV", ImGuiDataType_S16, &entityTypeToCreate.Importer_GLTF.AccessorIndexUV);
ImGui::InputScalar("GLTF Import Index - Index", ImGuiDataType_S16, &entityTypeToCreate.Importer_GLTF.AccessorIndexIndex);
ImGui::InputFloat("Physics - Mass", &entityTypeToCreate.bt.startingMass);
ImGui::InputScalar("Physics - Collision Layer", ImGuiDataType_U16, &entityTypeToCreate.bt.startingCollisionLayer);
ImGui::InputScalar("Physics - Collision Mask", ImGuiDataType_U16, &entityTypeToCreate.bt.startingCollisionMask);
ImGui::Separator();
if (ImGui::Button("Create")) {
// TODO some type of validation
memset(modelsDir, '\0', 64);
memset(modelFile, '\0', 64);
memset(entityTypeCode, '\0', 32);
// assert(entityTypeToCreate == nullptr || entityTypeToCreate == CAFE_BABE(EntityType));
// entityTypeToCreate = Pke_New<EntityType>();
char *sModelsDir = Pke_New<char>(strlen(modelsDir) + 1);
strncpy(sModelsDir, modelsDir, 63);
entityTypeToCreate.modelsDir = sModelsDir;
char *sModelFile = Pke_New<char>(strlen(modelFile) + 1);
strncpy(sModelFile, modelFile, 63);
entityTypeToCreate.modelFile = sModelFile;
char *sEntityTypeCode = Pke_New<char>(strlen(entityTypeCode) + 1);
strncpy(sEntityTypeCode, entityTypeCode, 31);
entityTypeToCreate.entityTypeCode = sEntityTypeCode;
shouldCreateEntityType = true;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
}
void RecordImGui_CompGrBinds(bool readonly, CompGrBinds *component) {
if (component == nullptr) return;
int inputTextFlags = 0;
if (readonly)
inputTextFlags |= ImGuiInputTextFlags_ReadOnly;
ImGui::Text("CompGRBinds");
ImGui::Separator();
if (ImGui::Button("Create Instance")) {
entityInstancesToCreate.Push({
.entityTypeEntityHandle = component->entHandle,
});
}
if (component->vkPipelineLayout)
ImGui::InputScalar("VkPipelineLayout", ImGuiDataType_U64, &component->vkPipelineLayout, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
if (component->vkDescriptorSets){
ImGui::InputScalarN("VkPipelineDescriptorSets", ImGuiDataType_U64, &component->vkDescriptorSets, MAX_FRAMES_IN_FLIGHT, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
}
if (component->vertexBuffer)
ImGui::InputScalar("VkVertexBuffer", ImGuiDataType_U64, &component->vertexBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("VertexFirstBinding", ImGuiDataType_U32, &component->vertexFirstBinding, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("VertexBindingCount", ImGuiDataType_U32, &component->vertexBindingCount, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("VertexOffsets", ImGuiDataType_U64, &component->vertexOffsets, nullptr, nullptr, nullptr, inputTextFlags);
if (component->normalsBuffer)
ImGui::InputScalar("VkNormalBuffer", ImGuiDataType_U64, &component->normalsBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("NormalFirstBinding", ImGuiDataType_U32, &component->normalsFirstBinding, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("NormalBindingCount", ImGuiDataType_U32, &component->normalsBindingCount, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("NormalOffsets", ImGuiDataType_U64, &component->normalsOffsets, nullptr, nullptr, nullptr, inputTextFlags);
if (component->uvBuffer)
ImGui::InputScalar("VkUVBuffer", ImGuiDataType_U64, &component->uvBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("UVFirstBinding", ImGuiDataType_U32, &component->uvFirstBinding, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("UVBindingCount", ImGuiDataType_U32, &component->uvBindingCount, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("UVOffsets", ImGuiDataType_U64, &component->uvOffsets, nullptr, nullptr, nullptr, inputTextFlags);
if (component->indexBuffer)
ImGui::InputScalar("VkIndexBuffer", ImGuiDataType_U64, &component->indexBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("IndexBindingCount", ImGuiDataType_U32, &component->indexBindingCount, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("IndexOffsets", ImGuiDataType_U64, &component->indexOffsets, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("IndexCount", ImGuiDataType_U32, &component->indexCount, nullptr, nullptr, nullptr, inputTextFlags);
if (component->instanceBuffer)
ImGui::InputScalar("VkInstanceBuffer", ImGuiDataType_U64, &component->instanceBuffer, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("InstanceFirstBinding", ImGuiDataType_U32, &component->instanceFirstBinding, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("InstanceBindingCount", ImGuiDataType_U32, &component->instanceBindingCount, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("InstanceOffsets", ImGuiDataType_U64, &component->instanceOffsets, nullptr, nullptr, nullptr, inputTextFlags);
ImGui::InputScalar("InstanceBufferMaxCount", ImGuiDataType_U32, &component->instanceBufferMaxCount, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("Instance Count", ImGuiDataType_U32, &component->instanceCounter, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly);
ImGui::Spacing();
}
void RecordImGui_CompInstPos(bool readonly, CompInstance *component) {
if (component == nullptr) return;
int inputTextFlags = 0;
if (readonly)
inputTextFlags |= ImGuiInputTextFlags_ReadOnly;
ImGui::Text("CompInststance + InstPos");
ImGui::Separator();
bool changed = false;
InstPos instPos;
component->bt.motionState->getWorldTransform(instPos.posRot);
instPos.scale = component->bt.rigidBody->getCollisionShape()->getLocalScaling();
instPos.mass = component->bt.rigidBody->getMass();
btVector3 pos = instPos.posRot.getOrigin();
btQuaternion rot = instPos.posRot.getRotation();
glm::vec3 eul;
rot.getEulerZYX(eul.z, eul.y, eul.x);
eul = glm::degrees(eul);
changed = ImGui::InputScalar("Instance Index", ImGuiDataType_U64, &component->index, nullptr, nullptr, nullptr, ImGuiInputTextFlags_ReadOnly) || changed;
changed = ImGui::InputScalarN("pos", ImGuiDataType_Float, &pos, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
changed = ImGui::InputScalarN("rot (eul)", ImGuiDataType_Float, &eul, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
changed = ImGui::InputScalarN("scale", ImGuiDataType_Float, &instPos.scale, 3, nullptr, nullptr, nullptr, inputTextFlags) || changed;
changed = ImGui::InputFloat("mass", &instPos.mass, 0.0, 0.0, "%.3f", inputTextFlags) || changed;
changed = ImGui::InputScalar("Phys - Collision Layer", ImGuiDataType_U16, &component->physicsLayer, nullptr, nullptr, nullptr, inputTextFlags) || changed;
changed = ImGui::InputScalar("Phys - Collision Mask", ImGuiDataType_U16, &component->physicsMask, nullptr, nullptr, nullptr, inputTextFlags) || changed;
ImGui::InputScalar("Phys - Rigid Body", ImGuiDataType_U64, &component->bt.rigidBody, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
ImGui::InputScalar("Phys - Motion State", ImGuiDataType_U64, &component->bt.motionState, nullptr, nullptr, "0x%016lX", ImGuiInputTextFlags_ReadOnly);
if (changed) {
instPos.posRot.setOrigin(pos);
eul = glm::radians(eul);
rot.setEulerZYX(eul.z, eul.y, eul.x);
instPos.posRot.setRotation(rot);
auto *broadphase = component->bt.rigidBody->getBroadphaseProxy();
broadphase->m_collisionFilterGroup = static_cast<PhysicsCollision_T>(component->physicsLayer);
broadphase->m_collisionFilterMask = static_cast<PhysicsCollision_T>(component->physicsMask);
ECS_UpdateInstance(component->entHandle, instPos, true);
}
ImGui::Spacing();
}
void RecordImGuiSceneEditor() {
{
if (!ImGui::Begin("SceneEditorEntityTypes", &pkeSettings.editorSettings.isShowingSceneEditor)) {
ImGui::End();
return;
}
if (ImGui::Button("Create Entity Type")) {
ImGui::OpenPopup("CreateEntityType");
}
if (ImGui::Button("Save")) {
// Game_SaveSceneFile(sceneName);
shouldOpenSaveSceneDialog = true;
}
if (ImGui::Button("Load")) {
// Game_LoadSceneFile(sceneName);
shouldOpenLoadSceneDialog = true;
}
if (ImGui::Button("Clear Selection")) {
selectedEntity = EntityHandle_MAX;
}
ImGui::Spacing();
if (selectedEntity != EntityHandle_MAX) {
RecordImGui_CompGrBinds(true, ECS_GetGrBinds(selectedEntity));
RecordImGui_CompInstPos(false, ECS_GetInstance(selectedEntity));
}
RecordImGuiModalCreateEntityType();
ImGui::End();
}
}
void PkeEditor_RecordImGui() {
if (pkeSettings.isShowingEditor) {
RecordImGuiEditorWrapper();
RecordImGuiEntityList();
RecordImGuiSceneEditor();
RecordImGuiUBO();
Game_RecordImGui();
}
}
void PkeEditor_Setup() {
shouldSetupEditor = false;
PkeInput_ActivateSet(debugControlsHandle);
}
void PkeEditor_Disable() {
shouldDisableEditor = false;
PkeInput_DeactivateSet(debugControlsHandle);
}
void PkeEditor_Teardown() {
PkeEditor_Disable();
PkeThreads_Teardown(threadPoolHandle);
entityInstancesToCreate.~DynArray();
}
void PkeEditor_Init() {
PkeInputSet debugControlsSet;
debugControlsSet.title = "debug-editor-controls";
debugControlsSet.actionCount = 12;
debugControlsSet.actions = Pke_New<PkeInputAction>(debugControlsSet.actionCount);
debugControlsSet.actions[0].name = dbgCtrl_CameraLeft;
debugControlsSet.actions[0].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_A,
};
debugControlsSet.actions[1].name = dbgCtrl_CameraRight;
debugControlsSet.actions[1].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_D,
};
debugControlsSet.actions[2].name = dbgCtrl_CameraForward;
debugControlsSet.actions[2].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_W,
};
debugControlsSet.actions[3].name = dbgCtrl_CameraBack;
debugControlsSet.actions[3].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_S,
};
debugControlsSet.actions[4].name = dbgCtrl_CameraUp;
debugControlsSet.actions[4].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_R,
};
debugControlsSet.actions[5].name = dbgCtrl_CameraDown;
debugControlsSet.actions[5].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_F,
};
debugControlsSet.actions[6].name = dbgCtrl_CameraRotCC;
debugControlsSet.actions[6].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_Q,
};
debugControlsSet.actions[7].name = dbgCtrl_CameraRotC;
debugControlsSet.actions[7].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_E,
};
debugControlsSet.actions[8].name = dbgCtrl_CameraRot;
debugControlsSet.actions[8].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_CURSOR_POS_EVENTS,
};
debugControlsSet.actions[9].name = dbgCtrl_UnlockCamera;
debugControlsSet.actions[9].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_KEY_EVENTS,
.button = GLFW_KEY_ESCAPE,
};
debugControlsSet.actions[10].name = dbgCtrl_SelectHovered;
debugControlsSet.actions[10].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_MOUSE_BUTTON_EVENTS,
.button = GLFW_MOUSE_BUTTON_LEFT,
};
debugControlsSet.actions[11].name = dbgCtrl_ClearSelection;
debugControlsSet.actions[11].primaryHash = PkeInputEventMask {
.computedHash = PKE_INPUT_HASH_ALL_MOUSE_BUTTON_EVENTS,
.button = GLFW_MOUSE_BUTTON_RIGHT,
};
debugControlsHandle = PkeInput_RegisterSet(debugControlsSet);
ActiveCamera = &cameraDefault;
threadPoolHandle = PkeThreads_Init(1, 1);
}
|