SerializeHelpers.h 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/Slice/SliceComponent.h>
  10. class UiEditorEntityContext;
  11. namespace SerializeHelpers
  12. {
  13. //! A struct that represents the data required to recreate one UI element and its
  14. //! descendant elements for undo/redo.
  15. //! It stores serialized saves for undo and redo. Each contains the element and its descendant elements
  16. //! along with any prefab references for the element or its children. It also stores where in the
  17. //! element hierarchy to restore it to.
  18. struct SerializedEntry
  19. {
  20. AZ::EntityId m_id;
  21. AZ::EntityId m_parentId;
  22. AZ::EntityId m_insertAboveThisId;
  23. AZStd::string m_undoXml;
  24. AZStd::string m_redoXml;
  25. AZStd::unordered_set<AZ::Data::AssetId> m_referencedSliceAssets;
  26. };
  27. //! A list of serialized elements
  28. using SerializedEntryList = std::list< SerializedEntry >;
  29. //! A vector of EntityRestoreInfo structs
  30. using EntityRestoreVec = AZStd::vector<AZ::SliceComponent::EntityRestoreInfo>;
  31. //! Restore UI elements and their children from the given xml
  32. //! Slice instance info is preserved
  33. //! \param canvasEntityId The entity ID of the UI canvas that contains the UI elements
  34. //! \param parent The parent element that the unserialized top-level elements will be children of, if nullptr the root element is the parent
  35. //! \param insertBefore The sibling element to place the top-level elements before, if nullptr then add as last child
  36. //! \param entityContext The UI Editor entity context for this UI canvas
  37. //! \param xml The XML string to unserialize, it contains all the elements puts slice restore info
  38. //! \param isCopyOperation True if we are creating new elements rather than restoring delete elements
  39. //! \param cumulativeListOfCreatedEntities If this is non-null then all the entities created are added to this list
  40. void RestoreSerializedElements(
  41. AZ::EntityId canvasEntityId,
  42. AZ::Entity* parent,
  43. AZ::Entity* insertBefore,
  44. UiEditorEntityContext* entityContext,
  45. const AZStd::string& xml,
  46. bool isCopyOperation,
  47. LyShine::EntityArray* cumulativeListOfCreatedEntities);
  48. //! Save the given elements to an XML string
  49. //! \param elements The top-level elements to save - all descendant elements will be saved also
  50. //! \param rootSlice The root slice for the canvas
  51. //! \param isCopyOperation True if this is a copy or cut operation, false if it is part of undo/redo
  52. //! \param referencedSliceAssets Out param, all prefab assets used by the saved elements
  53. AZStd::string SaveElementsToXmlString(
  54. const LyShine::EntityArray& elements,
  55. AZ::SliceComponent* rootSlice,
  56. bool isCopyOperation,
  57. AZStd::unordered_set<AZ::Data::AssetId>& referencedSliceAssets);
  58. //! Load elements from an XML string that was created by SaveElementsToXmlString
  59. //! \param canvasEntityId The entity ID of the UI canvas that contains the UI elements
  60. //! \param string The XML string containing the elements and associated data
  61. //! \param makeNewIDs If true new entity IDs and Element Ids will be created for the created elements
  62. //! \param insertBefore The sibling element to place the top-level elements before, if nullptr then add as last child
  63. //! \param entityContext The UI Editor entity context for this UI canvas
  64. //! \param listOfCreatedTopLevelElements Out param, the top-level elements created
  65. //! \param listOfAllCreatedElements Out param, all elements that were created
  66. //! \param entityRestoreInfos Out param, the slice infos in the same order as listOfAllCreatedElements
  67. void LoadElementsFromXmlString(
  68. AZ::EntityId canvasEntityId,
  69. const AZStd::string& string,
  70. bool makeNewIDs,
  71. AZ::Entity* insertionPoint,
  72. AZ::Entity* insertBefore,
  73. LyShine::EntityArray& listOfCreatedTopLevelElements,
  74. LyShine::EntityArray& listOfAllCreatedElements,
  75. EntityRestoreVec& entityRestoreInfos);
  76. } // namespace EntityHelpers