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.

132 lines
4.1KB

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