Component.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #pragma once
  24. #include "Matrix3x4.h"
  25. #include "Serializable.h"
  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. /// Save as binary data. Return true if successful.
  44. virtual bool Save(Serializer& dest);
  45. /// Save as XML data. Return true if successful.
  46. virtual bool SaveXML(XMLElement& dest);
  47. /// Return the depended on nodes to order network updates.
  48. virtual void GetDependencyNodes(PODVector<Node*>& dest) {};
  49. /// Visualize the component as debug geometry.
  50. virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest) {};
  51. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  52. void Remove();
  53. /// Return ID.
  54. unsigned GetID() const { return id_; }
  55. /// Return scene node.
  56. Node* GetNode() const { return node_; }
  57. /// Return the scene the node belongs to.
  58. Scene* GetScene() const;
  59. /// Return components in the same scene node by type.
  60. void GetComponents(PODVector<Component*>& dest, ShortStringHash type) const;
  61. /// Return component in the same scene node by type. If there are several, returns the first.
  62. Component* GetComponent(ShortStringHash type) const;
  63. /// Template version of returning a component in the same scene node by type.
  64. template <class T> T* GetComponent() const;
  65. /// Template version of returning components in the same scene node by type.
  66. template <class T> void GetComponents(PODVector<T*>& dest) const;
  67. /// Add a replication state that is tracking this component.
  68. void AddReplicationState(ComponentReplicationState* state);
  69. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  70. void PrepareNetworkUpdate();
  71. /// Clean up all references to a network connection that is about to be removed.
  72. void CleanupConnection(Connection* connection);
  73. /// Mark for attribute check on the next network update.
  74. void MarkNetworkUpdate();
  75. protected:
  76. /// Handle scene node being assigned at creation.
  77. virtual void OnNodeSet(Node* node) {};
  78. /// Handle scene node transform dirtied.
  79. virtual void OnMarkedDirty(Node* node) {};
  80. /// %Set ID. Called by Scene.
  81. void SetID(unsigned id);
  82. /// %Set scene node. Called by Node when creating the component.
  83. void SetNode(Node* node);
  84. /// Scene node.
  85. Node* node_;
  86. /// Unique ID within the scene.
  87. unsigned id_;
  88. /// Network update queued flag.
  89. bool networkUpdate_;
  90. };
  91. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  92. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }