Component.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // Copyright (c) 2008-2014 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. Component::Component(Context* context) :
  32. Animatable(context),
  33. node_(0),
  34. id_(0),
  35. networkUpdate_(false),
  36. enabled_(true)
  37. {
  38. }
  39. Component::~Component()
  40. {
  41. }
  42. bool Component::Save(Serializer& dest) const
  43. {
  44. // Write type and ID
  45. if (!dest.WriteStringHash(GetType()))
  46. return false;
  47. if (!dest.WriteUInt(id_))
  48. return false;
  49. // Write attributes
  50. return Animatable::Save(dest);
  51. }
  52. bool Component::SaveXML(XMLElement& dest) const
  53. {
  54. // Write type and ID
  55. if (!dest.SetString("type", GetTypeName()))
  56. return false;
  57. if (!dest.SetInt("id", id_))
  58. return false;
  59. // Write attributes
  60. return Animatable::SaveXML(dest);
  61. }
  62. void Component::MarkNetworkUpdate()
  63. {
  64. if (!networkUpdate_ && id_ < FIRST_LOCAL_ID)
  65. {
  66. Scene* scene = GetScene();
  67. if (scene)
  68. {
  69. scene->MarkNetworkUpdate(this);
  70. networkUpdate_ = true;
  71. }
  72. }
  73. }
  74. void Component::SetEnabled(bool enable)
  75. {
  76. if (enable != enabled_)
  77. {
  78. enabled_ = enable;
  79. OnSetEnabled();
  80. MarkNetworkUpdate();
  81. // Send change event for the component
  82. Scene* scene = GetScene();
  83. if (scene)
  84. {
  85. using namespace ComponentEnabledChanged;
  86. VariantMap& eventData = GetEventDataMap();
  87. eventData[P_SCENE] = scene;
  88. eventData[P_NODE] = node_;
  89. eventData[P_COMPONENT] = this;
  90. scene->SendEvent(E_COMPONENTENABLEDCHANGED, eventData);
  91. }
  92. }
  93. }
  94. void Component::Remove()
  95. {
  96. if (node_)
  97. node_->RemoveComponent(this);
  98. }
  99. Scene* Component::GetScene() const
  100. {
  101. return node_ ? node_->GetScene() : 0;
  102. }
  103. void Component::AddReplicationState(ComponentReplicationState* state)
  104. {
  105. if (!networkState_)
  106. AllocateNetworkState();
  107. networkState_->replicationStates_.Push(state);
  108. }
  109. void Component::PrepareNetworkUpdate()
  110. {
  111. if (!networkState_)
  112. AllocateNetworkState();
  113. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  114. if (!attributes)
  115. return;
  116. unsigned numAttributes = attributes->Size();
  117. if (networkState_->currentValues_.Size() != numAttributes)
  118. {
  119. networkState_->currentValues_.Resize(numAttributes);
  120. networkState_->previousValues_.Resize(numAttributes);
  121. // Copy the default attribute values to the previous state as a starting point
  122. for (unsigned i = 0; i < numAttributes; ++i)
  123. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  124. }
  125. // Check for attribute changes
  126. for (unsigned i = 0; i < numAttributes; ++i)
  127. {
  128. const AttributeInfo& attr = attributes->At(i);
  129. if (animationEnabled_ && IsAnimatedNetworkAttribute(attr))
  130. continue;
  131. OnGetAttribute(attr, networkState_->currentValues_[i]);
  132. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  133. {
  134. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  135. // Mark the attribute dirty in all replication states that are tracking this component
  136. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  137. networkState_->replicationStates_.End(); ++j)
  138. {
  139. ComponentReplicationState* compState = static_cast<ComponentReplicationState*>(*j);
  140. compState->dirtyAttributes_.Set(i);
  141. // Add component's parent node to the dirty set if not added yet
  142. NodeReplicationState* nodeState = compState->nodeState_;
  143. if (!nodeState->markedDirty_)
  144. {
  145. nodeState->markedDirty_ = true;
  146. nodeState->sceneState_->dirtyNodes_.Insert(node_->GetID());
  147. }
  148. }
  149. }
  150. }
  151. networkUpdate_ = false;
  152. }
  153. void Component::CleanupConnection(Connection* connection)
  154. {
  155. if (networkState_)
  156. {
  157. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  158. {
  159. if (networkState_->replicationStates_[i]->connection_ == connection)
  160. networkState_->replicationStates_.Erase(i);
  161. }
  162. }
  163. }
  164. void Component::OnAttributeAnimationAdded()
  165. {
  166. if (attributeAnimationInfos_.Size() == 1)
  167. SubscribeToEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE, HANDLER(Component, HandleAttributeAnimationUpdate));
  168. }
  169. void Component::OnAttributeAnimationRemoved()
  170. {
  171. if (attributeAnimationInfos_.Empty())
  172. UnsubscribeFromEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE);
  173. }
  174. void Component::SetID(unsigned id)
  175. {
  176. id_ = id;
  177. }
  178. void Component::SetNode(Node* node)
  179. {
  180. node_ = node;
  181. OnNodeSet(node_);
  182. }
  183. Component* Component::GetComponent(StringHash type) const
  184. {
  185. return node_ ? node_->GetComponent(type) : 0;
  186. }
  187. bool Component::IsEnabledEffective() const
  188. {
  189. return enabled_ && node_ && node_->IsEnabled();
  190. }
  191. void Component::GetComponents(PODVector<Component*>& dest, StringHash type) const
  192. {
  193. if (node_)
  194. node_->GetComponents(dest, type);
  195. else
  196. dest.Clear();
  197. }
  198. void Component::HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData)
  199. {
  200. using namespace AttributeAnimationUpdate;
  201. UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
  202. }
  203. }