Fix editor scene persistence and XC scene workflow

This commit is contained in:
2026-03-26 01:26:26 +08:00
parent 39edb0b497
commit 0651666d8c
35 changed files with 1958 additions and 256 deletions

View File

@@ -48,32 +48,30 @@ void GameObject::SetParent(GameObject* parent) {
}
void GameObject::SetParent(GameObject* parent, bool worldPositionStays) {
if (m_parent == parent) {
if (m_parent == parent || parent == this) {
return;
}
Math::Vector3 worldPos = worldPositionStays ? GetTransform()->GetPosition() : GetTransform()->GetLocalPosition();
Math::Quaternion worldRot = worldPositionStays ? GetTransform()->GetRotation() : GetTransform()->GetLocalRotation();
Math::Vector3 worldScale = worldPositionStays ? GetTransform()->GetScale() : GetTransform()->GetLocalScale();
if (m_parent) {
auto& siblings = m_parent->m_children;
siblings.erase(std::remove(siblings.begin(), siblings.end(), this), siblings.end());
} else if (m_scene) {
auto& roots = m_scene->m_rootGameObjects;
roots.erase(std::remove(roots.begin(), roots.end(), m_id), roots.end());
}
m_parent = parent;
if (m_parent) {
m_parent->m_children.push_back(this);
} else if (m_scene) {
auto& roots = m_scene->m_rootGameObjects;
if (std::find(roots.begin(), roots.end(), m_id) == roots.end()) {
roots.push_back(m_id);
}
}
if (worldPositionStays) {
GetTransform()->SetPosition(worldPos);
GetTransform()->SetRotation(worldRot);
GetTransform()->SetScale(worldScale);
}
GetTransform()->SetDirty();
GetTransform()->SetParent(parent ? parent->GetTransform() : nullptr, worldPositionStays);
}
GameObject* GameObject::GetChild(size_t index) const {
@@ -88,32 +86,17 @@ std::vector<GameObject*> GameObject::GetChildren() const {
}
void GameObject::DetachChildren() {
for (auto* child : m_children) {
auto children = m_children;
for (auto* child : children) {
if (child) {
child->m_parent = nullptr;
child->SetParent(nullptr, true);
}
}
m_children.clear();
}
void GameObject::DetachFromParent() {
if (m_parent) {
Math::Vector3 worldPos = GetTransform()->GetPosition();
Math::Quaternion worldRot = GetTransform()->GetRotation();
Math::Vector3 worldScale = GetTransform()->GetScale();
auto& siblings = m_parent->m_children;
siblings.erase(std::remove(siblings.begin(), siblings.end(), this), siblings.end());
m_parent = nullptr;
if (m_scene) {
m_scene->m_rootGameObjects.push_back(m_id);
}
GetTransform()->SetPosition(worldPos);
GetTransform()->SetRotation(worldRot);
GetTransform()->SetScale(worldScale);
GetTransform()->SetDirty();
SetParent(nullptr, true);
}
}
@@ -260,4 +243,4 @@ void GameObject::Deserialize(std::istream& is) {
}
} // namespace Components
} // namespace XCEngine
} // namespace XCEngine