ScriptCanvasGraphCommand.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 <AzCore/Component/EntityId.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzToolsFramework/Undo/UndoSystem.h>
  12. #include <ScriptCanvas/Bus/UndoBus.h>
  13. #include <ScriptCanvas/Core/Core.h>
  14. namespace ScriptCanvasEditor
  15. {
  16. class GraphItemCommandNotifications
  17. : public AZ::EBusTraits
  18. {
  19. public:
  20. static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::ById;
  21. using BusIdType = ScriptCanvas::ScriptCanvasId;
  22. virtual void PreRestore(const UndoData& /*oldData*/) {};
  23. virtual void PostRestore(const UndoData& /* restoredData*/) {};
  24. };
  25. using GraphItemCommandNotificationBus = AZ::EBus<GraphItemCommandNotifications>;
  26. // This command is the base URSequencePoint command from which all Script Canvas undo/redo commands derive
  27. class GraphItemCommand
  28. : public AzToolsFramework::UndoSystem::URSequencePoint
  29. {
  30. public:
  31. AZ_RTTI(GraphItemCommand, "{94B50FAC-ACAF-4B9B-BA3C-9F3EE36854BA}", AzToolsFramework::UndoSystem::URSequencePoint);
  32. AZ_CLASS_ALLOCATOR(GraphItemCommand, AZ::SystemAllocator);
  33. explicit GraphItemCommand(AZStd::string_view friendlyName);
  34. ~GraphItemCommand() override = default;
  35. void Undo() override;
  36. void Redo() override;
  37. virtual void Capture(EditorGraph* graph, bool captureUndo);
  38. bool Changed() const override;
  39. protected:
  40. GraphItemCommand(const GraphItemCommand&) = delete;
  41. const GraphItemCommand& operator=(const GraphItemCommand&) = delete;
  42. virtual void RestoreItem(const AZStd::vector<AZ::u8>& restoreBuffer);
  43. AZ::EntityId m_graphCanvasGraphId;
  44. AZ::EntityId m_scriptCanvasEntityId; ///< The id of the ScriptCanvas Entity with the Script Canvas Graph and Graph Canvas Scene
  45. ScriptCanvas::ScriptCanvasId m_scriptCanvasId;
  46. AZStd::vector<AZ::u8> m_undoState;
  47. AZStd::vector<AZ::u8> m_redoState;
  48. };
  49. // This commands captures a modification to a Script Canvas graph involving either a ScriptCanvas Node/Connection or a GraphCanvas Node/Connection
  50. class GraphItemChangeCommand
  51. : public GraphItemCommand
  52. {
  53. public:
  54. AZ_RTTI(GraphItemChangeCommand, "{9F8805F7-61CD-40FC-B426-020925F4E3DB}", GraphItemCommand);
  55. AZ_CLASS_ALLOCATOR(GraphItemChangeCommand, AZ::SystemAllocator);
  56. GraphItemChangeCommand(AZStd::string_view friendlyName);
  57. ~GraphItemChangeCommand() = default;
  58. void Undo() override;
  59. void Redo() override;
  60. void Capture(EditorGraph* graph, bool captureUndo) override;
  61. void RestoreItem(const AZStd::vector<AZ::u8>& restoreBuffer) override;
  62. protected:
  63. GraphItemChangeCommand(const GraphItemChangeCommand&) = delete;
  64. const GraphItemChangeCommand& operator=(const GraphItemChangeCommand&) = delete;
  65. void DeleteOldGraphData(const UndoData& oldData);
  66. void ActivateRestoredGraphData(const UndoData& restoredData);
  67. };
  68. // This command captures when a node or connection is added to the Script Canvas graph.
  69. class GraphItemAddCommand
  70. : public GraphItemChangeCommand
  71. {
  72. public:
  73. AZ_RTTI(GraphItemAddCommand, "{01E6BC39-0A2C-4C05-9384-804A63321D62}", GraphItemChangeCommand);
  74. AZ_CLASS_ALLOCATOR(GraphItemAddCommand, AZ::SystemAllocator);
  75. explicit GraphItemAddCommand(AZStd::string_view friendlyName);
  76. ~GraphItemAddCommand() override = default;
  77. void Undo() override;
  78. void Redo() override;
  79. void Capture(EditorGraph* graph, bool captureUndo) override;
  80. protected:
  81. GraphItemAddCommand(const GraphItemAddCommand&) = delete;
  82. const GraphItemAddCommand& operator=(const GraphItemAddCommand&) = delete;
  83. };
  84. // This command captures when a node or connection is removed from the Script Canvas graph
  85. class GraphItemRemovalCommand
  86. : public GraphItemChangeCommand
  87. {
  88. public:
  89. AZ_RTTI(GraphItemRemovalCommand, "{6257B3EC-E9E8-4419-AA25-2A768C21B635}", GraphItemChangeCommand);
  90. AZ_CLASS_ALLOCATOR(GraphItemRemovalCommand, AZ::SystemAllocator);
  91. explicit GraphItemRemovalCommand(AZStd::string_view friendlyName);
  92. ~GraphItemRemovalCommand() override = default;
  93. void Undo() override;
  94. void Redo() override;
  95. void Capture(EditorGraph* graph, bool captureUndo) override;
  96. protected:
  97. GraphItemRemovalCommand(const GraphItemRemovalCommand&) = delete;
  98. const GraphItemRemovalCommand& operator=(const GraphItemRemovalCommand&) = delete;
  99. };
  100. }