Node.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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 enabled)
  360. {
  361. // The enabled state of the whole scene can not be changed. SetActive() is used instead to start/stop updates.
  362. if (GetType() == Scene::GetTypeStatic())
  363. {
  364. LOGERROR("Can not change enabled state of the Scene");
  365. return;
  366. }
  367. if (enabled != enabled_)
  368. {
  369. enabled_ = enabled;
  370. MarkNetworkUpdate();
  371. // Send change event
  372. if (scene_)
  373. {
  374. using namespace NodeEnabledChanged;
  375. VariantMap eventData;
  376. eventData[P_SCENE] = (void*)scene_;
  377. eventData[P_NODE] = (void*)this;
  378. scene_->SendEvent(E_NODEENABLEDCHANGED, eventData);
  379. }
  380. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  381. {
  382. (*i)->OnSetEnabled();
  383. // Send change event for the component
  384. if (scene_)
  385. {
  386. using namespace ComponentEnabledChanged;
  387. VariantMap eventData;
  388. eventData[P_SCENE] = (void*)scene_;
  389. eventData[P_NODE] = (void*)this;
  390. eventData[P_COMPONENT] = (void*)(*i);
  391. scene_->SendEvent(E_COMPONENTENABLEDCHANGED, eventData);
  392. }
  393. }
  394. }
  395. }
  396. void Node::SetEnabledRecursive(bool enabled)
  397. {
  398. // The enabled state of the whole scene can not be changed. SetActive() is used instead to start/stop updates.
  399. if (GetType() == Scene::GetTypeStatic())
  400. {
  401. LOGERROR("Can not change enabled state of the Scene");
  402. return;
  403. }
  404. SetEnabled(enabled);
  405. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  406. (*i)->SetEnabledRecursive(enabled);
  407. }
  408. void Node::SetOwner(Connection* owner)
  409. {
  410. owner_ = owner;
  411. }
  412. void Node::MarkDirty()
  413. {
  414. if (dirty_)
  415. return;
  416. dirty_ = true;
  417. // Notify listener components first, then mark child nodes
  418. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End();)
  419. {
  420. if (*i)
  421. {
  422. (*i)->OnMarkedDirty(this);
  423. ++i;
  424. }
  425. // If listener has expired, erase from list
  426. else
  427. i = listeners_.Erase(i);
  428. }
  429. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  430. (*i)->MarkDirty();
  431. }
  432. Node* Node::CreateChild(const String& name, CreateMode mode)
  433. {
  434. Node* newNode = CreateChild(0, mode);
  435. newNode->SetName(name);
  436. return newNode;
  437. }
  438. void Node::AddChild(Node* node)
  439. {
  440. // Check for illegal or redundant parent assignment
  441. if (!node || node == this || node->parent_ == this)
  442. return;
  443. // Check for possible cyclic parent assignment
  444. Node* parent = parent_;
  445. while (parent)
  446. {
  447. if (parent == node)
  448. return;
  449. parent = parent->parent_;
  450. }
  451. // Add first, then remove from old parent, to ensure the node does not get deleted
  452. children_.Push(SharedPtr<Node>(node));
  453. node->Remove();
  454. // Add to the scene if not added yet
  455. if (scene_ && !node->GetScene())
  456. scene_->NodeAdded(node);
  457. node->parent_ = this;
  458. node->MarkDirty();
  459. node->MarkNetworkUpdate();
  460. // Send change event
  461. if (scene_)
  462. {
  463. using namespace NodeAdded;
  464. VariantMap eventData;
  465. eventData[P_SCENE] = (void*)scene_;
  466. eventData[P_PARENT] = (void*)this;
  467. eventData[P_NODE] = (void*)node;
  468. scene_->SendEvent(E_NODEADDED, eventData);
  469. }
  470. }
  471. void Node::RemoveChild(Node* node)
  472. {
  473. if (!node)
  474. return;
  475. for (Vector<SharedPtr<Node> >::Iterator i = children_.Begin(); i != children_.End(); ++i)
  476. {
  477. if (*i == node)
  478. {
  479. RemoveChild(i);
  480. return;
  481. }
  482. }
  483. }
  484. void Node::RemoveAllChildren()
  485. {
  486. while (children_.Size())
  487. RemoveChild(--children_.End());
  488. }
  489. Component* Node::CreateComponent(ShortStringHash type, CreateMode mode, unsigned id)
  490. {
  491. // Check that creation succeeds and that the object in fact is a component
  492. SharedPtr<Component> newComponent = DynamicCast<Component>(context_->CreateObject(type));
  493. if (!newComponent)
  494. {
  495. LOGERROR("Could not create unknown component type " + type.ToString());
  496. return 0;
  497. }
  498. AddComponent(newComponent, id, mode);
  499. return newComponent;
  500. }
  501. Component* Node::GetOrCreateComponent(ShortStringHash type, CreateMode mode)
  502. {
  503. Component* oldComponent = GetComponent(type);
  504. if (oldComponent)
  505. return oldComponent;
  506. else
  507. return CreateComponent(type, mode);
  508. }
  509. void Node::RemoveComponent(Component* component)
  510. {
  511. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  512. {
  513. if (*i == component)
  514. {
  515. RemoveComponent(i);
  516. // Mark node dirty in all replication states
  517. MarkReplicationDirty();
  518. return;
  519. }
  520. }
  521. }
  522. void Node::RemoveComponent(ShortStringHash type)
  523. {
  524. for (Vector<SharedPtr<Component> >::Iterator i = components_.Begin(); i != components_.End(); ++i)
  525. {
  526. if ((*i)->GetType() == type)
  527. {
  528. RemoveComponent(i);
  529. // Mark node dirty in all replication states
  530. MarkReplicationDirty();
  531. return;
  532. }
  533. }
  534. }
  535. void Node::RemoveAllComponents()
  536. {
  537. if (components_.Empty())
  538. return;
  539. while (components_.Size())
  540. RemoveComponent(--components_.End());
  541. // Mark node dirty in all replication states
  542. MarkReplicationDirty();
  543. }
  544. Node* Node::Clone(CreateMode mode)
  545. {
  546. // The scene itself can not be cloned
  547. if (this == scene_ || !parent_)
  548. {
  549. LOGERROR("Can not clone node without a parent");
  550. return 0;
  551. }
  552. PROFILE(CloneNode);
  553. SceneResolver resolver;
  554. Node* clone = CloneRecursive(parent_, resolver, mode);
  555. resolver.Resolve();
  556. clone->ApplyAttributes();
  557. return clone;
  558. }
  559. void Node::Remove()
  560. {
  561. if (parent_)
  562. parent_->RemoveChild(this);
  563. }
  564. void Node::SetParent(Node* parent)
  565. {
  566. if (parent)
  567. {
  568. Vector3 worldPosition;
  569. Quaternion worldRotation;
  570. Vector3 worldScale;
  571. GetWorldTransform().Decompose(worldPosition, worldRotation, worldScale);
  572. parent->AddChild(this);
  573. SetWorldTransform(worldPosition, worldRotation, worldScale);
  574. }
  575. }
  576. void Node::SetVar(ShortStringHash key, const Variant& value)
  577. {
  578. vars_[key] = value;
  579. MarkNetworkUpdate();
  580. }
  581. void Node::AddListener(Component* component)
  582. {
  583. if (!component)
  584. return;
  585. // Check for not adding twice
  586. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  587. {
  588. if (*i == component)
  589. return;
  590. }
  591. listeners_.Push(WeakPtr<Component>(component));
  592. // If the node is currently dirty, notify immediately
  593. if (dirty_)
  594. component->OnMarkedDirty(this);
  595. }
  596. void Node::RemoveListener(Component* component)
  597. {
  598. for (Vector<WeakPtr<Component> >::Iterator i = listeners_.Begin(); i != listeners_.End(); ++i)
  599. {
  600. if (*i == component)
  601. {
  602. listeners_.Erase(i);
  603. return;
  604. }
  605. }
  606. }
  607. Vector3 Node::LocalToWorld(const Vector3& position) const
  608. {
  609. return GetWorldTransform() * position;
  610. }
  611. Vector3 Node::LocalToWorld(const Vector4& vector) const
  612. {
  613. return GetWorldTransform() * vector;
  614. }
  615. Vector3 Node::WorldToLocal(const Vector3& position) const
  616. {
  617. return GetWorldTransform().Inverse() * position;
  618. }
  619. Vector3 Node::WorldToLocal(const Vector4& vector) const
  620. {
  621. return GetWorldTransform().Inverse() * vector;
  622. }
  623. unsigned Node::GetNumChildren(bool recursive) const
  624. {
  625. if (!recursive)
  626. return children_.Size();
  627. else
  628. {
  629. unsigned allChildren = children_.Size();
  630. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  631. allChildren += (*i)->GetNumChildren(true);
  632. return allChildren;
  633. }
  634. }
  635. void Node::GetChildren(PODVector<Node*>& dest, bool recursive) const
  636. {
  637. dest.Clear();
  638. if (!recursive)
  639. {
  640. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  641. dest.Push(*i);
  642. }
  643. else
  644. GetChildrenRecursive(dest);
  645. }
  646. void Node::GetChildrenWithComponent(PODVector<Node*>& dest, ShortStringHash type, bool recursive) const
  647. {
  648. dest.Clear();
  649. if (!recursive)
  650. {
  651. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  652. {
  653. if ((*i)->HasComponent(type))
  654. dest.Push(*i);
  655. }
  656. }
  657. else
  658. GetChildrenWithComponentRecursive(dest, type);
  659. }
  660. Node* Node::GetChild(unsigned index) const
  661. {
  662. return index < children_.Size() ? children_[index].Get() : 0;
  663. }
  664. Node* Node::GetChild(const String& name, bool recursive) const
  665. {
  666. return GetChild(StringHash(name), recursive);
  667. }
  668. Node* Node::GetChild(StringHash nameHash, bool recursive) const
  669. {
  670. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  671. {
  672. if ((*i)->GetNameHash() == nameHash)
  673. return *i;
  674. if (recursive)
  675. {
  676. Node* node = (*i)->GetChild(nameHash, true);
  677. if (node)
  678. return node;
  679. }
  680. }
  681. return 0;
  682. }
  683. unsigned Node::GetNumNetworkComponents() const
  684. {
  685. unsigned num = 0;
  686. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  687. {
  688. if ((*i)->GetID() < FIRST_LOCAL_ID)
  689. ++num;
  690. }
  691. return num;
  692. }
  693. void Node::GetComponents(PODVector<Component*>& dest, ShortStringHash type, bool recursive) const
  694. {
  695. dest.Clear();
  696. if (!recursive)
  697. {
  698. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  699. {
  700. if ((*i)->GetType() == type)
  701. dest.Push(*i);
  702. }
  703. }
  704. else
  705. GetComponentsRecursive(dest, type);
  706. }
  707. bool Node::HasComponent(ShortStringHash type) const
  708. {
  709. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  710. {
  711. if ((*i)->GetType() == type)
  712. return true;
  713. }
  714. return false;
  715. }
  716. const Variant& Node::GetVar(ShortStringHash key) const
  717. {
  718. VariantMap::ConstIterator i = vars_.Find(key);
  719. if (i != vars_.End())
  720. return i->second_;
  721. else
  722. return Variant::EMPTY;
  723. }
  724. Component* Node::GetComponent(ShortStringHash type) const
  725. {
  726. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  727. {
  728. if ((*i)->GetType() == type)
  729. return *i;
  730. }
  731. return 0;
  732. }
  733. void Node::SetID(unsigned id)
  734. {
  735. id_ = id;
  736. }
  737. void Node::SetScene(Scene* scene)
  738. {
  739. scene_ = scene;
  740. }
  741. void Node::ResetScene()
  742. {
  743. SetID(0);
  744. SetScene(0);
  745. SetOwner(0);
  746. }
  747. void Node::SetNetPositionAttr(const Vector3& value)
  748. {
  749. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  750. if (transform)
  751. transform->SetTargetPosition(value);
  752. else
  753. SetPosition(value);
  754. }
  755. void Node::SetNetRotationAttr(const PODVector<unsigned char>& value)
  756. {
  757. MemoryBuffer buf(value);
  758. SmoothedTransform* transform = GetComponent<SmoothedTransform>();
  759. if (transform)
  760. transform->SetTargetRotation(buf.ReadPackedQuaternion());
  761. else
  762. SetRotation(buf.ReadPackedQuaternion());
  763. }
  764. void Node::SetNetParentAttr(const PODVector<unsigned char>& value)
  765. {
  766. Scene* scene = GetScene();
  767. if (!scene)
  768. return;
  769. MemoryBuffer buf(value);
  770. // If nothing in the buffer, parent is the root node
  771. if (buf.IsEof())
  772. {
  773. scene->AddChild(this);
  774. return;
  775. }
  776. unsigned baseNodeID = buf.ReadNetID();
  777. Node* baseNode = scene->GetNode(baseNodeID);
  778. if (!baseNode)
  779. {
  780. LOGWARNING("Failed to find parent node " + String(baseNodeID));
  781. return;
  782. }
  783. // If buffer contains just an ID, the parent is replicated and we are done
  784. if (buf.IsEof())
  785. baseNode->AddChild(this);
  786. else
  787. {
  788. // Else the parent is local and we must find it recursively by name hash
  789. StringHash nameHash = buf.ReadStringHash();
  790. Node* parentNode = baseNode->GetChild(nameHash, true);
  791. if (!parentNode)
  792. LOGWARNING("Failed to find parent node with name hash " + nameHash.ToString());
  793. else
  794. parentNode->AddChild(this);
  795. }
  796. }
  797. const Vector3& Node::GetNetPositionAttr() const
  798. {
  799. return position_;
  800. }
  801. const PODVector<unsigned char>& Node::GetNetRotationAttr() const
  802. {
  803. attrBuffer_.Clear();
  804. attrBuffer_.WritePackedQuaternion(rotation_);
  805. return attrBuffer_.GetBuffer();
  806. }
  807. const PODVector<unsigned char>& Node::GetNetParentAttr() const
  808. {
  809. attrBuffer_.Clear();
  810. Scene* scene = GetScene();
  811. if (scene && parent_ && parent_ != scene)
  812. {
  813. // If parent is replicated, can write the ID directly
  814. unsigned parentID = parent_->GetID();
  815. if (parentID < FIRST_LOCAL_ID)
  816. attrBuffer_.WriteNetID(parentID);
  817. else
  818. {
  819. // Parent is local: traverse hierarchy to find a non-local base node
  820. // This iteration always stops due to the scene (root) being non-local
  821. Node* current = parent_;
  822. while (current->GetID() >= FIRST_LOCAL_ID)
  823. current = current->GetParent();
  824. // Then write the base node ID and the parent's name hash
  825. attrBuffer_.WriteNetID(current->GetID());
  826. attrBuffer_.WriteStringHash(parent_->GetNameHash());
  827. }
  828. }
  829. return attrBuffer_.GetBuffer();
  830. }
  831. bool Node::Load(Deserializer& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  832. {
  833. // Remove all children and components first in case this is not a fresh load
  834. RemoveAllChildren();
  835. RemoveAllComponents();
  836. // ID has been read at the parent level
  837. if (!Serializable::Load(source))
  838. return false;
  839. unsigned numComponents = source.ReadVLE();
  840. for (unsigned i = 0; i < numComponents; ++i)
  841. {
  842. VectorBuffer compBuffer(source, source.ReadVLE());
  843. ShortStringHash compType = compBuffer.ReadShortStringHash();
  844. unsigned compID = compBuffer.ReadUInt();
  845. Component* newComponent = CreateComponent(compType,
  846. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  847. if (newComponent)
  848. {
  849. resolver.AddComponent(compID, newComponent);
  850. // Do not abort if component fails to load, as the component buffer is nested and we can skip to the next
  851. newComponent->Load(compBuffer);
  852. }
  853. }
  854. if (!readChildren)
  855. return true;
  856. unsigned numChildren = source.ReadVLE();
  857. for (unsigned i = 0; i < numChildren; ++i)
  858. {
  859. unsigned nodeID = source.ReadUInt();
  860. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  861. LOCAL);
  862. resolver.AddNode(nodeID, newNode);
  863. if (!newNode->Load(source, resolver, readChildren, rewriteIDs, mode))
  864. return false;
  865. }
  866. return true;
  867. }
  868. bool Node::LoadXML(const XMLElement& source, SceneResolver& resolver, bool readChildren, bool rewriteIDs, CreateMode mode)
  869. {
  870. // Remove all children and components first in case this is not a fresh load
  871. RemoveAllChildren();
  872. RemoveAllComponents();
  873. if (!Serializable::LoadXML(source))
  874. return false;
  875. XMLElement compElem = source.GetChild("component");
  876. while (compElem)
  877. {
  878. String typeName = compElem.GetAttribute("type");
  879. unsigned compID = compElem.GetInt("id");
  880. Component* newComponent = CreateComponent(ShortStringHash(typeName),
  881. (mode == REPLICATED && compID < FIRST_LOCAL_ID) ? REPLICATED : LOCAL, rewriteIDs ? 0 : compID);
  882. if (newComponent)
  883. {
  884. resolver.AddComponent(compID, newComponent);
  885. if (!newComponent->LoadXML(compElem))
  886. return false;
  887. }
  888. compElem = compElem.GetNext("component");
  889. }
  890. if (!readChildren)
  891. return true;
  892. XMLElement childElem = source.GetChild("node");
  893. while (childElem)
  894. {
  895. unsigned nodeID = childElem.GetInt("id");
  896. Node* newNode = CreateChild(rewriteIDs ? 0 : nodeID, (mode == REPLICATED && nodeID < FIRST_LOCAL_ID) ? REPLICATED :
  897. LOCAL);
  898. resolver.AddNode(nodeID, newNode);
  899. if (!newNode->LoadXML(childElem, resolver, readChildren, rewriteIDs, mode))
  900. return false;
  901. childElem = childElem.GetNext("node");
  902. }
  903. return true;
  904. }
  905. void Node::PrepareNetworkUpdate()
  906. {
  907. // Update dependency nodes list first
  908. dependencyNodes_.Clear();
  909. // Add the parent node, but if it is local, traverse to the first non-local node
  910. if (parent_ && parent_ != scene_)
  911. {
  912. Node* current = parent_;
  913. while (current->id_ >= FIRST_LOCAL_ID)
  914. current = current->parent_;
  915. if (current && current != scene_)
  916. dependencyNodes_.Push(current);
  917. }
  918. // Let the components add their dependencies
  919. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  920. {
  921. Component* component = *i;
  922. if (component->GetID() < FIRST_LOCAL_ID)
  923. component->GetDependencyNodes(dependencyNodes_);
  924. }
  925. // Then check for node attribute changes
  926. if (!networkState_)
  927. AllocateNetworkState();
  928. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  929. unsigned numAttributes = attributes->Size();
  930. if (networkState_->currentValues_.Size() != numAttributes)
  931. {
  932. networkState_->currentValues_.Resize(numAttributes);
  933. networkState_->previousValues_.Resize(numAttributes);
  934. // Copy the default attribute values to the previous state as a starting point
  935. for (unsigned i = 0; i < numAttributes; ++i)
  936. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  937. }
  938. // Check for attribute changes
  939. for (unsigned i = 0; i < numAttributes; ++i)
  940. {
  941. const AttributeInfo& attr = attributes->At(i);
  942. OnGetAttribute(attr, networkState_->currentValues_[i]);
  943. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  944. {
  945. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  946. // Mark the attribute dirty in all replication states that are tracking this node
  947. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  948. networkState_->replicationStates_.End();
  949. ++j)
  950. {
  951. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  952. nodeState->dirtyAttributes_.Set(i);
  953. // Add node to the dirty set if not added yet
  954. if (!nodeState->markedDirty_)
  955. {
  956. nodeState->markedDirty_ = true;
  957. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  958. }
  959. }
  960. }
  961. }
  962. // Finally check for user var changes
  963. for (VariantMap::ConstIterator i = vars_.Begin(); i != vars_.End(); ++i)
  964. {
  965. VariantMap::ConstIterator j = networkState_->previousVars_.Find(i->first_);
  966. if (j == networkState_->previousVars_.End() || j->second_ != i->second_)
  967. {
  968. networkState_->previousVars_[i->first_] = i->second_;
  969. // Mark the var dirty in all replication states that are tracking this node
  970. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  971. networkState_->replicationStates_.End(); ++j)
  972. {
  973. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  974. nodeState->dirtyVars_.Insert(i->first_);
  975. if (!nodeState->markedDirty_)
  976. {
  977. nodeState->markedDirty_ = true;
  978. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  979. }
  980. }
  981. }
  982. }
  983. networkUpdate_ = false;
  984. }
  985. void Node::CleanupConnection(Connection* connection)
  986. {
  987. if (owner_ == connection)
  988. owner_ = 0;
  989. if (networkState_)
  990. {
  991. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  992. {
  993. if (networkState_->replicationStates_[i]->connection_ == connection)
  994. networkState_->replicationStates_.Erase(i);
  995. }
  996. }
  997. }
  998. void Node::MarkNetworkUpdate()
  999. {
  1000. if (!networkUpdate_ && scene_ && id_ < FIRST_LOCAL_ID)
  1001. {
  1002. scene_->MarkNetworkUpdate(this);
  1003. networkUpdate_ = true;
  1004. }
  1005. }
  1006. void Node::MarkReplicationDirty()
  1007. {
  1008. if (networkState_)
  1009. {
  1010. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  1011. networkState_->replicationStates_.End(); ++j)
  1012. {
  1013. NodeReplicationState* nodeState = static_cast<NodeReplicationState*>(*j);
  1014. if (!nodeState->markedDirty_)
  1015. {
  1016. nodeState->markedDirty_ = true;
  1017. nodeState->sceneState_->dirtyNodes_.Insert(id_);
  1018. }
  1019. }
  1020. }
  1021. }
  1022. Node* Node::CreateChild(unsigned id, CreateMode mode)
  1023. {
  1024. SharedPtr<Node> newNode(new Node(context_));
  1025. // If zero ID specified, or the ID is already taken, let the scene assign
  1026. if (scene_)
  1027. {
  1028. if (!id || scene_->GetNode(id))
  1029. id = scene_->GetFreeNodeID(mode);
  1030. newNode->SetID(id);
  1031. }
  1032. else
  1033. newNode->SetID(id);
  1034. AddChild(newNode);
  1035. return newNode;
  1036. }
  1037. void Node::AddComponent(Component* component, unsigned id, CreateMode mode)
  1038. {
  1039. if (!component)
  1040. return;
  1041. components_.Push(SharedPtr<Component>(component));
  1042. // If zero ID specified, or the ID is already taken, let the scene assign
  1043. if (scene_)
  1044. {
  1045. if (!id || scene_->GetComponent(id))
  1046. id = scene_->GetFreeComponentID(mode);
  1047. component->SetID(id);
  1048. scene_->ComponentAdded(component);
  1049. }
  1050. else
  1051. component->SetID(id);
  1052. component->SetNode(this);
  1053. component->OnMarkedDirty(this);
  1054. // Check attributes of the new component on next network update, and mark node dirty in all replication states
  1055. component->MarkNetworkUpdate();
  1056. MarkNetworkUpdate();
  1057. MarkReplicationDirty();
  1058. // Send change event
  1059. if (scene_)
  1060. {
  1061. using namespace ComponentAdded;
  1062. VariantMap eventData;
  1063. eventData[P_SCENE] = (void*)scene_;
  1064. eventData[P_NODE] = (void*)this;
  1065. eventData[P_COMPONENT] = (void*)component;
  1066. scene_->SendEvent(E_COMPONENTADDED, eventData);
  1067. }
  1068. }
  1069. void Node::UpdateWorldTransform() const
  1070. {
  1071. Matrix3x4 transform = GetTransform();
  1072. if (parent_)
  1073. worldTransform_ = parent_->GetWorldTransform() * transform;
  1074. else
  1075. worldTransform_ = transform;
  1076. dirty_ = false;
  1077. }
  1078. void Node::RemoveChild(Vector<SharedPtr<Node> >::Iterator i)
  1079. {
  1080. // Send change event. Do not send when already being destroyed
  1081. if (Refs() > 0 && scene_)
  1082. {
  1083. using namespace NodeRemoved;
  1084. VariantMap eventData;
  1085. eventData[P_SCENE] = (void*)scene_;
  1086. eventData[P_PARENT] = (void*)this;
  1087. eventData[P_NODE] = (void*)(*i).Get();
  1088. scene_->SendEvent(E_NODEREMOVED, eventData);
  1089. }
  1090. (*i)->parent_ = 0;
  1091. (*i)->MarkDirty();
  1092. (*i)->MarkNetworkUpdate();
  1093. children_.Erase(i);
  1094. }
  1095. void Node::GetChildrenRecursive(PODVector<Node*>& dest) const
  1096. {
  1097. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1098. {
  1099. Node* node = *i;
  1100. dest.Push(node);
  1101. if (!node->children_.Empty())
  1102. node->GetChildrenRecursive(dest);
  1103. }
  1104. }
  1105. void Node::GetChildrenWithComponentRecursive(PODVector<Node*>& dest, ShortStringHash type) const
  1106. {
  1107. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1108. {
  1109. Node* node = *i;
  1110. if (node->HasComponent(type))
  1111. dest.Push(node);
  1112. if (!node->children_.Empty())
  1113. node->GetChildrenWithComponentRecursive(dest, type);
  1114. }
  1115. }
  1116. void Node::GetComponentsRecursive(PODVector<Component*>& dest, ShortStringHash type) const
  1117. {
  1118. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1119. {
  1120. if ((*i)->GetType() == type)
  1121. dest.Push(*i);
  1122. }
  1123. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1124. (*i)->GetComponentsRecursive(dest, type);
  1125. }
  1126. Node* Node::CloneRecursive(Node* parent, SceneResolver& resolver, CreateMode mode)
  1127. {
  1128. // Create clone node
  1129. Node* cloneNode = parent->CreateChild(0, (mode == REPLICATED && id_ < FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1130. resolver.AddNode(id_, cloneNode);
  1131. // Copy attributes
  1132. unsigned numAttributes = GetNumAttributes();
  1133. for (unsigned j = 0; j < numAttributes; ++j)
  1134. cloneNode->SetAttribute(j, GetAttribute(j));
  1135. // Clone components
  1136. for (Vector<SharedPtr<Component> >::ConstIterator i = components_.Begin(); i != components_.End(); ++i)
  1137. {
  1138. Component* component = *i;
  1139. Component* cloneComponent = cloneNode->CreateComponent(component->GetType(), (mode == REPLICATED && component->GetID() <
  1140. FIRST_LOCAL_ID) ? REPLICATED : LOCAL);
  1141. if (!cloneComponent)
  1142. {
  1143. LOGERROR("Could not clone component " + component->GetTypeName());
  1144. continue;
  1145. }
  1146. resolver.AddComponent(component->GetID(), cloneComponent);
  1147. numAttributes = component->GetNumAttributes();
  1148. for (unsigned j = 0; j < numAttributes; ++j)
  1149. cloneComponent->SetAttribute(j, component->GetAttribute(j));
  1150. }
  1151. // Clone child nodes recursively
  1152. for (Vector<SharedPtr<Node> >::ConstIterator i = children_.Begin(); i != children_.End(); ++i)
  1153. {
  1154. Node* node = *i;
  1155. node->CloneRecursive(cloneNode, resolver, mode);
  1156. }
  1157. return cloneNode;
  1158. }
  1159. void Node::RemoveComponent(Vector<SharedPtr<Component> >::Iterator i)
  1160. {
  1161. WeakPtr<Component> componentWeak(*i);
  1162. // Send node change event. Do not send when already being destroyed
  1163. if (Refs() > 0 && scene_)
  1164. {
  1165. using namespace ComponentRemoved;
  1166. VariantMap eventData;
  1167. eventData[P_SCENE] = (void*)scene_;
  1168. eventData[P_NODE] = (void*)this;
  1169. eventData[P_COMPONENT] = (void*)(*i).Get();
  1170. scene_->SendEvent(E_COMPONENTREMOVED, eventData);
  1171. }
  1172. RemoveListener(*i);
  1173. if (scene_)
  1174. scene_->ComponentRemoved(*i);
  1175. components_.Erase(i);
  1176. // If the component is still referenced elsewhere, reset its node pointer now
  1177. if (componentWeak)
  1178. componentWeak->SetNode(0);
  1179. }
  1180. }