Component.cpp 7.2 KB

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