ScriptCanvasGraphCommand.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include <AzCore/Component/ComponentApplicationBus.h>
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/Serialization/Utils.h>
  11. #include <ScriptCanvas/Core/Graph.h>
  12. #include <ScriptCanvas/Components/EditorGraph.h>
  13. #include <ScriptCanvas/Variable/VariableBus.h>
  14. #include <GraphCanvas/Components/SceneBus.h>
  15. #include <Editor/Undo/ScriptCanvasGraphCommand.h>
  16. #include <Editor/Include/ScriptCanvas/Bus/EditorScriptCanvasBus.h>
  17. namespace ScriptCanvasEditor
  18. {
  19. GraphItemCommand::GraphItemCommand(AZStd::string_view friendlyName)
  20. : AzToolsFramework::UndoSystem::URSequencePoint(friendlyName, 0)
  21. {
  22. }
  23. void GraphItemCommand::Undo()
  24. {
  25. }
  26. void GraphItemCommand::Redo()
  27. {
  28. }
  29. void GraphItemCommand::Capture(EditorGraph*, bool)
  30. {
  31. }
  32. bool GraphItemCommand::Changed() const
  33. {
  34. return true;
  35. }
  36. void GraphItemCommand::RestoreItem(const AZStd::vector<AZ::u8>&)
  37. {
  38. }
  39. void GraphItemChangeCommand::DeleteOldGraphData(const UndoData& oldData)
  40. {
  41. EditorGraphRequestBus::Event(m_scriptCanvasId, &EditorGraphRequests::ClearGraphCanvasScene);
  42. ScriptCanvas::GraphVariableManagerRequestBus::Event(m_scriptCanvasId, &ScriptCanvas::GraphVariableManagerRequests::DeleteVariableData, oldData.m_variableData);
  43. }
  44. void GraphItemChangeCommand::ActivateRestoredGraphData(const UndoData& restoredData)
  45. {
  46. // Reset VariableData on the GraphVariableManager before re-init node entities, as the GetVariableNode/SetVariable Node
  47. // queries the VariableRequestBus
  48. ScriptCanvas::GraphVariableManagerRequestBus::Event(m_scriptCanvasId, &ScriptCanvas::GraphVariableManagerRequests::SetVariableData, restoredData.m_variableData);
  49. // Init Script Canvas Graph Data
  50. for (auto entityContainer : { restoredData.m_graphData.m_nodes })
  51. {
  52. for (AZ::Entity* entity : entityContainer)
  53. {
  54. if (entity->GetState() == AZ::Entity::State::Constructed)
  55. {
  56. entity->Init();
  57. }
  58. }
  59. }
  60. for (auto entityContainer : { restoredData.m_graphData.m_connections })
  61. {
  62. for (AZ::Entity* entity : entityContainer)
  63. {
  64. if (entity->GetState() == AZ::Entity::State::Constructed)
  65. {
  66. entity->Init();
  67. }
  68. }
  69. }
  70. ScriptCanvas::GraphRequestBus::Event(m_scriptCanvasId, &ScriptCanvas::GraphRequests::AddGraphData, restoredData.m_graphData);
  71. EditorGraphRequestBus::Event(m_scriptCanvasId, &EditorGraphRequests::UpdateGraphCanvasSaveData, restoredData.m_visualSaveData);
  72. }
  73. //// Graph Item Change Command
  74. GraphItemChangeCommand::GraphItemChangeCommand(AZStd::string_view friendlyName)
  75. : GraphItemCommand(friendlyName)
  76. {
  77. }
  78. void GraphItemChangeCommand::Undo()
  79. {
  80. RestoreItem(m_undoState);
  81. }
  82. void GraphItemChangeCommand::Redo()
  83. {
  84. RestoreItem(m_redoState);
  85. }
  86. void GraphItemChangeCommand::Capture(EditorGraph* graph, bool captureUndo)
  87. {
  88. m_scriptCanvasId = graph->GetScriptCanvasId();
  89. m_graphCanvasGraphId = graph->GetGraphCanvasGraphId();
  90. UndoCache* undoCache = nullptr;
  91. UndoRequestBus::EventResult(undoCache, m_scriptCanvasId, &UndoRequests::GetSceneUndoCache);
  92. if (!undoCache)
  93. {
  94. AZ_Error("Script Canvas", false, "Unable to find ScriptCanvas Undo Cache. Most likely the Undo Manager has no active scene");
  95. return;
  96. }
  97. if (captureUndo)
  98. {
  99. AZ_Assert(m_undoState.empty(), "Attempting to capture undo twice");
  100. m_undoState = undoCache->Retrieve(m_scriptCanvasId);
  101. if (m_undoState.empty())
  102. {
  103. undoCache->UpdateCache(m_scriptCanvasId);
  104. m_undoState = undoCache->Retrieve(m_scriptCanvasId);
  105. }
  106. undoCache->UpdateCache(m_scriptCanvasId);
  107. }
  108. else
  109. {
  110. UndoData undoData;
  111. UndoRequestBus::EventResult(undoData, m_scriptCanvasId , &UndoRequests::CreateUndoData);
  112. AZ::SerializeContext* serializeContext{};
  113. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  114. m_redoState.clear();
  115. AZ::IO::ByteContainerStream<AZStd::vector<AZ::u8>> byteStream(&m_redoState);
  116. AZ::ObjectStream* objStream = AZ::ObjectStream::Create(&byteStream, *serializeContext, AZ::DataStream::ST_BINARY);
  117. if (!objStream->WriteClass(&undoData))
  118. {
  119. AZ_Assert(false, "Unable to serialize Script Canvas scene and graph data for undo/redo");
  120. return;
  121. }
  122. objStream->Finalize();
  123. }
  124. }
  125. void GraphItemChangeCommand::RestoreItem(const AZStd::vector<AZ::u8>& restoreBuffer)
  126. {
  127. if (restoreBuffer.empty())
  128. {
  129. return;
  130. }
  131. AZ::SerializeContext* serializeContext{};
  132. AZ::ComponentApplicationBus::BroadcastResult(serializeContext, &AZ::ComponentApplicationRequests::GetSerializeContext);
  133. AZ::IO::MemoryStream byteStream(restoreBuffer.data(), restoreBuffer.size());
  134. UndoData oldData;
  135. UndoRequestBus::EventResult(oldData, m_scriptCanvasId , &UndoRequests::CreateUndoData);
  136. // Remove old Script Canvas data
  137. GraphItemCommandNotificationBus::Event(m_scriptCanvasId, &GraphItemCommandNotificationBus::Events::PreRestore, oldData);
  138. DeleteOldGraphData(oldData);
  139. UndoData restoreData;
  140. bool restoreSuccess = AZ::Utils::LoadObjectFromStreamInPlace(byteStream, restoreData, serializeContext, AZ::ObjectStream::FilterDescriptor(AZ::Data::AssetFilterNoAssetLoading));
  141. if (restoreSuccess)
  142. {
  143. ActivateRestoredGraphData(restoreData);
  144. UndoCache* undoCache = nullptr;
  145. UndoRequestBus::EventResult(undoCache, m_scriptCanvasId, &UndoRequests::GetSceneUndoCache);
  146. if (!undoCache)
  147. {
  148. AZ_Assert(false, "Unable to find ScriptCanvas Undo Cache. Most likely the ScriptCanvasEditor Undo Manager has not been created");
  149. return;
  150. }
  151. undoCache->UpdateCache(m_scriptCanvasEntityId);
  152. GraphItemCommandNotificationBus::Event(m_scriptCanvasId, &GraphItemCommandNotifications::PostRestore, restoreData);
  153. }
  154. }
  155. //// Graph Item Add Command
  156. GraphItemAddCommand::GraphItemAddCommand(AZStd::string_view friendlyName)
  157. : GraphItemChangeCommand(friendlyName)
  158. {
  159. }
  160. void GraphItemAddCommand::Undo()
  161. {
  162. RestoreItem(m_undoState);
  163. }
  164. void GraphItemAddCommand::Redo()
  165. {
  166. RestoreItem(m_redoState);
  167. }
  168. void GraphItemAddCommand::Capture(EditorGraph* graph, bool)
  169. {
  170. GraphItemChangeCommand::Capture(graph, false);
  171. }
  172. //// Graph Item Removal Command
  173. GraphItemRemovalCommand::GraphItemRemovalCommand(AZStd::string_view friendlyName)
  174. : GraphItemChangeCommand(friendlyName)
  175. {
  176. }
  177. void GraphItemRemovalCommand::Undo()
  178. {
  179. RestoreItem(m_undoState);
  180. }
  181. void GraphItemRemovalCommand::Redo()
  182. {
  183. RestoreItem(m_redoState);
  184. }
  185. void GraphItemRemovalCommand::Capture(EditorGraph* graph, bool)
  186. {
  187. GraphItemChangeCommand::Capture(graph, true);
  188. }
  189. }