Component.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Copyright (c) 2008-2013 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 "Serializable.h"
  24. namespace Urho3D
  25. {
  26. class DebugRenderer;
  27. class Node;
  28. class Scene;
  29. struct ComponentReplicationState;
  30. /// Base class for components. Components can be created to scene nodes.
  31. class Component : public Serializable
  32. {
  33. OBJECT(Component);
  34. friend class Node;
  35. friend class Scene;
  36. public:
  37. /// Construct.
  38. Component(Context* context);
  39. /// Destruct.
  40. virtual ~Component();
  41. /// Handle attribute write access.
  42. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  43. /// Handle enabled/disabled state change.
  44. virtual void OnSetEnabled() {}
  45. /// Save as binary data. Return true if successful.
  46. virtual bool Save(Serializer& dest);
  47. /// Save as XML data. Return true if successful.
  48. virtual bool SaveXML(XMLElement& dest);
  49. /// Return the depended on nodes to order network updates.
  50. virtual void GetDependencyNodes(PODVector<Node*>& dest) {};
  51. /// Visualize the component as debug geometry.
  52. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) {};
  53. /// Set enabled/disabled state.
  54. void SetEnabled(bool enable);
  55. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  56. void Remove();
  57. /// Return ID.
  58. unsigned GetID() const { return id_; }
  59. /// Return scene node.
  60. Node* GetNode() const { return node_; }
  61. /// Return the scene the node belongs to.
  62. Scene* GetScene() const;
  63. /// Return whether is enabled.
  64. bool IsEnabled() const { return enabled_; }
  65. /// Return whether is effectively enabled (node is also enabled.)
  66. bool IsEnabledEffective() const;
  67. /// Return component in the same scene node by type. If there are several, returns the first.
  68. Component* GetComponent(ShortStringHash type) const;
  69. /// Return components in the same scene node by type.
  70. void GetComponents(PODVector<Component*>& dest, ShortStringHash type) const;
  71. /// Template version of returning a component in the same scene node by type.
  72. template <class T> T* GetComponent() const;
  73. /// Template version of returning components in the same scene node by type.
  74. template <class T> void GetComponents(PODVector<T*>& dest) const;
  75. /// Add a replication state that is tracking this component.
  76. void AddReplicationState(ComponentReplicationState* state);
  77. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  78. void PrepareNetworkUpdate();
  79. /// Clean up all references to a network connection that is about to be removed.
  80. void CleanupConnection(Connection* connection);
  81. /// Mark for attribute check on the next network update.
  82. void MarkNetworkUpdate();
  83. protected:
  84. /// Handle scene node being assigned at creation.
  85. virtual void OnNodeSet(Node* node) {};
  86. /// Handle scene node transform dirtied.
  87. virtual void OnMarkedDirty(Node* node) {};
  88. /// Set ID. Called by Scene.
  89. void SetID(unsigned id);
  90. /// Set scene node. Called by Node when creating the component.
  91. void SetNode(Node* node);
  92. /// Scene node.
  93. Node* node_;
  94. /// Unique ID within the scene.
  95. unsigned id_;
  96. /// Network update queued flag.
  97. bool networkUpdate_;
  98. /// Enabled flag.
  99. bool enabled_;
  100. };
  101. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  102. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }
  103. }