Node.cpp 39 KB

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