BsCmdReparentSO.cpp 1.1 KB

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