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.

215 lines
6.3KB

  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. private TextEditor::Listener,
  24. private Button::Listener
  25. {
  26. public:
  27. SVGPathDataComponent()
  28. {
  29. desc.setJustificationType (Justification::centred);
  30. addAndMakeVisible (desc);
  31. userText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  32. userText.setMultiLine (true, true);
  33. userText.setReturnKeyStartsNewLine (true);
  34. addAndMakeVisible (userText);
  35. userText.addListener (this);
  36. resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  37. resultText.setMultiLine (true, true);
  38. resultText.setReadOnly (true);
  39. resultText.setSelectAllWhenFocused (true);
  40. addAndMakeVisible (resultText);
  41. userText.setText (getLastText());
  42. addAndMakeVisible (copyButton);
  43. copyButton.addListener (this);
  44. }
  45. void buttonClicked (Button* b) override
  46. {
  47. if (b == &copyButton)
  48. SystemClipboard::copyTextToClipboard (resultText.getText());
  49. }
  50. void textEditorTextChanged (TextEditor&) override
  51. {
  52. update();
  53. }
  54. void textEditorEscapeKeyPressed (TextEditor&) override
  55. {
  56. getTopLevelComponent()->exitModalState (0);
  57. }
  58. void update()
  59. {
  60. getLastText() = userText.getText();
  61. auto text = getLastText().trim().unquoted().trim();
  62. path = Drawable::parseSVGPath (text);
  63. if (path.isEmpty())
  64. path = pathFromPoints (text);
  65. String result = "No path generated.. Not a valid SVG path string?";
  66. if (! path.isEmpty())
  67. {
  68. MemoryOutputStream data;
  69. path.writePathToStream (data);
  70. MemoryOutputStream out;
  71. out << "static const unsigned char pathData[] = ";
  72. CodeHelpers::writeDataAsCppLiteral (data.getMemoryBlock(), out, false, true);
  73. out << newLine
  74. << newLine
  75. << "Path path;" << newLine
  76. << "path.loadPathFromData (pathData, sizeof (pathData));" << newLine;
  77. result = out.toString();
  78. }
  79. resultText.setText (result, false);
  80. repaint (previewPathArea);
  81. }
  82. void resized() override
  83. {
  84. auto r = getLocalBounds().reduced (8);
  85. copyButton.setBounds (r.removeFromBottom (30).removeFromLeft (50));
  86. r.removeFromBottom (5);
  87. desc.setBounds (r.removeFromTop (44));
  88. r.removeFromTop (8);
  89. userText.setBounds (r.removeFromTop (r.getHeight() / 2));
  90. r.removeFromTop (8);
  91. previewPathArea = r.removeFromRight (r.getHeight());
  92. resultText.setBounds (r);
  93. }
  94. void paint (Graphics& g) override
  95. {
  96. if (dragOver)
  97. {
  98. g.setColour (findColour (secondaryBackgroundColourId).brighter());
  99. g.fillAll();
  100. }
  101. g.setColour (findColour (defaultTextColourId));
  102. g.fillPath (path, path.getTransformToScaleToFit (previewPathArea.reduced (4).toFloat(), true));
  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 (ScopedPointer<XmlElement> e = XmlDocument::parse (File (files[0])))
  129. {
  130. if (auto* ePath = e->getChildByName ("path"))
  131. userText.setText (ePath->getStringAttribute ("d"), true);
  132. else if (auto* ePolygon = e->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. 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. Rectangle<int> previewPathArea;
  160. Path path;
  161. bool dragOver = false;
  162. String& getLastText()
  163. {
  164. static String t;
  165. return t;
  166. }
  167. };