CommandHierarchyItemToggleIsVisible.cpp 2.0 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. #include "EditorCommon.h"
  9. CommandHierarchyItemToggleIsVisible::CommandHierarchyItemToggleIsVisible(UndoStack* stack,
  10. HierarchyWidget* hierarchy,
  11. const HierarchyItemRawPtrList& items)
  12. : QUndoCommand()
  13. , m_stack(stack)
  14. , m_hierarchy(hierarchy)
  15. , m_ids()
  16. , m_toIsVisible(false)
  17. {
  18. setText((items.size() == 1) ? QString("toggle visibility of \"%1\"").arg((*items.begin())->GetElement()->GetName().c_str()) : QString("toggle visibility"));
  19. for (auto i : items)
  20. {
  21. m_ids.push_back(i->GetEntityId());
  22. }
  23. UiEditorBus::EventResult(m_toIsVisible, m_ids.front(), &UiEditorBus::Events::GetIsVisible);
  24. m_toIsVisible = !m_toIsVisible;
  25. }
  26. void CommandHierarchyItemToggleIsVisible::undo()
  27. {
  28. UndoStackExecutionScope s(m_stack);
  29. SetIsVisible(!m_toIsVisible);
  30. }
  31. void CommandHierarchyItemToggleIsVisible::redo()
  32. {
  33. UndoStackExecutionScope s(m_stack);
  34. SetIsVisible(m_toIsVisible);
  35. }
  36. void CommandHierarchyItemToggleIsVisible::SetIsVisible(bool isVisible)
  37. {
  38. if (!HierarchyHelpers::AllItemExists(m_hierarchy, m_ids))
  39. {
  40. // At least one item is missing.
  41. // Nothing to do.
  42. return;
  43. }
  44. for (auto i : m_ids)
  45. {
  46. AZ::Entity* element = EntityHelpers::GetEntity(i);
  47. // This will do both the Runtime-side and Editor-side.
  48. HierarchyItem::RttiCast(HierarchyHelpers::ElementToItem(m_hierarchy, element, false))->SetIsVisible(isVisible);
  49. }
  50. }
  51. void CommandHierarchyItemToggleIsVisible::Push(UndoStack* stack,
  52. HierarchyWidget* hierarchy,
  53. const HierarchyItemRawPtrList& items)
  54. {
  55. if (stack->GetIsExecuting())
  56. {
  57. // This is a redundant Qt notification.
  58. // Nothing else to do.
  59. return;
  60. }
  61. stack->push(new CommandHierarchyItemToggleIsVisible(stack,
  62. hierarchy,
  63. items));
  64. }