Node.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Component.h"
  24. #include "Context.h"
  25. #include "Log.h"
  26. #include "MemoryBuffer.h"
  27. #include "Profiler.h"
  28. #include "ReplicationState.h"
  29. #include "Scene.h"
  30. #include "SceneEvents.h"
  31. #include "SmoothedTransform.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. Node::Node(Context* context) :
  37. Serializable(context),
  38. worldTransform_(Matrix3x4::IDENTITY),
  39. dirty_(false),
  40. networkUpdate_(false),
  41. enabled_(true),
  42. parent_(0),
  43. scene_(0),
  44. id_(0),
  45. position_(Vector3::ZERO),
  46. rotation_(Quaternion::IDENTITY),
  47. scale_(Vector3::ONE),
  48. worldRotation_(Quaternion::IDENTITY),
  49. owner_(0)
  50. {
  51. }
  52. Node::~Node()
  53. {
  54. RemoveAllChildren();
  55. RemoveAllComponents();
  56. // Remove from the scene
  57. if (scene_)
  58. scene_->NodeRemoved(this);
  59. }
  60. void Node::RegisterObject(Context* context)
  61. {
  62. context->RegisterFactory<Node>();
  63. ACCESSOR_ATTRIBUTE(Node, VAR_BOOL, "Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  64. REF_ACCESSOR_ATTRIBUTE(Node, VAR_STRING, "Name", GetName, SetName, String, String::EMPTY, AM_DEFAULT);
  65. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Position", GetPosition, SetPosition, Vector3, Vector3::ZERO, AM_FILE);
  66. REF_ACCESSOR_ATTRIBUTE(Node, VAR_QUATERNION, "Rotation", GetRotation, SetRotation, Quaternion, Quaternion::IDENTITY, AM_FILE);
  67. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Scale", GetScale, SetScale, Vector3, Vector3::ONE, AM_DEFAULT);
  68. ATTRIBUTE(Node, VAR_VARIANTMAP, "Variables", vars_, Variant::emptyVariantMap, AM_FILE); // Network replication of vars uses custom data
  69. REF_ACCESSOR_ATTRIBUTE(Node, VAR_VECTOR3, "Network Position", GetNetPositionAttr, SetNetPositionAttr, Vector3, Vector3::ZERO, AM_NET | AM_LATESTDATA | AM_NOEDIT);
  70. REF_ACCESSOR_ATTRIBUTE(Node, VAR_BUFFER, "Network Rotation", GetNetRotationAttr, SetNetRotationAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_LATESTDATA | AM_NOEDIT);
  71. REF_ACCESSOR_ATTRIBUTE(Node, VAR_BUFFER, "Network Parent Node", GetNetParentAttr, SetNetParentAttr, PODVector<unsigned char>, Variant::emptyBuffer, AM_NET | AM_NOEDIT);
  72. }
  73. void Node::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  74. {
  75. Serializable::OnSetAttribute(attr, src);
  76. MarkNetworkUpdate();
  77. }
  78. bool Node::Load(Deserializer& source, bool setInstanceDefault)
  79. {
  80. SceneResolver resolver;
  81. // Read own ID. Will not be applied, only stored for resolving possible references
  82. unsigned nodeID = source.ReadInt();
  83. resolver.AddNode(nodeID, this);
  84. // Read attributes, components and child nodes
  85. bool success = Load(source, resolver);
  86. if (success)
  87. {
  88. resolver.Resolve();
  89. ApplyAttributes();
  90. }
  91. return success;
  92. }
  93. bool Node::Save(Serializer& dest) const
  94. {
  95. // Write node ID
  96. if (!dest.WriteUInt(id_))
  97. return false;
  98. // Write attributes
  99. if (!Serializable::Save(dest))
  100. return false;
  101. // Write components
  102. dest.WriteVLE(GetNumPersistentComponents());
  103. for (unsigned i = 0; i < components_.Size(); ++i)
  104. {
  105. Component* component = components_[i];
  106. if (component->IsTemporary())
  107. continue;
  108. // Create a separate buffer to be able to skip unknown components during deserialization
  109. VectorBuffer compBuffer;
  110. if (!component->Save(compBuffer))
  111. return false;
  112. dest.WriteVLE(compBuffer.GetSize());
  113. dest.Write(compBuffer.GetData(), compBuffer.GetSize());
  114. }
  115. // Write child nodes
  116. dest.WriteVLE(GetNumPersistentChildren());
  117. for (unsigned i = 0; i < children_.Size(); ++i)
  118. {
  119. Node* node = children_[i];
  120. if (node->IsTemporary())
  121. continue;
  122. if (!node->Save(dest))
  123. return false;
  124. }
  125. return true;
  126. }
  127. bool Node::LoadXML(const XMLElement& source, bool setInstanceDefault)
  128. {
  129. SceneResolver resolver;
  130. // Read own ID. Will not be applied, only stored for resolving possible references
  131. unsigned nodeID = source.GetInt("id");
  132. resolver.AddNode(nodeID, this);
  133. // Read attributes, components and child nodes
  134. bool success = LoadXML(source, resolver);
  135. if (success)
  136. {
  137. resolver.Resolve();
  138. ApplyAttributes();
  139. }
  140. return success;
  141. }
  142. bool Node::SaveXML(XMLElement& dest) const
  143. {
  144. // Write node ID
  145. if (!dest.SetInt("id", id_))
  146. return false;
  147. // Write attributes
  148. if (!Serializable::SaveXML(dest))
  149. return false;
  150. // Write components
  151. for (unsigned i = 0; i < components_.Size(); ++i)
  152. {
  153. Component* component = components_[i];
  154. if (component->IsTemporary())
  155. continue;
  156. XMLElement compElem = dest.CreateChild("component");
  157. if (!component->SaveXML(compElem))
  158. return false;
  159. }
  160. // Write child nodes
  161. for (unsigned i = 0; i < children_.Size(); ++i)
  162. {
  163. Node* node = children_[i];
  164. if (node->IsTemporary())
  165. continue;
  166. XMLElement childElem = dest.CreateChild("node");
  167. if (!node->SaveXML(childElem))
  168. return false;
  169. }
  170. return true;
  171. }
  172. void Node::ApplyAttributes()
  173. {
  174. for (unsigned i = 0; i < components_.Size(); ++i)
  175. components_[i]->ApplyAttributes();
  176. for (unsigned i = 0; i < children_.Size(); ++i)
  177. children_[i]->ApplyAttributes();
  178. }
  179. void Node::AddReplicationState(NodeReplicationState* state)
  180. {
  181. if (!networkState_)
  182. AllocateNetworkState();
  183. networkState_->replicationStates_.Push(state);
  184. }
  185. bool Node::SaveXML(Serializer& dest) const
  186. {
  187. SharedPtr<XMLFile> xml(new XMLFile(context_));
  188. XMLElement rootElem = xml->CreateRoot("node");
  189. if (!SaveXML(rootElem))
  190. return false;
  191. return xml->Save(dest);
  192. }
  193. void Node::SetName(const String& name)
  194. {
  195. name_ = name;
  196. nameHash_ = name_;
  197. MarkNetworkUpdate();
  198. // Send change event
  199. if (scene_)
  200. {
  201. using namespace NodeNameChanged;
  202. VariantMap eventData;
  203. eventData[P_SCENE] = (void*)scene_;
  204. eventData[P_NODE] = (void*)this;
  205. scene_->SendEvent(E_NODENAMECHANGED, eventData);
  206. }
  207. }
  208. void Node::SetPosition(const Vector3& position)
  209. {
  210. position_ = position;
  211. MarkDirty();
  212. MarkNetworkUpdate();
  213. }
  214. void Node::SetRotation(const Quaternion& rotation)
  215. {
  216. rotation_ = rotation;
  217. MarkDirty();
  218. MarkNetworkUpdate();
  219. }
  220. void Node::SetDirection(const Vector3& direction)
  221. {
  222. SetRotation(Quaternion(Vector3::FORWARD, direction));
  223. }
  224. void Node::SetScale(float scale)
  225. {
  226. SetScale(Vector3(scale, scale, scale));
  227. }
  228. void Node::SetScale(const Vector3& scale)
  229. {
  230. scale_ = scale.Abs();
  231. MarkDirty();
  232. MarkNetworkUpdate();
  233. }
  234. void Node::SetTransform(const Vector3& position, const Quaternion& rotation)
  235. {
  236. position_ = position;
  237. rotation_ = rotation;
  238. MarkDirty();
  239. MarkNetworkUpdate();
  240. }
  241. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, float scale)
  242. {
  243. SetTransform(position, rotation, Vector3(scale, scale, scale));
  244. }
  245. void Node::SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale)
  246. {
  247. position_ = position;
  248. rotation_ = rotation;
  249. scale_ = scale;
  250. MarkDirty();
  251. MarkNetworkUpdate();
  252. }
  253. void Node::SetWorldPosition(const Vector3& position)
  254. {
  255. SetPosition(parent_ ? parent_->GetWorldTransform().Inverse() * position : position);
  256. }
  257. void Node::SetWorldRotation(const Quaternion& rotation)
  258. {
  259. SetRotation(parent_ ? parent_->GetWorldRotation().Inverse() * rotation : rotation);
  260. }
  261. void Node::SetWorldDirection(const Vector3& direction)
  262. {
  263. Vector3 localDirection = parent_ ? parent_->GetWorldRotation().Inverse() * direction : direction;
  264. SetRotation(Quaternion(Vector3::FORWARD, localDirection));
  265. }
  266. void Node::SetWorldScale(float scale)
  267. {
  268. if (!parent_)
  269. SetScale(scale);
  270. else
  271. {
  272. Vector3 parentWorldScale = parent_->GetWorldScale();
  273. SetScale(Vector3(scale / parentWorldScale.x_, scale / parentWorldScale.y_, scale / parentWorldScale.z_));
  274. }
  275. }
  276. void Node::SetWorldScale(const Vector3& scale)
  277. {
  278. if (!parent_)
  279. SetScale(scale);
  280. else
  281. SetScale(scale / parent_->GetWorldScale());
  282. }
  283. void Node::SetWorldTransform(const Vector3& position, const Quaternion& rotation)
  284. {
  285. SetWorldPosition(position);
  286. SetWorldRotation(rotation);
  287. }
  288. void Node::SetWorldTransform(const Vector3& position, const Quaternion& rotation, float scale)
  289. {
  290. SetWorldPosition(position);
  291. SetWorldRotation(rotation);
  292. SetWorldScale(scale);
  293. }
  294. void Node::SetWorldTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale)
  295. {
  296. SetWorldPosition(position);
  297. SetWorldRotation(rotation);
  298. SetWorldScale(scale);
  299. }
  300. void Node::Translate(const Vector3& delta)
  301. {
  302. position_ += delta;
  303. MarkDirty();
  304. MarkNetworkUpdate();
  305. }
  306. void Node::TranslateRelative(const Vector3& delta)
  307. {
  308. position_ += rotation_ * delta;
  309. MarkDirty();
  310. MarkNetworkUpdate();
  311. }
  312. void Node::Rotate(const Quaternion& delta, bool fixedAxis)
  313. {
  314. if (!fixedAxis)
  315. rotation_ = (rotation_ * delta).Normalized();
  316. else
  317. rotation_ = (delta * rotation_).Normalized();
  318. MarkDirty();
  319. MarkNetworkUpdate();
  320. }
  321. void Node::Yaw(float angle, bool fixedAxis)
  322. {
  323. Rotate(Quaternion(angle, Vector3::UP), fixedAxis);
  324. }
  325. void Node::Pitch(float angle, bool fixedAxis)
  326. {
  327. Rotate(Quaternion(angle, Vector3::RIGHT), fixedAxis);
  328. }
  329. void Node::Roll(float angle, bool fixedAxis)
  330. {
  331. Rotate(Quaternion(angle, Vector3::FORWARD), fixedAxis);
  332. }
  333. void Node::LookAt(const Vector3& target, const Vector3& upAxis)
  334. {
  335. Vector3 targetZ = (target - GetWorldPosition()).Normalized();
  336. Vector3 targetX = upAxis.CrossProduct(targetZ).Normalized();
  337. Vector3 targetY = targetZ.CrossProduct(targetX).Normalized();
  338. Quaternion rotation(targetX, targetY, targetZ);
  339. SetRotation(parent_ ? parent_->GetWorldRotation().Inverse() * rotation : rotation);
  340. }
  341. void Node::Scale(float scale)
  342. {
  343. Scale(Vector3(scale, scale, scale));
  344. }
  345. void Node::Scale(const Vector3& scale)
  346. {
  347. scale_ *= scale;
  348. MarkDirty();
  349. MarkNetworkUpdate();
  350. }
  351. void Node::SetEnabled(bool enable)
  352. {
  353. SetEnabled(enable, false);
  354. }
  355. void Node::SetEnabled(bool enable, bool recursive)
  356. {
  357. // The enabled state of the whole scene can not be changed. SetUpdateEnabled() is used instead to start/stop updates.
  358. if (GetType() == Scene::GetTypeStatic())
  359. {
  360. LOGERROR("Can not change enabled state of the Scene");
  361. return;
  362. }
  363. if (enable != enabled_)
  364. {
  365. enabled_ = enable;
  366. MarkNetworkUpdate();
  367. // Send change event
  368. if (scene_)
  369. {
  370. using namespace NodeEnabledChanged;
  371. VariantMap eventData;
  372. eventData[P_SCENE] = (void*)scene_;
  373. eventData[P_NODE] = (void*)this;
  374. scene_->SendEvent(E_NODEENABLEDCHANGED, eventData);
  375. }
  376. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  377. {
  378. (*i)->OnSetEnabled();
  379. // Send change event for the component
  380. if (scene_)
  381. {
  382. using namespace ComponentEnabledChanged;
  383. VariantMap eventData;
  384. eventData[P_SCENE] = (void*)scene_;
  385. eventData[P_NODE] = (void*)this;
  386. eventData[P_COMPONENT] = (void*)(*i);
  387. scene_->SendEvent(E_COMPONENTENABLEDCHANGED, eventData);
  388. }
  389. }
  390. }
  391. if (recursive)
  392. {
  393. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  394. (*i)->SetEnabled(enable, recursive);
  395. }
  396. }
  397. void Node::SetOwner(Connection* owner)
  398. {
  399. owner_ = owner;
  400. }
  401. void Node::MarkDirty()
  402. {
  403. if (dirty_)
  404. return;
  405. dirty_ = true;
  406. // Notify listener components first, then mark child nodes
  407. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End();)
  408. {
  409. if (*i)
  410. {
  411. (*i)->OnMarkedDirty(this);
  412. ++i;
  413. }
  414. // If listener has expired, erase from list
  415. else
  416. i = listeners_.Erase(i);
  417. }
  418. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  419. (*i)->MarkDirty();
  420. }
  421. Node* Node::CreateChild(const String& name, CreateMode mode, unsigned id)
  422. {
  423. Node* newNode = CreateChild(id, mode);
  424. newNode->SetName(name);
  425. return newNode;
  426. }
  427. void Node::AddChild(Node* node)
  428. {
  429. // Check for illegal or redundant parent assignment
  430. if (!node || node == this || node->parent_ == this)
  431. return;
  432. // Check for possible cyclic parent assignment
  433. Node* parent = parent_;
  434. while (parent)
  435. {
  436. if (parent == node)
  437. return;
  438. parent = parent->parent_;
  439. }
  440. // Add first, then remove from old parent, to ensure the node does not get deleted
  441. children_.Push(SharedPtr<Node>(node));
  442. node->Remove();
  443. // Add to the scene if not added yet
  444. if (scene_ && node->GetScene() != scene_)
  445. scene_->NodeAdded(node);
  446. node->parent_ = this;
  447. node->MarkDirty();
  448. node->MarkNetworkUpdate();
  449. // Send change event
  450. if (scene_)
  451. {
  452. using namespace NodeAdded;
  453. VariantMap eventData;
  454. eventData[P_SCENE] = (void*)scene_;
  455. eventData[P_PARENT] = (void*)this;
  456. eventData[P_NODE] = (void*)node;
  457. scene_->SendEvent(E_NODEADDED, eventData);
  458. }
  459. }
  460. void Node::RemoveChild(Node* node)
  461. {
  462. if (!node)
  463. return;
  464. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  465. {
  466. if (*i == node)
  467. {
  468. RemoveChild(i);
  469. return;
  470. }
  471. }
  472. }
  473. void Node::RemoveAllChildren()
  474. {
  475. while (children_.Size())
  476. RemoveChild(--children_.End());
  477. }
  478. Component* Node::CreateComponent(ShortStringHash type, CreateMode mode, unsigned id)
  479. {
  480. // Check that creation succeeds and that the object in fact is a component
  481. SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
  482. if (!newComponent)
  483. {
  484. LOGERROR("Could not create unknown component type " + type.ToString());
  485. return 0;
  486. }
  487. AddComponent(newComponent, id, mode);
  488. return newComponent;
  489. }
  490. Component* Node::GetOrCreateComponent(ShortStringHash type, CreateMode mode, unsigned id)
  491. {
  492. Component* oldComponent = GetComponent(type);
  493. if (oldComponent)
  494. return oldComponent;
  495. else
  496. return CreateComponent(type, mode, id);
  497. }
  498. void Node::RemoveComponent(Component* component)
  499. {
  500. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  501. {
  502. if (*i == component)
  503. {
  504. RemoveComponent(i);
  505. // Mark node dirty in all replication states
  506. MarkReplicationDirty();
  507. return;
  508. }
  509. }
  510. }
  511. void Node::RemoveComponent(ShortStringHash type)
  512. {
  513. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  514. {
  515. if ((*i)->GetType() == type)
  516. {
  517. RemoveComponent(i);
  518. // Mark node dirty in all replication states
  519. MarkReplicationDirty();
  520. return;
  521. }
  522. }
  523. }
  524. void Node::RemoveAllComponents()
  525. {
  526. if (components_.Empty())
  527. return;
  528. while (components_.Size())
  529. RemoveComponent(--components_.End());
  530. // Mark node dirty in all replication states
  531. MarkReplicationDirty();
  532. }
  533. Node* Node::Clone(CreateMode mode)
  534. {
  535. // The scene itself can not be cloned
  536. if (this == scene_ || !parent_)
  537. {
  538. LOGERROR("Can not clone node without a parent");
  539. return 0;
  540. }
  541. PROFILE(CloneNode);
  542. SceneResolver resolver;
  543. Node* clone = CloneRecursive(parent_, resolver, mode);
  544. resolver.Resolve();
  545. clone->ApplyAttributes();
  546. return clone;
  547. }
  548. void Node::Remove()
  549. {
  550. if (parent_)
  551. parent_->RemoveChild(this);
  552. }
  553. void Node::SetParent(Node* parent)
  554. {
  555. if (parent)
  556. {
  557. Matrix3x4 oldWorldTransform = GetWorldTransform();
  558. parent->AddChild(this);
  559. Matrix3x4 newTransform = parent->GetWorldTransform().Inverse() * oldWorldTransform;
  560. SetTransform(newTransform.Translation(), newTransform.Rotation(), newTransform.Scale());
  561. }
  562. }
  563. void Node::SetVar(ShortStringHash key, const Variant& value)
  564. {
  565. vars_[key] = value;
  566. MarkNetworkUpdate();
  567. }
  568. void Node::AddListener(Component* component)
  569. {
  570. if (!component)
  571. return;
  572. // Check for not adding twice
  573. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  574. {
  575. if (*i == component)
  576. return;
  577. }
  578. listeners_.Push(WeakPtr<Component>(component));
  579. // If the node is currently dirty, notify immediately
  580. if (dirty_)
  581. component->OnMarkedDirty(this);
  582. }
  583. void Node::RemoveListener(Component* component)
  584. {
  585. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  586. {
  587. if (*i == component)
  588. {
  589. listeners_.Erase(i);
  590. return;
  591. }
  592. }
  593. }
  594. Vector3 Node::LocalToWorld(const Vector3& position) const
  595. {
  596. return GetWorldTransform() * position;
  597. }
  598. Vector3 Node::LocalToWorld(const Vector4& vector) const
  599. {
  600. return GetWorldTransform() * vector;
  601. }
  602. Vector3 Node::WorldToLocal(const Vector3& position) const
  603. {
  604. return GetWorldTransform().Inverse() * position;
  605. }
  606. Vector3 Node::WorldToLocal(const Vector4& vector) const
  607. {
  608. return GetWorldTransform().Inverse() * vector;
  609. }
  610. unsigned Node::GetNumChildren(bool recursive) const
  611. {
  612. if (!recursive)
  613. return children_.Size();
  614. else
  615. {
  616. unsigned allChildren = children_.Size();
  617. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  618. allChildren += (*i)->GetNumChildren(true);
  619. return allChildren;
  620. }
  621. }
  622. void Node::GetChildren(PODVector<Node*>& dest, bool recursive) const
  623. {
  624. dest.Clear();
  625. if (!recursive)
  626. {
  627. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  628. dest.Push(*i);
  629. }
  630. else
  631. GetChildrenRecursive(dest);
  632. }
  633. void Node::GetChildrenWithComponent(PODVector<Node*>& dest, ShortStringHash type, bool recursive) const
  634. {
  635. dest.Clear();
  636. if (!recursive)
  637. {
  638. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  639. {
  640. if ((*i)->HasComponent(type))
  641. dest.Push(*i);
  642. }
  643. }
  644. else
  645. GetChildrenWithComponentRecursive(dest, type);
  646. }
  647. Node* Node::GetChild(unsigned index) const
  648. {
  649. return index < children_.Size() ? children_[index].Get() : 0;
  650. }
  651. Node* Node::GetChild(const String& name, bool recursive) const
  652. {
  653. return GetChild(StringHash(name), recursive);
  654. }
  655. Node* Node::GetChild(const char* name, bool recursive) const
  656. {
  657. return GetChild(StringHash(name), recursive);
  658. }
  659. Node* Node::GetChild(StringHash nameHash, bool recursive) const
  660. {
  661. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  662. {
  663. if ((*i)->GetNameHash() == nameHash)
  664. return *i;
  665. if (recursive)
  666. {
  667. Node* node = (*i)->GetChild(nameHash, true);
  668. if (node)
  669. return node;
  670. }
  671. }
  672. return 0;
  673. }
  674. unsigned Node::GetNumNetworkComponents() const
  675. {
  676. unsigned num = 0;
  677. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  678. {
  679. if ((*i)->GetID() < FIRST_LOCAL_ID)
  680. ++num;
  681. }
  682. return num;
  683. }
  684. void Node::GetComponents(PODVector<Component*>& dest, ShortStringHash type, bool recursive) const
  685. {
  686. dest.Clear();
  687. if (!recursive)
  688. {
  689. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  690. {
  691. if ((*i)->GetType() == type)
  692. dest.Push(*i);
  693. }
  694. }
  695. else
  696. GetComponentsRecursive(dest, type);
  697. }
  698. bool Node::HasComponent(ShortStringHash type) const
  699. {
  700. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  701. {
  702. if ((*i)->GetType() == type)
  703. return true;
  704. }
  705. return false;
  706. }
  707. const Variant& Node::GetVar(ShortStringHash key) const
  708. {
  709. VariantMap::ConstIterator i = vars_.Find(key);
  710. return i != vars_.End() ? i->second_ : Variant::EMPTY;
  711. }
  712. Component* Node::GetComponent(ShortStringHash type) const
  713. {
  714. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  715. {
  716. if ((*i)->GetType() == type)
  717. return *i;
  718. }
  719. return 0;
  720. }
  721. void Node::SetID(unsigned id)
  722. {
  723. id_ = id;
  724. }
  725. void Node::SetScene(Scene* scene)
  726. {
  727. scene_ = scene;
  728. }
  729. void Node::ResetScene()
  730. {
  731. SetID(0);
  732. SetScene(0);
  733. SetOwner(0);
  734. }
  735. void Node::SetNetPositionAttr(const Vector3& value)
  736. {
  737. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  738. if (transform)
  739. transform->SetTargetPosition(value);
  740. else
  741. SetPosition(value);
  742. }
  743. void Node::SetNetRotationAttr(const PODVector<unsigned char>& value)
  744. {
  745. MemoryBuffer buf(value);
  746. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  747. if (transform)
  748. transform->SetTargetRotation(buf.ReadPackedQuaternion());
  749. else
  750. SetRotation(buf.ReadPackedQuaternion());
  751. }
  752. void Node::SetNetParentAttr(const PODVector<unsigned char>& value)
  753. {
  754. Scene* scene = GetScene();
  755. if (!scene)
  756. return;
  757. MemoryBuffer buf(value);
  758. // If nothing in the buffer, parent is the root node
  759. if (buf.IsEof())
  760. {
  761. scene->AddChild(this);
  762. return;
  763. }
  764. unsigned baseNodeID = buf.ReadNetID();
  765. Node* baseNode = scene->GetNode(baseNodeID);
  766. if (!baseNode)
  767. {
  768. LOGWARNING("Failed to find parent node " + String(baseNodeID));
  769. return;
  770. }
  771. // If buffer contains just an ID, the parent is replicated and we are done
  772. if (buf.IsEof())
  773. baseNode->AddChild(this);
  774. else
  775. {
  776. // Else the parent is local and we must find it recursively by name hash
  777. StringHash nameHash = buf.ReadStringHash();
  778. Node* parentNode = baseNode->GetChild(nameHash, true);
  779. if (!parentNode)
  780. LOGWARNING("Failed to find parent node with name hash " + nameHash.ToString());
  781. else
  782. parentNode->AddChild(this);
  783. }
  784. }
  785. const Vector3& Node::GetNetPositionAttr() const
  786. {
  787. return position_;
  788. }
  789. const PODVector<unsigned char>& Node::GetNetRotationAttr() const
  790. {
  791. attrBuffer_.Clear();
  792. attrBuffer_.WritePackedQuaternion(rotation_);
  793. return attrBuffer_.GetBuffer();
  794. }
  795. const PODVector<unsigned char>& Node::GetNetParentAttr() const
  796. {
  797. attrBuffer_.Clear();
  798. Scene* scene = GetScene();
  799. if (scene && parent_ && parent_ != scene)
  800. {
  801. // If parent is replicated, can write the ID directly
  802. unsigned parentID = parent_->GetID();
  803. if (parentID < FIRST_LOCAL_ID)
  804. attrBuffer_.WriteNetID(parentID);
  805. else
  806. {
  807. // Parent is local: traverse hierarchy to find a non-local base node
  808. // This iteration always stops due to the scene (root) being non-local
  809. Node* current = parent_;
  810. while (current->GetID() >= FIRST_LOCAL_ID)
  811. current = current->GetParent();
  812. // Then write the base node ID and the parent's name hash
  813. attrBuffer_.WriteNetID(current->GetID());
  814. attrBuffer_.WriteStringHash(parent_->GetNameHash());
  815. }
  816. }
  817. return attrBuffer_.GetBuffer();
  818. }
  819. bool Node::Load(Deserializer& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  820. {
  821. // Remove all children and components first in case this is not a fresh load
  822. RemoveAllChildren();
  823. RemoveAllComponents();
  824. // ID has been read at the parent level
  825. if (!Serializable::Load(source))
  826. return false;
  827. unsigned numComponents = source.ReadVLE();
  828. for (unsigned i = 0; i < numComponents; ++i)
  829. {
  830. VectorBuffer compBuffer(source, source.ReadVLE());
  831. ShortStringHash compType = compBuffer.ReadShortStringHash();
  832. unsigned compID = compBuffer.ReadUInt();
  833. Component* newComponent = CreateComponent(compType,
  834. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  835. if (newComponent)
  836. {
  837. resolver.AddComponent(compID, newComponent);
  838. // Do not abort if component fails to load, as the component buffer is nested and we can skip to the next
  839. newComponent->Load(compBuffer);
  840. }
  841. }
  842. if (!readChildren)
  843. return true;
  844. unsigned numChildren = source.ReadVLE();
  845. for (unsigned i = 0; i < numChildren; ++i)
  846. {
  847. unsigned nodeID = source.ReadUInt();
  848. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  849. LOCAL);
  850. resolver.AddNode(nodeID, newNode);
  851. if (!newNode->Load(source, resolver, readChildren, rewriteIDs, mode))
  852. return false;
  853. }
  854. return true;
  855. }
  856. bool Node::LoadXML(const XMLElement& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  857. {
  858. // Remove all children and components first in case this is not a fresh load
  859. RemoveAllChildren();
  860. RemoveAllComponents();
  861. if (!Serializable::LoadXML(source))
  862. return false;
  863. XMLElement compElem = source.GetChild("component");
  864. while (compElem)
  865. {
  866. String typeName = compElem.GetAttribute("type");
  867. unsigned compID = compElem.GetInt("id");
  868. Component* newComponent = CreateComponent(typeName,
  869. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  870. if (newComponent)
  871. {
  872. resolver.AddComponent(compID, newComponent);
  873. if (!newComponent->LoadXML(compElem))
  874. return false;
  875. }
  876. compElem = compElem.GetNext("component");
  877. }
  878. if (!readChildren)
  879. return true;
  880. XMLElement childElem = source.GetChild("node");
  881. while (childElem)
  882. {
  883. unsigned nodeID = childElem.GetInt("id");
  884. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  885. LOCAL);
  886. resolver.AddNode(nodeID, newNode);
  887. if (!newNode->LoadXML(childElem, resolver, readChildren, rewriteIDs, mode))
  888. return false;
  889. childElem = childElem.GetNext("node");
  890. }
  891. return true;
  892. }
  893. void Node::PrepareNetworkUpdate()
  894. {
  895. // Update dependency nodes list first
  896. dependencyNodes_.Clear();
  897. // Add the parent node, but if it is local, traverse to the first non-local node
  898. if (parent_ && parent_ != scene_)
  899. {
  900. Node* current = parent_;
  901. while (current->id_ >= FIRST_LOCAL_ID)
  902. current = current->parent_;
  903. if (current && current != scene_)
  904. dependencyNodes_.Push(current);
  905. }
  906. // Let the components add their dependencies
  907. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  908. {
  909. Component* component = *i;
  910. if (component->GetID() < FIRST_LOCAL_ID)
  911. component->GetDependencyNodes(dependencyNodes_);
  912. }
  913. // Then check for node attribute changes
  914. if (!networkState_)
  915. AllocateNetworkState();
  916. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  917. unsigned numAttributes = attributes->Size();
  918. if (networkState_->currentValues_.Size() != numAttributes)
  919. {
  920. networkState_->currentValues_.Resize(numAttributes);
  921. networkState_->previousValues_.Resize(numAttributes);
  922. // Copy the default attribute values to the previous state as a starting point
  923. for (unsigned i = 0; i < numAttributes; ++i)
  924. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  925. }
  926. // Check for attribute changes
  927. for (unsigned i = 0; i < numAttributes; ++i)
  928. {
  929. const AttributeInfo& attr = attributes->At(i);
  930. OnGetAttribute(attr, networkState_->currentValues_[i]);
  931. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  932. {
  933. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  934. // Mark the attribute dirty in all replication states that are tracking this node
  935. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  936. networkState_->replicationStates_.End();
  937. ++j)
  938. {
  939. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  940. nodeState->dirtyAttributes_.Set(i);
  941. // Add node to the dirty set if not added yet
  942. if (!nodeState->markedDirty_)
  943. {
  944. nodeState->markedDirty_ = true;
  945. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  946. }
  947. }
  948. }
  949. }
  950. // Finally check for user var changes
  951. for (VariantMap::ConstIterator i = vars_.Begin(); i != vars_.End(); ++i)
  952. {
  953. VariantMap::ConstIterator j = networkState_->previousVars_.Find(i->first_);
  954. if (j == networkState_->previousVars_.End() || j->second_ != i->second_)
  955. {
  956. networkState_->previousVars_[i->first_] = i->second_;
  957. // Mark the var dirty in all replication states that are tracking this node
  958. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  959. networkState_->replicationStates_.End(); ++j)
  960. {
  961. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  962. nodeState->dirtyVars_.Insert(i->first_);
  963. if (!nodeState->markedDirty_)
  964. {
  965. nodeState->markedDirty_ = true;
  966. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  967. }
  968. }
  969. }
  970. }
  971. networkUpdate_ = false;
  972. }
  973. void Node::CleanupConnection(Connection* connection)
  974. {
  975. if (owner_ == connection)
  976. owner_ = 0;
  977. if (networkState_)
  978. {
  979. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  980. {
  981. if (networkState_->replicationStates_[i]->connection_ == connection)
  982. networkState_->replicationStates_.Erase(i);
  983. }
  984. }
  985. }
  986. void Node::MarkNetworkUpdate()
  987. {
  988. if (!networkUpdate_ && scene_ && id_ < FIRST_LOCAL_ID)
  989. {
  990. scene_->MarkNetworkUpdate(this);
  991. networkUpdate_ = true;
  992. }
  993. }
  994. void Node::MarkReplicationDirty()
  995. {
  996. if (networkState_)
  997. {
  998. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  999. networkState_->replicationStates_.End(); ++j)
  1000. {
  1001. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  1002. if (!nodeState->markedDirty_)
  1003. {
  1004. nodeState->markedDirty_ = true;
  1005. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  1006. }
  1007. }
  1008. }
  1009. }
  1010. Node* Node::CreateChild(unsigned id, CreateMode mode)
  1011. {
  1012. SharedPtr<Node> newNode(new Node(context_));
  1013. // If zero ID specified, or the ID is already taken, let the scene assign
  1014. if (scene_)
  1015. {
  1016. if (!id || scene_->GetNode(id))
  1017. id = scene_->GetFreeNodeID(mode);
  1018. newNode->SetID(id);
  1019. }
  1020. else
  1021. newNode->SetID(id);
  1022. AddChild(newNode);
  1023. return newNode;
  1024. }
  1025. void Node::AddComponent(Component* component, unsigned id, CreateMode mode)
  1026. {
  1027. if (!component)
  1028. return;
  1029. components_.Push(SharedPtr<Component>(component));
  1030. // If zero ID specified, or the ID is already taken, let the scene assign
  1031. if (scene_)
  1032. {
  1033. if (!id || scene_->GetComponent(id))
  1034. id = scene_->GetFreeComponentID(mode);
  1035. component->SetID(id);
  1036. scene_->ComponentAdded(component);
  1037. }
  1038. else
  1039. component->SetID(id);
  1040. component->SetNode(this);
  1041. component->OnMarkedDirty(this);
  1042. // Check attributes of the new component on next network update, and mark node dirty in all replication states
  1043. component->MarkNetworkUpdate();
  1044. MarkNetworkUpdate();
  1045. MarkReplicationDirty();
  1046. // Send change event
  1047. if (scene_)
  1048. {
  1049. using namespace ComponentAdded;
  1050. VariantMap eventData;
  1051. eventData[P_SCENE] = (void*)scene_;
  1052. eventData[P_NODE] = (void*)this;
  1053. eventData[P_COMPONENT] = (void*)component;
  1054. scene_->SendEvent(E_COMPONENTADDED, eventData);
  1055. }
  1056. }
  1057. unsigned Node::GetNumPersistentChildren() const
  1058. {
  1059. unsigned ret = 0;
  1060. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1061. {
  1062. if (!(*i)->IsTemporary())
  1063. ++ret;
  1064. }
  1065. return ret;
  1066. }
  1067. unsigned Node::GetNumPersistentComponents() const
  1068. {
  1069. unsigned ret = 0;
  1070. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1071. {
  1072. if (!(*i)->IsTemporary())
  1073. ++ret;
  1074. }
  1075. return ret;
  1076. }
  1077. void Node::UpdateWorldTransform() const
  1078. {
  1079. Matrix3x4 transform = GetTransform();
  1080. if (parent_)
  1081. {
  1082. worldTransform_ = parent_->GetWorldTransform() * transform;
  1083. worldRotation_ = parent_->GetWorldRotation() * rotation_;
  1084. }
  1085. else
  1086. {
  1087. worldTransform_ = transform;
  1088. worldRotation_ = rotation_;
  1089. }
  1090. dirty_ = false;
  1091. }
  1092. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  1093. {
  1094. // Send change event. Do not send when already being destroyed
  1095. if (Refs() > 0 && scene_)
  1096. {
  1097. using namespace NodeRemoved;
  1098. VariantMap eventData;
  1099. eventData[P_SCENE] = (void*)scene_;
  1100. eventData[P_PARENT] = (void*)this;
  1101. eventData[P_NODE] = (void*)(*i).Get();
  1102. scene_->SendEvent(E_NODEREMOVED, eventData);
  1103. }
  1104. (*i)->parent_ = 0;
  1105. (*i)->MarkDirty();
  1106. (*i)->MarkNetworkUpdate();
  1107. children_.Erase(i);
  1108. }
  1109. void Node::GetChildrenRecursive(PODVector<Node*>& dest) const
  1110. {
  1111. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1112. {
  1113. Node* node = *i;
  1114. dest.Push(node);
  1115. if (!node->children_.Empty())
  1116. node->GetChildrenRecursive(dest);
  1117. }
  1118. }
  1119. void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, ShortStringHash type) const
  1120. {
  1121. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1122. {
  1123. Node* node = *i;
  1124. if (node->HasComponent(type))
  1125. dest.Push(node);
  1126. if (!node->children_.Empty())
  1127. node->GetChildrenWithComponentRecursive(dest, type);
  1128. }
  1129. }
  1130. void Node::GetComponentsRecursive(PODVector<Component*>& dest, ShortStringHash type) const
  1131. {
  1132. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1133. {
  1134. if ((*i)->GetType() == type)
  1135. dest.Push(*i);
  1136. }
  1137. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1138. (*i)->GetComponentsRecursive(dest, type);
  1139. }
  1140. Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mode)
  1141. {
  1142. // Create clone node
  1143. Node* cloneNode = parent->CreateChild(0, (mode == REPLICATED && id_ < FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1144. resolver.AddNode(id_, cloneNode);
  1145. // Copy attributes
  1146. unsigned numAttributes = GetNumAttributes();
  1147. for (unsigned j = 0; j < numAttributes; ++j)
  1148. cloneNode->SetAttribute(j, GetAttribute(j));
  1149. // Clone components
  1150. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1151. {
  1152. Component* component = *i;
  1153. Component* cloneComponent = cloneNode->CreateComponent(component->GetType(), (mode == REPLICATED && component->GetID() <
  1154. FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1155. if (!cloneComponent)
  1156. {
  1157. LOGERROR("Could not clone component " + component->GetTypeName());
  1158. continue;
  1159. }
  1160. resolver.AddComponent(component->GetID(), cloneComponent);
  1161. numAttributes = component->GetNumAttributes();
  1162. for (unsigned j = 0; j < numAttributes; ++j)
  1163. cloneComponent->SetAttribute(j, component->GetAttribute(j));
  1164. }
  1165. // Clone child nodes recursively
  1166. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1167. {
  1168. Node* node = *i;
  1169. node->CloneRecursive(cloneNode, resolver, mode);
  1170. }
  1171. return cloneNode;
  1172. }
  1173. void Node::RemoveComponent(Vector<SharedPtr<Component> >::Iterator i)
  1174. {
  1175. WeakPtr<Component> componentWeak(*i);
  1176. // Send node change event. Do not send when already being destroyed
  1177. if (Refs() > 0 && scene_)
  1178. {
  1179. using namespace ComponentRemoved;
  1180. VariantMap eventData;
  1181. eventData[P_SCENE] = (void*)scene_;
  1182. eventData[P_NODE] = (void*)this;
  1183. eventData[P_COMPONENT] = (void*)(*i).Get();
  1184. scene_->SendEvent(E_COMPONENTREMOVED, eventData);
  1185. }
  1186. RemoveListener(*i);
  1187. if (scene_)
  1188. scene_->ComponentRemoved(*i);
  1189. components_.Erase(i);
  1190. // If the component is still referenced elsewhere, reset its node pointer now
  1191. if (componentWeak)
  1192. componentWeak->SetNode(0);
  1193. }
  1194. }