Component.cpp 6.6 KB

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