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.

138 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. #include "../ui/jucer_JucerDocumentEditor.h"
  19. #include "jucer_PaintElementGroup.h"
  20. //==============================================================================
  21. template <class ElementType>
  22. class PaintElementUndoableAction : public UndoableAction
  23. {
  24. public:
  25. PaintElementUndoableAction (ElementType* const element)
  26. : routine (*element->getOwner()),
  27. elementIndex (element->getOwner()->indexOfElement (element))
  28. {
  29. jassert (element != 0);
  30. if (elementIndex < 0)
  31. findGroupIndices (element->getOwner(), element);
  32. jassert (elementIndex >= 0);
  33. }
  34. ElementType* getElement() const
  35. {
  36. if (containerGroups.size() > 0)
  37. {
  38. PaintElementGroup* group = 0;
  39. group = dynamic_cast<PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  40. if (group == 0)
  41. return 0;
  42. for (int i = 1; i < containerGroups.size(); ++i)
  43. {
  44. group = dynamic_cast<PaintElementGroup*> (group->getElement (containerGroups.getUnchecked(i)));
  45. if (group == 0)
  46. return 0;
  47. }
  48. ElementType* const e = dynamic_cast<ElementType*> (group->getElement (elementIndex));
  49. jassert (e != 0);
  50. return e;
  51. }
  52. else
  53. {
  54. ElementType* const e = dynamic_cast<ElementType*> (routine.getElement (elementIndex));
  55. jassert (e != 0);
  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() != 0);
  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. PaintElementGroup* const pg = dynamic_cast<PaintElementGroup*> (pr->getElement (i));
  83. if (pg != 0 && pg->containsElement (element))
  84. {
  85. containerGroups.add (i);
  86. findGroupIndices (pg, element);
  87. }
  88. }
  89. }
  90. void findGroupIndices (PaintElementGroup* const group, PaintElement* const element)
  91. {
  92. elementIndex = group->indexOfElement (element);
  93. if (elementIndex < 0)
  94. {
  95. for (int i = group->getNumElements(); --i >= 0;)
  96. {
  97. PaintElementGroup* pg = dynamic_cast<PaintElementGroup*> (group->getElement (i));
  98. if (pg != 0 && pg->containsElement (element))
  99. {
  100. containerGroups.add (i);
  101. findGroupIndices (pg, element);
  102. }
  103. }
  104. }
  105. }
  106. PaintElementUndoableAction (const PaintElementUndoableAction&);
  107. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  108. };