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.

140 lines
4.4KB

  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 != nullptr);
  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. auto group = dynamic_cast<PaintElementGroup*> (routine.getElement (containerGroups.getFirst()));
  41. if (group == nullptr)
  42. return nullptr;
  43. for (int i = 1; i < containerGroups.size(); ++i)
  44. {
  45. group = dynamic_cast<PaintElementGroup*> (group->getElement (containerGroups.getUnchecked(i)));
  46. if (group == nullptr)
  47. return nullptr;
  48. }
  49. auto e = dynamic_cast<ElementType*> (group->getElement (elementIndex));
  50. jassert (e != nullptr);
  51. return e;
  52. }
  53. else
  54. {
  55. auto e = dynamic_cast<ElementType*> (routine.getElement (elementIndex));
  56. jassert (e != nullptr);
  57. return e;
  58. }
  59. }
  60. int getSizeInUnits() { return 2; }
  61. protected:
  62. PaintRoutine& routine;
  63. int elementIndex;
  64. Array <int> containerGroups;
  65. void changed() const
  66. {
  67. jassert (routine.getDocument() != 0);
  68. routine.getDocument()->changed();
  69. }
  70. void showCorrectTab() const
  71. {
  72. if (JucerDocumentEditor* const docHolder = JucerDocumentEditor::getActiveDocumentHolder())
  73. docHolder->showGraphics (&routine);
  74. if (routine.getSelectedElements().getNumSelected() == 0)
  75. if (ElementType* const e = dynamic_cast<ElementType*> (routine.getElement (elementIndex)))
  76. routine.getSelectedElements().selectOnly (e);
  77. }
  78. private:
  79. void findGroupIndices (PaintRoutine* const pr, PaintElement* const element)
  80. {
  81. for (int i = pr->getNumElements(); --i >= 0;)
  82. {
  83. if (auto pg = dynamic_cast<PaintElementGroup*> (pr->getElement (i)))
  84. {
  85. if (pg->containsElement (element))
  86. {
  87. containerGroups.add (i);
  88. findGroupIndices (pg, element);
  89. }
  90. }
  91. }
  92. }
  93. void findGroupIndices (PaintElementGroup* const group, PaintElement* const element)
  94. {
  95. elementIndex = group->indexOfElement (element);
  96. if (elementIndex < 0)
  97. {
  98. for (int i = group->getNumElements(); --i >= 0;)
  99. {
  100. if (auto pg = dynamic_cast<PaintElementGroup*> (group->getElement (i)))
  101. {
  102. if (pg->containsElement (element))
  103. {
  104. containerGroups.add (i);
  105. findGroupIndices (pg, element);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. PaintElementUndoableAction (const PaintElementUndoableAction&);
  112. PaintElementUndoableAction& operator= (const PaintElementUndoableAction&);
  113. };