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.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #pragma once
  20. #include "../UI/jucer_JucerDocumentEditor.h"
  21. #include "jucer_PaintElementGroup.h"
  22. //==============================================================================
  23. template <class ElementType>
  24. class PaintElementUndoableAction : public UndoableAction
  25. {
  26. public:
  27. PaintElementUndoableAction (ElementType* const element)
  28. : routine (*element->getOwner()),
  29. elementIndex (element->getOwner()->indexOfElement (element))
  30. {
  31. jassert (element != 0);
  32. if (elementIndex < 0)
  33. findGroupIndices (element->getOwner(), element);
  34. jassert (elementIndex >= 0);
  35. }
  36. ElementType* getElement() const
  37. {
  38. if (containerGroups.size() > 0)
  39. {
  40. PaintElementGroup* group = 0;
  41. group = dynamic_cast<PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  42. if (group == 0)
  43. return 0;
  44. for (int i = 1; i < containerGroups.size(); ++i)
  45. {
  46. group = dynamic_cast<PaintElementGroup*> (group->getElement (containerGroups.getUnchecked(i)));
  47. if (group == 0)
  48. return 0;
  49. }
  50. ElementType* const e = dynamic_cast<ElementType*> (group->getElement (elementIndex));
  51. jassert (e != 0);
  52. return e;
  53. }
  54. else
  55. {
  56. ElementType* const e = dynamic_cast<ElementType*> (routine.getElement (elementIndex));
  57. jassert (e != 0);
  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() != 0);
  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. PaintElementGroup* const pg = dynamic_cast<PaintElementGroup*> (pr->getElement (i));
  85. if (pg != 0 && pg->containsElement (element))
  86. {
  87. containerGroups.add (i);
  88. findGroupIndices (pg, element);
  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. PaintElementGroup* pg = dynamic_cast<PaintElementGroup*> (group->getElement (i));
  100. if (pg != 0 && pg->containsElement (element))
  101. {
  102. containerGroups.add (i);
  103. findGroupIndices (pg, element);
  104. }
  105. }
  106. }
  107. }
  108. PaintElementUndoableAction (const PaintElementUndoableAction&);
  109. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  110. };