Component.cpp 7.9 KB

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