Node.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 "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_(Matrix3x4::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. REF_ACCESSOR_ATTRIBUTE(Node, VAR_STRING, "Name", GetName, SetName, String, String(), AM_DEFAULT);
  61. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_DEFAULT | AM_LATESTDATA);
  62. REF_ACCESSOR_ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_DEFAULT | AM_LATESTDATA);
  63. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Scale", GetScale, SetScale, Vector3, Vector3::UNITY, AM_DEFAULT);
  64. ACCESSOR_ATTRIBUTE(Node, VAR_BUFFER, "Parent Node", GetParentAttr, SetParentAttr, PODVector<unsigned char>, PODVector<unsigned char>(), AM_NET | AM_NOEDIT);
  65. ATTRIBUTE(Node, VAR_VARIANTMAP, "Variables", vars_, VariantMap(), AM_FILE);
  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. void Node::OnFinishUpdate()
  85. {
  86. for (unsigned i = 0; i < components_.Size(); ++i)
  87. components_[i]->OnFinishUpdate();
  88. for (unsigned i = 0; i < children_.Size(); ++i)
  89. children_[i]->OnFinishUpdate();
  90. }
  91. bool Node::Load(Deserializer& source)
  92. {
  93. return Load(source, true);
  94. }
  95. bool Node::Save(Serializer& dest)
  96. {
  97. if (!Serializable::Save(dest))
  98. return false;
  99. dest.WriteVLE(components_.Size());
  100. for (unsigned i = 0; i < components_.Size(); ++i)
  101. {
  102. Component* component = components_[i];
  103. // Create a separate buffer to be able to skip unknown components during deserialization
  104. VectorBuffer compBuffer;
  105. compBuffer.WriteShortStringHash(component->GetType());
  106. compBuffer.WriteUInt(component->GetID());
  107. if (!component->Save(compBuffer))
  108. return false;
  109. dest.WriteVLE(compBuffer.GetSize());
  110. dest.Write(compBuffer.GetData(), compBuffer.GetSize());
  111. }
  112. dest.WriteVLE(children_.Size());
  113. for (unsigned i = 0; i < children_.Size(); ++i)
  114. {
  115. Node* node = children_[i];
  116. dest.WriteUInt(node->GetID());
  117. if (!node->Save(dest))
  118. return false;
  119. }
  120. return true;
  121. }
  122. bool Node::LoadXML(const XMLElement& source)
  123. {
  124. return LoadXML(source, true);
  125. }
  126. bool Node::SaveXML(XMLElement& dest)
  127. {
  128. if (!Serializable::SaveXML(dest))
  129. return false;
  130. for (unsigned i = 0; i < components_.Size(); ++i)
  131. {
  132. Component* component = components_[i];
  133. XMLElement compElem = dest.CreateChild("component");
  134. compElem.SetString("type", component->GetTypeName());
  135. compElem.SetInt("id", component->GetID());
  136. if (!component->SaveXML(compElem))
  137. return false;
  138. }
  139. for (unsigned i = 0; i < children_.Size(); ++i)
  140. {
  141. Node* node = children_[i];
  142. XMLElement childElem = dest.CreateChild("node");
  143. childElem.SetInt("id", node->GetID());
  144. if (!node->SaveXML(childElem))
  145. return false;
  146. }
  147. return true;
  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::SetID(unsigned id)
  505. {
  506. id_ = id;
  507. }
  508. void Node::SetScene(Scene* scene)
  509. {
  510. scene_ = scene;
  511. }
  512. void Node::SetOwner(Connection* owner)
  513. {
  514. owner_ = owner;
  515. }
  516. void Node::SetParentAttr(PODVector<unsigned char> value)
  517. {
  518. Scene* scene = GetScene();
  519. if (!scene)
  520. return;
  521. MemoryBuffer buf(value);
  522. unsigned baseNodeID = buf.ReadVLE();
  523. if (!baseNodeID)
  524. return;
  525. if (baseNodeID < FIRST_LOCAL_ID)
  526. SetParent(scene->GetNodeByID(baseNodeID));
  527. else
  528. {
  529. Node* baseNode = scene->GetNodeByID(baseNodeID);
  530. if (baseNode)
  531. SetParent(baseNode->GetChild(buf.ReadStringHash(), true));
  532. }
  533. }
  534. PODVector<unsigned char> Node::GetParentAttr() const
  535. {
  536. VectorBuffer buf;
  537. Scene* scene = GetScene();
  538. if (scene && parent_)
  539. {
  540. unsigned parentID = parent_->GetID();
  541. if (parentID < FIRST_LOCAL_ID)
  542. buf.WriteVLE(parentID);
  543. else
  544. {
  545. // Parent is local: traverse hierarchy to find a non-local base node
  546. // This iteration always stops due to the scene (root) being non-local
  547. Node* current = parent_;
  548. while (current->GetID() >= FIRST_LOCAL_ID)
  549. current = current->GetParent();
  550. buf.WriteVLE(current->GetID());
  551. buf.WriteStringHash(parent_->GetNameHash());
  552. }
  553. }
  554. else
  555. buf.WriteVLE(0);
  556. return buf.GetBuffer();
  557. }
  558. bool Node::Load(Deserializer& source, bool readChildren)
  559. {
  560. // Remove all children and components first in case this is not a fresh load
  561. RemoveAllChildren();
  562. RemoveAllComponents();
  563. // ID has been read at the parent level
  564. if (!Serializable::Load(source))
  565. return false;
  566. unsigned numComponents = source.ReadVLE();
  567. for (unsigned i = 0; i < numComponents; ++i)
  568. {
  569. VectorBuffer compBuffer(source, source.ReadVLE());
  570. ShortStringHash newType = compBuffer.ReadShortStringHash();
  571. Component* newComponent = CreateComponent(newType, compBuffer.ReadUInt(), false);
  572. if (newComponent)
  573. {
  574. if (!newComponent->Load(compBuffer))
  575. return false;
  576. }
  577. }
  578. if (!readChildren)
  579. return true;
  580. unsigned numChildren = source.ReadVLE();
  581. for (unsigned i = 0; i < numChildren; ++i)
  582. {
  583. Node* newNode = CreateChild(source.ReadUInt(), false);
  584. if (!newNode->Load(source))
  585. return false;
  586. }
  587. return true;
  588. }
  589. bool Node::LoadXML(const XMLElement& source, bool readChildren)
  590. {
  591. // Remove all children and components first in case this is not a fresh load
  592. RemoveAllChildren();
  593. RemoveAllComponents();
  594. if (!Serializable::LoadXML(source))
  595. return false;
  596. XMLElement compElem = source.GetChild("component");
  597. while (compElem)
  598. {
  599. String typeName = compElem.GetString("type");
  600. Component* newComponent = CreateComponent(ShortStringHash(compElem.GetString("type")), compElem.GetInt("id"), false);
  601. if (newComponent)
  602. {
  603. if (!newComponent->LoadXML(compElem))
  604. return false;
  605. }
  606. compElem = compElem.GetNext("component");
  607. }
  608. if (!readChildren)
  609. return true;
  610. XMLElement childElem = source.GetChild("node");
  611. while (childElem)
  612. {
  613. Node* newNode = CreateChild(childElem.GetInt("id"), false);
  614. if (!newNode->LoadXML(childElem))
  615. return false;
  616. childElem = childElem.GetNext("node");
  617. }
  618. return true;
  619. }
  620. Component* Node::CreateComponent(ShortStringHash type, unsigned id, bool local)
  621. {
  622. // Make sure the object in question is a component
  623. SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
  624. if (!newComponent)
  625. {
  626. LOGERROR("Could not create unknown component type " + type);
  627. return 0;
  628. }
  629. components_.Push(newComponent);
  630. // If zero ID specified, let the scene assign
  631. if (scene_)
  632. {
  633. newComponent->SetID(id ? id : scene_->GetFreeComponentID(local));
  634. scene_->ComponentAdded(newComponent);
  635. }
  636. else
  637. newComponent->SetID(id);
  638. newComponent->SetNode(this);
  639. newComponent->OnMarkedDirty(this);
  640. return newComponent;
  641. }
  642. Node* Node::CreateChild(unsigned id, bool local)
  643. {
  644. SharedPtr<Node> newNode(new Node(context_));
  645. // If zero ID specified, let the scene assign
  646. if (scene_)
  647. newNode->SetID(id ? id : scene_->GetFreeNodeID(local));
  648. else
  649. newNode->SetID(id);
  650. AddChild(newNode);
  651. return newNode;
  652. }
  653. void Node::UpdateWorldTransform()
  654. {
  655. // For now, assume that the Scene has identity transform so that we skip one matrix multiply. However in the future
  656. // we may want dynamic root nodes for large worlds
  657. if (parent_ && parent_ != scene_)
  658. {
  659. if (parent_->dirty_)
  660. parent_->UpdateWorldTransform();
  661. worldTransform_ = parent_->worldTransform_ * Matrix3x4(position_, rotation_, scale_);
  662. }
  663. else
  664. worldTransform_ = Matrix3x4(position_, rotation_, scale_);
  665. dirty_ = false;
  666. }
  667. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  668. {
  669. (*i)->parent_ = 0;
  670. (*i)->MarkDirty();
  671. children_.Erase(i);
  672. }
  673. void Node::GetChildrenRecursive(PODVector<Node*>& dest) const
  674. {
  675. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  676. {
  677. Node* node = *i;
  678. dest.Push(node);
  679. if (!node->children_.Empty())
  680. node->GetChildrenRecursive(dest);
  681. }
  682. }
  683. void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, ShortStringHash type) const
  684. {
  685. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  686. {
  687. Node* node = *i;
  688. if (node->HasComponent(type))
  689. dest.Push(node);
  690. if (!node->children_.Empty())
  691. node->GetChildrenRecursive(dest);
  692. }
  693. }