BsCmdUtility.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "BsCmdUtility.h"
  2. #include "BsSceneObject.h"
  3. namespace BansheeEngine
  4. {
  5. CmdUtility::SceneObjProxy CmdUtility::createProxy(const HSceneObject& sceneObject)
  6. {
  7. struct TempData
  8. {
  9. TempData(SceneObjProxy& proxy, const HSceneObject& so)
  10. :proxy(proxy), obj(so)
  11. { }
  12. SceneObjProxy& proxy;
  13. HSceneObject obj;
  14. };
  15. SceneObjProxy rootProxy;
  16. Stack<TempData> todo;
  17. todo.push(TempData(rootProxy, sceneObject));
  18. while (!todo.empty())
  19. {
  20. TempData data = todo.top();
  21. todo.pop();
  22. data.proxy.instanceData = data.obj->_getInstanceData();
  23. const Vector<HComponent>& components = data.obj->getComponents();
  24. for (auto& component : components)
  25. data.proxy.componentInstanceData.push_back(component->_getInstanceData());
  26. UINT32 numChildren = data.obj->getNumChildren();
  27. data.proxy.children.resize(numChildren);
  28. for (UINT32 i = 0; i < numChildren; i++)
  29. {
  30. todo.push(TempData(data.proxy.children[i], data.obj->getChild(i)));
  31. }
  32. }
  33. return rootProxy;
  34. }
  35. void CmdUtility::restoreIds(const HSceneObject& restored, SceneObjProxy& proxy)
  36. {
  37. // Note: This method relies that all restored GameObject handles pointing to the
  38. // same object also have the same shared handle data (Since I only update instance
  39. // data on a single handle I know exists, and expect all others will be updated
  40. // by that as well).
  41. struct TempData
  42. {
  43. TempData(SceneObjProxy& proxy, const HSceneObject& restoredObj)
  44. :proxy(proxy), restoredObj(restoredObj)
  45. { }
  46. SceneObjProxy& proxy;
  47. HSceneObject restoredObj;
  48. };
  49. Stack<TempData> todo;
  50. todo.push(TempData(proxy, restored));
  51. while (!todo.empty())
  52. {
  53. TempData data = todo.top();
  54. todo.pop();
  55. data.restoredObj->_setInstanceData(data.proxy.instanceData);
  56. // Find components that are still active and swap the old ones with restored ones,
  57. // keep any other as is.
  58. const Vector<HComponent>& restoredComponents = data.restoredObj->getComponents();
  59. UINT32 idx = 0;
  60. for (auto& restoredComponent : restoredComponents)
  61. {
  62. restoredComponent->_setInstanceData(data.proxy.componentInstanceData[idx]);
  63. GameObjectPtr restoredPtr = std::static_pointer_cast<GameObject>(restoredComponent.getInternalPtr());
  64. HComponent restoredComponentCopy = restoredComponent; // To remove const
  65. restoredComponentCopy._setHandleData(restoredPtr);
  66. idx++;
  67. }
  68. // Find children that are still active and swap the old ones with restored ones,
  69. // keep any other as is
  70. UINT32 restoredNumChildren = data.restoredObj->getNumChildren();
  71. for (UINT32 i = 0; i < restoredNumChildren; i++)
  72. {
  73. todo.push(TempData(data.proxy.children[i], data.restoredObj->getChild(i)));
  74. }
  75. }
  76. }
  77. }