BsCmdUtility.cpp 3.0 KB

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