BsPrefab.cpp 3.8 KB

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