Node.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405
  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)
  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)
  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_ = StringHash(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)
  430. {
  431. Node* newNode = CreateChild(0, 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)
  499. {
  500. Component* oldComponent = GetComponent(type);
  501. if (oldComponent)
  502. return oldComponent;
  503. else
  504. return CreateComponent(type, mode);
  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(StringHash nameHash, bool recursive) const
  666. {
  667. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  668. {
  669. if ((*i)->GetNameHash() == nameHash)
  670. return *i;
  671. if (recursive)
  672. {
  673. Node* node = (*i)->GetChild(nameHash, true);
  674. if (node)
  675. return node;
  676. }
  677. }
  678. return 0;
  679. }
  680. unsigned Node::GetNumNetworkComponents() const
  681. {
  682. unsigned num = 0;
  683. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  684. {
  685. if ((*i)->GetID() < FIRST_LOCAL_ID)
  686. ++num;
  687. }
  688. return num;
  689. }
  690. void Node::GetComponents(PODVector<Component*>& dest, ShortStringHash type, bool recursive) const
  691. {
  692. dest.Clear();
  693. if (!recursive)
  694. {
  695. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  696. {
  697. if ((*i)->GetType() == type)
  698. dest.Push(*i);
  699. }
  700. }
  701. else
  702. GetComponentsRecursive(dest, type);
  703. }
  704. bool Node::HasComponent(ShortStringHash type) const
  705. {
  706. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  707. {
  708. if ((*i)->GetType() == type)
  709. return true;
  710. }
  711. return false;
  712. }
  713. const Variant& Node::GetVar(ShortStringHash key) const
  714. {
  715. VariantMap::ConstIterator i = vars_.Find(key);
  716. if (i != vars_.End())
  717. return i->second_;
  718. else
  719. return Variant::EMPTY;
  720. }
  721. Component* Node::GetComponent(ShortStringHash type) const
  722. {
  723. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  724. {
  725. if ((*i)->GetType() == type)
  726. return *i;
  727. }
  728. return 0;
  729. }
  730. void Node::SetID(unsigned id)
  731. {
  732. id_ = id;
  733. }
  734. void Node::SetScene(Scene* scene)
  735. {
  736. scene_ = scene;
  737. }
  738. void Node::ResetScene()
  739. {
  740. SetID(0);
  741. SetScene(0);
  742. SetOwner(0);
  743. }
  744. void Node::SetNetPositionAttr(const Vector3& value)
  745. {
  746. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  747. if (transform)
  748. transform->SetTargetPosition(value);
  749. else
  750. SetPosition(value);
  751. }
  752. void Node::SetNetRotationAttr(const PODVector<unsigned char>& value)
  753. {
  754. MemoryBuffer buf(value);
  755. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  756. if (transform)
  757. transform->SetTargetRotation(buf.ReadPackedQuaternion());
  758. else
  759. SetRotation(buf.ReadPackedQuaternion());
  760. }
  761. void Node::SetNetParentAttr(const PODVector<unsigned char>& value)
  762. {
  763. Scene* scene = GetScene();
  764. if (!scene)
  765. return;
  766. MemoryBuffer buf(value);
  767. // If nothing in the buffer, parent is the root node
  768. if (buf.IsEof())
  769. {
  770. scene->AddChild(this);
  771. return;
  772. }
  773. unsigned baseNodeID = buf.ReadNetID();
  774. Node* baseNode = scene->GetNode(baseNodeID);
  775. if (!baseNode)
  776. {
  777. LOGWARNING("Failed to find parent node " + String(baseNodeID));
  778. return;
  779. }
  780. // If buffer contains just an ID, the parent is replicated and we are done
  781. if (buf.IsEof())
  782. baseNode->AddChild(this);
  783. else
  784. {
  785. // Else the parent is local and we must find it recursively by name hash
  786. StringHash nameHash = buf.ReadStringHash();
  787. Node* parentNode = baseNode->GetChild(nameHash, true);
  788. if (!parentNode)
  789. LOGWARNING("Failed to find parent node with name hash " + nameHash.ToString());
  790. else
  791. parentNode->AddChild(this);
  792. }
  793. }
  794. const Vector3& Node::GetNetPositionAttr() const
  795. {
  796. return position_;
  797. }
  798. const PODVector<unsigned char>& Node::GetNetRotationAttr() const
  799. {
  800. attrBuffer_.Clear();
  801. attrBuffer_.WritePackedQuaternion(rotation_);
  802. return attrBuffer_.GetBuffer();
  803. }
  804. const PODVector<unsigned char>& Node::GetNetParentAttr() const
  805. {
  806. attrBuffer_.Clear();
  807. Scene* scene = GetScene();
  808. if (scene && parent_ && parent_ != scene)
  809. {
  810. // If parent is replicated, can write the ID directly
  811. unsigned parentID = parent_->GetID();
  812. if (parentID < FIRST_LOCAL_ID)
  813. attrBuffer_.WriteNetID(parentID);
  814. else
  815. {
  816. // Parent is local: traverse hierarchy to find a non-local base node
  817. // This iteration always stops due to the scene (root) being non-local
  818. Node* current = parent_;
  819. while (current->GetID() >= FIRST_LOCAL_ID)
  820. current = current->GetParent();
  821. // Then write the base node ID and the parent's name hash
  822. attrBuffer_.WriteNetID(current->GetID());
  823. attrBuffer_.WriteStringHash(parent_->GetNameHash());
  824. }
  825. }
  826. return attrBuffer_.GetBuffer();
  827. }
  828. bool Node::Load(Deserializer& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  829. {
  830. // Remove all children and components first in case this is not a fresh load
  831. RemoveAllChildren();
  832. RemoveAllComponents();
  833. // ID has been read at the parent level
  834. if (!Serializable::Load(source))
  835. return false;
  836. unsigned numComponents = source.ReadVLE();
  837. for (unsigned i = 0; i < numComponents; ++i)
  838. {
  839. VectorBuffer compBuffer(source, source.ReadVLE());
  840. ShortStringHash compType = compBuffer.ReadShortStringHash();
  841. unsigned compID = compBuffer.ReadUInt();
  842. Component* newComponent = CreateComponent(compType,
  843. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  844. if (newComponent)
  845. {
  846. resolver.AddComponent(compID, newComponent);
  847. // Do not abort if component fails to load, as the component buffer is nested and we can skip to the next
  848. newComponent->Load(compBuffer);
  849. }
  850. }
  851. if (!readChildren)
  852. return true;
  853. unsigned numChildren = source.ReadVLE();
  854. for (unsigned i = 0; i < numChildren; ++i)
  855. {
  856. unsigned nodeID = source.ReadUInt();
  857. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  858. LOCAL);
  859. resolver.AddNode(nodeID, newNode);
  860. if (!newNode->Load(source, resolver, readChildren, rewriteIDs, mode))
  861. return false;
  862. }
  863. return true;
  864. }
  865. bool Node::LoadXML(const XMLElement& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  866. {
  867. // Remove all children and components first in case this is not a fresh load
  868. RemoveAllChildren();
  869. RemoveAllComponents();
  870. if (!Serializable::LoadXML(source))
  871. return false;
  872. XMLElement compElem = source.GetChild("component");
  873. while (compElem)
  874. {
  875. String typeName = compElem.GetAttribute("type");
  876. unsigned compID = compElem.GetInt("id");
  877. Component* newComponent = CreateComponent(ShortStringHash(typeName),
  878. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  879. if (newComponent)
  880. {
  881. resolver.AddComponent(compID, newComponent);
  882. if (!newComponent->LoadXML(compElem))
  883. return false;
  884. }
  885. compElem = compElem.GetNext("component");
  886. }
  887. if (!readChildren)
  888. return true;
  889. XMLElement childElem = source.GetChild("node");
  890. while (childElem)
  891. {
  892. unsigned nodeID = childElem.GetInt("id");
  893. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  894. LOCAL);
  895. resolver.AddNode(nodeID, newNode);
  896. if (!newNode->LoadXML(childElem, resolver, readChildren, rewriteIDs, mode))
  897. return false;
  898. childElem = childElem.GetNext("node");
  899. }
  900. return true;
  901. }
  902. void Node::PrepareNetworkUpdate()
  903. {
  904. // Update dependency nodes list first
  905. dependencyNodes_.Clear();
  906. // Add the parent node, but if it is local, traverse to the first non-local node
  907. if (parent_ && parent_ != scene_)
  908. {
  909. Node* current = parent_;
  910. while (current->id_ >= FIRST_LOCAL_ID)
  911. current = current->parent_;
  912. if (current && current != scene_)
  913. dependencyNodes_.Push(current);
  914. }
  915. // Let the components add their dependencies
  916. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  917. {
  918. Component* component = *i;
  919. if (component->GetID() < FIRST_LOCAL_ID)
  920. component->GetDependencyNodes(dependencyNodes_);
  921. }
  922. // Then check for node attribute changes
  923. if (!networkState_)
  924. AllocateNetworkState();
  925. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  926. unsigned numAttributes = attributes->Size();
  927. if (networkState_->currentValues_.Size() != numAttributes)
  928. {
  929. networkState_->currentValues_.Resize(numAttributes);
  930. networkState_->previousValues_.Resize(numAttributes);
  931. // Copy the default attribute values to the previous state as a starting point
  932. for (unsigned i = 0; i < numAttributes; ++i)
  933. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  934. }
  935. // Check for attribute changes
  936. for (unsigned i = 0; i < numAttributes; ++i)
  937. {
  938. const AttributeInfo& attr = attributes->At(i);
  939. OnGetAttribute(attr, networkState_->currentValues_[i]);
  940. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  941. {
  942. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  943. // Mark the attribute dirty in all replication states that are tracking this node
  944. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  945. networkState_->replicationStates_.End();
  946. ++j)
  947. {
  948. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  949. nodeState->dirtyAttributes_.Set(i);
  950. // Add node to the dirty set if not added yet
  951. if (!nodeState->markedDirty_)
  952. {
  953. nodeState->markedDirty_ = true;
  954. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  955. }
  956. }
  957. }
  958. }
  959. // Finally check for user var changes
  960. for (VariantMap::ConstIterator i = vars_.Begin(); i != vars_.End(); ++i)
  961. {
  962. VariantMap::ConstIterator j = networkState_->previousVars_.Find(i->first_);
  963. if (j == networkState_->previousVars_.End() || j->second_ != i->second_)
  964. {
  965. networkState_->previousVars_[i->first_] = i->second_;
  966. // Mark the var dirty in all replication states that are tracking this node
  967. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  968. networkState_->replicationStates_.End(); ++j)
  969. {
  970. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  971. nodeState->dirtyVars_.Insert(i->first_);
  972. if (!nodeState->markedDirty_)
  973. {
  974. nodeState->markedDirty_ = true;
  975. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  976. }
  977. }
  978. }
  979. }
  980. networkUpdate_ = false;
  981. }
  982. void Node::CleanupConnection(Connection* connection)
  983. {
  984. if (owner_ == connection)
  985. owner_ = 0;
  986. if (networkState_)
  987. {
  988. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  989. {
  990. if (networkState_->replicationStates_[i]->connection_ == connection)
  991. networkState_->replicationStates_.Erase(i);
  992. }
  993. }
  994. }
  995. void Node::MarkNetworkUpdate()
  996. {
  997. if (!networkUpdate_ && scene_ && id_ < FIRST_LOCAL_ID)
  998. {
  999. scene_->MarkNetworkUpdate(this);
  1000. networkUpdate_ = true;
  1001. }
  1002. }
  1003. void Node::MarkReplicationDirty()
  1004. {
  1005. if (networkState_)
  1006. {
  1007. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  1008. networkState_->replicationStates_.End(); ++j)
  1009. {
  1010. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  1011. if (!nodeState->markedDirty_)
  1012. {
  1013. nodeState->markedDirty_ = true;
  1014. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  1015. }
  1016. }
  1017. }
  1018. }
  1019. Node* Node::CreateChild(unsigned id, CreateMode mode)
  1020. {
  1021. SharedPtr<Node> newNode(new Node(context_));
  1022. // If zero ID specified, or the ID is already taken, let the scene assign
  1023. if (scene_)
  1024. {
  1025. if (!id || scene_->GetNode(id))
  1026. id = scene_->GetFreeNodeID(mode);
  1027. newNode->SetID(id);
  1028. }
  1029. else
  1030. newNode->SetID(id);
  1031. AddChild(newNode);
  1032. return newNode;
  1033. }
  1034. void Node::AddComponent(Component* component, unsigned id, CreateMode mode)
  1035. {
  1036. if (!component)
  1037. return;
  1038. components_.Push(SharedPtr<Component>(component));
  1039. // If zero ID specified, or the ID is already taken, let the scene assign
  1040. if (scene_)
  1041. {
  1042. if (!id || scene_->GetComponent(id))
  1043. id = scene_->GetFreeComponentID(mode);
  1044. component->SetID(id);
  1045. scene_->ComponentAdded(component);
  1046. }
  1047. else
  1048. component->SetID(id);
  1049. component->SetNode(this);
  1050. component->OnMarkedDirty(this);
  1051. // Check attributes of the new component on next network update, and mark node dirty in all replication states
  1052. component->MarkNetworkUpdate();
  1053. MarkNetworkUpdate();
  1054. MarkReplicationDirty();
  1055. // Send change event
  1056. if (scene_)
  1057. {
  1058. using namespace ComponentAdded;
  1059. VariantMap eventData;
  1060. eventData[P_SCENE] = (void*)scene_;
  1061. eventData[P_NODE] = (void*)this;
  1062. eventData[P_COMPONENT] = (void*)component;
  1063. scene_->SendEvent(E_COMPONENTADDED, eventData);
  1064. }
  1065. }
  1066. void Node::UpdateWorldTransform() const
  1067. {
  1068. Matrix3x4 transform = GetTransform();
  1069. if (parent_)
  1070. worldTransform_ = parent_->GetWorldTransform() * transform;
  1071. else
  1072. worldTransform_ = transform;
  1073. dirty_ = false;
  1074. }
  1075. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  1076. {
  1077. // Send change event. Do not send when already being destroyed
  1078. if (Refs() > 0 && scene_)
  1079. {
  1080. using namespace NodeRemoved;
  1081. VariantMap eventData;
  1082. eventData[P_SCENE] = (void*)scene_;
  1083. eventData[P_PARENT] = (void*)this;
  1084. eventData[P_NODE] = (void*)(*i).Get();
  1085. scene_->SendEvent(E_NODEREMOVED, eventData);
  1086. }
  1087. (*i)->parent_ = 0;
  1088. (*i)->MarkDirty();
  1089. (*i)->MarkNetworkUpdate();
  1090. children_.Erase(i);
  1091. }
  1092. void Node::GetChildrenRecursive(PODVector<Node*>& dest) const
  1093. {
  1094. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1095. {
  1096. Node* node = *i;
  1097. dest.Push(node);
  1098. if (!node->children_.Empty())
  1099. node->GetChildrenRecursive(dest);
  1100. }
  1101. }
  1102. void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, ShortStringHash type) const
  1103. {
  1104. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1105. {
  1106. Node* node = *i;
  1107. if (node->HasComponent(type))
  1108. dest.Push(node);
  1109. if (!node->children_.Empty())
  1110. node->GetChildrenWithComponentRecursive(dest, type);
  1111. }
  1112. }
  1113. void Node::GetComponentsRecursive(PODVector<Component*>& dest, ShortStringHash type) const
  1114. {
  1115. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1116. {
  1117. if ((*i)->GetType() == type)
  1118. dest.Push(*i);
  1119. }
  1120. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1121. (*i)->GetComponentsRecursive(dest, type);
  1122. }
  1123. Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mode)
  1124. {
  1125. // Create clone node
  1126. Node* cloneNode = parent->CreateChild(0, (mode == REPLICATED && id_ < FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1127. resolver.AddNode(id_, cloneNode);
  1128. // Copy attributes
  1129. unsigned numAttributes = GetNumAttributes();
  1130. for (unsigned j = 0; j < numAttributes; ++j)
  1131. cloneNode->SetAttribute(j, GetAttribute(j));
  1132. // Clone components
  1133. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1134. {
  1135. Component* component = *i;
  1136. Component* cloneComponent = cloneNode->CreateComponent(component->GetType(), (mode == REPLICATED && component->GetID() <
  1137. FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1138. if (!cloneComponent)
  1139. {
  1140. LOGERROR("Could not clone component " + component->GetTypeName());
  1141. continue;
  1142. }
  1143. resolver.AddComponent(component->GetID(), cloneComponent);
  1144. numAttributes = component->GetNumAttributes();
  1145. for (unsigned j = 0; j < numAttributes; ++j)
  1146. cloneComponent->SetAttribute(j, component->GetAttribute(j));
  1147. }
  1148. // Clone child nodes recursively
  1149. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1150. {
  1151. Node* node = *i;
  1152. node->CloneRecursive(cloneNode, resolver, mode);
  1153. }
  1154. return cloneNode;
  1155. }
  1156. void Node::RemoveComponent(Vector<SharedPtr<Component> >::Iterator i)
  1157. {
  1158. WeakPtr<Component> componentWeak(*i);
  1159. // Send node change event. Do not send when already being destroyed
  1160. if (Refs() > 0 && scene_)
  1161. {
  1162. using namespace ComponentRemoved;
  1163. VariantMap eventData;
  1164. eventData[P_SCENE] = (void*)scene_;
  1165. eventData[P_NODE] = (void*)this;
  1166. eventData[P_COMPONENT] = (void*)(*i).Get();
  1167. scene_->SendEvent(E_COMPONENTREMOVED, eventData);
  1168. }
  1169. RemoveListener(*i);
  1170. if (scene_)
  1171. scene_->ComponentRemoved(*i);
  1172. components_.Erase(i);
  1173. // If the component is still referenced elsewhere, reset its node pointer now
  1174. if (componentWeak)
  1175. componentWeak->SetNode(0);
  1176. }
  1177. }