BsPrefab.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrefab.h"
  4. #include "BsPrefabRTTI.h"
  5. #include "BsResources.h"
  6. #include "BsSceneObject.h"
  7. #include "BsPrefabUtility.h"
  8. #include "BsCoreApplication.h"
  9. namespace BansheeEngine
  10. {
  11. Prefab::Prefab()
  12. :Resource(false), mHash(0), mNextLinkId(0), mIsScene(true)
  13. {
  14. }
  15. Prefab::~Prefab()
  16. {
  17. if (mRoot != nullptr)
  18. mRoot->destroy(true);
  19. }
  20. HPrefab Prefab::create(const HSceneObject& sceneObject, bool isScene)
  21. {
  22. SPtr<Prefab> newPrefab = createEmpty();
  23. newPrefab->mIsScene = isScene;
  24. newPrefab->initialize(sceneObject);
  25. HPrefab handle = static_resource_cast<Prefab>(gResources()._createResourceHandle(newPrefab));
  26. newPrefab->mUUID = handle.getUUID();
  27. sceneObject->mPrefabLinkUUID = newPrefab->mUUID;
  28. newPrefab->_getRoot()->mPrefabLinkUUID = newPrefab->mUUID;
  29. return handle;
  30. }
  31. SPtr<Prefab> Prefab::createEmpty()
  32. {
  33. SPtr<Prefab> newPrefab = bs_core_ptr<Prefab>(new (bs_alloc<Prefab>()) Prefab());
  34. newPrefab->_setThisPtr(newPrefab);
  35. return newPrefab;
  36. }
  37. void Prefab::initialize(const HSceneObject& sceneObject)
  38. {
  39. sceneObject->mPrefabDiff = nullptr;
  40. UINT32 newNextLinkId = PrefabUtility::generatePrefabIds(sceneObject, mNextLinkId);
  41. if (newNextLinkId < mNextLinkId)
  42. {
  43. BS_EXCEPT(InternalErrorException, "Prefab ran out of IDs to assign. " \
  44. "Consider increasing the size of the prefab ID data type.");
  45. }
  46. mNextLinkId = newNextLinkId;
  47. // If there are any child prefab instances, make sure to update their diffs so they are saved with this prefab
  48. Stack<HSceneObject> todo;
  49. todo.push(sceneObject);
  50. while (!todo.empty())
  51. {
  52. HSceneObject current = todo.top();
  53. todo.pop();
  54. UINT32 childCount = current->getNumChildren();
  55. for (UINT32 i = 0; i < childCount; i++)
  56. {
  57. HSceneObject child = current->getChild(i);
  58. if (!child->mPrefabLinkUUID.empty())
  59. PrefabUtility::recordPrefabDiff(child);
  60. else
  61. todo.push(child);
  62. }
  63. }
  64. // Clone the hierarchy for internal storage
  65. mRoot = sceneObject->clone(false);
  66. mRoot->mParent = nullptr;
  67. // Remove objects with "dont save" flag
  68. todo.push(mRoot);
  69. while (!todo.empty())
  70. {
  71. HSceneObject current = todo.top();
  72. todo.pop();
  73. if (current->hasFlag(SOF_DontSave))
  74. current->destroy();
  75. else
  76. {
  77. UINT32 numChildren = current->getNumChildren();
  78. for (UINT32 i = 0; i < numChildren; i++)
  79. todo.push(current->getChild(i));
  80. }
  81. }
  82. }
  83. void Prefab::update(const HSceneObject& sceneObject)
  84. {
  85. initialize(sceneObject);
  86. sceneObject->mPrefabLinkUUID = mUUID;
  87. mRoot->mPrefabLinkUUID = mUUID;
  88. mHash++;
  89. }
  90. void Prefab::_updateChildInstances()
  91. {
  92. Stack<HSceneObject> todo;
  93. todo.push(mRoot);
  94. while (!todo.empty())
  95. {
  96. HSceneObject current = todo.top();
  97. todo.pop();
  98. UINT32 childCount = current->getNumChildren();
  99. for (UINT32 i = 0; i < childCount; i++)
  100. {
  101. HSceneObject child = current->getChild(i);
  102. if (!child->mPrefabLinkUUID.empty())
  103. PrefabUtility::updateFromPrefab(child);
  104. else
  105. todo.push(child);
  106. }
  107. }
  108. }
  109. HSceneObject Prefab::instantiate()
  110. {
  111. if (mRoot == nullptr)
  112. return HSceneObject();
  113. #if BS_EDITOR_BUILD
  114. if (gCoreApplication().isEditor())
  115. {
  116. // Update any child prefab instances in case their prefabs changed
  117. _updateChildInstances();
  118. }
  119. #endif
  120. HSceneObject clone = _clone();
  121. clone->_instantiate();
  122. return clone;
  123. }
  124. HSceneObject Prefab::_clone()
  125. {
  126. if (mRoot == nullptr)
  127. return HSceneObject();
  128. mRoot->mPrefabHash = mHash;
  129. return mRoot->clone(false);
  130. }
  131. RTTITypeBase* Prefab::getRTTIStatic()
  132. {
  133. return PrefabRTTI::instance();
  134. }
  135. RTTITypeBase* Prefab::getRTTI() const
  136. {
  137. return Prefab::getRTTIStatic();
  138. }
  139. }