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.

219 lines
6.9KB

  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. //==============================================================================
  20. class SVGPathDataComponent : public Component,
  21. public FileDragAndDropTarget
  22. {
  23. public:
  24. SVGPathDataComponent()
  25. {
  26. desc.setJustificationType (Justification::centred);
  27. addAndMakeVisible (desc);
  28. userText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  29. userText.setMultiLine (true, true);
  30. userText.setReturnKeyStartsNewLine (true);
  31. addAndMakeVisible (userText);
  32. userText.onTextChange = [this] { update(); };
  33. userText.onEscapeKey = [this] { getTopLevelComponent()->exitModalState (0); };
  34. resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  35. resultText.setMultiLine (true, true);
  36. resultText.setReadOnly (true);
  37. resultText.setSelectAllWhenFocused (true);
  38. addAndMakeVisible (resultText);
  39. userText.setText (getLastText());
  40. addAndMakeVisible (copyButton);
  41. copyButton.onClick = [this] { SystemClipboard::copyTextToClipboard (resultText.getText()); };
  42. addAndMakeVisible (closeSubPathButton);
  43. closeSubPathButton.onClick = [this] { update(); };
  44. closeSubPathButton.setToggleState (true, NotificationType::dontSendNotification);
  45. addAndMakeVisible (fillPathButton);
  46. fillPathButton.onClick = [this] { update(); };
  47. fillPathButton.setToggleState (true, NotificationType::dontSendNotification);
  48. }
  49. void update()
  50. {
  51. getLastText() = userText.getText();
  52. auto text = getLastText().trim().unquoted().trim();
  53. path = Drawable::parseSVGPath (text);
  54. if (path.isEmpty())
  55. path = pathFromPoints (text);
  56. String result = "No path generated.. Not a valid SVG path string?";
  57. if (! path.isEmpty())
  58. {
  59. MemoryOutputStream data;
  60. path.writePathToStream (data);
  61. MemoryOutputStream out;
  62. out << "static const unsigned char pathData[] = ";
  63. build_tools::writeDataAsCppLiteral (data.getMemoryBlock(), out, false, true);
  64. out << newLine
  65. << newLine
  66. << "Path path;" << newLine
  67. << "path.loadPathFromData (pathData, sizeof (pathData));" << newLine;
  68. result = out.toString();
  69. }
  70. resultText.setText (result, false);
  71. repaint (previewPathArea);
  72. }
  73. void resized() override
  74. {
  75. auto r = getLocalBounds().reduced (8);
  76. auto bottomSection = r.removeFromBottom (30);
  77. copyButton.setBounds (bottomSection.removeFromLeft (50));
  78. bottomSection.removeFromLeft (25);
  79. fillPathButton.setBounds (bottomSection.removeFromLeft (bottomSection.getWidth() / 2));
  80. closeSubPathButton.setBounds (bottomSection);
  81. r.removeFromBottom (5);
  82. desc.setBounds (r.removeFromTop (44));
  83. r.removeFromTop (8);
  84. userText.setBounds (r.removeFromTop (r.getHeight() / 2));
  85. r.removeFromTop (8);
  86. previewPathArea = r.removeFromRight (r.getHeight());
  87. resultText.setBounds (r);
  88. }
  89. void paint (Graphics& g) override
  90. {
  91. if (dragOver)
  92. {
  93. g.setColour (findColour (secondaryBackgroundColourId).brighter());
  94. g.fillAll();
  95. }
  96. g.setColour (findColour (defaultTextColourId));
  97. path.applyTransform (path.getTransformToScaleToFit (previewPathArea.reduced (4).toFloat(), true));
  98. if (fillPathButton.getToggleState())
  99. g.fillPath (path);
  100. else
  101. g.strokePath (path, PathStrokeType (2.0f));
  102. }
  103. void lookAndFeelChanged() override
  104. {
  105. userText.applyFontToAllText (userText.getFont());
  106. resultText.applyFontToAllText (resultText.getFont());
  107. }
  108. bool isInterestedInFileDrag (const StringArray& files) override
  109. {
  110. return files.size() == 1
  111. && File (files[0]).hasFileExtension ("svg");
  112. }
  113. void fileDragEnter (const StringArray&, int, int) override
  114. {
  115. dragOver = true;
  116. repaint();
  117. }
  118. void fileDragExit (const StringArray&) override
  119. {
  120. dragOver = false;
  121. repaint();
  122. }
  123. void filesDropped (const StringArray& files, int, int) override
  124. {
  125. dragOver = false;
  126. repaint();
  127. if (auto element = parseXML (File (files[0])))
  128. {
  129. if (auto* ePath = element->getChildByName ("path"))
  130. userText.setText (ePath->getStringAttribute ("d"), true);
  131. else if (auto* ePolygon = element->getChildByName ("polygon"))
  132. userText.setText (ePolygon->getStringAttribute ("points"), true);
  133. }
  134. }
  135. Path pathFromPoints (String pointsText)
  136. {
  137. auto points = StringArray::fromTokens (pointsText, " ,", "");
  138. points.removeEmptyStrings();
  139. jassert (points.size() % 2 == 0);
  140. Path p;
  141. for (int i = 0; i < points.size() / 2; i++)
  142. {
  143. auto x = points[i * 2].getFloatValue();
  144. auto y = points[i * 2 + 1].getFloatValue();
  145. if (i == 0)
  146. p.startNewSubPath ({ x, y });
  147. else
  148. p.lineTo ({ x, y });
  149. }
  150. if (closeSubPathButton.getToggleState())
  151. p.closeSubPath();
  152. return p;
  153. }
  154. private:
  155. Label desc { {}, "Paste an SVG path string into the top box, and it'll be converted to some C++ "
  156. "code that will load it as a Path object.." };
  157. TextButton copyButton { "Copy" };
  158. TextEditor userText, resultText;
  159. ToggleButton closeSubPathButton { "Close sub-path" };
  160. ToggleButton fillPathButton { "Fill path" };
  161. Rectangle<int> previewPathArea;
  162. Path path;
  163. bool dragOver = false;
  164. String& getLastText()
  165. {
  166. static String t;
  167. return t;
  168. }
  169. };