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.

306 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. #include "../../ProjectSaving/jucer_ProjectExporter.h"
  15. #include "../../Utility/UI/PropertyComponents/jucer_FilePathPropertyComponent.h"
  16. #include "../../Utility/Helpers/jucer_ValueWithDefaultWrapper.h"
  17. #include "jucer_NewProjectWizard.h"
  18. //==============================================================================
  19. class ItemHeader : public Component
  20. {
  21. public:
  22. ItemHeader (StringRef name, StringRef description, const char* iconSvgData)
  23. : nameLabel ({}, name),
  24. descriptionLabel ({}, description),
  25. icon (makeIcon (iconSvgData))
  26. {
  27. addAndMakeVisible (nameLabel);
  28. nameLabel.setFont (18.0f);
  29. nameLabel.setMinimumHorizontalScale (1.0f);
  30. addAndMakeVisible (descriptionLabel);
  31. descriptionLabel.setMinimumHorizontalScale (1.0f);
  32. }
  33. void resized() override
  34. {
  35. auto bounds = getLocalBounds();
  36. auto topSlice = bounds.removeFromTop (50);
  37. iconBounds = topSlice.removeFromRight (75);
  38. nameLabel.setBounds (topSlice);
  39. bounds.removeFromTop (10);
  40. descriptionLabel.setBounds (bounds.removeFromTop (50));
  41. bounds.removeFromTop (20);
  42. }
  43. void paint (Graphics& g) override
  44. {
  45. g.fillAll (findColour (secondaryBackgroundColourId));
  46. if (icon != nullptr)
  47. icon->drawWithin (g, iconBounds.toFloat(), RectanglePlacement::centred, 1.0f);
  48. }
  49. private:
  50. static std::unique_ptr<Drawable> makeIcon (const char* iconSvgData)
  51. {
  52. if (auto svg = XmlDocument::parse (iconSvgData))
  53. return Drawable::createFromSVG (*svg);
  54. return {};
  55. }
  56. Label nameLabel, descriptionLabel;
  57. Rectangle<int> iconBounds;
  58. std::unique_ptr<Drawable> icon;
  59. //==============================================================================
  60. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ItemHeader)
  61. };
  62. //==============================================================================
  63. class TemplateComponent : public Component
  64. {
  65. public:
  66. TemplateComponent (const NewProjectTemplates::ProjectTemplate& temp,
  67. std::function<void (std::unique_ptr<Project>)>&& createdCallback)
  68. : projectTemplate (temp),
  69. projectCreatedCallback (std::move (createdCallback)),
  70. header (projectTemplate.displayName, projectTemplate.description, projectTemplate.icon)
  71. {
  72. createProjectButton.onClick = [this]
  73. {
  74. FileChooser fc ("Save Project", NewProjectWizard::getLastWizardFolder());
  75. if (fc.browseForDirectory())
  76. {
  77. auto dir = fc.getResult();
  78. if (auto project = NewProjectWizard::createNewProject (projectTemplate,
  79. dir.getChildFile (projectNameValue.get().toString()),
  80. projectNameValue.get(),
  81. modulesValue.get(),
  82. exportersValue.get(),
  83. fileOptionsValue.get(),
  84. modulePathValue.getCurrentValue(),
  85. modulePathValue.getWrappedValueWithDefault().isUsingDefault()))
  86. {
  87. projectCreatedCallback (std::move (project));
  88. getAppSettings().lastWizardFolder = dir;
  89. }
  90. }
  91. };
  92. addAndMakeVisible (createProjectButton);
  93. addAndMakeVisible (header);
  94. modulePathValue.init ({ settingsTree, Ids::defaultJuceModulePath, nullptr },
  95. getAppSettings().getStoredPath (Ids::defaultJuceModulePath, TargetOS::getThisOS()),
  96. TargetOS::getThisOS());
  97. panel.addProperties (buildPropertyList());
  98. addAndMakeVisible (panel);
  99. }
  100. void resized() override
  101. {
  102. auto bounds = getLocalBounds().reduced (10);
  103. header.setBounds (bounds.removeFromTop (150));
  104. createProjectButton.setBounds (bounds.removeFromBottom (30).removeFromRight (150));
  105. bounds.removeFromBottom (5);
  106. panel.setBounds (bounds);
  107. }
  108. void paint (Graphics& g) override
  109. {
  110. g.fillAll (findColour (secondaryBackgroundColourId));
  111. }
  112. private:
  113. NewProjectTemplates::ProjectTemplate projectTemplate;
  114. std::function<void (std::unique_ptr<Project>)> projectCreatedCallback;
  115. ItemHeader header;
  116. TextButton createProjectButton { "Create Project..." };
  117. ValueTree settingsTree { "NewProjectSettings" };
  118. ValueWithDefault projectNameValue { settingsTree, Ids::name, nullptr, "NewProject" },
  119. modulesValue { settingsTree, Ids::dependencies_, nullptr, projectTemplate.requiredModules, "," },
  120. exportersValue { settingsTree, Ids::exporters, nullptr, StringArray (ProjectExporter::getCurrentPlatformExporterTypeInfo().identifier.toString()), "," },
  121. fileOptionsValue { settingsTree, Ids::file, nullptr, NewProjectTemplates::getVarForFileOption (projectTemplate.defaultFileOption) };
  122. ValueWithDefaultWrapper modulePathValue;
  123. PropertyPanel panel;
  124. //==============================================================================
  125. PropertyComponent* createProjectNamePropertyComponent()
  126. {
  127. return new TextPropertyComponent (projectNameValue, "Project Name", 1024, false);
  128. }
  129. PropertyComponent* createModulesPropertyComponent()
  130. {
  131. Array<var> moduleVars;
  132. var requiredModules;
  133. for (auto& m : getJUCEModules())
  134. {
  135. moduleVars.add (m);
  136. if (projectTemplate.requiredModules.contains (m))
  137. requiredModules.append (m);
  138. }
  139. modulesValue = requiredModules;
  140. return new MultiChoicePropertyComponent (modulesValue, "Modules",
  141. getJUCEModules(), moduleVars);
  142. }
  143. PropertyComponent* createModulePathPropertyComponent()
  144. {
  145. return new FilePathPropertyComponent (modulePathValue.getWrappedValueWithDefault(), "Path to Modules", true);
  146. }
  147. PropertyComponent* createExportersPropertyValue()
  148. {
  149. Array<var> exporterVars;
  150. StringArray exporterNames;
  151. for (auto& exporterTypeInfo : ProjectExporter::getExporterTypeInfos())
  152. {
  153. exporterVars.add (exporterTypeInfo.identifier.toString());
  154. exporterNames.add (exporterTypeInfo.displayName);
  155. }
  156. return new MultiChoicePropertyComponent (exportersValue, "Exporters", exporterNames, exporterVars);
  157. }
  158. PropertyComponent* createFileCreationOptionsPropertyComponent()
  159. {
  160. Array<var> optionVars;
  161. StringArray optionStrings;
  162. for (auto& opt : projectTemplate.fileOptionsAndFiles)
  163. {
  164. optionVars.add (NewProjectTemplates::getVarForFileOption (opt.first));
  165. optionStrings.add (NewProjectTemplates::getStringForFileOption (opt.first));
  166. }
  167. return new ChoicePropertyComponent (fileOptionsValue, "File Creation Options", optionStrings, optionVars);
  168. }
  169. Array<PropertyComponent*> buildPropertyList()
  170. {
  171. PropertyListBuilder builder;
  172. builder.add (createProjectNamePropertyComponent());
  173. builder.add (createModulesPropertyComponent());
  174. builder.add (createModulePathPropertyComponent());
  175. builder.add (createExportersPropertyValue());
  176. if (! projectTemplate.fileOptionsAndFiles.empty())
  177. builder.add (createFileCreationOptionsPropertyComponent());
  178. return builder.components;
  179. }
  180. //==============================================================================
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemplateComponent)
  182. };
  183. //==============================================================================
  184. class ExampleComponent : public Component
  185. {
  186. public:
  187. ExampleComponent (const File& f, std::function<void (const File&)> selectedCallback)
  188. : exampleFile (f),
  189. metadata (parseJUCEHeaderMetadata (exampleFile)),
  190. exampleSelectedCallback (std::move (selectedCallback)),
  191. header (metadata[Ids::name].toString(), metadata[Ids::description].toString(), BinaryData::background_logo_svg),
  192. codeViewer (doc, &cppTokeniser)
  193. {
  194. addAndMakeVisible (header);
  195. openExampleButton.onClick = [this] { exampleSelectedCallback (exampleFile); };
  196. addAndMakeVisible (openExampleButton);
  197. setupCodeViewer();
  198. addAndMakeVisible (codeViewer);
  199. }
  200. void paint (Graphics& g) override
  201. {
  202. g.fillAll (findColour (secondaryBackgroundColourId));
  203. }
  204. void resized() override
  205. {
  206. auto bounds = getLocalBounds().reduced (10);
  207. header.setBounds (bounds.removeFromTop (125));
  208. openExampleButton.setBounds (bounds.removeFromBottom (30).removeFromRight (150));
  209. codeViewer.setBounds (bounds);
  210. }
  211. private:
  212. void setupCodeViewer()
  213. {
  214. auto fileString = exampleFile.loadFileAsString();
  215. doc.replaceAllContent (fileString);
  216. codeViewer.setScrollbarThickness (6);
  217. codeViewer.setReadOnly (true);
  218. getAppSettings().appearance.applyToCodeEditor (codeViewer);
  219. codeViewer.scrollToLine (findBestLineToScrollToForClass (StringArray::fromLines (fileString),
  220. metadata[Ids::name].toString(),
  221. metadata[Ids::type] == "AudioProcessor"));
  222. }
  223. //==============================================================================
  224. File exampleFile;
  225. var metadata;
  226. std::function<void (const File&)> exampleSelectedCallback;
  227. ItemHeader header;
  228. CPlusPlusCodeTokeniser cppTokeniser;
  229. CodeDocument doc;
  230. CodeEditorComponent codeViewer;
  231. TextButton openExampleButton { "Open Example..." };
  232. //==============================================================================
  233. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ExampleComponent)
  234. };