Files
Seele/src/Editor/ComponentRegistry.cpp
T

208 lines
8.3 KiB
C++
Raw Normal View History

#include "ComponentRegistry.h"
#include "Asset/MeshAsset.h"
#include "Component/Camera.h"
#include "Component/DirectionalLight.h"
#include "Component/Mesh.h"
#include "Component/PointLight.h"
#include "Component/Transform.h"
#include <ostream>
#include <sstream>
using namespace Seele;
using namespace Seele::Editor;
// --- small formatting helpers ------------------------------------------------------
namespace {
// Render a glm::vec / Quaternion (which all have operator<<) into a trimmed string.
template <typename T>
std::string fmtVec(const T& v) {
std::ostringstream oss;
oss.precision(4);
oss << std::fixed << v;
return oss.str();
}
std::string fmtFloat(float f) {
std::ostringstream oss;
oss.precision(4);
oss << std::fixed << f;
return oss.str();
}
} // namespace
// All of the editor's component-inspection entry points live in Seele::Editor (matching the
// declarations in ComponentRegistry.h). The using-directives above (using namespace Seele;
// using namespace Seele::Editor;) make the types (PEntity, PScene, ComponentEntry, Array,
// glm, the Component::* types) resolvable unqualified inside this block, while the function
// *definitions* themselves are correctly placed in the same namespace as their declarations -
// no duplicate/ambiguous global-scope symbols.
namespace Seele {
namespace Editor {
std::vector<ComponentEntry>& componentCatalog() {
static std::vector<ComponentEntry> catalog;
return catalog;
}
void initComponentRegistry() {
auto& catalog = componentCatalog();
// Idempotent: build the catalog only once. The set of displayable component *types* is
// fixed for the editor's lifetime, so we never add to it after the first call.
if (!catalog.empty()) {
return;
}
// Every entry works through the handle's own hasComponent/accessComponent - the exact
// same call shape game code uses to read components, just applied to an OEntity view.
// Transform: position / rotation / scale. The single most important component to
// display - every placed actor has one.
catalog.push_back(ComponentEntry{
.name = "Transform",
.isAttached = [](PEntity e) -> bool { return e != nullptr && e->hasComponent<Component::Transform>(); },
.describe = [](PEntity e) -> std::string {
if (e == nullptr || !e->hasComponent<Component::Transform>()) {
return "";
}
const auto& t = e->accessComponent<Component::Transform>();
std::ostringstream oss;
oss << "Position: " << fmtVec(t.getPosition()) << "\n";
oss << "Rotation: " << fmtVec(t.getRotation()) << "\n";
oss << "Scale: " << fmtVec(t.getScale());
return oss.str();
},
});
// Camera: projection params.
catalog.push_back(ComponentEntry{
.name = "Camera",
.isAttached = [](PEntity e) -> bool { return e != nullptr && e->hasComponent<Component::Camera>(); },
.describe = [](PEntity e) -> std::string {
if (e == nullptr || !e->hasComponent<Component::Camera>()) {
return "";
}
const auto& c = e->accessComponent<Component::Camera>();
std::ostringstream oss;
oss << "Field Of View (deg): " << fmtFloat(glm::degrees(c.fieldOfView)) << "\n";
oss << "Aspect Ratio: " << fmtFloat(c.aspectRatio) << "\n";
oss << "Near Plane: " << fmtFloat(c.nearPlane) << "\n";
oss << "Far Plane: " << fmtFloat(c.farPlane) << "\n";
oss << "Main Camera: " << (c.mainCamera ? "true" : "false");
return oss.str();
},
});
// PointLight: color + intensity + attenuation.
catalog.push_back(ComponentEntry{
.name = "PointLight",
.isAttached = [](PEntity e) -> bool { return e != nullptr && e->hasComponent<Component::PointLight>(); },
.describe = [](PEntity e) -> std::string {
if (e == nullptr || !e->hasComponent<Component::PointLight>()) {
return "";
}
const auto& l = e->accessComponent<Component::PointLight>();
std::ostringstream oss;
oss << "Color: " << fmtVec(l.color) << "\n";
oss << "Intensity: " << fmtFloat(l.intensity) << "\n";
oss << "Attenuation: " << fmtFloat(l.attenuation);
return oss.str();
},
});
// DirectionalLight: color + intensity.
catalog.push_back(ComponentEntry{
.name = "DirectionalLight",
.isAttached = [](PEntity e) -> bool { return e != nullptr && e->hasComponent<Component::DirectionalLight>(); },
.describe = [](PEntity e) -> std::string {
if (e == nullptr || !e->hasComponent<Component::DirectionalLight>()) {
return "";
}
const auto& l = e->accessComponent<Component::DirectionalLight>();
std::ostringstream oss;
oss << "Color: " << fmtVec(l.color) << "\n";
oss << "Intensity: " << fmtFloat(l.intensity);
return oss.str();
},
});
// Mesh (static): the mesh asset's name/folder + staticness + meshlet count.
catalog.push_back(ComponentEntry{
.name = "Mesh",
.isAttached = [](PEntity e) -> bool { return e != nullptr && e->hasComponent<Component::Mesh>(); },
.describe = [](PEntity e) -> std::string {
if (e == nullptr || !e->hasComponent<Component::Mesh>()) {
return "";
}
const auto& m = e->accessComponent<Component::Mesh>();
std::ostringstream oss;
oss << "Asset: ";
if (m.asset != nullptr) {
oss << m.asset->getFolderPath() << "/" << m.asset->getName();
} else {
oss << "(none)";
}
oss << "\n";
oss << "Static: " << (m.isStatic ? "true" : "false") << "\n";
oss << "Meshlets: " << m.meshletOffsets.size();
return oss.str();
},
});
}
std::vector<EntityInfo> enumerateEntities(const PScene& scene) {
std::vector<EntityInfo> out;
if (scene == nullptr) {
return out;
}
// The scene's roster is the source of truth for "what entities exist" - this is the
// "OEntity you can query via the scene" the user asked for. We iterate handles (PEntity
// views), never entt ids. The roster is appended in creation order and pruned on
// destroy, so it is stable across frames (no re-sorting needed, no entt::entity anywhere).
const Array<PEntity>& roster = scene->getEntities();
for (size_t i = 0; i < roster.size(); ++i) {
PEntity e = roster[i];
if (e == nullptr) {
continue;
}
EntityInfo info;
info.entity = e; // non-owning handle the row points at
info.label = e->getLabel(); // "#<id>" formatted in the engine; no entt type leaks
// Component summary: the names of every cataloged component this entity actually
// has, comma-joined. Uses isAttached (handle-space) - no registry access in the editor.
std::vector<std::string> names;
for (auto& entry : componentCatalog()) {
if (entry.isAttached(e)) {
names.push_back(entry.name);
}
}
if (names.empty()) {
info.componentSummary = "no components";
} else {
info.componentSummary = names[0];
for (size_t n = 1; n < names.size(); ++n) {
info.componentSummary += ", " + names[n];
}
}
out.push_back(std::move(info));
}
return out;
}
std::vector<ComponentDisplay> describeEntityComponents(const PScene& scene, PEntity entity) {
std::vector<ComponentDisplay> out;
if (scene == nullptr || entity == nullptr) {
return out;
}
// Walk the catalog in order; for each component the entity has, render its fields via
// the handle's own accessComponent. The editor never touches the registry directly.
for (auto& entry : componentCatalog()) {
if (!entry.isAttached(entity)) {
continue; // entity does not have this component (or it is not displayable)
}
out.push_back(ComponentDisplay{.name = entry.name, .fieldText = entry.describe(entity)});
}
return out;
}
} // namespace Editor
} // namespace Seele