Node.cpp 41 KB

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