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.

175 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 "jucer_ColouredElement.h"
  20. #include "jucer_ElementSiblingComponent.h"
  21. class PathPointComponent;
  22. class PaintElementPath;
  23. //==============================================================================
  24. class PathPoint
  25. {
  26. public:
  27. PathPoint (PaintElementPath* const owner);
  28. PathPoint (const PathPoint& other);
  29. PathPoint& operator= (const PathPoint& other);
  30. ~PathPoint();
  31. static constexpr auto maxRects = 3;
  32. PaintElementPath* owner;
  33. Path::Iterator::PathElementType type;
  34. RelativePositionedRectangle pos[maxRects] = {};
  35. int getNumPoints() const;
  36. void changePointType (Path::Iterator::PathElementType newType,
  37. const Rectangle<int>& parentArea,
  38. bool undoable);
  39. void deleteFromPath();
  40. void getEditableProperties (Array<PropertyComponent*>& props, bool multipleSelected);
  41. private:
  42. PathPoint withChangedPointType (Path::Iterator::PathElementType newType,
  43. const Rectangle<int>& parentArea) const;
  44. };
  45. //==============================================================================
  46. class PaintElementPath : public ColouredElement
  47. {
  48. public:
  49. PaintElementPath (PaintRoutine* owner);
  50. ~PaintElementPath() override;
  51. //==============================================================================
  52. void setInitialBounds (int parentWidth, int parentHeight) override;
  53. Rectangle<int> getCurrentBounds (const Rectangle<int>& parentArea) const override;
  54. void setCurrentBounds (const Rectangle<int>& b, const Rectangle<int>& parentArea, bool undoable) override;
  55. //==============================================================================
  56. bool getPoint (int index, int pointNumber, double& x, double& y, const Rectangle<int>& parentArea) const;
  57. void movePoint (int index, int pointNumber, double newX, double newY, const Rectangle<int>& parentArea, bool undoable);
  58. RelativePositionedRectangle getPoint (int index, int pointNumber) const;
  59. void setPoint (int index, int pointNumber, const RelativePositionedRectangle& newPoint, bool undoable);
  60. int getNumPoints() const noexcept { return points.size(); }
  61. PathPoint* getPoint (int index) const noexcept { return points [index]; }
  62. int indexOfPoint (const PathPoint* p) const noexcept { return points.indexOf (p); }
  63. PathPoint* addPoint (int pointIndexToAddItAfter, bool undoable);
  64. void deletePoint (int pointIndex, bool undoable);
  65. void pointListChanged();
  66. int findSegmentAtXY (int x, int y) const;
  67. //==============================================================================
  68. bool isSubpathClosed (int pointIndex) const;
  69. void setSubpathClosed (int pointIndex, bool closed, bool undoable);
  70. bool isNonZeroWinding() const noexcept { return nonZeroWinding; }
  71. void setNonZeroWinding (bool nonZero, bool undoable);
  72. //==============================================================================
  73. void getEditableProperties (Array<PropertyComponent*>& props, bool multipleSelected) override;
  74. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) override;
  75. void applyCustomPaintSnippets (StringArray& snippets) override;
  76. //==============================================================================
  77. static const char* getTagName() noexcept { return "PATH"; }
  78. XmlElement* createXml() const override;
  79. bool loadFromXml (const XmlElement& xml) override;
  80. void setToPath (const Path& p);
  81. //==============================================================================
  82. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea) override;
  83. void drawExtraEditorGraphics (Graphics& g, const Rectangle<int>& relativeTo) override;
  84. void resized() override;
  85. void parentSizeChanged() override;
  86. void mouseDown (const MouseEvent& e) override;
  87. void mouseDrag (const MouseEvent& e) override;
  88. void mouseUp (const MouseEvent& e) override;
  89. void createSiblingComponents() override;
  90. void changed() override;
  91. private:
  92. OwnedArray<PathPoint> points;
  93. bool nonZeroWinding;
  94. mutable Path path;
  95. mutable Rectangle<int> lastPathBounds;
  96. int mouseDownOnSegment;
  97. bool mouseDownSelectSegmentStatus;
  98. String customPaintCode;
  99. String pathToString() const;
  100. void restorePathFromString (const String& s);
  101. void updateStoredPath (const ComponentLayout* layout, const Rectangle<int>& parentArea) const;
  102. int getBorderSize() const;
  103. void rescalePoint (RelativePositionedRectangle& pos, int dx, int dy,
  104. double scaleX, double scaleY,
  105. double scaleStartX, double scaleStartY,
  106. const Rectangle<int>& parentArea) const;
  107. };
  108. //==============================================================================
  109. class PathPointComponent : public ElementSiblingComponent
  110. {
  111. public:
  112. PathPointComponent (PaintElementPath* const path_,
  113. int index, int pointNumber);
  114. ~PathPointComponent();
  115. void updatePosition();
  116. void showPopupMenu();
  117. void paint (Graphics& g);
  118. void mouseDown (const MouseEvent& e);
  119. void mouseDrag (const MouseEvent& e);
  120. void mouseUp (const MouseEvent& e);
  121. void changeListenerCallback (ChangeBroadcaster*);
  122. private:
  123. PaintElementPath* const path;
  124. PaintRoutine* const routine;
  125. const int index;
  126. const int pointNumber;
  127. int dragX, dragY;
  128. bool selected, dragging, mouseDownSelectStatus;
  129. };