Component.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // Copyright (c) 2008-2020 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. const char* autoRemoveModeNames[] = {
  42. "Disabled",
  43. "Component",
  44. "Node",
  45. nullptr
  46. };
  47. Component::Component(Context* context) :
  48. Animatable(context),
  49. node_(nullptr),
  50. id_(0),
  51. networkUpdate_(false),
  52. enabled_(true)
  53. {
  54. }
  55. Component::~Component() = default;
  56. bool Component::Save(Serializer& dest) const
  57. {
  58. // Write type and ID
  59. if (!dest.WriteStringHash(GetType()))
  60. return false;
  61. if (!dest.WriteUInt(id_))
  62. return false;
  63. // Write attributes
  64. return Animatable::Save(dest);
  65. }
  66. bool Component::SaveXML(XMLElement& dest) const
  67. {
  68. // Write type and ID
  69. if (!dest.SetString("type", GetTypeName()))
  70. return false;
  71. if (!dest.SetUInt("id", id_))
  72. return false;
  73. // Write attributes
  74. return Animatable::SaveXML(dest);
  75. }
  76. bool Component::SaveJSON(JSONValue& dest) const
  77. {
  78. // Write type and ID
  79. dest.Set("type", GetTypeName());
  80. dest.Set("id", id_);
  81. // Write attributes
  82. return Animatable::SaveJSON(dest);
  83. }
  84. void Component::MarkNetworkUpdate()
  85. {
  86. if (!networkUpdate_ && IsReplicated())
  87. {
  88. Scene* scene = GetScene();
  89. if (scene)
  90. {
  91. scene->MarkNetworkUpdate(this);
  92. networkUpdate_ = true;
  93. }
  94. }
  95. }
  96. void Component::GetDependencyNodes(PODVector<Node*>& dest)
  97. {
  98. }
  99. void Component::DrawDebugGeometry(DebugRenderer* debug, bool depthTest)
  100. {
  101. }
  102. void Component::SetEnabled(bool enable)
  103. {
  104. if (enable != enabled_)
  105. {
  106. enabled_ = enable;
  107. OnSetEnabled();
  108. MarkNetworkUpdate();
  109. // Send change event for the component
  110. Scene* scene = GetScene();
  111. if (scene)
  112. {
  113. using namespace ComponentEnabledChanged;
  114. VariantMap& eventData = GetEventDataMap();
  115. eventData[P_SCENE] = scene;
  116. eventData[P_NODE] = node_;
  117. eventData[P_COMPONENT] = this;
  118. scene->SendEvent(E_COMPONENTENABLEDCHANGED, eventData);
  119. }
  120. }
  121. }
  122. void Component::Remove()
  123. {
  124. if (node_)
  125. node_->RemoveComponent(this);
  126. }
  127. bool Component::IsReplicated() const
  128. {
  129. return Scene::IsReplicatedID(id_);
  130. }
  131. Scene* Component::GetScene() const
  132. {
  133. return node_ ? node_->GetScene() : nullptr;
  134. }
  135. void Component::AddReplicationState(ComponentReplicationState* state)
  136. {
  137. if (!networkState_)
  138. AllocateNetworkState();
  139. networkState_->replicationStates_.Push(state);
  140. }
  141. void Component::PrepareNetworkUpdate()
  142. {
  143. if (!networkState_)
  144. AllocateNetworkState();
  145. const Vector<AttributeInfo>* attributes = networkState_->attributes_;
  146. if (!attributes)
  147. return;
  148. unsigned numAttributes = attributes->Size();
  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. auto* 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, URHO3D_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) : nullptr;
  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 = nullptr;
  242. Scene* scene = GetScene();
  243. if (scene)
  244. {
  245. #ifdef URHO3D_PHYSICS
  246. ret = scene->GetComponent<PhysicsWorld>();
  247. #endif
  248. #ifdef URHO3D_URHO2D
  249. if (!ret)
  250. ret = scene->GetComponent<PhysicsWorld2D>();
  251. #endif
  252. }
  253. return ret;
  254. }
  255. void Component::DoAutoRemove(AutoRemoveMode mode)
  256. {
  257. switch (mode)
  258. {
  259. case REMOVE_COMPONENT:
  260. Remove();
  261. return;
  262. case REMOVE_NODE:
  263. if (node_)
  264. node_->Remove();
  265. return;
  266. default:
  267. return;
  268. }
  269. }
  270. }