BsSceneObject.h 14 KB

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