BsSceneObjectRTTI.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsRTTIType.h"
  6. #include "BsSceneObject.h"
  7. #include "BsGameObjectHandle.h"
  8. #include "BsGameObjectManager.h"
  9. #include "BsComponent.h"
  10. #include "BsGameObjectRTTI.h"
  11. #include "BsPrefabDiff.h"
  12. namespace bs
  13. {
  14. /** @cond RTTI */
  15. /** @addtogroup RTTI-Impl-Core
  16. * @{
  17. */
  18. /** Provides temporary storage for data used during SceneObject deserialization. */
  19. struct SODeserializationData
  20. {
  21. Vector<SPtr<SceneObject>> children;
  22. Vector<SPtr<Component>> components;
  23. };
  24. class BS_CORE_EXPORT SceneObjectRTTI : public RTTIType<SceneObject, GameObject, SceneObjectRTTI>
  25. {
  26. private:
  27. Vector3& getPosition(SceneObject* obj) { return obj->mPosition; }
  28. void setPosition(SceneObject* obj, Vector3& value) { obj->mPosition = value; }
  29. Quaternion& getRotation(SceneObject* obj) { return obj->mRotation; }
  30. void setRotation(SceneObject* obj, Quaternion& value) { obj->mRotation = value; }
  31. Vector3& getScale(SceneObject* obj) { return obj->mScale; }
  32. void setScale(SceneObject* obj, Vector3& value) { obj->mScale = value; }
  33. bool& getActive(SceneObject* obj) { return obj->mActiveSelf; }
  34. void setActive(SceneObject* obj, bool& value) { obj->mActiveSelf = value; }
  35. // NOTE - These can only be set sequentially, specific array index is ignored
  36. SPtr<SceneObject> getChild(SceneObject* obj, UINT32 idx) { return obj->mChildren[idx].getInternalPtr(); }
  37. void setChild(SceneObject* obj, UINT32 idx, SPtr<SceneObject> param)
  38. {
  39. SceneObject* so = static_cast<SceneObject*>(obj);
  40. GODeserializationData& goDeserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  41. SODeserializationData& soDeserializationData = any_cast_ref<SODeserializationData>(goDeserializationData.moreData);
  42. soDeserializationData.children.push_back(param);
  43. }
  44. UINT32 getNumChildren(SceneObject* obj) { return (UINT32)obj->mChildren.size(); }
  45. void setNumChildren(SceneObject* obj, UINT32 size) { /* DO NOTHING */ }
  46. // NOTE - These can only be set sequentially, specific array index is ignored
  47. SPtr<Component> getComponent(SceneObject* obj, UINT32 idx) { return obj->mComponents[idx].getInternalPtr(); }
  48. void setComponent(SceneObject* obj, UINT32 idx, SPtr<Component> param)
  49. {
  50. SceneObject* so = static_cast<SceneObject*>(obj);
  51. GODeserializationData& goDeserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  52. SODeserializationData& soDeserializationData = any_cast_ref<SODeserializationData>(goDeserializationData.moreData);
  53. soDeserializationData.components.push_back(param);
  54. }
  55. UINT32 getNumComponents(SceneObject* obj) { return (UINT32)obj->mComponents.size(); }
  56. void setNumComponents(SceneObject* obj, UINT32 size) { /* DO NOTHING */ }
  57. String& getPrefabLink(SceneObject* obj) { return obj->mPrefabLinkUUID; }
  58. void setPrefabLink(SceneObject* obj, String& value) { obj->mPrefabLinkUUID = value; }
  59. SPtr<PrefabDiff> getPrefabDiff(SceneObject* obj) { return obj->mPrefabDiff; }
  60. void setPrefabDiff(SceneObject* obj, SPtr<PrefabDiff> value) { obj->mPrefabDiff = value; }
  61. UINT32& getFlags(SceneObject* obj) { return obj->mFlags; }
  62. void setFlags(SceneObject* obj, UINT32& value) { obj->mFlags = value; }
  63. UINT32& getPrefabHash(SceneObject* obj) { return obj->mPrefabHash; }
  64. void setPrefabHash(SceneObject* obj, UINT32& value) { obj->mPrefabHash = value; }
  65. ObjectMobility& getMobility(SceneObject* obj) { return obj->mMobility; }
  66. void setMobility(SceneObject* obj, ObjectMobility& value) { obj->mMobility = value; }
  67. public:
  68. SceneObjectRTTI()
  69. {
  70. addReflectablePtrArrayField("mChildren", 0, &SceneObjectRTTI::getChild,
  71. &SceneObjectRTTI::getNumChildren, &SceneObjectRTTI::setChild, &SceneObjectRTTI::setNumChildren);
  72. addReflectablePtrArrayField("mComponents", 1, &SceneObjectRTTI::getComponent,
  73. &SceneObjectRTTI::getNumComponents, &SceneObjectRTTI::setComponent, &SceneObjectRTTI::setNumComponents);
  74. addPlainField("mPrefabLink", 2, &SceneObjectRTTI::getPrefabLink, &SceneObjectRTTI::setPrefabLink);
  75. addPlainField("mFlags", 3, &SceneObjectRTTI::getFlags, &SceneObjectRTTI::setFlags);
  76. addReflectablePtrField("mPrefabDiff", 4, &SceneObjectRTTI::getPrefabDiff, &SceneObjectRTTI::setPrefabDiff);
  77. addPlainField("mPrefabHash", 5, &SceneObjectRTTI::getPrefabHash, &SceneObjectRTTI::setPrefabHash);
  78. addPlainField("mPosition", 6, &SceneObjectRTTI::getPosition, &SceneObjectRTTI::setPosition);
  79. addPlainField("mRotation", 7, &SceneObjectRTTI::getRotation, &SceneObjectRTTI::setRotation);
  80. addPlainField("mScale", 8, &SceneObjectRTTI::getScale, &SceneObjectRTTI::setScale);
  81. addPlainField("mActiveSelf", 9, &SceneObjectRTTI::getActive, &SceneObjectRTTI::setActive);
  82. addPlainField("mMobility", 10, &SceneObjectRTTI::getMobility, &SceneObjectRTTI::setMobility);
  83. }
  84. void onDeserializationStarted(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  85. {
  86. // If this is the root scene object we're deserializing, activate game object deserialization so the system
  87. // can resolve deserialized handles to the newly created objects
  88. SceneObject* so = static_cast<SceneObject*>(obj);
  89. // It's possible we're just accessing the game object fields, in which case the process below is not needed
  90. // (it's only required for new scene objects).
  91. if (so->mRTTIData.empty())
  92. return;
  93. // Every GameObject must store GODeserializationData in its RTTI data field during deserialization
  94. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  95. // We delay adding children/components and instead store them here
  96. deserializationData.moreData = SODeserializationData();
  97. if (!GameObjectManager::instance().isGameObjectDeserializationActive())
  98. {
  99. GameObjectManager::instance().startDeserialization();
  100. // Mark it as the object that started the GO deserialization so it knows to end it
  101. deserializationData.isDeserializationParent = true;
  102. }
  103. else
  104. deserializationData.isDeserializationParent = false;
  105. }
  106. void onDeserializationEnded(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  107. {
  108. SceneObject* so = static_cast<SceneObject*>(obj);
  109. // It's possible we're just accessing the game object fields, in which case the process below is not needed
  110. // (it's only required for new scene objects).
  111. if (so->mRTTIData.empty())
  112. return;
  113. GODeserializationData& goDeserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  114. // Register the newly created SO with the GameObjectManager and provide it with the original ID so that
  115. // deserialized handles pointing to this object can be resolved.
  116. SPtr<SceneObject> soPtr = std::static_pointer_cast<SceneObject>(goDeserializationData.ptr);
  117. SceneObject::createInternal(soPtr, goDeserializationData.originalId);
  118. // We stored all components and children in a temporary structure because they rely on the SceneObject being
  119. // initialized with the GameObjectManager. Now that it is, we add them.
  120. SODeserializationData& soDeserializationData = any_cast_ref<SODeserializationData>(goDeserializationData.moreData);
  121. for (auto& component : soDeserializationData.components)
  122. so->addComponentInternal(component);
  123. for (auto& child : soDeserializationData.children)
  124. child->_setParent(so->mThisHandle, false);
  125. // If this is the deserialization parent, end deserialization (which resolves all game object handles, if we
  126. // provided valid IDs), and instantiate (i.e. activate) the deserialized hierarchy.
  127. if (goDeserializationData.isDeserializationParent)
  128. {
  129. GameObjectManager::instance().endDeserialization();
  130. bool parentActive = true;
  131. if (so->getParent() != nullptr)
  132. parentActive = so->getParent()->getActive();
  133. so->setActiveHierarchy(parentActive, false);
  134. if ((so->mFlags & SOF_DontInstantiate) == 0)
  135. so->_instantiate();
  136. }
  137. so->mRTTIData = nullptr;
  138. }
  139. const String& getRTTIName() override
  140. {
  141. static String name = "SceneObject";
  142. return name;
  143. }
  144. UINT32 getRTTIId() override
  145. {
  146. return TID_SceneObject;
  147. }
  148. SPtr<IReflectable> newRTTIObject() override
  149. {
  150. SPtr<SceneObject> sceneObject = SPtr<SceneObject>(new (bs_alloc<SceneObject>()) SceneObject("", SOF_DontInstantiate),
  151. &bs_delete<SceneObject>, StdAlloc<SceneObject>());
  152. sceneObject->mRTTIData = sceneObject;
  153. return sceneObject;
  154. }
  155. };
  156. /** @} */
  157. /** @endcond */
  158. }