BsSceneObjectRTTI.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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) 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. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  87. if (!GameObjectManager::instance().isGameObjectDeserializationActive())
  88. {
  89. GameObjectManager::instance().startDeserialization();
  90. // Mark it as the object that started the GO deserialization so it knows to end it
  91. deserializationData.isDeserializationParent = true;
  92. }
  93. else
  94. deserializationData.isDeserializationParent = false;
  95. }
  96. void onDeserializationEnded(IReflectable* obj) override
  97. {
  98. SceneObject* so = static_cast<SceneObject*>(obj);
  99. GODeserializationData& goDeserializationData = any_cast_ref<GODeserializationData>(so->mRTTIData);
  100. // Register the newly created SO with the GameObjectManager and provide it with the original ID so that
  101. // deserialized handles pointing to this object can be resolved.
  102. SPtr<SceneObject> soPtr = std::static_pointer_cast<SceneObject>(goDeserializationData.ptr);
  103. SceneObject::createInternal(soPtr, goDeserializationData.originalId);
  104. // We stored all components and children in a temporary structure because they rely on the SceneObject being
  105. // initialized with the GameObjectManager. Now that it is, we add them.
  106. SODeserializationData& soDeserializationData = any_cast_ref<SODeserializationData>(goDeserializationData.moreData);
  107. for (auto& component : soDeserializationData.components)
  108. so->addComponentInternal(component);
  109. for (auto& child : soDeserializationData.children)
  110. child->_setParent(so->mThisHandle, false);
  111. // If this is the deserialization parent, end deserialization (which resolves all game object handles, if we
  112. // provided valid IDs), and instantiate (i.e. activate) the deserialized hierarchy.
  113. if (goDeserializationData.isDeserializationParent)
  114. {
  115. GameObjectManager::instance().endDeserialization();
  116. bool parentActive = true;
  117. if (so->getParent() != nullptr)
  118. parentActive = so->getParent()->getActive();
  119. so->setActiveHierarchy(parentActive, false);
  120. if ((so->mFlags & SOF_DontInstantiate) == 0)
  121. so->_instantiate();
  122. }
  123. so->mRTTIData = nullptr;
  124. }
  125. const String& getRTTIName() override
  126. {
  127. static String name = "SceneObject";
  128. return name;
  129. }
  130. UINT32 getRTTIId() override
  131. {
  132. return TID_SceneObject;
  133. }
  134. SPtr<IReflectable> newRTTIObject() override
  135. {
  136. SPtr<SceneObject> sceneObjectPtr =
  137. SPtr<SceneObject>(new (bs_alloc<SceneObject>()) SceneObject("", SOF_DontInstantiate),
  138. &bs_delete<SceneObject>, StdAlloc<SceneObject>());
  139. // Every GameObject must store GODeserializationData in its RTTI data field during deserialization
  140. sceneObjectPtr->mRTTIData = GODeserializationData();
  141. GODeserializationData& deserializationData = any_cast_ref<GODeserializationData>(sceneObjectPtr->mRTTIData);
  142. // Store shared pointer since the system only provides us with raw ones
  143. deserializationData.ptr = sceneObjectPtr;
  144. // We delay adding children/components and instead store them here
  145. deserializationData.moreData = SODeserializationData();
  146. return sceneObjectPtr;
  147. }
  148. };
  149. /** @} */
  150. /** @endcond */
  151. }