2
0

BsSceneObjectRTTI.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 BansheeEngine
  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. public:
  66. SceneObjectRTTI()
  67. {
  68. addReflectablePtrArrayField("mChildren", 0, &SceneObjectRTTI::getChild,
  69. &SceneObjectRTTI::getNumChildren, &SceneObjectRTTI::setChild, &SceneObjectRTTI::setNumChildren);
  70. addReflectablePtrArrayField("mComponents", 1, &SceneObjectRTTI::getComponent,
  71. &SceneObjectRTTI::getNumComponents, &SceneObjectRTTI::setComponent, &SceneObjectRTTI::setNumComponents);
  72. addPlainField("mPrefabLink", 2, &SceneObjectRTTI::getPrefabLink, &SceneObjectRTTI::setPrefabLink);
  73. addPlainField("mFlags", 3, &SceneObjectRTTI::getFlags, &SceneObjectRTTI::setFlags);
  74. addReflectablePtrField("mPrefabDiff", 4, &SceneObjectRTTI::getPrefabDiff, &SceneObjectRTTI::setPrefabDiff);
  75. addPlainField("mPrefabHash", 5, &SceneObjectRTTI::getPrefabHash, &SceneObjectRTTI::setPrefabHash);
  76. addPlainField("mPosition", 6, &SceneObjectRTTI::getPosition, &SceneObjectRTTI::setPosition);
  77. addPlainField("mRotation", 7, &SceneObjectRTTI::getRotation, &SceneObjectRTTI::setRotation);
  78. addPlainField("mScale", 8, &SceneObjectRTTI::getScale, &SceneObjectRTTI::setScale);
  79. addPlainField("mActiveSelf", 9, &SceneObjectRTTI::getActive, &SceneObjectRTTI::setActive);
  80. }
  81. void onDeserializationStarted(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  82. {
  83. // If this is the root scene object we're deserializing, activate game object deserialization so the system
  84. // can resolve deserialized handles to the newly created objects
  85. SceneObject* so = static_cast<SceneObject*>(obj);
  86. // It's possible we're just accessing the game object fields, in which case the process below is not needed
  87. // (it's only required for new scene objects).
  88. if (so->mRTTIData.empty())
  89. return;
  90. // Every GameObject must store GODeserializationData in its RTTI data field during deserialization
  91. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  92. // We delay adding children/components and instead store them here
  93. deserializationData.moreData = SODeserializationData();
  94. if (!GameObjectManager::instance().isGameObjectDeserializationActive())
  95. {
  96. GameObjectManager::instance().startDeserialization();
  97. // Mark it as the object that started the GO deserialization so it knows to end it
  98. deserializationData.isDeserializationParent = true;
  99. }
  100. else
  101. deserializationData.isDeserializationParent = false;
  102. }
  103. void onDeserializationEnded(IReflectable* obj, const UnorderedMap<String, UINT64>& params) override
  104. {
  105. SceneObject* so = static_cast<SceneObject*>(obj);
  106. // It's possible we're just accessing the game object fields, in which case the process below is not needed
  107. // (it's only required for new scene objects).
  108. if (so->mRTTIData.empty())
  109. return;
  110. GODeserializationData& goDeserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  111. // Register the newly created SO with the GameObjectManager and provide it with the original ID so that
  112. // deserialized handles pointing to this object can be resolved.
  113. SPtr<SceneObject> soPtr = std::static_pointer_cast<SceneObject>(goDeserializationData.ptr);
  114. SceneObject::createInternal(soPtr, goDeserializationData.originalId);
  115. // We stored all components and children in a temporary structure because they rely on the SceneObject being
  116. // initialized with the GameObjectManager. Now that it is, we add them.
  117. SODeserializationData& soDeserializationData = any_cast_ref<SODeserializationData>(goDeserializationData.moreData);
  118. for (auto& component : soDeserializationData.components)
  119. so->addComponentInternal(component);
  120. for (auto& child : soDeserializationData.children)
  121. child->_setParent(so->mThisHandle, false);
  122. // If this is the deserialization parent, end deserialization (which resolves all game object handles, if we
  123. // provided valid IDs), and instantiate (i.e. activate) the deserialized hierarchy.
  124. if (goDeserializationData.isDeserializationParent)
  125. {
  126. GameObjectManager::instance().endDeserialization();
  127. bool parentActive = true;
  128. if (so->getParent() != nullptr)
  129. parentActive = so->getParent()->getActive();
  130. so->setActiveHierarchy(parentActive, false);
  131. if ((so->mFlags & SOF_DontInstantiate) == 0)
  132. so->_instantiate();
  133. }
  134. so->mRTTIData = nullptr;
  135. }
  136. const String& getRTTIName() override
  137. {
  138. static String name = "SceneObject";
  139. return name;
  140. }
  141. UINT32 getRTTIId() override
  142. {
  143. return TID_SceneObject;
  144. }
  145. SPtr<IReflectable> newRTTIObject() override
  146. {
  147. SPtr<SceneObject> sceneObject = SPtr<SceneObject>(new (bs_alloc<SceneObject>()) SceneObject("", SOF_DontInstantiate),
  148. &bs_delete<SceneObject>, StdAlloc<SceneObject>());
  149. sceneObject->mRTTIData = sceneObject;
  150. return sceneObject;
  151. }
  152. };
  153. /** @} */
  154. /** @endcond */
  155. }