Component.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. /// \file
  4. #pragma once
  5. #include "../Scene/Animatable.h"
  6. namespace Urho3D
  7. {
  8. class DebugRenderer;
  9. class Node;
  10. class Scene;
  11. struct ComponentReplicationState;
  12. /// Autoremove is used by some components for automatic removal from the scene hierarchy upon completion of an action, for example sound or particle effect.
  13. enum AutoRemoveMode
  14. {
  15. REMOVE_DISABLED = 0,
  16. REMOVE_COMPONENT,
  17. REMOVE_NODE
  18. };
  19. using ComponentId = id32;
  20. /// Base class for components. Components can be created to scene nodes.
  21. /// @templateversion
  22. class URHO3D_API Component : public Animatable
  23. {
  24. URHO3D_OBJECT(Component, Animatable);
  25. friend class Node;
  26. friend class Scene;
  27. public:
  28. /// Construct.
  29. explicit Component(Context* context);
  30. /// Destruct.
  31. ~Component() override;
  32. /// Handle enabled/disabled state change.
  33. virtual void OnSetEnabled() { }
  34. /// Save as binary data. Return true if successful.
  35. bool Save(Serializer& dest) const override;
  36. /// Save as XML data. Return true if successful.
  37. bool SaveXML(XMLElement& dest) const override;
  38. /// Save as JSON data. Return true if successful.
  39. bool SaveJSON(JSONValue& dest) const override;
  40. /// Mark for attribute check on the next network update.
  41. void MarkNetworkUpdate() override;
  42. /// Return the depended on nodes to order network updates.
  43. virtual void GetDependencyNodes(Vector<Node*>& dest);
  44. /// Visualize the component as debug geometry.
  45. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  46. /// Set enabled/disabled state.
  47. /// @property
  48. void SetEnabled(bool enable);
  49. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  50. void Remove();
  51. /// Return ID.
  52. /// @property{get_id}
  53. ComponentId GetID() const { return id_; }
  54. /// Return whether the component is replicated or local to a scene.
  55. /// @property
  56. bool IsReplicated() const;
  57. /// Return scene node.
  58. /// @property
  59. Node* GetNode() const { return node_; }
  60. /// Return the scene the node belongs to.
  61. Scene* GetScene() const;
  62. /// Return whether is enabled.
  63. /// @property
  64. bool IsEnabled() const { return enabled_; }
  65. /// Return whether is effectively enabled (node is also enabled).
  66. /// @property
  67. bool IsEnabledEffective() const;
  68. /// Return component in the same scene node by type. If there are several, returns the first.
  69. Component* GetComponent(StringHash type) const;
  70. /// Return components in the same scene node by type.
  71. void GetComponents(Vector<Component*>& dest, StringHash type) const;
  72. /// Template version of returning a component in the same scene node by type.
  73. template <class T> T* GetComponent() const;
  74. /// Template version of returning components in the same scene node by type.
  75. template <class T> void GetComponents(Vector<T*>& dest) const;
  76. /// Add a replication state that is tracking this component.
  77. void AddReplicationState(ComponentReplicationState* state);
  78. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  79. void PrepareNetworkUpdate();
  80. /// Clean up all references to a network connection that is about to be removed.
  81. /// @manualbind
  82. void CleanupConnection(Connection* connection);
  83. protected:
  84. /// Handle attribute animation added.
  85. void OnAttributeAnimationAdded() override;
  86. /// Handle attribute animation removed.
  87. void OnAttributeAnimationRemoved() override;
  88. /// Handle scene node being assigned at creation.
  89. virtual void OnNodeSet(Node* node);
  90. /// Handle scene being assigned. This may happen several times during the component's lifetime. Scene-wide subsystems and events are subscribed to here.
  91. virtual void OnSceneSet(Scene* scene);
  92. /// Handle scene node transform dirtied.
  93. virtual void OnMarkedDirty(Node* node);
  94. /// Handle scene node enabled status changing.
  95. virtual void OnNodeSetEnabled(Node* node);
  96. /// Set ID. Called by Scene.
  97. /// @property{set_id}
  98. void SetID(ComponentId id);
  99. /// Set scene node. Called by Node when creating the component.
  100. void SetNode(Node* node);
  101. /// Handle scene attribute animation update event.
  102. void HandleAttributeAnimationUpdate(StringHash eventType, VariantMap& eventData);
  103. /// Return a component from the scene root that sends out fixed update events (either PhysicsWorld or PhysicsWorld2D). Return null if neither exists.
  104. Component* GetFixedUpdateSource();
  105. /// Perform autoremove. Called by subclasses. Caller should keep a weak pointer to itself to check whether was actually removed, and return immediately without further member operations in that case.
  106. void DoAutoRemove(AutoRemoveMode mode);
  107. /// Scene node.
  108. Node* node_;
  109. /// Unique ID within the scene.
  110. ComponentId id_;
  111. /// Network update queued flag.
  112. bool networkUpdate_;
  113. /// Enabled flag.
  114. bool enabled_;
  115. };
  116. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  117. template <class T> void Component::GetComponents(Vector<T*>& dest) const
  118. {
  119. GetComponents(reinterpret_cast<Vector<Component*>&>(dest), T::GetTypeStatic());
  120. }
  121. }