BsCmdReparentSO.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "UndoRedo/BsCmdReparentSO.h"
  4. #include "Scene/BsSceneObject.h"
  5. namespace bs
  6. {
  7. CmdReparentSO::CmdReparentSO(const String& description, const Vector<HSceneObject>& sceneObjects, const HSceneObject& newParent)
  8. :EditorCommand(description), mSceneObjects(sceneObjects), mNewParent(newParent)
  9. {
  10. for(auto& sceneObject : mSceneObjects)
  11. {
  12. mOldParents.push_back(sceneObject->getParent());
  13. }
  14. }
  15. void CmdReparentSO::execute(const Vector<HSceneObject>& sceneObjects, const HSceneObject& newParent,
  16. const String& description)
  17. {
  18. // Register command and commit it
  19. CmdReparentSO* command = new (bs_alloc<CmdReparentSO>()) CmdReparentSO(description, sceneObjects, newParent);
  20. SPtr<CmdReparentSO> commandPtr = bs_shared_ptr(command);
  21. UndoRedo::instance().registerCommand(commandPtr);
  22. commandPtr->commit();
  23. }
  24. void CmdReparentSO::execute(HSceneObject& sceneObject, const HSceneObject& newParent, const String& description)
  25. {
  26. // Register command and commit it
  27. CmdReparentSO* command = new (bs_alloc<CmdReparentSO>()) CmdReparentSO(description, { sceneObject }, newParent);
  28. SPtr<CmdReparentSO> commandPtr = bs_shared_ptr(command);
  29. UndoRedo::instance().registerCommand(commandPtr);
  30. commandPtr->commit();
  31. }
  32. void CmdReparentSO::commit()
  33. {
  34. if(mNewParent.isDestroyed())
  35. return;
  36. UINT32 cnt = 0;
  37. for(auto& sceneObject : mSceneObjects)
  38. {
  39. if(!sceneObject.isDestroyed())
  40. sceneObject->setParent(mNewParent);
  41. cnt++;
  42. }
  43. }
  44. void CmdReparentSO::revert()
  45. {
  46. UINT32 cnt = 0;
  47. for(auto& sceneObject : mSceneObjects)
  48. {
  49. if(!sceneObject.isDestroyed() && !mOldParents[cnt].isDestroyed())
  50. sceneObject->setParent(mOldParents[cnt]);
  51. cnt++;
  52. }
  53. }
  54. }