Component.h 6.4 KB

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