Node.cpp 39 KB

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