BsSceneObject.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 Gets world scale of the object.
  116. *
  117. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  118. */
  119. const Vector3& getWorldScale() const;
  120. /**
  121. * @brief Orients the object so it is looking at the provided "location" (local space)
  122. * where "up" is used for determining the location of the objects Y axis.
  123. *
  124. */
  125. void lookAt(const Vector3& location, const Vector3& up = Vector3::UNIT_Y);
  126. /**
  127. * @brief Gets the objects world transform matrix.
  128. *
  129. * @note Performance warning: This might involve updating the transforms if the transform is dirty.
  130. */
  131. const Matrix4& getWorldTfrm() const;
  132. /**
  133. * @brief Gets the objects local transform matrix.
  134. */
  135. const Matrix4& getLocalTfrm() const;
  136. /**
  137. * @brief Moves the object's position by the vector offset provided along world axes.
  138. */
  139. void move(const Vector3& vec);
  140. /**
  141. * @brief Moves the object's position by the vector offset provided along it's own axes (relative to orientation).
  142. */
  143. void moveRelative(const Vector3& vec);
  144. /**
  145. * @brief Gets the Z (forward) axis of the object, in world space.
  146. *
  147. * @return Forward axis of the object.
  148. */
  149. Vector3 getForward() const { return getWorldRotation().rotate(-Vector3::UNIT_Z); }
  150. /**
  151. * @brief Gets the Y (up) axis of the object, in world space.
  152. *
  153. * @return Up axis of the object.
  154. */
  155. Vector3 getUp() const { return getWorldRotation().rotate(Vector3::UNIT_Y); }
  156. /**
  157. * @brief Gets the X (right) axis of the object, in world space.
  158. *
  159. * @return Right axis of the object.
  160. */
  161. Vector3 getRight() const { return getWorldRotation().rotate(Vector3::UNIT_X); }
  162. /**
  163. * @brief Rotates the game object so it's forward axis faces the provided
  164. * direction.
  165. *
  166. * @note Local forward axis is considered to be negative Z.
  167. *
  168. * @param forwardDir The forward direction to face, in world space.
  169. */
  170. void setForward(const Vector3& forwardDir);
  171. /**
  172. * @brief Rotate the object around an arbitrary axis.
  173. */
  174. void rotate(const Vector3& axis, const Radian& angle);
  175. /**
  176. * @brief Rotate the object around an arbitrary axis using a Quaternion.
  177. */
  178. void rotate(const Quaternion& q);
  179. /**
  180. * @brief Rotates around local Z axis.
  181. *
  182. * @param angle Angle to rotate by.
  183. */
  184. void roll(const Radian& angle);
  185. /**
  186. * @brief Rotates around Y axis.
  187. *
  188. * @param angle Angle to rotate by.
  189. */
  190. void yaw(const Radian& angle);
  191. /**
  192. * @brief Rotates around X axis
  193. *
  194. * @param angle Angle to rotate by.
  195. */
  196. void pitch(const Radian& angle);
  197. /**
  198. * @brief Forces any dirty transform matrices on this object to be updated.
  199. *
  200. * @note Normally this is done internally when retrieving a transform, but sometimes
  201. * it is useful to update transforms manually.
  202. */
  203. void updateTransformsIfDirty();
  204. /**
  205. * @brief Returns a hash value that changes whenever a scene objects
  206. * transform gets updated. It allows you to detect changes with
  207. * the local or world transforms without directly comparing their
  208. * values with some older state.
  209. */
  210. UINT32 getTransformHash() const { return mDirtyHash; }
  211. private:
  212. Vector3 mPosition;
  213. Quaternion mRotation;
  214. Vector3 mScale;
  215. mutable Vector3 mWorldPosition;
  216. mutable Quaternion mWorldRotation;
  217. mutable Vector3 mWorldScale;
  218. mutable Matrix4 mCachedLocalTfrm;
  219. mutable Matrix4 mCachedWorldTfrm;
  220. mutable UINT32 mDirtyFlags;
  221. mutable UINT32 mDirtyHash;
  222. /**
  223. * @brief Marks the transform as dirty so that we know to update
  224. * it when the transform is requested.
  225. */
  226. void markTfrmDirty() const;
  227. /**
  228. * @brief Updates the local transform. Normally just reconstructs the
  229. * transform matrix from the position/rotation/scale.
  230. */
  231. void updateLocalTfrm() const;
  232. /**
  233. * @brief Updates the world transform. Reconstructs the local transform
  234. * matrix and multiplies it with any parent transforms.
  235. *
  236. * @note If parent transforms are dirty they will be updated.
  237. */
  238. void updateWorldTfrm() const;
  239. /**
  240. * @brief Checks if cached local transform needs updating.
  241. */
  242. bool isCachedLocalTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::LocalTfrmDirty) == 0; }
  243. /**
  244. * @brief Checks if cached world transform needs updating.
  245. */
  246. bool isCachedWorldTfrmUpToDate() const { return (mDirtyFlags & DirtyFlags::WorldTfrmDirty) == 0; }
  247. /************************************************************************/
  248. /* Hierarchy */
  249. /************************************************************************/
  250. public:
  251. /**
  252. * @brief Changes the parent of this object. Also removes the object from the current parent,
  253. * and assigns it to the new parent.
  254. *
  255. * @param [in] parent New parent.
  256. */
  257. void setParent(const HSceneObject& parent);
  258. /**
  259. * @brief Gets the parent of this object.
  260. *
  261. * @return Parent object, or nullptr if this SceneObject is at root level.
  262. */
  263. HSceneObject getParent() const { return mParent; }
  264. /**
  265. * @brief Gets a child of this item.
  266. *
  267. * @param idx The zero based index of the child.
  268. *
  269. * @return SceneObject of the child.
  270. */
  271. HSceneObject getChild(UINT32 idx) const;
  272. /**
  273. * @brief Find the index of the specified child. Don't persist this value as
  274. * it may change whenever you add/remove children.
  275. *
  276. * @param child The child to look for.
  277. *
  278. * @return The zero-based index of the found child, or -1 if no match was found.
  279. */
  280. int indexOfChild(const HSceneObject& child) const;
  281. /**
  282. * @brief Gets the number of all child GameObjects.
  283. */
  284. UINT32 getNumChildren() const { return (UINT32)mChildren.size(); }
  285. /**
  286. * @brief Enables or disables this object. Disabled objects also implicitly disable
  287. * all their child objects. No components on the disabled object are updated.
  288. */
  289. void setActive(bool active);
  290. /**
  291. * @brief Returns whether or not an object is active.
  292. *
  293. * @param self If true, the method will only check if this particular object was activated
  294. * or deactivated directly via setActive. If false we we also check if any of
  295. * the objects parents are inactive.
  296. */
  297. bool getActive(bool self = false);
  298. /**
  299. * @brief Makes a deep copy of this object.
  300. */
  301. HSceneObject clone();
  302. private:
  303. HSceneObject mParent;
  304. Vector<HSceneObject> mChildren;
  305. bool mActiveSelf;
  306. bool mActiveHierarchy;
  307. /**
  308. * @brief Adds a child to the child array. This method doesn't check for null or duplicate values.
  309. *
  310. * @param [in] object New child.
  311. */
  312. void addChild(const HSceneObject& object);
  313. /**
  314. * @brief Removes the child from the object.
  315. *
  316. * @param [in] object Child to remove.
  317. *
  318. * @throws INTERNAL_ERROR If the provided child isn't a child of the current object.
  319. */
  320. void removeChild(const HSceneObject& object);
  321. /**
  322. * @brief Changes the object active in hierarchy state.
  323. */
  324. void setActiveHierarchy(bool active);
  325. /************************************************************************/
  326. /* Component */
  327. /************************************************************************/
  328. public:
  329. /**
  330. * @brief Constructs a new component of the specified type and adds it to
  331. * the internal component list.
  332. */
  333. template<class T, class... Args>
  334. GameObjectHandle<T> addComponent(Args &&... args)
  335. {
  336. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  337. "Specified type is not a valid Component.");
  338. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(mThisHandle,
  339. std::forward<Args>(args)...),
  340. &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  341. GameObjectHandle<T> newComponent =
  342. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  343. mComponents.push_back(newComponent);
  344. newComponent->onInitialized();
  345. return newComponent;
  346. }
  347. /**
  348. * @brief Searches for a component with the specific type and returns the first one
  349. * it finds.
  350. *
  351. * @note Don't call this too often as it is relatively slow. It is more efficient
  352. * to call it once and store the result for further use.
  353. *
  354. * @tparam typename T Type of the component.
  355. *
  356. * @return Component if found, nullptr otherwise.
  357. */
  358. template <typename T>
  359. GameObjectHandle<T> getComponent()
  360. {
  361. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  362. "Specified type is not a valid Component.");
  363. return static_object_cast<T>(getComponent(T::getRTTIStatic()->getRTTIId()));
  364. }
  365. /**
  366. * @brief Checks if the current object contains the specified component
  367. *
  368. * @note Don't call this too often as it is relatively slow.
  369. *
  370. * @tparam typename T Type of the component.
  371. *
  372. * @return True if component exists on the object.
  373. */
  374. template <typename T>
  375. bool hasComponent()
  376. {
  377. static_assert((std::is_base_of<BansheeEngine::Component, T>::value),
  378. "Specified type is not a valid Component.");
  379. for (auto iter = mComponents.begin(); iter != mComponents.end(); ++iter)
  380. {
  381. if ((*iter)->getRTTI()->getRTTIId() == T::getRTTIStatic()->getRTTIId())
  382. return true;
  383. }
  384. return false;
  385. }
  386. /**
  387. * @brief Searches for a component with the specified type id and returns the first one it
  388. * finds.
  389. *
  390. * @note Don't call this too often as it is relatively slow. It is more efficient to
  391. * call it once and store the result for further use.
  392. *
  393. * @param typeId Identifier for the type.
  394. *
  395. * @return Component if found, nullptr otherwise.
  396. */
  397. HComponent getComponent(UINT32 typeId) const;
  398. /**
  399. * @brief Removes the component from this object, and deallocates it.
  400. *
  401. * @param [in] component The component to destroy.
  402. * @param [in] immediate If true, the component will be deallocated and become unusable
  403. * right away. Otherwise the deallocation will be delayed to the end of
  404. * frame (preferred method).
  405. */
  406. void destroyComponent(const HComponent& component, bool immediate = false);
  407. /**
  408. * @brief Removes the component from this object, and deallocates it.
  409. *
  410. * @param [in] component The component to destroy.
  411. * @param [in] immediate If true, the component will be deallocated and become unusable
  412. * right away. Otherwise the deallocation will be delayed to the end of
  413. * frame (preferred method).
  414. */
  415. void destroyComponent(Component* component, bool immediate = false);
  416. /**
  417. * @brief Returns all components on this object.
  418. */
  419. const Vector<HComponent>& getComponents() const { return mComponents; }
  420. private:
  421. /**
  422. * @brief Creates an empty component with the default constructor.
  423. */
  424. template <typename T>
  425. static std::shared_ptr<T> createEmptyComponent()
  426. {
  427. static_assert((std::is_base_of<BansheeEngine::Component, T>::value), "Specified type is not a valid Component.");
  428. std::shared_ptr<T> gameObject(new (bs_alloc<T, PoolAlloc>()) T(), &bs_delete<PoolAlloc, T>, StdAlloc<T, PoolAlloc>());
  429. GameObjectHandle<T>(GameObjectManager::instance().registerObject(gameObject));
  430. return gameObject;
  431. }
  432. /**
  433. * @brief Adds the component to the internal component array.
  434. */
  435. void addComponentInternal(const std::shared_ptr<Component> component);
  436. Vector<HComponent> mComponents;
  437. /************************************************************************/
  438. /* RTTI */
  439. /************************************************************************/
  440. public:
  441. friend class GameObjectRTTI;
  442. friend class SceneObjectRTTI;
  443. static RTTITypeBase* getRTTIStatic();
  444. virtual RTTITypeBase* getRTTI() const;
  445. };
  446. }