BsCmdCloneSO.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. }
  11. CmdCloneSO::~CmdCloneSO()
  12. {
  13. }
  14. HSceneObject CmdCloneSO::execute(const HSceneObject& sceneObject, const String& description)
  15. {
  16. // Register command and commit it
  17. CmdCloneSO* command = new (bs_alloc<CmdCloneSO>()) CmdCloneSO(description, { sceneObject });
  18. SPtr<CmdCloneSO> commandPtr = bs_shared_ptr(command);
  19. UndoRedo::instance().registerCommand(commandPtr);
  20. commandPtr->commit();
  21. if (commandPtr->mClones.size() > 0)
  22. return commandPtr->mClones[0];
  23. return HSceneObject();
  24. }
  25. Vector<HSceneObject> CmdCloneSO::execute(const Vector<HSceneObject>& sceneObjects, const String& description)
  26. {
  27. // Register command and commit it
  28. CmdCloneSO* command = new (bs_alloc<CmdCloneSO>()) CmdCloneSO(description, sceneObjects);
  29. SPtr<CmdCloneSO> commandPtr = bs_shared_ptr(command);
  30. UndoRedo::instance().registerCommand(commandPtr);
  31. commandPtr->commit();
  32. return commandPtr->mClones;
  33. }
  34. void CmdCloneSO::commit()
  35. {
  36. mClones.clear();
  37. for (auto& original : mOriginals)
  38. {
  39. if (!original.isDestroyed())
  40. mClones.push_back(original->clone());
  41. }
  42. }
  43. void CmdCloneSO::revert()
  44. {
  45. for (auto& clone : mClones)
  46. {
  47. if (!clone.isDestroyed())
  48. clone->destroy(true);
  49. }
  50. mClones.clear();
  51. }
  52. }