Component.cpp 7.1 KB

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