Component.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /// 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. /// Save as binary data. Return true if successful.
  39. virtual bool Save(Serializer& dest);
  40. /// Save as XML data. Return true if successful.
  41. virtual bool SaveXML(XMLElement& dest);
  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 transform matrix in world space.
  51. const Matrix3x4& GetWorldTransform() const;
  52. /// Return parent node's position in world space.
  53. Vector3 GetWorldPosition() const { return GetWorldTransform().Translation(); }
  54. /// Return parent node's rotation in world space.
  55. Quaternion GetWorldRotation() const { return GetWorldTransform().Rotation(); }
  56. /// Return parent node's scale in world space.
  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. /// Handle scene node being assigned at creation.
  68. virtual void OnNodeSet(Node* node) {};
  69. /// Handle scene node transform dirtied.
  70. virtual void OnMarkedDirty(Node* node) {};
  71. /// %Set ID. Called by Scene.
  72. void SetID(unsigned id);
  73. /// %Set scene node. Called by Node when creating the component.
  74. void SetNode(Node* node);
  75. /// Unique ID within the scene.
  76. unsigned id_;
  77. /// Scene node.
  78. Node* node_;
  79. };
  80. template <class T> T* Component::GetComponent() const { return static_cast<T*>(GetComponent(T::GetTypeStatic())); }
  81. template <class T> void Component::GetComponents(PODVector<T*>& dest) const { GetComponents(reinterpret_cast<PODVector<Component*>&>(dest), T::GetTypeStatic()); }