Node.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "Component.h"
  25. #include "Context.h"
  26. #include "Log.h"
  27. #include "MemoryBuffer.h"
  28. #include "Scene.h"
  29. #include "XMLElement.h"
  30. #include "DebugNew.h"
  31. // Normalize rotation quaternion after this many incremental updates to prevent distortion
  32. static const int NORMALIZE_ROTATION_EVERY = 32;
  33. OBJECTTYPESTATIC(Node);
  34. Node::Node(Context* context) :
  35. Serializable(context),
  36. id_(0),
  37. parent_(0),
  38. scene_(0),
  39. owner_(0),
  40. position_(Vector3::ZERO),
  41. rotation_(Quaternion::IDENTITY),
  42. scale_(Vector3::UNITY),
  43. worldTransform_(Matrix3x4::IDENTITY),
  44. rotateCount_(0),
  45. dirty_(false)
  46. {
  47. }
  48. Node::~Node()
  49. {
  50. RemoveAllChildren();
  51. RemoveAllComponents();
  52. // Remove from the scene
  53. if (scene_)
  54. scene_->NodeRemoved(this);
  55. }
  56. void Node::RegisterObject(Context* context)
  57. {
  58. context->RegisterFactory<Node>();
  59. REF_ACCESSOR_ATTRIBUTE(Node, VAR_STRING, "Name", GetName, SetName, String, String(), AM_DEFAULT);
  60. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  61. REF_ACCESSOR_ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_FILE);
  62. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Scale", GetScale, SetScale, Vector3, Vector3::UNITY, AM_DEFAULT);
  63. ATTRIBUTE(Node, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_FILE); // Network replication of vars uses custom data
  64. REF_ACCESSOR_ATTRIBUTE(Node, VAR_BUFFER, "Network Rotation", GetNetRotationAttr, SetNetRotationAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_NET | AM_LATESTDATA | AM_NOEDIT);
  65. REF_ACCESSOR_ATTRIBUTE(Node, VAR_BUFFER, "Network Parent Node", GetNetParentAttr, SetNetParentAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_NET | AM_NOEDIT);
  66. }
  67. void Node::OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData)
  68. {
  69. // Make a weak pointer to self to check for destruction during event handling
  70. WeakPtr<Node> self(this);
  71. // If this is a targeted event, forward it to all components
  72. if (!broadcast)
  73. {
  74. for (unsigned i = components_.Size() - 1; i < components_.Size(); --i)
  75. {
  76. components_[i]->OnEvent(sender, broadcast, eventType, eventData);
  77. if (self.Expired())
  78. return;
  79. }
  80. }
  81. else
  82. Object::OnEvent(sender, broadcast, eventType, eventData);
  83. }
  84. bool Node::Load(Deserializer& source)
  85. {
  86. return Load(source, true);
  87. }
  88. bool Node::Save(Serializer& dest)
  89. {
  90. if (!Serializable::Save(dest))
  91. return false;
  92. dest.WriteVLE(components_.Size());
  93. for (unsigned i = 0; i < components_.Size(); ++i)
  94. {
  95. Component* component = components_[i];
  96. // Create a separate buffer to be able to skip unknown components during deserialization
  97. VectorBuffer compBuffer;
  98. compBuffer.WriteShortStringHash(component->GetType());
  99. compBuffer.WriteUInt(component->GetID());
  100. if (!component->Save(compBuffer))
  101. return false;
  102. dest.WriteVLE(compBuffer.GetSize());
  103. dest.Write(compBuffer.GetData(), compBuffer.GetSize());
  104. }
  105. dest.WriteVLE(children_.Size());
  106. for (unsigned i = 0; i < children_.Size(); ++i)
  107. {
  108. Node* node = children_[i];
  109. dest.WriteUInt(node->GetID());
  110. if (!node->Save(dest))
  111. return false;
  112. }
  113. return true;
  114. }
  115. bool Node::LoadXML(const XMLElement& source)
  116. {
  117. return LoadXML(source, true);
  118. }
  119. bool Node::SaveXML(XMLElement& dest)
  120. {
  121. if (!Serializable::SaveXML(dest))
  122. return false;
  123. for (unsigned i = 0; i < components_.Size(); ++i)
  124. {
  125. Component* component = components_[i];
  126. XMLElement compElem = dest.CreateChild("component");
  127. compElem.SetString("type", component->GetTypeName());
  128. compElem.SetInt("id", component->GetID());
  129. if (!component->SaveXML(compElem))
  130. return false;
  131. }
  132. for (unsigned i = 0; i < children_.Size(); ++i)
  133. {
  134. Node* node = children_[i];
  135. XMLElement childElem = dest.CreateChild("node");
  136. childElem.SetInt("id", node->GetID());
  137. if (!node->SaveXML(childElem))
  138. return false;
  139. }
  140. return true;
  141. }
  142. void Node::FinishUpdate()
  143. {
  144. for (unsigned i = 0; i < components_.Size(); ++i)
  145. components_[i]->FinishUpdate();
  146. for (unsigned i = 0; i < children_.Size(); ++i)
  147. children_[i]->FinishUpdate();
  148. }
  149. void Node::SetName(const String& name)
  150. {
  151. name_ = name;
  152. nameHash_ = StringHash(name);
  153. }
  154. void Node::SetPosition(const Vector3& position)
  155. {
  156. position_ = position;
  157. if (!dirty_)
  158. MarkDirty();
  159. }
  160. void Node::SetRotation(const Quaternion& rotation)
  161. {
  162. rotation_ = rotation;
  163. rotateCount_ = 0;
  164. if (!dirty_)
  165. MarkDirty();
  166. }
  167. void Node::SetDirection(const Vector3& direction)
  168. {
  169. SetRotation(Quaternion(Vector3::FORWARD, direction));
  170. }
  171. void Node::SetScale(float scale)
  172. {
  173. scale_ = Vector3(scale, scale, scale);
  174. if (!dirty_)
  175. MarkDirty();
  176. }
  177. void Node::SetScale(const Vector3& scale)
  178. {
  179. scale_ = scale;
  180. if (!dirty_)
  181. MarkDirty();
  182. }
  183. void Node::SetTransform(const Vector3& position, const Quaternion& rotation)
  184. {
  185. position_ = position;
  186. rotation_ = rotation;
  187. rotateCount_ = 0;
  188. if (!dirty_)
  189. MarkDirty();
  190. }
  191. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, float scale)
  192. {
  193. position_ = position;
  194. rotation_ = rotation;
  195. scale_ = Vector3(scale, scale, scale);
  196. rotateCount_ = 0;
  197. if (!dirty_)
  198. MarkDirty();
  199. }
  200. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale)
  201. {
  202. position_ = position;
  203. rotation_ = rotation;
  204. scale_ = scale;
  205. rotateCount_ = 0;
  206. if (!dirty_)
  207. MarkDirty();
  208. }
  209. void Node::Translate(const Vector3& delta)
  210. {
  211. position_ += delta;
  212. if (!dirty_)
  213. MarkDirty();
  214. }
  215. void Node::TranslateRelative(const Vector3& delta)
  216. {
  217. position_ += rotation_ * delta;
  218. if (!dirty_)
  219. MarkDirty();
  220. }
  221. void Node::Rotate(const Quaternion& delta, bool fixedAxis)
  222. {
  223. if (!fixedAxis)
  224. rotation_ = rotation_ * delta;
  225. else
  226. rotation_ = delta * rotation_;
  227. ++rotateCount_;
  228. if (rotateCount_ >= NORMALIZE_ROTATION_EVERY)
  229. {
  230. rotation_.Normalize();
  231. rotateCount_ = 0;
  232. }
  233. if (!dirty_)
  234. MarkDirty();
  235. }
  236. void Node::Yaw(float angle, bool fixedAxis)
  237. {
  238. Rotate(Quaternion(angle, Vector3::UP), fixedAxis);
  239. }
  240. void Node::Pitch(float angle, bool fixedAxis)
  241. {
  242. Rotate(Quaternion(angle, Vector3::RIGHT), fixedAxis);
  243. }
  244. void Node::Roll(float angle, bool fixedAxis)
  245. {
  246. Rotate(Quaternion(angle, Vector3::FORWARD), fixedAxis);
  247. }
  248. void Node::Scale(float scale)
  249. {
  250. scale_ *= scale;
  251. if (!dirty_)
  252. MarkDirty();
  253. }
  254. void Node::Scale(const Vector3& scale)
  255. {
  256. scale_ *= scale;
  257. if (!dirty_)
  258. MarkDirty();
  259. }
  260. void Node::MarkDirty()
  261. {
  262. dirty_ = true;
  263. // Notify listener components first, then mark child nodes
  264. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End();)
  265. {
  266. if (*i)
  267. {
  268. (*i)->OnMarkedDirty(this);
  269. ++i;
  270. }
  271. // If listener has expired, erase from list
  272. else
  273. i = listeners_.Erase(i);
  274. }
  275. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  276. (*i)->MarkDirty();
  277. }
  278. Node* Node::CreateChild(const String& name, bool local)
  279. {
  280. Node* newNode = CreateChild(0, local);
  281. newNode->SetName(name);
  282. return newNode;
  283. }
  284. void Node::AddChild(Node* node)
  285. {
  286. // Check for illegal parent assignments, including attempt to reparent the scene
  287. if (!node || node == this || node->parent_ == this || parent_ == node || scene_ == node)
  288. return;
  289. // Add first, then remove from old parent, to ensure the node does not get deleted
  290. children_.Push(SharedPtr<Node>(node));
  291. if (node->parent_)
  292. node->parent_->RemoveChild(node);
  293. // Add to the scene if not added yet
  294. if (scene_ && !node->GetScene())
  295. scene_->NodeAdded(node);
  296. node->parent_ = this;
  297. node->MarkDirty();
  298. }
  299. void Node::RemoveChild(Node* node)
  300. {
  301. if (!node)
  302. return;
  303. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  304. {
  305. if (i->RawPtr() == node)
  306. {
  307. RemoveChild(i);
  308. return;
  309. }
  310. }
  311. }
  312. void Node::RemoveAllChildren()
  313. {
  314. while (children_.Size())
  315. RemoveChild(children_.End() - 1);
  316. }
  317. Component* Node::CreateComponent(ShortStringHash type, bool local)
  318. {
  319. return CreateComponent(type, 0, local);
  320. }
  321. Component* Node::GetOrCreateComponent(ShortStringHash type, bool local)
  322. {
  323. Component* oldComponent = GetComponent(type);
  324. if (oldComponent)
  325. return oldComponent;
  326. else
  327. return CreateComponent(type, 0, local);
  328. }
  329. void Node::RemoveComponent(Component* component)
  330. {
  331. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  332. {
  333. if (*i == component)
  334. {
  335. WeakPtr<Component> componentWeak(*i);
  336. RemoveListener(*i);
  337. if (scene_)
  338. scene_->ComponentRemoved(*i);
  339. components_.Erase(i);
  340. // If the component is still referenced elsewhere, reset its node pointer now
  341. if (componentWeak)
  342. componentWeak->SetNode(0);
  343. return;
  344. }
  345. }
  346. }
  347. void Node::RemoveAllComponents()
  348. {
  349. while (components_.Size())
  350. {
  351. Vector<SharedPtr<Component> >::Iterator i = components_.End() - 1;
  352. WeakPtr<Component> componentWeak(*i);
  353. RemoveListener(*i);
  354. if (scene_)
  355. scene_->ComponentRemoved(*i);
  356. components_.Erase(i);
  357. // If the component is still referenced elsewhere, reset its node pointer now
  358. if (componentWeak)
  359. componentWeak->SetNode(0);
  360. }
  361. }
  362. void Node::AddListener(Component* component)
  363. {
  364. if (!component)
  365. return;
  366. // Check for not adding twice
  367. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  368. {
  369. if ((*i) == component)
  370. return;
  371. }
  372. listeners_.Push(WeakPtr<Component>(component));
  373. // If the node is currently dirty, notify immediately
  374. if (dirty_)
  375. component->OnMarkedDirty(this);
  376. }
  377. void Node::RemoveListener(Component* component)
  378. {
  379. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  380. {
  381. if ((*i) == component)
  382. {
  383. listeners_.Erase(i);
  384. return;
  385. }
  386. }
  387. }
  388. void Node::Remove()
  389. {
  390. if (parent_)
  391. parent_->RemoveChild(this);
  392. }
  393. void Node::SetParent(Node* parent)
  394. {
  395. if (parent)
  396. parent->AddChild(this);
  397. }
  398. unsigned Node::GetNumChildren(bool recursive) const
  399. {
  400. if (!recursive)
  401. return children_.Size();
  402. else
  403. {
  404. unsigned allChildren = children_.Size();
  405. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  406. allChildren += (*i)->GetNumChildren(true);
  407. return allChildren;
  408. }
  409. }
  410. void Node::GetChildren(PODVector<Node*>& dest, bool recursive) const
  411. {
  412. dest.Clear();
  413. if (!recursive)
  414. {
  415. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  416. dest.Push(*i);
  417. }
  418. else
  419. GetChildrenRecursive(dest);
  420. }
  421. void Node::GetChildrenWithComponent(PODVector<Node*>& dest, ShortStringHash type, bool recursive) const
  422. {
  423. dest.Clear();
  424. if (!recursive)
  425. {
  426. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  427. {
  428. if ((*i)->HasComponent(type))
  429. dest.Push(*i);
  430. }
  431. }
  432. else
  433. GetChildrenWithComponentRecursive(dest, type);
  434. }
  435. Node* Node::GetChild(unsigned index) const
  436. {
  437. return index < children_.Size() ? children_[index].RawPtr() : 0;
  438. }
  439. Node* Node::GetChild(const String& name, bool recursive) const
  440. {
  441. return GetChild(StringHash(name), recursive);
  442. }
  443. Node* Node::GetChild(StringHash nameHash, bool recursive) const
  444. {
  445. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  446. {
  447. if ((*i)->GetNameHash() == nameHash)
  448. return *i;
  449. if (recursive)
  450. {
  451. Node* node = (*i)->GetChild(nameHash, true);
  452. if (node)
  453. return node;
  454. }
  455. }
  456. return 0;
  457. }
  458. unsigned Node::GetNumNetworkComponents() const
  459. {
  460. unsigned num = 0;
  461. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  462. {
  463. if ((*i)->GetID() < FIRST_LOCAL_ID)
  464. ++num;
  465. }
  466. return num;
  467. }
  468. void Node::GetComponents(PODVector<Component*>& dest, ShortStringHash type) const
  469. {
  470. dest.Clear();
  471. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  472. {
  473. if ((*i)->GetType() == type)
  474. dest.Push(*i);
  475. }
  476. }
  477. bool Node::HasComponent(ShortStringHash type) const
  478. {
  479. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  480. {
  481. if ((*i)->GetType() == type)
  482. return true;
  483. }
  484. return false;
  485. }
  486. Component* Node::GetComponent(unsigned index) const
  487. {
  488. return index < components_.Size() ? components_[index].RawPtr() : 0;
  489. }
  490. Component* Node::GetComponent(ShortStringHash type, unsigned index) const
  491. {
  492. unsigned cmpIndex = 0;
  493. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  494. {
  495. if ((*i)->GetType() == type)
  496. {
  497. if (cmpIndex == index)
  498. return *i;
  499. ++cmpIndex;
  500. }
  501. }
  502. return 0;
  503. }
  504. void Node::GetDependencyNodes(PODVector<Node*>& dest) const
  505. {
  506. // Add the parent node, but if it is local, traverse to the first non-local node
  507. if (parent_ && parent_ != scene_)
  508. {
  509. Node* current = parent_;
  510. while (current->id_ >= FIRST_LOCAL_ID)
  511. current = current->parent_;
  512. dest.Push(current);
  513. }
  514. // Then let the components add their dependencies
  515. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  516. (*i)->GetDependencyNodes(dest);
  517. }
  518. void Node::SetID(unsigned id)
  519. {
  520. id_ = id;
  521. }
  522. void Node::SetScene(Scene* scene)
  523. {
  524. scene_ = scene;
  525. }
  526. void Node::SetOwner(Connection* owner)
  527. {
  528. owner_ = owner;
  529. }
  530. void Node::SetNetRotationAttr(const PODVector<unsigned char>& value)
  531. {
  532. MemoryBuffer buf(value);
  533. SetRotation(buf.ReadPackedQuaternion());
  534. }
  535. void Node::SetNetParentAttr(const PODVector<unsigned char>& value)
  536. {
  537. Scene* scene = GetScene();
  538. if (!scene)
  539. return;
  540. MemoryBuffer buf(value);
  541. unsigned baseNodeID = buf.ReadVLE();
  542. if (!baseNodeID)
  543. return;
  544. if (baseNodeID < FIRST_LOCAL_ID)
  545. SetParent(scene->GetNodeByID(baseNodeID));
  546. else
  547. {
  548. Node* baseNode = scene->GetNodeByID(baseNodeID);
  549. if (baseNode)
  550. SetParent(baseNode->GetChild(buf.ReadStringHash(), true));
  551. }
  552. }
  553. const PODVector<unsigned char>& Node::GetNetRotationAttr() const
  554. {
  555. attrBuffer_.Clear();
  556. attrBuffer_.WritePackedQuaternion(rotation_);
  557. return attrBuffer_.GetBuffer();
  558. }
  559. const PODVector<unsigned char>& Node::GetNetParentAttr() const
  560. {
  561. attrBuffer_.Clear();
  562. Scene* scene = GetScene();
  563. if (scene && parent_)
  564. {
  565. unsigned parentID = parent_->GetID();
  566. if (parentID < FIRST_LOCAL_ID)
  567. attrBuffer_.WriteVLE(parentID);
  568. else
  569. {
  570. // Parent is local: traverse hierarchy to find a non-local base node
  571. // This iteration always stops due to the scene (root) being non-local
  572. Node* current = parent_;
  573. while (current->GetID() >= FIRST_LOCAL_ID)
  574. current = current->GetParent();
  575. attrBuffer_.WriteVLE(current->GetID());
  576. attrBuffer_.WriteStringHash(parent_->GetNameHash());
  577. }
  578. }
  579. else
  580. attrBuffer_.WriteVLE(0);
  581. return attrBuffer_.GetBuffer();
  582. }
  583. bool Node::Load(Deserializer& source, bool readChildren)
  584. {
  585. // Remove all children and components first in case this is not a fresh load
  586. RemoveAllChildren();
  587. RemoveAllComponents();
  588. // ID has been read at the parent level
  589. if (!Serializable::Load(source))
  590. return false;
  591. unsigned numComponents = source.ReadVLE();
  592. for (unsigned i = 0; i < numComponents; ++i)
  593. {
  594. VectorBuffer compBuffer(source, source.ReadVLE());
  595. ShortStringHash newType = compBuffer.ReadShortStringHash();
  596. Component* newComponent = CreateComponent(newType, compBuffer.ReadUInt(), false);
  597. if (newComponent)
  598. {
  599. if (!newComponent->Load(compBuffer))
  600. return false;
  601. }
  602. }
  603. if (!readChildren)
  604. return true;
  605. unsigned numChildren = source.ReadVLE();
  606. for (unsigned i = 0; i < numChildren; ++i)
  607. {
  608. Node* newNode = CreateChild(source.ReadUInt(), false);
  609. if (!newNode->Load(source))
  610. return false;
  611. }
  612. return true;
  613. }
  614. bool Node::LoadXML(const XMLElement& source, bool readChildren)
  615. {
  616. // Remove all children and components first in case this is not a fresh load
  617. RemoveAllChildren();
  618. RemoveAllComponents();
  619. if (!Serializable::LoadXML(source))
  620. return false;
  621. XMLElement compElem = source.GetChild("component");
  622. while (compElem)
  623. {
  624. String typeName = compElem.GetString("type");
  625. Component* newComponent = CreateComponent(ShortStringHash(compElem.GetString("type")), compElem.GetInt("id"), false);
  626. if (newComponent)
  627. {
  628. if (!newComponent->LoadXML(compElem))
  629. return false;
  630. }
  631. compElem = compElem.GetNext("component");
  632. }
  633. if (!readChildren)
  634. return true;
  635. XMLElement childElem = source.GetChild("node");
  636. while (childElem)
  637. {
  638. Node* newNode = CreateChild(childElem.GetInt("id"), false);
  639. if (!newNode->LoadXML(childElem))
  640. return false;
  641. childElem = childElem.GetNext("node");
  642. }
  643. return true;
  644. }
  645. Component* Node::CreateComponent(ShortStringHash type, unsigned id, bool local)
  646. {
  647. // Make sure the object in question is a component
  648. SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
  649. if (!newComponent)
  650. {
  651. LOGERROR("Could not create unknown component type " + type.ToString());
  652. return 0;
  653. }
  654. components_.Push(newComponent);
  655. // If zero ID specified, let the scene assign
  656. if (scene_)
  657. {
  658. newComponent->SetID(id ? id : scene_->GetFreeComponentID(local));
  659. scene_->ComponentAdded(newComponent);
  660. }
  661. else
  662. newComponent->SetID(id);
  663. newComponent->SetNode(this);
  664. newComponent->OnMarkedDirty(this);
  665. return newComponent;
  666. }
  667. Node* Node::CreateChild(unsigned id, bool local)
  668. {
  669. SharedPtr<Node> newNode(new Node(context_));
  670. // If zero ID specified, let the scene assign
  671. if (scene_)
  672. newNode->SetID(id ? id : scene_->GetFreeNodeID(local));
  673. else
  674. newNode->SetID(id);
  675. AddChild(newNode);
  676. return newNode;
  677. }
  678. void Node::UpdateWorldTransform()
  679. {
  680. // For now, assume that the Scene has identity transform so that we skip one matrix multiply. However in the future
  681. // we may want dynamic root nodes for large worlds
  682. if (parent_ && parent_ != scene_)
  683. {
  684. if (parent_->dirty_)
  685. parent_->UpdateWorldTransform();
  686. worldTransform_ = parent_->worldTransform_ * Matrix3x4(position_, rotation_, scale_);
  687. }
  688. else
  689. worldTransform_ = Matrix3x4(position_, rotation_, scale_);
  690. dirty_ = false;
  691. }
  692. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  693. {
  694. (*i)->parent_ = 0;
  695. (*i)->MarkDirty();
  696. children_.Erase(i);
  697. }
  698. void Node::GetChildrenRecursive(PODVector<Node*>& dest) const
  699. {
  700. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  701. {
  702. Node* node = *i;
  703. dest.Push(node);
  704. if (!node->children_.Empty())
  705. node->GetChildrenRecursive(dest);
  706. }
  707. }
  708. void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, ShortStringHash type) const
  709. {
  710. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  711. {
  712. Node* node = *i;
  713. if (node->HasComponent(type))
  714. dest.Push(node);
  715. if (!node->children_.Empty())
  716. node->GetChildrenRecursive(dest);
  717. }
  718. }