BsSceneObject.h 17 KB

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