Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion Tetragrama/Components/InspectorViewUIComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ namespace
ImGui::PopID();
}

void AddComponentPopup(Scene& scene, EntityID id)
{
if (!ImGui::BeginPopup("##add_component_popup"))
return;

const ArchetypeMask mask = scene.GetMask(id);
bool any = false;

ComponentReflectionRegistry::Get().ForEach([&](const ComponentMeta& meta) {
if (MaskHas(mask, meta.TypeID))
{
return; // already on the entity
}
if (!meta.Add)
{
return;
}
if (ImGui::Selectable(meta.TypeName))
{
scene.AddComponentRaw(id, meta.TypeID);
}
});

if (!any)
{
ImGui::TextDisabled("No components left to add");
}

ImGui::EndPopup();
}

void DrawEntityComponents(Scene& scene, EntityID id)
{
static constexpr ImGuiTableFlags kTableFlags = ImGuiTableFlags_NoPadInnerX | ImGuiTableFlags_NoPadOuterX;
Expand Down Expand Up @@ -326,7 +357,9 @@ namespace Tetragrama::Components
float tx = btn_scr.x + 8.f;
cdl->AddText({tx, ty}, IM_COL32(90, 210, 120, 255), "+");
cdl->AddText({tx + ImGui::CalcTextSize("+").x + 5.f, ty}, ImGui::GetColorU32(ImGuiCol_Text), "Add");
(void) add_clicked;
if (add_clicked)
ImGui::OpenPopup("##add_component_popup");
AddComponentPopup(*ctx->Scene, actor->GetEntityID());

ImGui::EndChild();
ImGui::PopStyleColor();
Expand Down
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/CameraComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/CameraComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -23,6 +24,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Camera",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<CC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/LightComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/LightComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand Down Expand Up @@ -33,6 +34,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Lighting",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<LC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/MaterialComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/MaterialComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -19,6 +20,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Rendering",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<MatC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/MeshComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/MeshComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -20,6 +21,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Rendering",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<MC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/NameComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/NameComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -19,6 +20,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "General",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<NC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/RigidBodyComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/RigidBodyComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand Down Expand Up @@ -29,6 +30,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Physics",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<RBC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/TransformComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/TransformComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -23,6 +24,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "Transform",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<TC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
2 changes: 2 additions & 0 deletions ZEngine/ZEngine/ECS/Components/UUIDComponent.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <ZEngine/ECS/Components/UUIDComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>

namespace ZEngine::ECS::Components
{
Expand All @@ -19,6 +20,7 @@ namespace ZEngine::ECS::Components
.Fields = kFields,
.FieldCount = static_cast<uint32_t>(sizeof(kFields) / sizeof(kFields[0])),
.Category = "General",
.Add = [](Scene& scene, EntityID id) { scene.AddComponent<UC>(id, {}); },
});
}
} // namespace ZEngine::ECS::Components
5 changes: 5 additions & 0 deletions ZEngine/ZEngine/ECS/Reflection/ComponentMeta.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include <ZEngine/ECS/ComponentTypeID.h>
#include <ZEngine/ECS/EntityID.h>
#include <ZEngine/ECS/Reflection/FieldType.h>
#include <cstdint>

namespace ZEngine::ECS
{
class Scene;

struct EnumValue
{
Expand Down Expand Up @@ -40,6 +42,9 @@ namespace ZEngine::ECS
uint32_t FieldCount = 0;
const char* Category = "General"; // compare with strcmp, not ==
const char* Tooltip = nullptr;

using AddFn = void (*)(Scene&, EntityID);
AddFn Add = nullptr;
};

} // namespace ZEngine::ECS
15 changes: 15 additions & 0 deletions ZEngine/ZEngine/ECS/Scene.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <ZEngine/ECS/Components/TransformComponent.h>
#include <ZEngine/ECS/Reflection/ComponentReflectionRegistry.h>
#include <ZEngine/ECS/Scene.h>
#include <ZEngine/ZEngineDef.h>

Expand Down Expand Up @@ -70,6 +71,20 @@ namespace ZEngine::ECS
});
}

void Scene::AddComponentRaw(EntityID id, ComponentTypeID type_id)
{
if (!IsAlive(id) || MaskHas(m_registry.GetMask(id), type_id))
{
return;
}

const ComponentMeta* meta = ComponentReflectionRegistry::Get().Lookup(type_id);
if (meta && meta->Add)
{
meta->Add(*this, id);
}
}

void* Scene::GetComponentRaw(EntityID id, ComponentTypeID type_id)
{
IComponentStorage** found = m_storages.find(type_id);
Expand Down
5 changes: 5 additions & 0 deletions ZEngine/ZEngine/ECS/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ namespace ZEngine::ECS
// Call once per render frame before submitting to the renderer.
void FillRenderableTransforms(float alpha, Core::Containers::Array<RenderableTransform>& out);

// Adds a default-constructed component via the type's registered reflection
// factory. No-op if the type is unregistered, has no factory, or the entity
// already has it.
void AddComponentRaw(EntityID id, ComponentTypeID type_id);

void* GetComponentRaw(EntityID id, ComponentTypeID type_id);
const void* GetComponentRaw(EntityID id, ComponentTypeID type_id) const;

Expand Down
Loading