BsCmdReparentSO.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "BsCmdReparentSO.h"
  2. #include "CmSceneObject.h"
  3. using namespace BansheeEngine;
  4. using namespace CamelotFramework;
  5. namespace BansheeEditor
  6. {
  7. CmdReparentSO::CmdReparentSO(const CM::Vector<CM::HSceneObject>::type& sceneObjects, const CM::HSceneObject& newParent)
  8. :mSceneObjects(sceneObjects), mNewParent(newParent)
  9. {
  10. for(auto& sceneObject : mSceneObjects)
  11. {
  12. mOldParents.push_back(sceneObject->getParent());
  13. }
  14. }
  15. void CmdReparentSO::execute(const CM::Vector<CM::HSceneObject>::type& sceneObjects, const CM::HSceneObject& newParent)
  16. {
  17. // Register command and commit it
  18. CmdReparentSO* command = new (cm_alloc<CmdReparentSO>()) CmdReparentSO(sceneObjects, newParent);
  19. UndoRedo::instance().registerCommand(command);
  20. command->commit();
  21. }
  22. void CmdReparentSO::commit()
  23. {
  24. if(mNewParent.isDestroyed())
  25. return;
  26. UINT32 cnt = 0;
  27. for(auto& sceneObject : mSceneObjects)
  28. {
  29. if(!sceneObject.isDestroyed())
  30. sceneObject->setParent(mNewParent);
  31. cnt++;
  32. }
  33. }
  34. void CmdReparentSO::revert()
  35. {
  36. UINT32 cnt = 0;
  37. for(auto& sceneObject : mSceneObjects)
  38. {
  39. if(!sceneObject.isDestroyed() && !mOldParents[cnt].isDestroyed())
  40. sceneObject->setParent(mOldParents[cnt]);
  41. cnt++;
  42. }
  43. }
  44. }