9#include <unordered_map>
10#include <unordered_set>
15int appendDetachedNode(SceneHost::Tree& tree, NodeDesc&&
desc,
int parentIndex) {
16 const int index = int(tree.nodes.size());
19 node.persistentId =
desc.persistentId;
23 if (
node.space.empty())
node.space =
"3d";
43 node.localDirty =
true;
44 node.world = glm::mat4(1.f);
46 node.nextSibling = -1;
47 node.parent = parentIndex;
49 tree.nodes.push_back(std::move(
node));
54 int childIndex = appendDetachedNode(tree, std::move(child),
index);
55 if (firstChild < 0) firstChild = childIndex;
56 if (prevChild >= 0) tree.nodes[size_t(prevChild)].nextSibling = childIndex;
57 prevChild = childIndex;
59 tree.nodes[size_t(
index)].firstChild = firstChild;
63bool structureMatches(
const SceneHost::Tree &tree,
int nodeIndex,
const NodeDesc &
desc) {
64 if (nodeIndex < 0 || nodeIndex >=
int(tree.nodes.size()))
return false;
65 const SceneNode &
n = tree.nodes[size_t(
nodeIndex)];
66 const std::string &dk =
desc.reconcileKey();
67 if (!dk.empty() && !
n.key.empty() &&
n.key != dk)
return false;
69 std::vector<std::string> oldKeys;
70 for (
int c =
n.firstChild;
c >= 0;
c = tree.nodes[size_t(
c)].nextSibling) {
71 oldKeys.push_back(tree.nodes[
size_t(
c)].key);
73 if (oldKeys.size() !=
desc.children.size())
return false;
74 for (
size_t i = 0; i <
desc.children.size(); ++i) {
75 const std::string &ck =
desc.children[i].reconcileKey();
76 if (ck.empty() || oldKeys[i].empty()) {
77 if (!(ck.empty() && oldKeys[i].empty()))
return false;
78 }
else if (ck != oldKeys[i]) {
85 if (!structureMatches(tree, child, ch))
return false;
86 child = tree.nodes[size_t(child)].nextSibling;
95bool structureMatchesSet(
const SceneHost::Tree &tree,
int nodeIndex,
96 const NodeDesc &
desc) {
97 if (nodeIndex < 0 || nodeIndex >=
int(tree.nodes.size()))
return false;
98 const SceneNode &
n = tree.nodes[size_t(
nodeIndex)];
99 const std::string &dk =
desc.reconcileKey();
100 if (!dk.empty() && !
n.key.empty() &&
n.key != dk)
return false;
102 std::vector<std::string> oldKeys;
103 for (
int c =
n.firstChild;
c >= 0;
c = tree.nodes[size_t(
c)].nextSibling) {
104 oldKeys.push_back(tree.nodes[
size_t(
c)].key);
106 if (oldKeys.size() !=
desc.children.size())
return false;
108 std::unordered_multiset<std::string> want;
109 for (
const auto &ch :
desc.
children) want.insert(ch.reconcileKey());
110 for (
const auto &k : oldKeys) {
111 auto it = want.find(k);
112 if (it == want.end())
return false;
117 std::vector<bool>
used(
desc.children.size(),
false);
118 for (
int c =
n.firstChild;
c >= 0;
c = tree.nodes[size_t(
c)].nextSibling) {
120 const std::string &ck = tree.nodes[size_t(
c)].key;
121 for (
size_t i = 0; i <
desc.children.size(); ++i) {
122 if (
used[i])
continue;
123 const std::string &dk2 =
desc.children[i].reconcileKey();
124 if (ck == dk2 || (ck.empty() && dk2.empty())) {
125 if (structureMatchesSet(tree,
c,
desc.children[i])) {
132 if (!
found)
return false;
138bool reorderChildrenRecursive(SceneHost::Tree &tree,
int nodeIndex,
139 const NodeDesc &
desc) {
141 std::unordered_map<std::string, std::vector<int>> byKey;
142 for (
int c =
n.firstChild;
c >= 0;
c = tree.nodes[size_t(
c)].nextSibling) {
143 byKey[tree.nodes[size_t(
c)].key].push_back(
c);
145 std::vector<int>
order;
148 auto it = byKey.find(ch.reconcileKey());
149 if (it == byKey.end() || it->second.empty())
return false;
150 order.push_back(it->second.back());
151 it->second.pop_back();
157 for (
size_t i = 0; i <
order.size(); ++i) {
158 tree.nodes[size_t(
order[i])].nextSibling =
162 for (
size_t i = 0; i <
order.size(); ++i) {
163 if (!reorderChildrenRecursive(tree,
order[i],
desc.children[i]))
return false;
169 SceneHost::Tree &tree = *
host->tree();
171 n.visible =
desc.visible;
173 n.layer =
desc.layer;
174 n.bminX =
desc.bminX;
175 n.bminY =
desc.bminY;
176 n.bminZ =
desc.bminZ;
177 n.bmaxX =
desc.bmaxX;
178 n.bmaxY =
desc.bmaxY;
179 n.bmaxZ =
desc.bmaxZ;
180 n.hasBounds =
desc.hasBounds;
185 n.pitch =
desc.pitch;
190 if (!
desc.persistentId.isNil())
n.persistentId =
desc.persistentId;
191 if (!
desc.space.empty())
n.space =
desc.space;
193 if (!
desc.name.empty())
n.name =
desc.name;
195 host->fireEvent(
"node_changed",
n.id,
196 n.parent >= 0 ? tree.nodes[
size_t(
n.parent)].id :
"");
200 patchProps(
host, child, std::move(ch));
201 child = tree.nodes[size_t(child)].nextSibling;
208 std::unordered_set<std::string> seen;
210 if (!
d.id.empty() && !seen.insert(
d.id).second) {
211 throw std::runtime_error(
"scene: duplicate node id '" +
d.id +
"'");
213 for (
const auto &
c :
d.children) walk(
c);
220 d.
id = std::move(
id);
222 d.name =
name.empty() ?
d.id : std::move(
name);
229 d.
id = std::move(
id);
231 d.name =
d.id.empty() ?
"group" :
d.id;
237 if (!cond)
return group({},
"__when_empty");
242 return cond ? std::move(ifTrue) : std::move(ifFalse);
248 auto t =
host->tree();
250 std::unordered_set<std::string> oldIds;
251 for (
const auto &
n :
t->nodes) {
252 if (!
n.id.empty()) oldIds.insert(
n.id);
255 std::unordered_map<std::string, std::vector<SceneLink>> saved;
256 for (
const auto &
n :
t->nodes) {
257 if (!
n.links.empty() && !
n.id.empty()) saved[
n.id] =
n.links;
262 std::unordered_map<std::string, uint32_t> savedObjects;
263 for (
const auto &
n :
t->nodes) {
264 if (
n.objectId != 0 && !
n.id.empty()) savedObjects[
n.id] =
n.objectId;
269 t->root = appendDetachedNode(*
t, std::move(
root), -1);
270 for (
auto &
n :
t->nodes) {
271 auto it = saved.find(
n.id);
272 if (it != saved.end()) {
273 n.links = it->second;
275 auto oit = savedObjects.find(
n.id);
276 if (oit != savedObjects.end())
n.objectId = oit->second;
278 host->invalidateIndex();
280 t->transformDirty =
true;
283 std::unordered_set<std::string> newIds;
284 for (
const auto &
n :
t->nodes) {
285 if (!
n.id.empty()) newIds.insert(
n.id);
287 for (
const auto &
id : oldIds) {
288 if (!newIds.count(
id))
host->fireEvent(
"node_removed",
id);
290 for (
const auto &
n :
t->nodes) {
291 if (!oldIds.count(
n.id)) {
292 host->fireEvent(
"node_added",
n.id,
293 n.parent >= 0 ?
t->nodes[
size_t(
n.parent)].id :
"");
304 const auto parentWorld =
node.parent < 0 ? glm::mat4(1.f) :
target.nodes[size_t(
node.parent)].world;
307 for (
unsigned row = 0; row < 4; ++row)
311 "detached scene transform must produce a finite world matrix",
node.id));
314 std::unordered_map<std::string, const SceneNode *> preserved;
315 preserved.reserve(
previous->nodes.size());
317 if (!
node.id.empty()) preserved.emplace(
node.id, &
node);
319 const auto found = preserved.find(
node.id);
320 if (
found == preserved.end())
continue;
326 target.transformDirty =
true;
327 target.indexValid =
false;
329 }
catch (
const std::exception& exception) {
336 if (!
host)
return true;
337 auto t =
host->tree();
338 if (
t->root < 0 ||
t->nodes.empty() ||
339 !structureMatchesSet(*
t,
t->root,
root)) {
346 if (!reorderChildrenRecursive(*
t,
t->root,
root)) {
350 patchProps(
host,
t->root, std::move(
root));
352 t->transformDirty =
true;
std::shared_ptr< const OceanSprayFrame > previous
AbilityActivationGroup group
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
ECS mount point for one scene graph (full scene or nested subtree root). Isomorphic to eve::ui::UIHos...
static eve::Result< Tree > buildDetachedTree(const Tree *previous, NodeDesc root)
Builds a detached replacement tree without mutating a host or firing callbacks.
ShapeState * child(const JPH::Body &body, const JPH::SubShapeID &id)
Resolves a native child record for a synchronous callback or query. @ownership Borrowed from the worl...
glm::mat4 localMatrix(const SceneNode &node)
bool applyTreeReconcile(SceneHost *host, NodeDesc root)
Key-aware patch when structure matches; else full replace. Returns true if full rebuild.
void validateUniqueIds(const NodeDesc &root)
NodeDesc when(bool cond, NodeDesc child)
Conditional: include child only when cond is true (empty group otherwise).
void applyTree(SceneHost *host, NodeDesc root)
Applies tree.
NodeDesc whenElse(bool cond, NodeDesc ifTrue, NodeDesc ifFalse)
When else.
Declarative scene-node description (build once / on dirty → flatten into SceneHost::Tree)....