ScriptCanvasUndoManager.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <ScriptCanvas/Bus/UndoBus.h>
  10. #include <AzToolsFramework/Undo/UndoSystem.h>
  11. #include <GraphCanvas/Editor/AssetEditorBus.h>
  12. namespace ScriptCanvasEditor
  13. {
  14. class ScopedUndoBatch
  15. {
  16. public:
  17. ScopedUndoBatch(AZStd::string_view undoLabel);
  18. ~ScopedUndoBatch();
  19. private:
  20. ScopedUndoBatch(const ScopedUndoBatch&) = delete;
  21. ScopedUndoBatch& operator=(const ScopedUndoBatch&) = delete;
  22. };
  23. // This cache maintains the previous state of the Script Canvas graph items recorded for Undo
  24. class UndoCache
  25. {
  26. public:
  27. AZ_CLASS_ALLOCATOR(UndoCache, AZ::SystemAllocator);
  28. UndoCache() = default;
  29. ~UndoCache() = default;
  30. // Update the graph item within the cache
  31. void UpdateCache(ScriptCanvas::ScriptCanvasId);
  32. // remove the graph item from the cache
  33. void PurgeCache(ScriptCanvas::ScriptCanvasId);
  34. // retrieve the last known state for the graph item
  35. const AZStd::vector<AZ::u8>& Retrieve(ScriptCanvas::ScriptCanvasId);
  36. // Populate the cache from a ScriptCanvas Entity graph entity
  37. void PopulateCache(ScriptCanvas::ScriptCanvasId);
  38. // clear the entire cache:
  39. void Clear();
  40. protected:
  41. AZStd::unordered_map <ScriptCanvas::ScriptCanvasId, AZStd::vector<AZ::u8>> m_dataMap; ///< Stores an Entity Id to serialized graph data(Node/Connection) map
  42. AZStd::vector<AZ::u8> m_emptyData;
  43. };
  44. class SceneUndoState
  45. {
  46. public:
  47. AZ_CLASS_ALLOCATOR(SceneUndoState, AZ::SystemAllocator);
  48. SceneUndoState() = default;
  49. SceneUndoState(AzToolsFramework::UndoSystem::IUndoNotify* undoNotify);
  50. ~SceneUndoState();
  51. void BeginUndoBatch(AZStd::string_view label);
  52. void EndUndoBatch();
  53. AZStd::unique_ptr<UndoCache> m_undoCache;
  54. AZStd::unique_ptr<AzToolsFramework::UndoSystem::UndoStack> m_undoStack;
  55. AzToolsFramework::UndoSystem::URSequencePoint* m_currentUndoBatch = nullptr;
  56. };
  57. }