Component.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "Matrix4x3.h"
  25. #include "Node.h"
  26. /// Base class for components. Components can be added to scene nodes
  27. class Component : public Serializable
  28. {
  29. OBJECT(Component);
  30. friend class Node;
  31. friend class Renderer;
  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. /// Remove from the scene node. If no other shared pointer references exist, causes immediate deletion
  43. void Remove();
  44. /// Return ID
  45. unsigned GetID() const { return id_; }
  46. /// Get scene node
  47. Node* GetNode() const { return node_; }
  48. /// Return parent node's world transform
  49. const Matrix4x3& GetWorldTransform() const
  50. {
  51. if (node_)
  52. return node_->GetWorldTransform();
  53. else
  54. return Matrix4x3::IDENTITY;
  55. }
  56. /// Return parent node's world position
  57. Vector3 GetWorldPosition() const
  58. {
  59. return GetWorldTransform().GetTranslation();
  60. }
  61. /// Return parent node's world rotation
  62. Quaternion GetWorldRotation() const
  63. {
  64. return GetWorldTransform().GetRotation();
  65. }
  66. /// Return parent node's world scale
  67. Vector3 GetWorldScale() const
  68. {
  69. return GetWorldTransform().GetScale();
  70. }
  71. /// Return components in the same scene node by type
  72. void GetComponents(std::vector<Component*>& dest, ShortStringHash type) const;
  73. /// Return component in the same scene node by type. The optional index allows to specify which component, if there are several
  74. Component* GetComponent(ShortStringHash type, unsigned index = 0) const;
  75. /// Return whether the same scene node has a specific component
  76. bool HasComponent(ShortStringHash type) const;
  77. /// Template version of returning a component in the same scene node by type
  78. template <class T> T* GetComponent(unsigned index = 0) const;
  79. /// Template version of returning components in the same scene node by type
  80. template <class T> void GetComponents(std::vector<T*>& dest) const;
  81. protected:
  82. /// Set ID. Called by Scene
  83. void SetID(unsigned id);
  84. /// Set scene node. Called by Node when creating the component
  85. void SetNode(Node* node);
  86. /// Unique ID within the scene
  87. unsigned id_;
  88. /// Scene node
  89. Node* node_;
  90. };
  91. template <class T> T* Component::GetComponent(unsigned index) const
  92. {
  93. return static_cast<T*>(GetComponent(T::GetTypeStatic(), index));
  94. }
  95. template <class T> void Component::GetComponents(std::vector<T*>& dest) const
  96. {
  97. GetComponents(reinterpret_cast<std::vector<Component*>&>(dest), T::GetTypeStatic());
  98. }