Component.cpp 7.6 KB

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