Component.cpp 7.0 KB

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