BsCmdReparentSO.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "BsCmdReparentSO.h"
  2. #include "BsSceneObject.h"
  3. namespace BansheeEngine
  4. {
  5. CmdReparentSO::CmdReparentSO(const WString& description, const Vector<HSceneObject>& sceneObjects, const HSceneObject& newParent)
  6. :EditorCommand(description), 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. const WString& description)
  15. {
  16. // Register command and commit it
  17. CmdReparentSO* command = new (bs_alloc<CmdReparentSO>()) CmdReparentSO(description, sceneObjects, newParent);
  18. UndoRedo::instance().registerCommand(command);
  19. command->commit();
  20. }
  21. void CmdReparentSO::execute(HSceneObject& sceneObject, const HSceneObject& newParent, const WString& description)
  22. {
  23. // Register command and commit it
  24. CmdReparentSO* command = new (bs_alloc<CmdReparentSO>()) CmdReparentSO(description, { sceneObject }, newParent);
  25. UndoRedo::instance().registerCommand(command);
  26. command->commit();
  27. }
  28. void CmdReparentSO::commit()
  29. {
  30. if(mNewParent.isDestroyed())
  31. return;
  32. UINT32 cnt = 0;
  33. for(auto& sceneObject : mSceneObjects)
  34. {
  35. if(!sceneObject.isDestroyed())
  36. sceneObject->setParent(mNewParent);
  37. cnt++;
  38. }
  39. }
  40. void CmdReparentSO::revert()
  41. {
  42. UINT32 cnt = 0;
  43. for(auto& sceneObject : mSceneObjects)
  44. {
  45. if(!sceneObject.isDestroyed() && !mOldParents[cnt].isDestroyed())
  46. sceneObject->setParent(mOldParents[cnt]);
  47. cnt++;
  48. }
  49. }
  50. }