Component.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 "ReplicationState.h"
  26. #include "Scene.h"
  27. #include "SceneEvents.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. OBJECTTYPESTATIC(Component);
  32. Component::Component(Context* context) :
  33. Serializable(context),
  34. node_(0),
  35. id_(0),
  36. networkUpdate_(false)
  37. {
  38. }
  39. Component::~Component()
  40. {
  41. }
  42. void Component::OnSetAttribute(const AttributeInfo& attr, const Variant& src)
  43. {
  44. Serializable::OnSetAttribute(attr, src);
  45. MarkNetworkUpdate();
  46. }
  47. bool Component::Save(Serializer& dest)
  48. {
  49. // Write type and ID
  50. if (!dest.WriteShortStringHash(GetType()))
  51. return false;
  52. if (!dest.WriteUInt(id_))
  53. return false;
  54. // Write attributes
  55. return Serializable::Save(dest);
  56. }
  57. bool Component::SaveXML(XMLElement& dest)
  58. {
  59. // Write type and ID
  60. if (!dest.SetString("type", GetTypeName()))
  61. return false;
  62. if (!dest.SetInt("id", id_))
  63. return false;
  64. // Write attributes
  65. return Serializable::SaveXML(dest);
  66. }
  67. void Component::Remove()
  68. {
  69. if (node_)
  70. node_->RemoveComponent(this);
  71. }
  72. Scene* Component::GetScene() const
  73. {
  74. return node_ ? node_->GetScene() : 0;
  75. }
  76. void Component::AddReplicationState(ComponentReplicationState* state)
  77. {
  78. if (!networkState_)
  79. AllocateNetworkState();
  80. networkState_->replicationStates_.Push(state);
  81. }
  82. void Component::PrepareNetworkUpdate()
  83. {
  84. if (!networkState_)
  85. AllocateNetworkState();
  86. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  87. if (!attributes)
  88. return;
  89. unsigned numAttributes = attributes->Size();
  90. if (networkState_->currentValues_.Size() != numAttributes)
  91. {
  92. networkState_->currentValues_.Resize(numAttributes);
  93. networkState_->previousValues_.Resize(numAttributes);
  94. // Copy the default attribute values to the previous state as a starting point
  95. for (unsigned i = 0; i < numAttributes; ++i)
  96. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  97. }
  98. // Check for attribute changes
  99. for (unsigned i = 0; i < numAttributes; ++i)
  100. {
  101. const AttributeInfo& attr = attributes->At(i);
  102. OnGetAttribute(attr, networkState_->currentValues_[i]);
  103. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  104. {
  105. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  106. // Mark the attribute dirty in all replication states that are tracking this component
  107. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  108. networkState_->replicationStates_.End(); ++j)
  109. {
  110. ComponentReplicationState* compState = static_cast<ComponentReplicationState*>(*j);
  111. compState->dirtyAttributes_.Set(i);
  112. // Add component's parent node to the dirty set if not added yet
  113. NodeReplicationState* nodeState = compState->nodeState_;
  114. if (!nodeState->markedDirty_)
  115. {
  116. nodeState->markedDirty_ = true;
  117. nodeState->sceneState_->dirtyNodes_.Insert(node_->GetID());
  118. }
  119. }
  120. }
  121. }
  122. networkUpdate_ = false;
  123. }
  124. void Component::CleanupConnection(Connection* connection)
  125. {
  126. if (networkState_)
  127. {
  128. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  129. {
  130. if (networkState_->replicationStates_[i]->connection_ == connection)
  131. networkState_->replicationStates_.Erase(i);
  132. }
  133. }
  134. }
  135. void Component::MarkNetworkUpdate()
  136. {
  137. if (!networkUpdate_ && id_ < FIRST_LOCAL_ID)
  138. {
  139. Scene* scene = GetScene();
  140. if (scene)
  141. {
  142. scene->MarkNetworkUpdate(this);
  143. networkUpdate_ = true;
  144. }
  145. }
  146. }
  147. void Component::SetID(unsigned id)
  148. {
  149. id_ = id;
  150. }
  151. void Component::SetNode(Node* node)
  152. {
  153. node_ = node;
  154. OnNodeSet(node_);
  155. }
  156. void Component::SendAttributeListChange()
  157. {
  158. Scene* scene = GetScene();
  159. if (scene)
  160. {
  161. using namespace AttributeListChanged;
  162. VariantMap eventData;
  163. eventData[P_SCENE] = (void*)scene;
  164. eventData[P_NODE] = (void*)GetNode();
  165. eventData[P_COMPONENT] = (void*)this;
  166. scene->SendEvent(E_ATTRIBUTELISTCHANGED, eventData);
  167. }
  168. }
  169. Component* Component::GetComponent(ShortStringHash type) const
  170. {
  171. return node_ ? node_->GetComponent(type) : 0;
  172. }
  173. void Component::GetComponents(PODVector<Component*>& dest, ShortStringHash type) const
  174. {
  175. if (node_)
  176. node_->GetComponents(dest, type);
  177. else
  178. dest.Clear();
  179. }
  180. }