BsCmdCloneSO.cpp 1.6 KB

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