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.

285 lines
10KB

  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. #ifndef JUCER_TEMPLATETHUMBNAILSCOMPONENT_H_INCLUDED
  18. #define JUCER_TEMPLATETHUMBNAILSCOMPONENT_H_INCLUDED
  19. //=====================================================================================================
  20. /**
  21. Template option tile button.
  22. The drawable button object class for the tile icons and buttons in the TemplateTileBrowser
  23. */
  24. class TemplateOptionButton : public DrawableButton
  25. {
  26. public:
  27. TemplateOptionButton (const String& buttonName, ButtonStyle buttonStyle, const char* thumbSvg)
  28. : DrawableButton (buttonName, buttonStyle)
  29. {
  30. // svg for thumbnail icon
  31. ScopedPointer<XmlElement> svg (XmlDocument::parse (thumbSvg));
  32. jassert (svg != nullptr);
  33. thumb = Drawable::createFromSVG (*svg);
  34. // svg for thumbnail background highlight
  35. ScopedPointer<XmlElement> backSvg (XmlDocument::parse (BinaryData::wizard_Highlight_svg));
  36. jassert (backSvg != nullptr);
  37. hoverBackground = Drawable::createFromSVG (*backSvg);
  38. name = buttonName;
  39. description = "<insert description>";
  40. }
  41. void paintButton (Graphics& g, bool isMouseOverButton, bool /*isButtonDown*/) override
  42. {
  43. const Rectangle<float> r (getLocalBounds().toFloat());
  44. const Colour buttonColour (0xfff29300);
  45. if (isMouseOverButton)
  46. {
  47. if (getStyle() == ImageFitted)
  48. {
  49. hoverBackground->drawWithin (g, r, RectanglePlacement::centred, 1.0);
  50. thumb->drawWithin (g, r, RectanglePlacement::centred, 1.0);
  51. }
  52. else
  53. {
  54. g.setColour (buttonColour.withAlpha (0.3f));
  55. g.fillRoundedRectangle (r.reduced (2.0f, 2.0f), 10.0f);
  56. g.setColour (buttonColour);
  57. g.drawRoundedRectangle (r.reduced (2.0f, 2.0f), 10.0f, 2.0f);
  58. }
  59. }
  60. else
  61. {
  62. if (getStyle() == ImageFitted)
  63. {
  64. thumb->drawWithin (g, r, RectanglePlacement::centred, 1.0);
  65. }
  66. else
  67. {
  68. g.setColour (buttonColour);
  69. g.drawRoundedRectangle (r.reduced (2.0f, 2.0f), 10.0f, 2.0f);
  70. }
  71. }
  72. Rectangle<float> textTarget;
  73. // center the text for the text buttons or position the text in the image buttons
  74. if (getStyle() != ImageFitted)
  75. {
  76. textTarget = getLocalBounds().toFloat();
  77. }
  78. else
  79. {
  80. textTarget = RectanglePlacement (RectanglePlacement::centred).appliedTo (thumb->getDrawableBounds(), r);
  81. textTarget = textTarget.removeFromBottom (textTarget.getHeight() * 0.3f);
  82. }
  83. g.setColour (Colours::white);
  84. g.drawText (name, textTarget, Justification::centred, true);
  85. }
  86. void resized() override
  87. {
  88. thumb->setBoundsToFit (0, 0, getWidth(), getHeight(), Justification::centred, false);
  89. }
  90. void setDescription (String descript) noexcept
  91. {
  92. description = descript;
  93. }
  94. String getDescription() const noexcept
  95. {
  96. return description;
  97. }
  98. private:
  99. ScopedPointer<Drawable> thumb, hoverBackground;
  100. String name, description;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemplateOptionButton)
  102. };
  103. //=====================================================================================================
  104. /**
  105. Project Template Component for front page.
  106. Features multiple icon buttons to select the type of project template
  107. */
  108. class TemplateTileBrowser : public Component,
  109. private Button::Listener
  110. {
  111. public:
  112. TemplateTileBrowser (WizardComp* projectWizard)
  113. {
  114. const int numWizardButtons = getNumWizards() - 1; // ( - 1 because the last one is blank)
  115. for (int i = 0; i < numWizardButtons; ++i)
  116. {
  117. ScopedPointer<NewProjectWizard> wizard (createWizardType (i));
  118. TemplateOptionButton* b = new TemplateOptionButton (wizard->getName(),
  119. TemplateOptionButton::ImageFitted,
  120. wizard->getIcon());
  121. optionButtons.add (b);
  122. addAndMakeVisible (b);
  123. b->setDescription (wizard->getDescription());
  124. b->addListener (this);
  125. }
  126. // Handle Open Project button functionality
  127. ApplicationCommandManager& commandManager = IntrojucerApp::getCommandManager();
  128. addAndMakeVisible (blankProjectButton = new TemplateOptionButton ("Create Blank Project", TemplateOptionButton::ImageOnButtonBackground, BinaryData::wizard_Openfile_svg));
  129. addAndMakeVisible (exampleProjectButton = new TemplateOptionButton ("Open Example Project", TemplateOptionButton::ImageOnButtonBackground, BinaryData::wizard_Openfile_svg));
  130. addAndMakeVisible (openProjectButton = new TemplateOptionButton ("Open Existing Project", TemplateOptionButton::ImageOnButtonBackground, BinaryData::wizard_Openfile_svg));
  131. blankProjectButton->addListener (this);
  132. exampleProjectButton->addListener (this);
  133. openProjectButton->setCommandToTrigger (&commandManager, CommandIDs::open, true);
  134. newProjectWizard = projectWizard;
  135. }
  136. void paint (Graphics& g) override
  137. {
  138. g.setColour (Colours::black.withAlpha (0.2f));
  139. g.fillRect (getLocalBounds().removeFromTop (60));
  140. g.setColour (Colours::white);
  141. g.setFont (20.0f);
  142. g.drawText ("Create New Project", 0, 0, getWidth(), 60, Justification::centred, true);
  143. // draw the descriptions of each template if hovered;
  144. // (repaint is called by the button listener on change state)
  145. Rectangle<int> descriptionBox (getLocalBounds().reduced (30).removeFromBottom (50));
  146. g.setColour (Colours::white.withAlpha (0.4f));
  147. g.setFont (15.0f);
  148. for (int i = 0; i < optionButtons.size(); ++i)
  149. if (optionButtons.getUnchecked(i)->isOver())
  150. g.drawFittedText (optionButtons.getUnchecked(i)->getDescription(), descriptionBox, Justification::centred, 5, 1.0f);
  151. }
  152. void resized() override
  153. {
  154. Rectangle<int> allOpts = getLocalBounds().reduced (40, 60);
  155. allOpts.removeFromBottom (allOpts.getHeight() / 4);
  156. const int numHorizIcons = 4;
  157. const int optStep = allOpts.getWidth() / numHorizIcons;
  158. for (int i = 0; i < optionButtons.size(); ++i)
  159. {
  160. const int yShift = i < numHorizIcons ? 0 : 1;
  161. optionButtons.getUnchecked(i)->setBounds (Rectangle<int> (allOpts.getX() + (i % numHorizIcons) * optStep,
  162. allOpts.getY() + yShift * allOpts.getHeight() / 2,
  163. optStep, allOpts.getHeight() / 2)
  164. .reduced (10, 10));
  165. }
  166. Rectangle<int> openButtonBounds = getLocalBounds();
  167. openButtonBounds.removeFromBottom (proportionOfHeight (0.12f));
  168. openButtonBounds = openButtonBounds.removeFromBottom (120);
  169. openButtonBounds.reduce (50, 40);
  170. blankProjectButton->setBounds (openButtonBounds.removeFromLeft (optStep - 20));
  171. exampleProjectButton->setBounds (openButtonBounds.removeFromRight (optStep - 20));
  172. openProjectButton->setBounds (openButtonBounds.reduced (18, 0));
  173. }
  174. void showWizard (const String& name)
  175. {
  176. newProjectWizard->projectType.setText (name);
  177. if (SlidingPanelComponent* parent = findParentComponentOfClass<SlidingPanelComponent>())
  178. parent->goToTab (1);
  179. else
  180. jassertfalse;
  181. }
  182. void createBlankProject()
  183. {
  184. showWizard (BlankAppWizard().getName());
  185. }
  186. void openExampleProject()
  187. {
  188. FileChooser fc ("Open File", findExamplesFolder());
  189. if (fc.browseForFileToOpen())
  190. IntrojucerApp::getApp().openFile (fc.getResult());
  191. }
  192. static File findExamplesFolder()
  193. {
  194. File appFolder (File::getSpecialLocation (File::currentApplicationFile));
  195. while (appFolder.exists()
  196. && appFolder.getParentDirectory() != appFolder)
  197. {
  198. File examples (appFolder.getSiblingFile ("examples"));
  199. if (examples.exists())
  200. return examples;
  201. appFolder = appFolder.getParentDirectory();
  202. }
  203. return File::nonexistent;
  204. }
  205. private:
  206. OwnedArray<TemplateOptionButton> optionButtons;
  207. NewProjectWizardClasses::WizardComp* newProjectWizard;
  208. ScopedPointer<TemplateOptionButton> blankProjectButton, openProjectButton, exampleProjectButton;
  209. void buttonClicked (Button* b) override
  210. {
  211. if (b == blankProjectButton)
  212. createBlankProject();
  213. else if (b == exampleProjectButton)
  214. openExampleProject();
  215. else if (dynamic_cast<TemplateOptionButton*> (b) != nullptr)
  216. showWizard (b->getButtonText());
  217. }
  218. void buttonStateChanged (Button*) override
  219. {
  220. repaint();
  221. }
  222. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TemplateTileBrowser)
  223. };
  224. #endif // JUCER_TEMPLATETHUMBNAILSCOMPONENT_H_INCLUDED