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.

220 lines
6.9KB

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