Component.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 ATOMIC_PHYSICS
  30. #include "../Physics/PhysicsWorld.h"
  31. #endif
  32. #ifdef ATOMIC_ATOMIC2D
  33. #include "../Atomic2D/PhysicsWorld2D.h"
  34. #endif
  35. #include "../DebugNew.h"
  36. #ifdef _MSC_VER
  37. #pragma warning(disable:6293)
  38. #endif
  39. namespace Atomic
  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. if (networkState_->currentValues_.Size() != numAttributes)
  142. {
  143. networkState_->currentValues_.Resize(numAttributes);
  144. networkState_->previousValues_.Resize(numAttributes);
  145. // Copy the default attribute values to the previous state as a starting point
  146. for (unsigned i = 0; i < numAttributes; ++i)
  147. networkState_->previousValues_[i] = attributes->At(i).defaultValue_;
  148. }
  149. // Check for attribute changes
  150. for (unsigned i = 0; i < numAttributes; ++i)
  151. {
  152. const AttributeInfo& attr = attributes->At(i);
  153. if (animationEnabled_ && IsAnimatedNetworkAttribute(attr))
  154. continue;
  155. OnGetAttribute(attr, networkState_->currentValues_[i]);
  156. if (networkState_->currentValues_[i] != networkState_->previousValues_[i])
  157. {
  158. networkState_->previousValues_[i] = networkState_->currentValues_[i];
  159. // Mark the attribute dirty in all replication states that are tracking this component
  160. for (PODVector<ReplicationState*>::Iterator j = networkState_->replicationStates_.Begin();
  161. j != networkState_->replicationStates_.End(); ++j)
  162. {
  163. ComponentReplicationState* compState = static_cast<ComponentReplicationState*>(*j);
  164. compState->dirtyAttributes_.Set(i);
  165. // Add component's parent node to the dirty set if not added yet
  166. NodeReplicationState* nodeState = compState->nodeState_;
  167. if (!nodeState->markedDirty_)
  168. {
  169. nodeState->markedDirty_ = true;
  170. nodeState->sceneState_->dirtyNodes_.Insert(node_->GetID());
  171. }
  172. }
  173. }
  174. }
  175. networkUpdate_ = false;
  176. }
  177. void Component::CleanupConnection(Connection* connection)
  178. {
  179. if (networkState_)
  180. {
  181. for (unsigned i = networkState_->replicationStates_.Size() - 1; i < networkState_->replicationStates_.Size(); --i)
  182. {
  183. if (networkState_->replicationStates_[i]->connection_ == connection)
  184. networkState_->replicationStates_.Erase(i);
  185. }
  186. }
  187. }
  188. void Component::OnAttributeAnimationAdded()
  189. {
  190. if (attributeAnimationInfos_.Size() == 1)
  191. SubscribeToEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE, ATOMIC_HANDLER(Component, HandleAttributeAnimationUpdate));
  192. }
  193. void Component::OnAttributeAnimationRemoved()
  194. {
  195. if (attributeAnimationInfos_.Empty())
  196. UnsubscribeFromEvent(GetScene(), E_ATTRIBUTEANIMATIONUPDATE);
  197. }
  198. void Component::OnNodeSet(Node* node)
  199. {
  200. }
  201. void Component::OnSceneSet(Scene* scene)
  202. {
  203. }
  204. void Component::OnMarkedDirty(Node* node)
  205. {
  206. }
  207. void Component::OnNodeSetEnabled(Node* node)
  208. {
  209. }
  210. void Component::SetID(unsigned id)
  211. {
  212. id_ = id;
  213. }
  214. void Component::SetNode(Node* node)
  215. {
  216. node_ = node;
  217. OnNodeSet(node_);
  218. }
  219. Component* Component::GetComponent(StringHash type) const
  220. {
  221. return node_ ? node_->GetComponent(type) : 0;
  222. }
  223. bool Component::IsEnabledEffective() const
  224. {
  225. return enabled_ && node_ && node_->IsEnabled();
  226. }
  227. void Component::GetComponents(PODVector<Component*>& dest, StringHash type) const
  228. {
  229. if (node_)
  230. node_->GetComponents(dest, type);
  231. else
  232. dest.Clear();
  233. }
  234. void Component::HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData)
  235. {
  236. using namespace AttributeAnimationUpdate;
  237. UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
  238. }
  239. Component* Component::GetFixedUpdateSource()
  240. {
  241. Component* ret = 0;
  242. Scene* scene = GetScene();
  243. if (scene)
  244. {
  245. #ifdef ATOMIC_PHYSICS
  246. ret = scene->GetComponent<PhysicsWorld>();
  247. #endif
  248. #ifdef ATOMIC_ATOMIC2D
  249. if (!ret)
  250. ret = scene->GetComponent<PhysicsWorld2D>();
  251. #endif
  252. }
  253. return ret;
  254. }
  255. }