The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "../UI/jucer_JucerDocumentEditor.h"
  20. #include "jucer_PaintElementGroup.h"
  21. //==============================================================================
  22. template <class ElementType>
  23. class PaintElementUndoableAction : public UndoableAction
  24. {
  25. public:
  26. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6011)
  27. PaintElementUndoableAction (ElementType* const element)
  28. : routine (*element->getOwner()),
  29. elementIndex (element->getOwner()->indexOfElement (element))
  30. {
  31. jassert (element != nullptr);
  32. if (element != nullptr && elementIndex < 0)
  33. findGroupIndices (element->getOwner(), element);
  34. jassert (elementIndex >= 0);
  35. }
  36. JUCE_END_IGNORE_WARNINGS_MSVC
  37. ElementType* getElement() const
  38. {
  39. if (containerGroups.size() > 0)
  40. {
  41. auto group = dynamic_cast<PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  42. if (group == nullptr)
  43. return nullptr;
  44. for (int i = 1; i < containerGroups.size(); ++i)
  45. {
  46. group = dynamic_cast<PaintElementGroup*> (group->getElement (containerGroups.getUnchecked (i)));
  47. if (group == nullptr)
  48. return nullptr;
  49. }
  50. auto e = dynamic_cast<ElementType*> (group->getElement (elementIndex));
  51. jassert (e != nullptr);
  52. return e;
  53. }
  54. else
  55. {
  56. auto e = dynamic_cast<ElementType*> (routine.getElement (elementIndex));
  57. jassert (e != nullptr);
  58. return e;
  59. }
  60. }
  61. int getSizeInUnits() { return 2; }
  62. protected:
  63. PaintRoutine& routine;
  64. int elementIndex;
  65. Array <int> containerGroups;
  66. void changed() const
  67. {
  68. jassert (routine.getDocument() != nullptr);
  69. routine.getDocument()->changed();
  70. }
  71. void showCorrectTab() const
  72. {
  73. if (JucerDocumentEditor* const docHolder = JucerDocumentEditor::getActiveDocumentHolder())
  74. docHolder->showGraphics (&routine);
  75. if (routine.getSelectedElements().getNumSelected() == 0)
  76. if (ElementType* const e = dynamic_cast<ElementType*> (routine.getElement (elementIndex)))
  77. routine.getSelectedElements().selectOnly (e);
  78. }
  79. private:
  80. void findGroupIndices (PaintRoutine* const pr, PaintElement* const element)
  81. {
  82. for (int i = pr->getNumElements(); --i >= 0;)
  83. {
  84. if (auto pg = dynamic_cast<PaintElementGroup*> (pr->getElement (i)))
  85. {
  86. if (pg->containsElement (element))
  87. {
  88. containerGroups.add (i);
  89. findGroupIndices (pg, element);
  90. }
  91. }
  92. }
  93. }
  94. void findGroupIndices (PaintElementGroup* const group, PaintElement* const element)
  95. {
  96. elementIndex = group->indexOfElement (element);
  97. if (elementIndex < 0)
  98. {
  99. for (int i = group->getNumElements(); --i >= 0;)
  100. {
  101. if (auto pg = dynamic_cast<PaintElementGroup*> (group->getElement (i)))
  102. {
  103. if (pg->containsElement (element))
  104. {
  105. containerGroups.add (i);
  106. findGroupIndices (pg, element);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. PaintElementUndoableAction (const PaintElementUndoableAction&);
  113. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  114. };