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.

125 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class SVGPathDataComponent : public Component,
  18. private TextEditorListener
  19. {
  20. public:
  21. SVGPathDataComponent()
  22. : desc (String(),
  23. "Paste an SVG path string into the top box, and it'll be converted to some C++ "
  24. "code that will load it as a Path object..")
  25. {
  26. desc.setJustificationType (Justification::centred);
  27. desc.setColour (Label::textColourId, Colours::white);
  28. addAndMakeVisible (desc);
  29. const Colour bkgd (Colours::white.withAlpha (0.6f));
  30. userText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  31. userText.setMultiLine (true, true);
  32. userText.setReturnKeyStartsNewLine (true);
  33. userText.setColour (TextEditor::backgroundColourId, bkgd);
  34. addAndMakeVisible (userText);
  35. userText.addListener (this);
  36. resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  37. resultText.setMultiLine (true, true);
  38. resultText.setColour (TextEditor::backgroundColourId, bkgd);
  39. resultText.setReadOnly (true);
  40. resultText.setSelectAllWhenFocused (true);
  41. addAndMakeVisible (resultText);
  42. userText.setText (getLastText());
  43. }
  44. void textEditorTextChanged (TextEditor&) override
  45. {
  46. update();
  47. }
  48. void textEditorEscapeKeyPressed (TextEditor&) override
  49. {
  50. getTopLevelComponent()->exitModalState (0);
  51. }
  52. void update()
  53. {
  54. getLastText() = userText.getText();
  55. path = Drawable::parseSVGPath (getLastText().trim().unquoted().trim());
  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. CodeHelpers::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. Rectangle<int> r (getLocalBounds().reduced (8));
  76. desc.setBounds (r.removeFromTop (44));
  77. r.removeFromTop (8);
  78. userText.setBounds (r.removeFromTop (r.getHeight() / 2));
  79. r.removeFromTop (8);
  80. previewPathArea = r.removeFromRight (r.getHeight());
  81. resultText.setBounds (r);
  82. }
  83. void paint (Graphics& g) override
  84. {
  85. g.setColour (Colours::white);
  86. g.fillPath (path, path.getTransformToScaleToFit (previewPathArea.reduced (4).toFloat(), true));
  87. }
  88. private:
  89. Label desc;
  90. TextEditor userText, resultText;
  91. Rectangle<int> previewPathArea;
  92. Path path;
  93. String& getLastText()
  94. {
  95. static String t;
  96. return t;
  97. }
  98. };