Component.cpp 7.0 KB

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