BsPrefab.cpp 3.8 KB

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