Node.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 "Serializable.h"
  26. class Component;
  27. class Connection;
  28. class Scene;
  29. /// Scene node that may contain components and child nodes
  30. class Node : public Serializable
  31. {
  32. OBJECT(Node);
  33. public:
  34. /// Construct
  35. Node(Context* context);
  36. /// Destruct. Any child nodes are detached
  37. virtual ~Node();
  38. /// Register object factory
  39. static void RegisterObject(Context* context);
  40. /// Handle event
  41. virtual void OnEvent(Object* sender, bool broadcast, StringHash eventType, VariantMap& eventData);
  42. /// Handle attribute write access
  43. virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& value);
  44. /// Load from binary data. Return true if successful
  45. virtual bool Load(Deserializer& source);
  46. /// Load from XML data. Return true if successful
  47. virtual bool LoadXML(const XMLElement& source);
  48. /// Save as binary data. Return true if successful
  49. virtual bool Save(Serializer& dest);
  50. /// Save as XML data. Return true if successful
  51. virtual bool SaveXML(XMLElement& dest);
  52. /// Perform post-load for components and child nodes
  53. virtual void PostLoad();
  54. /// Set name
  55. void SetName(const String& name);
  56. /// Set position
  57. void SetPosition(const Vector3& position);
  58. /// Set rotation
  59. void SetRotation(const Quaternion& rotation);
  60. /// Set direction. Positive Z equals identity
  61. void SetDirection(const Vector3& direction);
  62. /// Set uniform scale
  63. void SetScale(float scale);
  64. /// Set scale
  65. void SetScale(const Vector3& scale);
  66. /// Set transform
  67. void SetTransform(const Vector3& position, const Quaternion& rotation);
  68. /// Set transform
  69. void SetTransform(const Vector3& position, const Quaternion& rotation, float scale);
  70. /// Set transform
  71. void SetTransform(const Vector3& position, const Quaternion& rotation, const Vector3& scale);
  72. /// Move the scene node
  73. void Translate(const Vector3& delta);
  74. /// Move the scene node relative to its rotation
  75. void TranslateRelative(const Vector3& delta);
  76. /// Rotate the scene node
  77. void Rotate(const Quaternion& delta, bool fixedAxis = false);
  78. /// Rotate around the X axis
  79. void Pitch(float angle, bool fixedAxis = false);
  80. /// Rotate around the Y axis
  81. void Yaw(float angle, bool fixedAxis = false);
  82. /// Rotate around the Z axis
  83. void Roll(float angle, bool fixedAxis = false);
  84. /// Modify scale uniformly
  85. void Scale(float scale);
  86. /// Modify scale
  87. void Scale(const Vector3& scale);
  88. /// Mark node and child nodes to need world transform recalculation. Notify listener components
  89. void MarkDirty();
  90. /// Create a child scene node
  91. Node* CreateChild(const String& name = String(), bool local = false);
  92. /// Add a child scene node
  93. void AddChild(Node* node);
  94. /// Remove a child scene node
  95. void RemoveChild(Node* node);
  96. /// Remove all child scene nodes
  97. void RemoveAllChildren();
  98. /// Create a component to this node
  99. Component* CreateComponent(ShortStringHash type, bool local = false);
  100. /// Create a component to this node if it does not exist already
  101. Component* GetOrCreateComponent(ShortStringHash type, bool local = false);
  102. /// Remove a component from this node
  103. void RemoveComponent(Component* component);
  104. /// Remove all components from this node
  105. void RemoveAllComponents();
  106. /// Add listener component that is notified of node being dirtied. Can either be in the same node or another
  107. void AddListener(Component* component);
  108. /// Remove listener component
  109. void RemoveListener(Component* component);
  110. /// Remove from the parent node. If no other shared pointer references exist, causes immediate deletion
  111. void Remove();
  112. /// Set parent scene node. Same as parent->AddChild(this)
  113. void SetParent(Node* parent);
  114. /// Template version of creating a component
  115. template <class T> T* CreateComponent(bool local = false);
  116. /// Template version of getting or creating a component
  117. template <class T> T* GetOrCreateComponent(bool local = false);
  118. /// Return ID
  119. unsigned GetID() const { return id_; }
  120. /// Return name
  121. const String& GetName() const { return name_; }
  122. /// Return name hash
  123. StringHash GetNameHash() const { return nameHash_; }
  124. /// Return parent scene node
  125. Node* GetParent() const { return parent_; }
  126. /// Return scene
  127. Scene* GetScene() const { return scene_; }
  128. /// Return owner connection in multiplayer
  129. Connection* GetOwner() const { return owner_; }
  130. /// Return position
  131. const Vector3& GetPosition() const { return position_; }
  132. /// Return rotation
  133. const Quaternion& GetRotation() const { return rotation_; }
  134. /// Return direction. Identity rotation equals positive Z
  135. Vector3 GetDirection() const { return rotation_ * Vector3::FORWARD; }
  136. /// Return scale
  137. const Vector3& GetScale() const { return scale_; }
  138. /// Return local transform
  139. Matrix4x3 GetTransform() const { return Matrix4x3(position_, rotation_, scale_); }
  140. /// Return world-space position
  141. Vector3 GetWorldPosition()
  142. {
  143. if (dirty_)
  144. UpdateWorldTransform();
  145. return worldTransform_.GetTranslation();
  146. }
  147. /// Return world-space rotation
  148. Quaternion GetWorldRotation()
  149. {
  150. if (dirty_)
  151. UpdateWorldTransform();
  152. return worldTransform_.GetRotation();
  153. }
  154. /// Return world-space direction
  155. Vector3 GetWorldDirection()
  156. {
  157. if (dirty_)
  158. UpdateWorldTransform();
  159. return worldTransform_.GetRotationMatrix() * Vector3::FORWARD;
  160. }
  161. /// Return world-space scale
  162. Vector3 GetWorldScale()
  163. {
  164. if (dirty_)
  165. UpdateWorldTransform();
  166. return worldTransform_.GetScale();
  167. }
  168. /// Return world-space transform
  169. const Matrix4x3& GetWorldTransform()
  170. {
  171. if (dirty_)
  172. UpdateWorldTransform();
  173. return worldTransform_;
  174. }
  175. /// Return whether transform has changed and world transform needs recalculation
  176. bool IsDirty() const { return dirty_; }
  177. /// Return number of child scene nodes
  178. unsigned GetNumChildren(bool recursive = false) const;
  179. /// Return immediate child scene nodes
  180. const Vector<SharedPtr<Node> >& GetChildren() const { return children_; }
  181. /// Return child scene nodes, optionally recursive
  182. void GetChildren(Vector<Node*>& dest, bool recursive = false) const;
  183. /// Return child scene nodes with a specific component
  184. void GetChildrenWithComponent(Vector<Node*>& dest, ShortStringHash type, bool recursive = false) const;
  185. /// Return child scene node by index
  186. Node* GetChild(unsigned index) const;
  187. /// Return child scene node by name
  188. Node* GetChild(const String& name, bool recursive = false) const;
  189. /// Return child scene node by name hash
  190. Node* GetChild(StringHash nameHash, bool recursive = false) const;
  191. /// Return number of components
  192. unsigned GetNumComponents() const { return components_.Size(); }
  193. /// Return all components
  194. const Vector<SharedPtr<Component> >& GetComponents() const { return components_; }
  195. /// Return all components of type
  196. void GetComponents(Vector<Component*>& dest, ShortStringHash type) const;
  197. /// Return component by index
  198. Component* GetComponent(unsigned index) const;
  199. /// Return component by type. The optional index allows to specify which component, if there are several
  200. Component* GetComponent(ShortStringHash type, unsigned index = 0) const;
  201. /// Return whether has a specific component
  202. bool HasComponent(ShortStringHash type) const;
  203. /// Return listener components
  204. const Vector<WeakPtr<Component> > GetListeners() const { return listeners_; }
  205. /// Return user variables
  206. VariantMap& GetVars() { return vars_; }
  207. /// Template version of returning child nodes with a specific component
  208. template <class T> void GetChildrenWithComponent(Vector<Node*>& dest, bool recursive = false) const;
  209. /// Template version of returning a component by type
  210. template <class T> T* GetComponent(unsigned index = 0) const;
  211. /// Template version of returning all components of type
  212. template <class T> void GetComponents(Vector<T*>& dest) const;
  213. /// Template version of checking whether has a specific component
  214. template <class T> bool HasComponent() const;
  215. /// Set ID. Called by Scene
  216. void SetID(unsigned id);
  217. /// Set scene. Called by Scene
  218. void SetScene(Scene* scene);
  219. /// Set owner connection for multiplayer
  220. void SetOwner(Connection* owner);
  221. /// User variables
  222. VariantMap vars_;
  223. protected:
  224. /// Load components and optionally load child nodes. Used internally
  225. bool Load(Deserializer& source, bool loadChildren);
  226. /// Load components from XML data and optionally load child nodes. Used internally
  227. bool LoadXML(const XMLElement& source, bool loadChildren);
  228. /// Create a component with specific ID. Used internally
  229. Component* CreateComponent(ShortStringHash type, unsigned id, bool local);
  230. /// Create a child node with specific ID. Used internally
  231. Node* CreateChild(unsigned id, bool local);
  232. private:
  233. /// Recalculate the world transform
  234. void UpdateWorldTransform();
  235. /// Remove child node by iterator
  236. void RemoveChild(Vector<SharedPtr<Node> >::Iterator i);
  237. /// Return child nodes recursively
  238. void GetChildrenRecursive(Vector<Node*>& dest) const;
  239. /// Return child nodes with a specific component recursively
  240. void GetChildrenWithComponentRecursive(Vector<Node*>& dest, ShortStringHash type) const;
  241. /// Unique ID within the scene
  242. unsigned id_;
  243. /// Parent scene node
  244. Node* parent_;
  245. /// Scene (root node)
  246. Scene* scene_;
  247. /// Owner connection in multiplayer
  248. Connection* owner_;
  249. /// Position
  250. Vector3 position_;
  251. /// Rotation
  252. Quaternion rotation_;
  253. /// Scale
  254. Vector3 scale_;
  255. /// World-space transform matrix
  256. Matrix4x3 worldTransform_;
  257. /// Name
  258. String name_;
  259. /// Name hash
  260. StringHash nameHash_;
  261. /// Child scene nodes
  262. Vector<SharedPtr<Node> > children_;
  263. /// Components
  264. Vector<SharedPtr<Component> > components_;
  265. /// Node listeners
  266. Vector<WeakPtr<Component> > listeners_;
  267. /// Consecutive rotation count for rotation renormalization
  268. unsigned char rotateCount_;
  269. /// World transform needs update flag
  270. bool dirty_;
  271. };
  272. template <class T> T* Node::CreateComponent(bool local)
  273. {
  274. return static_cast<T*>(CreateComponent(T::GetTypeStatic(), local));
  275. }
  276. template <class T> T* Node::GetOrCreateComponent(bool local)
  277. {
  278. return static_cast<T*>(GetOrCreateComponent(T::GetTypeStatic(), local));
  279. }
  280. template <class T> void Node::GetChildrenWithComponent(Vector<Node*>& dest, bool recursive) const
  281. {
  282. GetChildrenWithComponent(dest, T::GetTypeStatic(), recursive);
  283. }
  284. template <class T> T* Node::GetComponent(unsigned index) const
  285. {
  286. return static_cast<T*>(GetComponent(T::GetTypeStatic(), index));
  287. }
  288. template <class T> void Node::GetComponents(Vector<T*>& dest) const
  289. {
  290. GetComponents(reinterpret_cast<Vector<Component*>&>(dest), T::GetTypeStatic());
  291. }
  292. template <class T> bool Node::HasComponent() const
  293. {
  294. return HasComponent(T::GetTypeStatic());
  295. }