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.

139 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. PaintElementUndoableAction (ElementType* const element)
  27. : routine (*element->getOwner()),
  28. elementIndex (element->getOwner()->indexOfElement (element))
  29. {
  30. jassert (element != nullptr);
  31. if (elementIndex < 0)
  32. findGroupIndices (element->getOwner(), element);
  33. jassert (elementIndex >= 0);
  34. }
  35. ElementType* getElement() const
  36. {
  37. if (containerGroups.size() > 0)
  38. {
  39. auto group = dynamic_cast<PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  40. if (group == nullptr)
  41. return nullptr;
  42. for (int i = 1; i < containerGroups.size(); ++i)
  43. {
  44. group = dynamic_cast<PaintElementGroup*> (group->getElement (containerGroups.getUnchecked(i)));
  45. if (group == nullptr)
  46. return nullptr;
  47. }
  48. auto e = dynamic_cast<ElementType*> (group->getElement (elementIndex));
  49. jassert (e != nullptr);
  50. return e;
  51. }
  52. else
  53. {
  54. auto e = dynamic_cast<ElementType*> (routine.getElement (elementIndex));
  55. jassert (e != nullptr);
  56. return e;
  57. }
  58. }
  59. int getSizeInUnits() { return 2; }
  60. protected:
  61. PaintRoutine& routine;
  62. int elementIndex;
  63. Array <int> containerGroups;
  64. void changed() const
  65. {
  66. jassert (routine.getDocument() != nullptr);
  67. routine.getDocument()->changed();
  68. }
  69. void showCorrectTab() const
  70. {
  71. if (JucerDocumentEditor* const docHolder = JucerDocumentEditor::getActiveDocumentHolder())
  72. docHolder->showGraphics (&routine);
  73. if (routine.getSelectedElements().getNumSelected() == 0)
  74. if (ElementType* const e = dynamic_cast<ElementType*> (routine.getElement (elementIndex)))
  75. routine.getSelectedElements().selectOnly (e);
  76. }
  77. private:
  78. void findGroupIndices (PaintRoutine* const pr, PaintElement* const element)
  79. {
  80. for (int i = pr->getNumElements(); --i >= 0;)
  81. {
  82. if (auto pg = dynamic_cast<PaintElementGroup*> (pr->getElement (i)))
  83. {
  84. if (pg->containsElement (element))
  85. {
  86. containerGroups.add (i);
  87. findGroupIndices (pg, element);
  88. }
  89. }
  90. }
  91. }
  92. void findGroupIndices (PaintElementGroup* const group, PaintElement* const element)
  93. {
  94. elementIndex = group->indexOfElement (element);
  95. if (elementIndex < 0)
  96. {
  97. for (int i = group->getNumElements(); --i >= 0;)
  98. {
  99. if (auto pg = dynamic_cast<PaintElementGroup*> (group->getElement (i)))
  100. {
  101. if (pg->containsElement (element))
  102. {
  103. containerGroups.add (i);
  104. findGroupIndices (pg, element);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. PaintElementUndoableAction (const PaintElementUndoableAction&);
  111. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  112. };