Node.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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 "Scene.h"
  28. #include "StringUtils.h"
  29. #include "VectorBuffer.h"
  30. #include "XMLElement.h"
  31. #include "DebugNew.h"
  32. // Normalize rotation quaternion after this many incremental updates to prevent distortion
  33. static const int NORMALIZE_ROTATION_EVERY = 32;
  34. OBJECTTYPESTATIC(Node);
  35. Node::Node(Context* context) :
  36. Serializable(context),
  37. id_(0),
  38. parent_(0),
  39. scene_(0),
  40. owner_(0),
  41. position_(Vector3::ZERO),
  42. rotation_(Quaternion::IDENTITY),
  43. scale_(Vector3::UNITY),
  44. worldTransform_(Matrix4x3::IDENTITY),
  45. rotateCount_(0),
  46. dirty_(false)
  47. {
  48. }
  49. Node::~Node()
  50. {
  51. RemoveAllChildren();
  52. RemoveAllComponents();
  53. // Remove from the scene
  54. if (scene_)
  55. scene_->NodeRemoved(this);
  56. }
  57. void Node::RegisterObject(Context* context)
  58. {
  59. context->RegisterFactory<Node>();
  60. ATTRIBUTE(Node, VAR_STRING, "Name", name_, String());
  61. ATTRIBUTE(Node, VAR_VECTOR3, "Position", position_, Vector3::ZERO);
  62. ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", rotation_, Quaternion::IDENTITY);
  63. ATTRIBUTE(Node, VAR_VECTOR3, "Scale", scale_, Vector3::UNITY);
  64. ATTRIBUTE_MODE(Node, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_SERIALIZATION);
  65. }
  66. void Node::OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData)
  67. {
  68. // Make a weak pointer to self to check for destruction during event handling
  69. WeakPtr<Node> self(this);
  70. // If this is a targeted event, forward it to all components
  71. if (!broadcast)
  72. {
  73. for (unsigned i = components_.Size() - 1; i < components_.Size(); --i)
  74. {
  75. components_[i]->OnEvent(sender, broadcast, eventType, eventData);
  76. if (self.IsExpired())
  77. return;
  78. }
  79. }
  80. else
  81. Object::OnEvent(sender, broadcast, eventType, eventData);
  82. }
  83. void Node::OnSetAttribute(const AttributeInfo& attr, const Variant& value)
  84. {
  85. switch (attr.offset_)
  86. {
  87. case offsetof(Node, name_):
  88. SetName(value.GetString());
  89. break;
  90. case offsetof(Node, position_):
  91. case offsetof(Node, rotation_):
  92. case offsetof(Node, scale_):
  93. Serializable::OnSetAttribute(attr, value);
  94. // If transform changes, dirty the node as applicable
  95. if (!dirty_)
  96. MarkDirty();
  97. break;
  98. default:
  99. Serializable::OnSetAttribute(attr, value);
  100. break;
  101. }
  102. }
  103. bool Node::Load(Deserializer& source)
  104. {
  105. return Load(source, true);
  106. }
  107. bool Node::Save(Serializer& dest)
  108. {
  109. if (!Serializable::Save(dest))
  110. return false;
  111. dest.WriteVLE(components_.Size());
  112. for (unsigned i = 0; i < components_.Size(); ++i)
  113. {
  114. Component* component = components_[i];
  115. // Create a separate buffer to be able to skip unknown components during deserialization
  116. VectorBuffer compBuffer;
  117. compBuffer.WriteShortStringHash(component->GetType());
  118. compBuffer.WriteUInt(component->GetID());
  119. if (!component->Save(compBuffer))
  120. return false;
  121. dest.WriteVLE(compBuffer.GetSize());
  122. dest.Write(compBuffer.GetData(), compBuffer.GetSize());
  123. }
  124. dest.WriteVLE(children_.Size());
  125. for (unsigned i = 0; i < children_.Size(); ++i)
  126. {
  127. Node* node = children_[i];
  128. dest.WriteUInt(node->GetID());
  129. if (!node->Save(dest))
  130. return false;
  131. }
  132. return true;
  133. }
  134. bool Node::LoadXML(const XMLElement& source)
  135. {
  136. return LoadXML(source, true);
  137. }
  138. bool Node::SaveXML(XMLElement& dest)
  139. {
  140. if (!Serializable::SaveXML(dest))
  141. return false;
  142. for (unsigned i = 0; i < components_.Size(); ++i)
  143. {
  144. Component* component = components_[i];
  145. XMLElement compElem = dest.CreateChildElement("component");
  146. compElem.SetString("type", component->GetTypeName());
  147. compElem.SetInt("id", component->GetID());
  148. if (!component->SaveXML(compElem))
  149. return false;
  150. }
  151. for (unsigned i = 0; i < children_.Size(); ++i)
  152. {
  153. Node* node = children_[i];
  154. XMLElement childElem = dest.CreateChildElement("node");
  155. childElem.SetInt("id", node->GetID());
  156. if (!node->SaveXML(childElem))
  157. return false;
  158. }
  159. return true;
  160. }
  161. void Node::PostLoad()
  162. {
  163. for (unsigned i = 0; i < components_.Size(); ++i)
  164. components_[i]->PostLoad();
  165. for (unsigned i = 0; i < children_.Size(); ++i)
  166. children_[i]->PostLoad();
  167. }
  168. void Node::SetName(const String& name)
  169. {
  170. name_ = name;
  171. nameHash_ = StringHash(name);
  172. }
  173. void Node::SetPosition(const Vector3& position)
  174. {
  175. position_ = position;
  176. if (!dirty_)
  177. MarkDirty();
  178. }
  179. void Node::SetRotation(const Quaternion& rotation)
  180. {
  181. rotation_ = rotation;
  182. rotateCount_ = 0;
  183. if (!dirty_)
  184. MarkDirty();
  185. }
  186. void Node::SetDirection(const Vector3& direction)
  187. {
  188. SetRotation(Quaternion(Vector3::FORWARD, direction));
  189. }
  190. void Node::SetScale(float scale)
  191. {
  192. scale_ = Vector3(scale, scale, scale);
  193. if (!dirty_)
  194. MarkDirty();
  195. }
  196. void Node::SetScale(const Vector3& scale)
  197. {
  198. scale_ = scale;
  199. if (!dirty_)
  200. MarkDirty();
  201. }
  202. void Node::SetTransform(const Vector3& position, const Quaternion& rotation)
  203. {
  204. position_ = position;
  205. rotation_ = rotation;
  206. rotateCount_ = 0;
  207. if (!dirty_)
  208. MarkDirty();
  209. }
  210. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, float scale)
  211. {
  212. position_ = position;
  213. rotation_ = rotation;
  214. scale_ = Vector3(scale, scale, scale);
  215. rotateCount_ = 0;
  216. if (!dirty_)
  217. MarkDirty();
  218. }
  219. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale)
  220. {
  221. position_ = position;
  222. rotation_ = rotation;
  223. scale_ = scale;
  224. rotateCount_ = 0;
  225. if (!dirty_)
  226. MarkDirty();
  227. }
  228. void Node::Translate(const Vector3& delta)
  229. {
  230. position_ += delta;
  231. if (!dirty_)
  232. MarkDirty();
  233. }
  234. void Node::TranslateRelative(const Vector3& delta)
  235. {
  236. position_ += rotation_ * delta;
  237. if (!dirty_)
  238. MarkDirty();
  239. }
  240. void Node::Rotate(const Quaternion& delta, bool fixedAxis)
  241. {
  242. if (!fixedAxis)
  243. rotation_ = rotation_ * delta;
  244. else
  245. rotation_ = delta * rotation_;
  246. ++rotateCount_;
  247. if (rotateCount_ >= NORMALIZE_ROTATION_EVERY)
  248. {
  249. rotation_.Normalize();
  250. rotateCount_ = 0;
  251. }
  252. if (!dirty_)
  253. MarkDirty();
  254. }
  255. void Node::Yaw(float angle, bool fixedAxis)
  256. {
  257. Rotate(Quaternion(angle, Vector3::UP), fixedAxis);
  258. }
  259. void Node::Pitch(float angle, bool fixedAxis)
  260. {
  261. Rotate(Quaternion(angle, Vector3::RIGHT), fixedAxis);
  262. }
  263. void Node::Roll(float angle, bool fixedAxis)
  264. {
  265. Rotate(Quaternion(angle, Vector3::FORWARD), fixedAxis);
  266. }
  267. void Node::Scale(float scale)
  268. {
  269. scale_ *= scale;
  270. if (!dirty_)
  271. MarkDirty();
  272. }
  273. void Node::Scale(const Vector3& scale)
  274. {
  275. scale_ *= scale;
  276. if (!dirty_)
  277. MarkDirty();
  278. }
  279. void Node::MarkDirty()
  280. {
  281. dirty_ = true;
  282. // Notify listener components first, then mark child nodes
  283. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End();)
  284. {
  285. if (*i)
  286. {
  287. (*i)->OnMarkedDirty(this);
  288. ++i;
  289. }
  290. // If listener has expired, erase from list
  291. else
  292. i = listeners_.Erase(i);
  293. }
  294. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  295. (*i)->MarkDirty();
  296. }
  297. Node* Node::CreateChild(const String& name, bool local)
  298. {
  299. Node* newNode = CreateChild(0, local);
  300. newNode->SetName(name);
  301. return newNode;
  302. }
  303. void Node::AddChild(Node* node)
  304. {
  305. // Check for illegal parent assignments, including attempt to reparent the scene
  306. if ((!node) || (node == this) || (node->parent_ == this) || (parent_ == node) || (scene_ == node))
  307. return;
  308. // Add first, then remove from old parent, to ensure the node does not get deleted
  309. children_.Push(SharedPtr<Node>(node));
  310. if (node->parent_)
  311. node->parent_->RemoveChild(node);
  312. // Add to the scene if not added yet
  313. if ((scene_) && (!node->GetScene()))
  314. scene_->NodeAdded(node);
  315. node->parent_ = this;
  316. node->MarkDirty();
  317. }
  318. void Node::RemoveChild(Node* node)
  319. {
  320. if (!node)
  321. return;
  322. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  323. {
  324. if (i->GetPtr() == node)
  325. {
  326. RemoveChild(i);
  327. return;
  328. }
  329. }
  330. }
  331. void Node::RemoveAllChildren()
  332. {
  333. while (children_.Size())
  334. RemoveChild(children_.End() - 1);
  335. }
  336. Component* Node::CreateComponent(ShortStringHash type, bool local)
  337. {
  338. return CreateComponent(type, 0, local);
  339. }
  340. Component* Node::GetOrCreateComponent(ShortStringHash type, bool local)
  341. {
  342. Component* oldComponent = GetComponent(type);
  343. if (oldComponent)
  344. return oldComponent;
  345. else
  346. return CreateComponent(type, 0, local);
  347. }
  348. void Node::RemoveComponent(Component* component)
  349. {
  350. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  351. {
  352. if (*i == component)
  353. {
  354. WeakPtr<Component> componentWeak(*i);
  355. RemoveListener(*i);
  356. if (scene_)
  357. scene_->ComponentRemoved(*i);
  358. components_.Erase(i);
  359. // If the component is still referenced elsewhere, reset its node pointer now
  360. if (componentWeak)
  361. componentWeak->SetNode(0);
  362. return;
  363. }
  364. }
  365. }
  366. void Node::RemoveAllComponents()
  367. {
  368. while (components_.Size())
  369. {
  370. Vector<SharedPtr<Component> >::Iterator i = components_.End() - 1;
  371. WeakPtr<Component> componentWeak(*i);
  372. RemoveListener(*i);
  373. if (scene_)
  374. scene_->ComponentRemoved(*i);
  375. components_.Erase(i);
  376. // If the component is still referenced elsewhere, reset its node pointer now
  377. if (componentWeak)
  378. componentWeak->SetNode(0);
  379. }
  380. }
  381. void Node::AddListener(Component* component)
  382. {
  383. if (!component)
  384. return;
  385. // Check for not adding twice
  386. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  387. {
  388. if ((*i) == component)
  389. return;
  390. }
  391. listeners_.Push(WeakPtr<Component>(component));
  392. // If the node is currently dirty, notify immediately
  393. if (dirty_)
  394. component->OnMarkedDirty(this);
  395. }
  396. void Node::RemoveListener(Component* component)
  397. {
  398. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  399. {
  400. if ((*i) == component)
  401. {
  402. listeners_.Erase(i);
  403. return;
  404. }
  405. }
  406. }
  407. void Node::Remove()
  408. {
  409. if (parent_)
  410. parent_->RemoveChild(this);
  411. }
  412. void Node::SetParent(Node* parent)
  413. {
  414. if (parent)
  415. parent->AddChild(this);
  416. }
  417. unsigned Node::GetNumChildren(bool recursive) const
  418. {
  419. if (!recursive)
  420. return children_.Size();
  421. else
  422. {
  423. unsigned allChildren = children_.Size();
  424. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  425. allChildren += (*i)->GetNumChildren(true);
  426. return allChildren;
  427. }
  428. }
  429. void Node::GetChildren(Vector<Node*>& dest, bool recursive) const
  430. {
  431. dest.Clear();
  432. if (!recursive)
  433. {
  434. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  435. dest.Push(*i);
  436. }
  437. else
  438. GetChildrenRecursive(dest);
  439. }
  440. void Node::GetChildrenWithComponent(Vector<Node*>& dest, ShortStringHash type, bool recursive) const
  441. {
  442. dest.Clear();
  443. if (!recursive)
  444. {
  445. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  446. {
  447. if ((*i)->HasComponent(type))
  448. dest.Push(*i);
  449. }
  450. }
  451. else
  452. GetChildrenWithComponentRecursive(dest, type);
  453. }
  454. Node* Node::GetChild(unsigned index) const
  455. {
  456. return index < children_.Size() ? children_[index].GetPtr() : 0;
  457. }
  458. Node* Node::GetChild(const String& name, bool recursive) const
  459. {
  460. return GetChild(StringHash(name), recursive);
  461. }
  462. Node* Node::GetChild(StringHash nameHash, bool recursive) const
  463. {
  464. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  465. {
  466. if ((*i)->GetNameHash() == nameHash)
  467. return *i;
  468. if (recursive)
  469. {
  470. Node* node = (*i)->GetChild(nameHash, true);
  471. if (node)
  472. return node;
  473. }
  474. }
  475. return 0;
  476. }
  477. void Node::GetComponents(Vector<Component*>& dest, ShortStringHash type) const
  478. {
  479. dest.Clear();
  480. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  481. {
  482. if ((*i)->GetType() == type)
  483. dest.Push(*i);
  484. }
  485. }
  486. bool Node::HasComponent(ShortStringHash type) const
  487. {
  488. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  489. {
  490. if ((*i)->GetType() == type)
  491. return true;
  492. }
  493. return false;
  494. }
  495. Component* Node::GetComponent(unsigned index) const
  496. {
  497. return index < components_.Size() ? components_[index].GetPtr() : 0;
  498. }
  499. Component* Node::GetComponent(ShortStringHash type, unsigned index) const
  500. {
  501. unsigned cmpIndex = 0;
  502. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  503. {
  504. if ((*i)->GetType() == type)
  505. {
  506. if (cmpIndex == index)
  507. return *i;
  508. ++cmpIndex;
  509. }
  510. }
  511. return 0;
  512. }
  513. void Node::SetID(unsigned id)
  514. {
  515. id_ = id;
  516. }
  517. void Node::SetScene(Scene* scene)
  518. {
  519. scene_ = scene;
  520. }
  521. void Node::SetOwner(Connection* owner)
  522. {
  523. owner_ = owner;
  524. }
  525. bool Node::Load(Deserializer& source, bool readChildren)
  526. {
  527. // Remove all children and components first in case this is not a fresh load
  528. RemoveAllChildren();
  529. RemoveAllComponents();
  530. // ID has been read at the parent level
  531. if (!Serializable::Load(source))
  532. return false;
  533. unsigned numComponents = source.ReadVLE();
  534. for (unsigned i = 0; i < numComponents; ++i)
  535. {
  536. VectorBuffer compBuffer(source, source.ReadVLE());
  537. ShortStringHash newType = compBuffer.ReadShortStringHash();
  538. Component* newComponent = CreateComponent(newType, compBuffer.ReadUInt(), false);
  539. if (newComponent)
  540. {
  541. if (!newComponent->Load(compBuffer))
  542. return false;
  543. }
  544. }
  545. if (!readChildren)
  546. return true;
  547. unsigned numChildren = source.ReadVLE();
  548. for (unsigned i = 0; i < numChildren; ++i)
  549. {
  550. Node* newNode = CreateChild(source.ReadUInt(), false);
  551. if (!newNode->Load(source))
  552. return false;
  553. }
  554. return true;
  555. }
  556. bool Node::LoadXML(const XMLElement& source, bool readChildren)
  557. {
  558. // Remove all children and components first in case this is not a fresh load
  559. RemoveAllChildren();
  560. RemoveAllComponents();
  561. if (!Serializable::LoadXML(source))
  562. return false;
  563. XMLElement compElem = source.GetChildElement("component");
  564. while (compElem)
  565. {
  566. String typeName = compElem.GetString("type");
  567. Component* newComponent = CreateComponent(ShortStringHash(compElem.GetString("type")), compElem.GetInt("id"), false);
  568. if (newComponent)
  569. {
  570. if (!newComponent->LoadXML(compElem))
  571. return false;
  572. }
  573. compElem = compElem.GetNextElement("component");
  574. }
  575. if (!readChildren)
  576. return true;
  577. XMLElement childElem = source.GetChildElement("node");
  578. while (childElem)
  579. {
  580. Node* newNode = CreateChild(childElem.GetInt("id"), false);
  581. if (!newNode->LoadXML(childElem))
  582. return false;
  583. childElem = childElem.GetNextElement("node");
  584. }
  585. return true;
  586. }
  587. Component* Node::CreateComponent(ShortStringHash type, unsigned id, bool local)
  588. {
  589. // Make sure the object in question is a component
  590. SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
  591. if (!newComponent)
  592. {
  593. LOGERROR("Could not create unknown component type " + type);
  594. return 0;
  595. }
  596. components_.Push(newComponent);
  597. // If zero ID specified, let the scene assign
  598. if (scene_)
  599. {
  600. newComponent->SetID(id ? id : scene_->GetFreeComponentID(local));
  601. scene_->ComponentAdded(newComponent);
  602. }
  603. else
  604. newComponent->SetID(id);
  605. newComponent->SetNode(this);
  606. newComponent->OnMarkedDirty(this);
  607. return newComponent;
  608. }
  609. Node* Node::CreateChild(unsigned id, bool local)
  610. {
  611. SharedPtr<Node> newNode(new Node(context_));
  612. // If zero ID specified, let the scene assign
  613. if (scene_)
  614. newNode->SetID(id ? id : scene_->GetFreeunsigned(local));
  615. else
  616. newNode->SetID(id);
  617. AddChild(newNode);
  618. return newNode;
  619. }
  620. void Node::UpdateWorldTransform()
  621. {
  622. // For now, assume that the Scene has identity transform so that we skip one matrix multiply. However in the future
  623. // we may want dynamic root nodes for large worlds
  624. if ((parent_) && (parent_ != scene_))
  625. {
  626. if (parent_->dirty_)
  627. parent_->UpdateWorldTransform();
  628. worldTransform_ = parent_->worldTransform_ * Matrix4x3(position_, rotation_, scale_);
  629. }
  630. else
  631. worldTransform_ = Matrix4x3(position_, rotation_, scale_);
  632. dirty_ = false;
  633. }
  634. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  635. {
  636. (*i)->parent_ = 0;
  637. (*i)->MarkDirty();
  638. children_.Erase(i);
  639. }
  640. void Node::GetChildrenRecursive(Vector<Node*>& dest) const
  641. {
  642. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  643. {
  644. Node* node = *i;
  645. dest.Push(node);
  646. if (!node->children_.Empty())
  647. node->GetChildrenRecursive(dest);
  648. }
  649. }
  650. void Node::GetChildrenWithComponentRecursive(Vector<Node*>& dest, ShortStringHash type) const
  651. {
  652. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  653. {
  654. Node* node = *i;
  655. if (node->HasComponent(type))
  656. dest.Push(node);
  657. if (!node->children_.Empty())
  658. node->GetChildrenRecursive(dest);
  659. }
  660. }