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.

212 lines
6.6KB

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