Component.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 Node;
  27. class Scene;
  28. struct ComponentReplicationState;
  29. /// Base class for components. Components can be created to scene nodes.
  30. class Component : public Serializable
  31. {
  32. OBJECT(Component);
  33. friend class Node;
  34. friend class Scene;
  35. public:
  36. /// Construct.
  37. Component(Context* context);
  38. /// Destruct.
  39. virtual ~Component();
  40. /// Save as binary data. Return true if successful.
  41. virtual bool Save(Serializer& dest);
  42. /// Save as XML data. Return true if successful.
  43. virtual bool SaveXML(XMLElement& dest);
  44. /// Return the depended on nodes to order network updates.
  45. virtual void GetDependencyNodes(PODVector<Node*>& dest) {};
  46. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  47. void Remove();
  48. /// Return ID.
  49. unsigned GetID() const { return id_; }
  50. /// Return scene node.
  51. Node* GetNode() const { return node_; }
  52. /// Return the scene the node belongs to.
  53. Scene* GetScene() const;
  54. /// Return parent node's transform matrix in world space.
  55. const Matrix3x4& GetWorldTransform() const;
  56. /// Return parent node's position in world space.
  57. Vector3 GetWorldPosition() const { return GetWorldTransform().Translation(); }
  58. /// Return parent node's rotation in world space.
  59. Quaternion GetWorldRotation() const { return GetWorldTransform().Rotation(); }
  60. /// Return parent node's scale in world space.
  61. Vector3 GetWorldScale() const { return GetWorldTransform().Scale(); }
  62. /// Return components in the same scene node by type.
  63. void GetComponents(PODVector<Component*>& dest, ShortStringHash type) const;
  64. /// Return component in the same scene node by type. If there are several, returns the first.
  65. Component* GetComponent(ShortStringHash type) const;
  66. /// Template version of returning a component in the same scene node by type.
  67. template <class T> T* GetComponent() const;
  68. /// Template version of returning components in the same scene node by type.
  69. template <class T> void GetComponents(PODVector<T*>& dest) const;
  70. /// Add a replication state that is tracking this component.
  71. void AddReplicationState(ComponentReplicationState* state);
  72. /// Prepare network update by comparing attributes and marking replication states dirty as necessary.
  73. void PrepareNetworkUpdate();
  74. /// Clean up all references to a network connection that is about to be removed.
  75. void CleanupConnection(Connection* connection);
  76. protected:
  77. /// Handle scene node being assigned at creation.
  78. virtual void OnNodeSet(Node* node) {};
  79. /// Handle scene node transform dirtied.
  80. virtual void OnMarkedDirty(Node* node) {};
  81. /// %Set ID. Called by Scene.
  82. void SetID(unsigned id);
  83. /// %Set scene node. Called by Node when creating the component.
  84. void SetNode(Node* node);
  85. /// Unique ID within the scene.
  86. unsigned id_;
  87. /// Scene node.
  88. Node* node_;
  89. /// Per-user network replication states.
  90. PODVector<ComponentReplicationState*> replicationStates_;
  91. };
  92. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  93. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }