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.

126 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. 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.addListener (this);
  33. resultText.setFont (getAppSettings().appearance.getCodeFont().withHeight (13.0f));
  34. resultText.setMultiLine (true, true);
  35. resultText.setReadOnly (true);
  36. resultText.setSelectAllWhenFocused (true);
  37. addAndMakeVisible (resultText);
  38. userText.setText (getLastText());
  39. }
  40. void textEditorTextChanged (TextEditor&) override
  41. {
  42. update();
  43. }
  44. void textEditorEscapeKeyPressed (TextEditor&) override
  45. {
  46. getTopLevelComponent()->exitModalState (0);
  47. }
  48. void update()
  49. {
  50. getLastText() = userText.getText();
  51. path = Drawable::parseSVGPath (getLastText().trim().unquoted().trim());
  52. String result = "No path generated.. Not a valid SVG path string?";
  53. if (! path.isEmpty())
  54. {
  55. MemoryOutputStream data;
  56. path.writePathToStream (data);
  57. MemoryOutputStream out;
  58. out << "static const unsigned char pathData[] = ";
  59. CodeHelpers::writeDataAsCppLiteral (data.getMemoryBlock(), out, false, true);
  60. out << newLine
  61. << newLine
  62. << "Path path;" << newLine
  63. << "path.loadPathFromData (pathData, sizeof (pathData));" << newLine;
  64. result = out.toString();
  65. }
  66. resultText.setText (result, false);
  67. repaint (previewPathArea);
  68. }
  69. void resized() override
  70. {
  71. Rectangle<int> r (getLocalBounds().reduced (8));
  72. desc.setBounds (r.removeFromTop (44));
  73. r.removeFromTop (8);
  74. userText.setBounds (r.removeFromTop (r.getHeight() / 2));
  75. r.removeFromTop (8);
  76. previewPathArea = r.removeFromRight (r.getHeight());
  77. resultText.setBounds (r);
  78. }
  79. void paint (Graphics& g) override
  80. {
  81. g.setColour (findColour (secondaryBackgroundColourId));
  82. g.fillPath (path, path.getTransformToScaleToFit (previewPathArea.reduced (4).toFloat(), true));
  83. }
  84. void lookAndFeelChanged() override
  85. {
  86. userText.applyFontToAllText (userText.getFont());
  87. resultText.applyFontToAllText (resultText.getFont());
  88. }
  89. private:
  90. Label desc;
  91. TextEditor userText, resultText;
  92. Rectangle<int> previewPathArea;
  93. Path path;
  94. String& getLastText()
  95. {
  96. static String t;
  97. return t;
  98. }
  99. };