BsSceneObject.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsMatrix4.h"
  4. #include "BsVector3.h"
  5. #include "BsQuaternion.h"
  6. #include "BsRTTIType.h"
  7. #include "BsCoreSceneManager.h"
  8. #include "BsGameObjectManager.h"
  9. #include "BsGameObject.h"
  10. #include "BsComponent.h"
  11. namespace BansheeEngine
  12. {
  13. /**
  14. * @brief SceneObject represents an object in the scene graph. It has a world position,
  15. * place in the hierarchy and optionally a number of attached components.
  16. */
  17. class BS_CORE_EXPORT SceneObject : public GameObject
  18. {
  19. /**
  20. * @brief Flags that signify which part of the SceneObject needs updating.
  21. */
  22. enum DirtyFlags
  23. {
  24. LocalTfrmDirty = 0x01,
  25. WorldTfrmDirty = 0x02
  26. };
  27. friend class CoreSceneManager;
  28. public:
  29. ~SceneObject();
  30. /**
  31. * @brief Creates a new SceneObject with the specified name. Object will be placed in the top
  32. * of the scene hierarchy.
  33. */
  34. static HSceneObject create(const String& name);
  35. /**
  36. * @brief Destroys this object and any of its held components.
  37. *
  38. * @param [in] immediate If true, the object will be deallocated and become unusable
  39. * right away. Otherwise the deallocation will be delayed to the end of
  40. * frame (preferred method).
  41. */
  42. void destroy(bool immediate = false);
  43. /**
  44. * @copydoc GameObject::_setInstanceData
  45. */
  46. void _setInstanceData(GameObjectInstanceDataPtr& other);
  47. /**
  48. * @brief Returns a handle to this object.
  49. */
  50. HSceneObject getHandle() const { return mThisHandle; }
  51. private:
  52. SceneObject(const String& name);
  53. static HSceneObject createInternal(const String& name);
  54. /**
  55. * @brief Destroys this object and any of its held components.
  56. *
  57. * @param [in] immediate If true, the object will be deallocated and become unusable
  58. * right away. Otherwise the deallocation will be delayed to the end of
  59. * frame (preferred method).
  60. *
  61. * @note Unlike "destroy", does not remove the object from its parent.
  62. */
  63. void destroyInternal(bool immediate = false);
  64. private:
  65. HSceneObject mThisHandle;
  66. /************************************************************************/
  67. /* Transform */
  68. /************************************************************************/
  69. public:
  70. /**
  71. * @brief Sets the local position of the object.
  72. */
  73. void setPosition(const Vector3& position);
  74. /**
  75. * @brief Gets the local position of the object.
  76. */
  77. const Vector3& getPosition() const { return mPosition; }
  78. /**
  79. * @brief Sets the world position of the object.
  80. */
  81. void setWorldPosition(const Vector3& position);
  82. /**
  83. * @brief Gets the world position of the object.
  84. *
  85. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  86. */
  87. const Vector3& getWorldPosition() const;
  88. /**
  89. * @brief Sets the local rotation of the object.
  90. */
  91. void setRotation(const Quaternion& rotation);
  92. /**
  93. * @brief Gets the local rotation of the object.
  94. */
  95. const Quaternion& getRotation() const { return mRotation; }
  96. /**
  97. * @brief Sets the world rotation of the object.
  98. */
  99. void setWorldRotation(const Quaternion& rotation);
  100. /**
  101. * @brief Gets world rotation of the object.
  102. *
  103. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  104. */
  105. const Quaternion& getWorldRotation() const;
  106. /**
  107. * @brief Sets the local scale of the object.
  108. */
  109. void setScale(const Vector3& scale);
  110. /**
  111. * @brief Gets the local scale of the object.
  112. */
  113. const Vector3& getScale() const { return mScale; }
  114. /**
  115. * @brief Sets the world scale of the object.
  116. *
  117. * @note This will not work properly if this object or any of its parents
  118. * have non-affine transform matrices.
  119. */
  120. void setWorldScale(const Vector3& scale);
  121. /**
  122. * @brief Gets world scale of the object.
  123. *
  124. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  125. */
  126. const Vector3& getWorldScale() const;
  127. /**
  128. * @brief Orients the object so it is looking at the provided "location" (local space)
  129. * where "up" is used for determining the location of the objects Y axis.
  130. *
  131. */
  132. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  133. /**
  134. * @brief Gets the objects world transform matrix.
  135. *
  136. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  137. */
  138. const Matrix4& getWorldTfrm() const;
  139. /**
  140. * @brief Gets the objects local transform matrix.
  141. */
  142. const Matrix4& getLocalTfrm() const;
  143. /**
  144. * @brief Moves the object's position by the vector offset provided along world axes.
  145. */
  146. void move(const Vector3& vec);
  147. /**
  148. * @brief Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  149. */
  150. void moveRelative(const Vector3& vec);
  151. /**
  152. * @brief Gets the Z (forward) axis of the object, in world space.
  153. *
  154. * @return Forward axis of the object.
  155. */
  156. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  157. /**
  158. * @brief Gets the Y (up) axis of the object, in world space.
  159. *
  160. * @return Up axis of the object.
  161. */
  162. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  163. /**
  164. * @brief Gets the X (right) axis of the object, in world space.
  165. *
  166. * @return Right axis of the object.
  167. */
  168. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  169. /**
  170. * @brief Rotates the game object so it's forward axis faces the provided
  171. * direction.
  172. *
  173. * @note Local forward axis is considered to be negative Z.
  174. *
  175. * @param forwardDir The forward direction to face, in world space.
  176. */
  177. void setForward(const Vector3& forwardDir);
  178. /**
  179. * @brief Rotate the object around an arbitrary axis.
  180. */
  181. void rotate(const Vector3& axis, const Radian& angle);
  182. /**
  183. * @brief Rotate the object around an arbitrary axis using a Quaternion.
  184. */
  185. void rotate(const Quaternion& q);
  186. /**
  187. * @brief Rotates around local Z axis.
  188. *
  189. * @param angle Angle to rotate by.
  190. */
  191. void roll(const Radian& angle);
  192. /**
  193. * @brief Rotates around Y axis.
  194. *
  195. * @param angle Angle to rotate by.
  196. */
  197. void yaw(const Radian& angle);
  198. /**
  199. * @brief Rotates around X axis
  200. *
  201. * @param angle Angle to rotate by.
  202. */
  203. void pitch(const Radian& angle);
  204. /**
  205. * @brief Forces any dirty transform matrices on this object to be updated.
  206. *
  207. * @note Normally this is done internally when retrieving a transform, but sometimes
  208. * it is useful to update transforms manually.
  209. */
  210. void updateTransformsIfDirty();
  211. /**
  212. * @brief Returns a hash value that changes whenever a scene objects
  213. * transform gets updated. It allows you to detect changes with
  214. * the local or world transforms without directly comparing their
  215. * values with some older state.
  216. */
  217. UINT32 getTransformHash() const { return mDirtyHash; }
  218. private:
  219. Vector3 mPosition;
  220. Quaternion mRotation;
  221. Vector3 mScale;
  222. mutable Vector3 mWorldPosition;
  223. mutable Quaternion mWorldRotation;
  224. mutable Vector3 mWorldScale;
  225. mutable Matrix4 mCachedLocalTfrm;
  226. mutable Matrix4 mCachedWorldTfrm;
  227. mutable UINT32 mDirtyFlags;
  228. mutable UINT32 mDirtyHash;
  229. /**
  230. * @brief Marks the transform as dirty so that we know to update
  231. * it when the transform is requested.
  232. */
  233. void markTfrmDirty() const;
  234. /**
  235. * @brief Updates the local transform. Normally just reconstructs the
  236. * transform matrix from the position/rotation/scale.
  237. */
  238. void updateLocalTfrm() const;
  239. /**
  240. * @brief Updates the world transform. Reconstructs the local transform
  241. * matrix and multiplies it with any parent transforms.
  242. *
  243. * @note If parent transforms are dirty they will be updated.
  244. */
  245. void updateWorldTfrm() const;
  246. /**
  247. * @brief Checks if cached local transform needs updating.
  248. */
  249. bool isCachedLocalTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::LocalTfrmDirty) == 0; }
  250. /**
  251. * @brief Checks if cached world transform needs updating.
  252. */
  253. bool isCachedWorldTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::WorldTfrmDirty) == 0; }
  254. /************************************************************************/
  255. /* Hierarchy */
  256. /************************************************************************/
  257. public:
  258. /**
  259. * @brief Changes the parent of this object. Also removes the object from the current parent,
  260. * and assigns it to the new parent.
  261. *
  262. * @param [in] parent New parent.
  263. */
  264. void setParent(const HSceneObject& parent);
  265. /**
  266. * @brief Gets the parent of this object.
  267. *
  268. * @return Parent object, or nullptr if this SceneObject is at root level.
  269. */
  270. HSceneObject getParent() const { return mParent; }
  271. /**
  272. * @brief Gets a child of this item.
  273. *
  274. * @param idx The zero based index of the child.
  275. *
  276. * @return SceneObject of the child.
  277. */
  278. HSceneObject getChild(UINT32 idx) const;
  279. /**
  280. * @brief Find the index of the specified child. Don't persist this value as
  281. * it may change whenever you add/remove children.
  282. *
  283. * @param child The child to look for.
  284. *
  285. * @return The zero-based index of the found child, or -1 if no match was found.
  286. */
  287. int indexOfChild(const HSceneObject& child) const;
  288. /**
  289. * @brief Gets the number of all child GameObjects.
  290. */
  291. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  292. /**
  293. * @brief Enables or disables this object. Disabled objects also implicitly disable
  294. * all their child objects. No components on the disabled object are updated.
  295. */
  296. void setActive(bool active);
  297. /**
  298. * @brief Returns whether or not an object is active.
  299. *
  300. * @param self If true, the method will only check if this particular object was activated
  301. * or deactivated directly via setActive. If false we we also check if any of
  302. * the objects parents are inactive.
  303. */
  304. bool getActive(bool self = false);
  305. /**
  306. * @brief Makes a deep copy of this object.
  307. */
  308. HSceneObject clone();
  309. private:
  310. HSceneObject mParent;
  311. Vector<HSceneObject> mChildren;
  312. bool mActiveSelf;
  313. bool mActiveHierarchy;
  314. /**
  315. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  316. *
  317. * @param [in] object New child.
  318. */
  319. void addChild(const HSceneObject& object);
  320. /**
  321. * @brief Removes the child from the object.
  322. *
  323. * @param [in] object Child to remove.
  324. *
  325. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  326. */
  327. void removeChild(const HSceneObject& object);
  328. /**
  329. * @brief Changes the object active in hierarchy state.
  330. */
  331. void setActiveHierarchy(bool active);
  332. /************************************************************************/
  333. /* Component */
  334. /************************************************************************/
  335. public:
  336. /**
  337. * @brief Constructs a new component of the specified type and adds it to
  338. * the internal component list.
  339. */
  340. template<class T, class... Args>
  341. GameObjectHandle<T> addComponent(Args &&... args)
  342. {
  343. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  344. "Specified type is not a valid Component.");
  345. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  346. std::forward<Args>(args)...),
  347. &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  348. GameObjectHandle<T> newComponent =
  349. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  350. mComponents.push_back(newComponent);
  351. newComponent->onInitialized();
  352. return newComponent;
  353. }
  354. /**
  355. * @brief Searches for a component with the specific type and returns the first one
  356. * it finds.
  357. *
  358. * @note Don't call this too often as it is relatively slow. It is more efficient
  359. * to call it once and store the result for further use.
  360. *
  361. * @tparam typename T Type of the component.
  362. *
  363. * @return Component if found, nullptr otherwise.
  364. */
  365. template <typename T>
  366. GameObjectHandle<T> getComponent()
  367. {
  368. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  369. "Specified type is not a valid Component.");
  370. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  371. }
  372. /**
  373. * @brief Checks if the current object contains the specified component
  374. *
  375. * @note Don't call this too often as it is relatively slow.
  376. *
  377. * @tparam typename T Type of the component.
  378. *
  379. * @return True if component exists on the object.
  380. */
  381. template <typename T>
  382. bool hasComponent()
  383. {
  384. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  385. "Specified type is not a valid Component.");
  386. for (auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  387. {
  388. if ((*iter)->getRTTI()->getRTTIId() == T::getRTTIStatic()->getRTTIId())
  389. return true;
  390. }
  391. return false;
  392. }
  393. /**
  394. * @brief Searches for a component with the specified type id and returns the first one it
  395. * finds.
  396. *
  397. * @note Don't call this too often as it is relatively slow. It is more efficient to
  398. * call it once and store the result for further use.
  399. *
  400. * @param typeId Identifier for the type.
  401. *
  402. * @return Component if found, nullptr otherwise.
  403. */
  404. HComponent getComponent(UINT32 typeId) const;
  405. /**
  406. * @brief Removes the component from this object, and deallocates it.
  407. *
  408. * @param [in] component The component to destroy.
  409. * @param [in] immediate If true, the component will be deallocated and become unusable
  410. * right away. Otherwise the deallocation will be delayed to the end of
  411. * frame (preferred method).
  412. */
  413. void destroyComponent(const HComponent& component, bool immediate = false);
  414. /**
  415. * @brief Removes the component from this object, and deallocates it.
  416. *
  417. * @param [in] component The component to destroy.
  418. * @param [in] immediate If true, the component will be deallocated and become unusable
  419. * right away. Otherwise the deallocation will be delayed to the end of
  420. * frame (preferred method).
  421. */
  422. void destroyComponent(Component* component, bool immediate = false);
  423. /**
  424. * @brief Returns all components on this object.
  425. */
  426. const Vector<HComponent>& getComponents() const { return mComponents; }
  427. private:
  428. /**
  429. * @brief Creates an empty component with the default constructor.
  430. */
  431. template <typename T>
  432. static std::shared_ptr<T> createEmptyComponent()
  433. {
  434. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  435. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  436. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  437. return gameObject;
  438. }
  439. /**
  440. * @brief Adds the component to the internal component array.
  441. */
  442. void addComponentInternal(const std::shared_ptr<Component> component);
  443. Vector<HComponent> mComponents;
  444. /************************************************************************/
  445. /* RTTI */
  446. /************************************************************************/
  447. public:
  448. friend class GameObjectRTTI;
  449. friend class SceneObjectRTTI;
  450. static RTTITypeBase* getRTTIStatic();
  451. virtual RTTITypeBase* getRTTI() const;
  452. };
  453. }