Component.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. /// Base class for components. Components can be created to scene nodes.
  28. class Component : public Serializable
  29. {
  30. OBJECT(Component);
  31. friend class Node;
  32. friend class Scene;
  33. public:
  34. /// Construct.
  35. Component(Context* context);
  36. /// Destruct.
  37. virtual ~Component();
  38. /// Handle scene node being assigned at creation.
  39. virtual void OnNodeSet(Node* node) {};
  40. /// Handle scene node transform dirtied.
  41. virtual void OnMarkedDirty(Node* node) {};
  42. /// Return the depended on nodes to order network updates.
  43. virtual void GetDependencyNodes(PODVector<Node*>& dest) {};
  44. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion.
  45. void Remove();
  46. /// Return ID.
  47. unsigned GetID() const { return id_; }
  48. /// Get scene node.
  49. Node* GetNode() const { return node_; }
  50. /// Return parent node's world transform.
  51. const Matrix3x4& GetWorldTransform() const;
  52. /// Return parent node's world position.
  53. Vector3 GetWorldPosition() const { return GetWorldTransform().Translation(); }
  54. /// Return parent node's world rotation.
  55. Quaternion GetWorldRotation() const { return GetWorldTransform().Rotation(); }
  56. /// Return parent node's world scale.
  57. Vector3 GetWorldScale() const { return GetWorldTransform().Scale(); }
  58. /// Return components in the same scene node by type.
  59. void GetComponents(PODVector<Component*>& dest, ShortStringHash type) const;
  60. /// Return component in the same scene node by type. If there are several, returns the first.
  61. Component* GetComponent(ShortStringHash type) const;
  62. /// Template version of returning a component in the same scene node by type.
  63. template <class T> T* GetComponent() const;
  64. /// Template version of returning components in the same scene node by type.
  65. template <class T> void GetComponents(PODVector<T*>& dest) const;
  66. protected:
  67. /// %Set ID. Called by Scene.
  68. void SetID(unsigned id);
  69. /// %Set scene node. Called by Node when creating the component.
  70. void SetNode(Node* node);
  71. /// Unique ID within the scene.
  72. unsigned id_;
  73. /// Scene node.
  74. Node* node_;
  75. };
  76. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  77. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }