Component.h 6.1 KB

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