Node.h 13 KB

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