CommandCanvasPropertiesChange.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "EditorCommon.h"
  9. CommandCanvasPropertiesChange::CommandCanvasPropertiesChange(UndoStack* stack,
  10. AZStd::string& undoXml, AZStd::string& redoXml, EditorWindow* editorWindow, const char* commandName)
  11. : QUndoCommand()
  12. , m_stack(stack)
  13. , m_isFirstExecution(true)
  14. , m_selectionWasEmpty(true)
  15. , m_undoXml(undoXml)
  16. , m_redoXml(redoXml)
  17. , m_editorWindow(editorWindow)
  18. {
  19. setText(commandName);
  20. }
  21. void CommandCanvasPropertiesChange::undo()
  22. {
  23. UndoStackExecutionScope s(m_stack);
  24. Recreate(true);
  25. // Some canvas properties (such as whether guides are locked) affect the menus
  26. m_editorWindow->RefreshEditorMenu();
  27. }
  28. void CommandCanvasPropertiesChange::redo()
  29. {
  30. UndoStackExecutionScope s(m_stack);
  31. Recreate(false);
  32. // Some canvas properties (such as whether guides are locked) affect the menus
  33. m_editorWindow->RefreshEditorMenu();
  34. }
  35. void CommandCanvasPropertiesChange::Recreate(bool isUndo)
  36. {
  37. if (m_isFirstExecution)
  38. {
  39. m_isFirstExecution = false;
  40. HierarchyWidget* hierarchyWidget = m_editorWindow->GetHierarchy();
  41. if (hierarchyWidget)
  42. {
  43. m_selectionWasEmpty = (hierarchyWidget->CurrentSelectedElement()) ? false : true;
  44. }
  45. // Nothing else to do.
  46. }
  47. else
  48. {
  49. // We are going to load a saved canvas from XML and replace the existing canvas
  50. // with it.
  51. // Create a new entity context for the new canvas
  52. UiEditorEntityContext* newEntityContext = new UiEditorEntityContext(m_editorWindow);
  53. // Create a new canvas from the XML and release the old canvas, use the new entity context for
  54. // the new canvas
  55. const AZStd::string& xml = isUndo ? m_undoXml : m_redoXml;
  56. AZ::Interface<ILyShine>::Get()->ReloadCanvasFromXml(xml, newEntityContext);
  57. // Tell the editor window to use the new entity context
  58. m_editorWindow->ReplaceEntityContext(newEntityContext);
  59. // Tell the UI animation system that the active canvas has changed
  60. UiEditorAnimationBus::Broadcast(&UiEditorAnimationBus::Events::ActiveCanvasChanged);
  61. // Some toolbar sections display canvas properties
  62. ViewportWidget* viewportWidget = m_editorWindow->GetViewport();
  63. if (viewportWidget)
  64. {
  65. viewportWidget->GetViewportInteraction()->InitializeToolbars();
  66. }
  67. // Clear any selected elements from the hierarchy widget. If an element is selected,
  68. // this will trigger the properties pane to refresh with the new canvas, but the
  69. // refresh is on a timer so it won't happen right away.
  70. HierarchyWidget* hierarchyWidget = m_editorWindow->GetHierarchy();
  71. if (hierarchyWidget)
  72. {
  73. // check if the selection was empty when the command was executed,
  74. // if so we set the selection back to empty so that the properties pane shows the
  75. // canvas properties and the result of the undo can be seen
  76. if (m_selectionWasEmpty)
  77. {
  78. hierarchyWidget->SetUniqueSelectionHighlight((QTreeWidgetItem*)nullptr);
  79. }
  80. }
  81. // Tell the properties pane that the entity pointer changed
  82. PropertiesWidget* propertiesWidget = m_editorWindow->GetProperties();
  83. if (propertiesWidget)
  84. {
  85. propertiesWidget->SelectedEntityPointersChanged();
  86. }
  87. }
  88. }
  89. void CommandCanvasPropertiesChange::Push(UndoStack* stack, AZStd::string& undoXml,
  90. AZStd::string& redoXml, EditorWindow* editorWindow, const char* commandName)
  91. {
  92. if (stack->GetIsExecuting())
  93. {
  94. // This is a redundant Qt notification.
  95. // Nothing else to do.
  96. return;
  97. }
  98. stack->push(new CommandCanvasPropertiesChange(stack, undoXml, redoXml, editorWindow, commandName));
  99. }