diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2024-01-10 19:24:12 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2024-01-10 19:24:12 -0500 |
| commit | f07294ca65143fac8b1b426d1854212403721226 (patch) | |
| tree | 2edeb8f2c9beea1cbb065f69910d53957ebda0ce /editor | |
| parent | 294c85f91ac5b2ff9e4ad3d99588ed0d1a72e6b7 (diff) | |
checkpoint - handle breaking ECS changes - compiles
Diffstat (limited to 'editor')
| -rw-r--r-- | editor/editor.cpp | 262 |
1 files changed, 114 insertions, 148 deletions
diff --git a/editor/editor.cpp b/editor/editor.cpp index 625a49f..2f7e804 100644 --- a/editor/editor.cpp +++ b/editor/editor.cpp @@ -2,6 +2,7 @@ #include "editor.hpp" #include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h" +#include "array.hpp" #include "camera.hpp" #include "ecs.hpp" #include "entities.hpp" @@ -61,12 +62,9 @@ bool shouldDisableEditor = false; bool shouldRebuildProjectDir = true; bool shouldRebuildAssetList = true; -struct EntityTypeInstanceCreateInfo { - EntityHandle entityTypeEntityHandle; -}; -DynArray<EntityTypeInstanceCreateInfo> entityInstancesToCreate{16}; -EntityHandle selectedEntity = EntityHandle_MAX; -EntityHandle hoveredEntity = EntityHandle_MAX; +DynArray<EntityType *> entityInstancesToCreate{16}; +CompInstance *selectedEntity = nullptr; +CompInstance *hoveredEntity = nullptr; bool shouldCreateEntityType = false; EntityType entityTypeToCreate{}; CameraHandle selectedCamera = CameraHandle_MAX; @@ -178,26 +176,26 @@ void PkeEditor_Tick(double delta) { shouldRebuildProjectDir = true; } - if (EntitiesToBeRemoved.Has(selectedEntity)) { - selectedEntity = EntityHandle_MAX; + if (selectedEntity && EntitiesToBeRemoved.Has(ECS_GetEntity(selectedEntity->entHandle))) { + selectedEntity = nullptr; } - if (EntitiesToBeRemoved.Has(hoveredEntity)) { - hoveredEntity = EntityHandle_MAX; + if (hoveredEntity && EntitiesToBeRemoved.Has(ECS_GetEntity(hoveredEntity->entHandle))) { + hoveredEntity = nullptr; } bool imGuiHovered = ImGui::GetIO().WantCaptureMouse; - if (selectedEntity != EntityHandle_MAX && !imGuiHovered) { + if (selectedEntity != nullptr && !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; + selectedEntity = nullptr; } } } // raycast for hovering - hoveredEntity = EntityHandle_MAX; + hoveredEntity = nullptr; if (!imGuiHovered) { double xMousePos, yMousePos; if (pkeSettings.editorSettings.isUsingDebugCamera) { @@ -220,11 +218,11 @@ void PkeEditor_Tick(double delta) { BtDynamicsWorld->rayTest(rayOrigin, rayDestination, rayResult); if (rayResult.hasHit()) { - hoveredEntity.hash = reinterpret_cast<uint64_t>(rayResult.m_collisionObject->getUserPointer()); + hoveredEntity = reinterpret_cast<CompInstance *>(rayResult.m_collisionObject->getUserPointer());; } } - if (hoveredEntity != EntityHandle_MAX) { + if (hoveredEntity != nullptr) { auto holder = PkeInput_Query(dbgCtrl_SelectHovered); if (holder.type != InputEventHash{0}) { const PkeMouseButtonEvent *mbEvent = static_cast<PkeMouseButtonEvent *>(holder.ptr); @@ -234,17 +232,14 @@ void PkeEditor_Tick(double delta) { } } - EntityHandle focusedHandle = selectedEntity != EntityHandle_MAX ? selectedEntity : hoveredEntity; + CompInstance *focusedInst = selectedEntity != nullptr ? selectedEntity : hoveredEntity; bool found = false; - if (focusedHandle != EntityHandle_MAX) { - const auto *inst = ECS_GetInstance(focusedHandle); - if (inst != nullptr) { - const auto *grBinds = ECS_GetGrBinds(inst->grBindsHandle); - if (grBinds != nullptr) { - pkeDebugHitbox.instanceBuffer = grBinds->instanceBuffer; - pkeDebugHitbox.instanceStartingIndex = inst->index; - found = true; - } + if (focusedInst != nullptr) { + const auto *grBinds = ECS_GetGrBinds(focusedInst->grBindsHandle); + if (grBinds != nullptr) { + pkeDebugHitbox.instanceBuffer = grBinds->instanceBuffer; + pkeDebugHitbox.instanceStartingIndex = focusedInst->index; + found = true; } } if (!found) { @@ -253,46 +248,40 @@ void PkeEditor_Tick(double delta) { } 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 *existingEntity = EntityType_FindByTypeCode(entityTypeToCreate.entityTypeCode); + if (existingEntity == nullptr) { + + EntityType *newEntType = EntityType_Create(); + strncpy(newEntType->modelAssetKey, entityTypeToCreate.modelAssetKey, AssetKeyLength); + newEntType->entityTypeCode = entityTypeToCreate.entityTypeCode; + if (entityTypeToCreate.createInstanceCallback.name[0] != '\0') { + strncpy(newEntType->createInstanceCallback.name, entityTypeToCreate.createInstanceCallback.name, CallbackSignatureLength); + PkePlugin_SetSignatureFunc(&newEntType->createInstanceCallback); + } + for (int64_t i = 0; i < entityTypeToCreate.detailsCount; ++i) { + newEntType->details[i] = entityTypeToCreate.details[i]; + strncpy(newEntType->details[i].textureAssetKey, entityTypeToCreate.details[i].textureAssetKey, AssetKeyLength); + } + EntityType_Load(entityTypeToCreate); - GlobalEntityTypes.Push(entityTypeToCreate); + + for (int64_t i = 0; i < entityTypeToCreate.detailsCount; ++i) { + strncpy(newEntType->details[i].grBinds->collisionCallback.name, entityTypeToCreate.details[i].grBinds->collisionCallback.name, CallbackSignatureLength); + PkePlugin_SetSignatureFunc(&newEntType->details[i].grBinds->collisionCallback); + } + } else { + // TODO leaks entityTypeToCreate.entityTypeCode } entityTypeToCreate = EntityType{}; shouldCreateEntityType = false; } while (entityInstancesToCreate.Count() > 0) { - auto createInfo = entityInstancesToCreate.Pop(); + EntityType *et = entityInstancesToCreate.Pop(); // TODO needs to be more elegant - int64_t detailIndex = 0; - int64_t etIndex = EntityType_FindByEntityHandle(createInfo.entityTypeEntityHandle, detailIndex); - auto &et = GlobalEntityTypes[etIndex]; - EntityHandle parentEntity = ECS_CreateEntity(); - - for (size_t i = 0; i < et.detailsCount; ++i) { - auto &etd = et.details[i]; - EntityHandle newEntity = ECS_CreateEntity(parentEntity); - auto &compInst = ECS_CreateInstance(newEntity, etd.entityHandle); - compInst.physicsLayer = etd.bt.startingCollisionLayer; - compInst.physicsMask = etd.bt.startingCollisionMask; - btVector3 localInertia(0, 0, 0); - etd.bt.shape->calculateLocalInertia(etd.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(etd.bt.startingMass, compInst.bt.motionState, etd.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>(etd.bt.startingCollisionLayer); - compInst.bt.rigidBody->getBroadphaseProxy()->m_collisionFilterMask = static_cast<PhysicsCollision_T>(etd.bt.startingCollisionMask); - compInst.bt.rigidBody->setUserPointer(reinterpret_cast<void *>(compInst.entHandle.hash)); + if (et->createInstanceCallback.func) { + reinterpret_cast<void(*)()>(et->createInstanceCallback.func)(); + } else { + EntityType_CreateGenericInstance(et, nullptr); } } @@ -423,12 +412,12 @@ void PkeEditor_Tick(double delta) { } holder = PkeInput_Query(dbgCtrl_DeleteSelectedItem); - if (holder.type != InputEventHash{0} && selectedEntity != EntityHandle_MAX) { + if (holder.type != InputEventHash{0} && selectedEntity != nullptr) { PkeKeyEvent *delSelectedItemEvent; delSelectedItemEvent = static_cast<PkeKeyEvent *>(holder.ptr); if (delSelectedItemEvent->isPressed == true) { - ECS_MarkForRemoval(selectedEntity); - selectedEntity = EntityHandle_MAX; + ECS_MarkForRemoval(ECS_GetEntity(selectedEntity->entHandle)); + selectedEntity = nullptr; } } } @@ -637,66 +626,6 @@ void RecordImGuiEditorWrapper() { 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]; - if (entity->handle == EntityHandle_MAX) { - continue; - } - 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", entity->handle.hash); - ImGui::TableSetColumnIndex(2); - ImGui::Text("0x%016lX", entity->parentHandle.hash); - ImGui::TableSetColumnIndex(3); - ImGui::Text("0x%016lX", entity->grBindsHandle.hash); - ImGui::TableSetColumnIndex(4); - ImGui::Text("0x%016lX", entity->instanceHandle.hash); - 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 RecordImGuiCameras() { if (!ImGui::Begin("Cameras")) { ImGui::End(); @@ -848,7 +777,8 @@ bool RecordImGui_CallbackSelectModal(long &selectedIndex) { void RecordImGuiModalCreateEntityType() { if (ImGui::BeginPopupModal("CreateEntityType", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) { - static char entityTypeCode[32]; + static char entityTypeCode[32] = {'\0'}; + static char createInstanceSig[CallbackSignatureLength + 1] = {'\0'}; static AssetPickerSearchStruct apssModel{ .source = {"cet_mdl"}, .safeKey = {""}, @@ -867,6 +797,23 @@ void RecordImGuiModalCreateEntityType() { ImGui::Text("%s", apssModel.safeKey); ImGui::InputText("Entity Type Code", entityTypeCode, 31); + static long index = -1; + if (ImGui::Button("Clear")) { + index = -1; + createInstanceSig[0] = '\0'; + } + ImGui::SameLine(); + if (ImGui::Button("Change")) { + index = -1; + ImGui::OpenPopup("CallbackSelect"); + } + ImGui::SameLine(); + ImGui::Text("Collision Callback: '%s'", createInstanceSig); + if (RecordImGui_CallbackSelectModal(index)) { + long x = 0; + memcpy(createInstanceSig, PkePlugin_GetSortedSignatures(x)[index], CallbackSignatureLength); + } + ImGui::BeginDisabled(); ImGui::InputScalar("Sub-Types", ImGuiDataType_S64, &entityTypeToCreate.detailsCount); ImGui::SameLine(); @@ -893,9 +840,11 @@ void RecordImGuiModalCreateEntityType() { ImGui::Separator(); + bool shouldReset = false; if (ImGui::Button("Create")) { // TODO some type of validation + strncpy(entityTypeToCreate.createInstanceCallback.name, createInstanceSig, CallbackSignatureLength); strncpy(entityTypeToCreate.modelAssetKey, apssModel.safeKey, AssetKeyLength); // TODO this needs to be an array strncpy(entityTypeToCreate.details[0].textureAssetKey, apssTexture.safeKey, AssetKeyLength); @@ -906,19 +855,19 @@ void RecordImGuiModalCreateEntityType() { shouldCreateEntityType = true; - apssModel.safeKey[0] = '\0'; - apssTexture.safeKey[0] = '\0'; - entityTypeCode[0] = '\0'; - apssModel.safeKey[AssetKeyLength] = '\0'; - apssTexture.safeKey[AssetKeyLength] = '\0'; - entityTypeCode[31] = '\0'; - ImGui::CloseCurrentPopup(); + shouldReset = true; } ImGui::SameLine(); if (ImGui::Button("Cancel")) { + shouldReset = true; + } + + if (shouldReset) { + createInstanceSig[0] = '\0'; apssModel.safeKey[0] = '\0'; apssTexture.safeKey[0] = '\0'; entityTypeCode[0] = '\0'; + createInstanceSig[CallbackSignatureLength] = '\0'; apssModel.safeKey[AssetKeyLength] = '\0'; apssTexture.safeKey[AssetKeyLength] = '\0'; entityTypeCode[31] = '\0'; @@ -938,23 +887,20 @@ void RecordImGuiLevels() { ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg }; - if (ImGui::BeginTable("PkeLevels", 5, tableFlags)) { + if (ImGui::BeginTable("PkeLevels", 3, tableFlags)) { ImGui::TableSetupColumn("Name"); ImGui::TableSetupColumn("Handle"); - ImGui::TableSetupColumn("EntityCount"); ImGui::TableSetupColumn("CameraCount"); ImGui::TableHeadersRow(); for (long i = 0; i < MAX_LEVEL_COUNT; ++i) { - if (LEVELS[i].handle == LevelHandle_MAX) + if (LEVELS[i].levelHandle == LevelHandle_MAX) continue; ImGui::TableNextRow(); ImGui::TableSetColumnIndex(0); ImGui::Text("%s", LEVELS[i].name); ImGui::TableSetColumnIndex(1); - ImGui::Text("0x%04hx", LEVELS[i].handle); + ImGui::Text("0x%04hx", LEVELS[i].levelHandle); ImGui::TableSetColumnIndex(2); - ImGui::Text("%u", LEVELS[i].wrappingEntities.next - 1); - ImGui::TableSetColumnIndex(3); ImGui::Text("%u", LEVELS[i].cameras.next - 1); } ImGui::EndTable(); @@ -971,12 +917,6 @@ void RecordImGui_CompGrBinds(bool readonly, CompGrBinds *component) { 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){ @@ -1030,7 +970,7 @@ void RecordImGui_CompGrBinds(bool readonly, CompGrBinds *component) { ImGui::Text("Collision Callback: '%s'", component->collisionCallback.name); if (RecordImGui_CallbackSelectModal(index)) { long x = 0; - memcpy(component->collisionCallback.name, PkePlugin_GetSortedSignatures(x)[index], 16); + memcpy(component->collisionCallback.name, PkePlugin_GetSortedSignatures(x)[index], CallbackSignatureLength); PkePlugin_SetSignatureFunc(&component->collisionCallback); } @@ -1096,7 +1036,7 @@ void RecordImGui_CompInstPos(bool readonly, CompInstance *component) { 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); + ECS_UpdateInstance(component, instPos, true); } ImGui::Spacing(); @@ -1226,14 +1166,41 @@ void RecordImGuiSceneEditor() { ImGui::OpenPopup("CreateEntityType"); } if (ImGui::Button("Clear Selection")) { - selectedEntity = EntityHandle_MAX; + selectedEntity = nullptr; } ImGui::Spacing(); - if (selectedEntity != EntityHandle_MAX) { - RecordImGui_CompGrBinds(true, ECS_GetGrBinds(selectedEntity)); - RecordImGui_CompInstPos(false, ECS_GetInstance(selectedEntity)); + if (selectedEntity != nullptr) { + Entity_Base *entity = ECS_GetEntity(selectedEntity->entHandle); + auto *entityType = static_cast<EntityType *>(entity); + if (ImGui::Button("Create Instance")) { + entityInstancesToCreate.Push(entityType); + } + static PkeArray<CompGrBinds *> entGrBinds; + static PkeArray<CompInstance *> entInstances; + bool reset = false; + if (entGrBinds.next > 0) { + if (entGrBinds.data[0]->entHandle != selectedEntity->entHandle) { + reset = true; + } + } else if (entInstances.next > 0) { + if (entInstances.data[0]->entHandle != selectedEntity->entHandle) { + reset = true; + } + } + if (reset) { + PkeArray_SoftReset(&entGrBinds); + PkeArray_SoftReset(&entInstances); + } + ECS_GetGrBinds(ECS_GetEntity(selectedEntity->entHandle), entGrBinds); + ECS_GetInstances(ECS_GetEntity(selectedEntity->entHandle), entInstances); + for (int64_t i = 0; i < entGrBinds.next; ++i) { + RecordImGui_CompGrBinds(true, entGrBinds.data[i]); + } + for (int64_t i = 0; i < entInstances.next; ++i) { + RecordImGui_CompInstPos(false, entInstances.data[i]); + } } RecordImGuiModalCreateAsset(); @@ -1247,7 +1214,6 @@ void PkeEditor_RecordImGui() { RecordImGuiEditorWrapper(); RecordImGuiProjectSettingsEditor(); RecordImGuiProjectBrowser(); - RecordImGuiEntityList(); RecordImGuiSceneEditor(); RecordImGuiUBO(); RecordImGuiCameras(); |
