BsCmdReparentSO.cpp 1.8 KB

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