Component.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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::GetDependencyNodes(PODVector<Node*>& dest)
  75. {
  76. }
  77. void Component::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  78. {
  79. }
  80. void Component::SetEnabled(bool enable)
  81. {
  82. if (enable != enabled_)
  83. {
  84. enabled_ = enable;
  85. OnSetEnabled();
  86. MarkNetworkUpdate();
  87. // Send change event for the component
  88. Scene* scene = GetScene();
  89. if (scene)
  90. {
  91. using namespace ComponentEnabledChanged;
  92. VariantMap& eventData = GetEventDataMap();
  93. eventData[P_SCENE] = scene;
  94. eventData[P_NODE] = node_;
  95. eventData[P_COMPONENT] = this;
  96. scene->SendEvent(E_COMPONENTENABLEDCHANGED, eventData);
  97. }
  98. }
  99. }
  100. void Component::Remove()
  101. {
  102. if (node_)
  103. node_->RemoveComponent(this);
  104. }
  105. Scene* Component::GetScene() const
  106. {
  107. return node_ ? node_->GetScene() : 0;
  108. }
  109. void Component::AddReplicationState(ComponentReplicationState* state)
  110. {
  111. if (!networkState_)
  112. AllocateNetworkState();
  113. networkState_->replicationStates_.Push(state);
  114. }
  115. void Component::PrepareNetworkUpdate()
  116. {
  117. if (!networkState_)
  118. AllocateNetworkState();
  119. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  120. if (!attributes)
  121. return;
  122. unsigned numAttributes = attributes->Size();
  123. if (networkState_->currentValues_.Size() != numAttributes)
  124. {
  125. networkState_->currentValues_.Resize(numAttributes);
  126. networkState_->previousValues_.Resize(numAttributes);
  127. // Copy the default attribute values to the previous state as a starting point
  128. for (unsigned i = 0; i < numAttributes; ++i)
  129. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  130. }
  131. // Check for attribute changes
  132. for (unsigned i = 0; i < numAttributes; ++i)
  133. {
  134. const AttributeInfo& attr = attributes->At(i);
  135. if (animationEnabled_ && IsAnimatedNetworkAttribute(attr))
  136. continue;
  137. OnGetAttribute(attr, networkState_->currentValues_[i]);
  138. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  139. {
  140. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  141. // Mark the attribute dirty in all replication states that are tracking this component
  142. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin(); j !=
  143. networkState_->replicationStates_.End(); ++j)
  144. {
  145. ComponentReplicationState* compState = static_cast<ComponentReplicationState*>(*j);
  146. compState->dirtyAttributes_.Set(i);
  147. // Add component's parent node to the dirty set if not added yet
  148. NodeReplicationState* nodeState = compState->nodeState_;
  149. if (!nodeState->markedDirty_)
  150. {
  151. nodeState->markedDirty_ = true;
  152. nodeState->sceneState_->dirtyNodes_.Insert(node_->GetID());
  153. }
  154. }
  155. }
  156. }
  157. networkUpdate_ = false;
  158. }
  159. void Component::CleanupConnection(Connection* connection)
  160. {
  161. if (networkState_)
  162. {
  163. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  164. {
  165. if (networkState_->replicationStates_[i]->connection_ == connection)
  166. networkState_->replicationStates_.Erase(i);
  167. }
  168. }
  169. }
  170. void Component::OnAttributeAnimationAdded()
  171. {
  172. if (attributeAnimationInfos_.Size() == 1)
  173. SubscribeToEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE, HANDLER(Component, HandleAttributeAnimationUpdate));
  174. }
  175. void Component::OnAttributeAnimationRemoved()
  176. {
  177. if (attributeAnimationInfos_.Empty())
  178. UnsubscribeFromEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE);
  179. }
  180. void Component::OnNodeSet(Node* node)
  181. {
  182. }
  183. void Component::OnMarkedDirty(Node* node)
  184. {
  185. }
  186. void Component::OnNodeSetEnabled(Node* node)
  187. {
  188. }
  189. void Component::SetID(unsigned id)
  190. {
  191. id_ = id;
  192. }
  193. void Component::SetNode(Node* node)
  194. {
  195. node_ = node;
  196. OnNodeSet(node_);
  197. }
  198. Component* Component::GetComponent(StringHash type) const
  199. {
  200. return node_ ? node_->GetComponent(type) : 0;
  201. }
  202. bool Component::IsEnabledEffective() const
  203. {
  204. return enabled_ && node_ && node_->IsEnabled();
  205. }
  206. void Component::GetComponents(PODVector<Component*>& dest, StringHash type) const
  207. {
  208. if (node_)
  209. node_->GetComponents(dest, type);
  210. else
  211. dest.Clear();
  212. }
  213. void Component::HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData)
  214. {
  215. using namespace AttributeAnimationUpdate;
  216. UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
  217. }
  218. }