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.

415 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "MainComponent.h"
  20. //==============================================================================
  21. struct SidePanelHeader : public Component
  22. {
  23. SidePanelHeader (MainComponent& o)
  24. : owner (o)
  25. {
  26. setOpaque (true);
  27. static const unsigned char homeIconPathData[]
  28. = { 110,109,0,0,64,65,0,0,64,64,98,0,0,64,65,0,0,64,64,74,12,186,64,164,112,5,65,24,217,22,64,70,182,
  29. 51,65,98,35,219,9,64,240,167,54,65,0,0,0,64,170,241,58,65,0,0,0,64,0,0,64,65,98,0,0,0,64,22,217,
  30. 72,65,166,155,28,64,0,0,80,65,0,0,64,64,0,0,80,65,108,0,0,160,64,0,0,80,65,108,0,0,160,64,0,0,
  31. 160,65,98,0,0,160,64,139,108,164,65,211,77,174,64,0,0,168,65,0,0,192,64,0,0,168,65,108,0,0,16,65,
  32. 0,0,168,65,98,22,217,24,65,0,0,168,65,0,0,32,65,127,106,164,65,0,0,32,65,0,0,160,65,108,0,0,32,65,0,0,128,
  33. 65,108,0,0,96,65,0,0,128,65,108,0,0,96,65,0,0,160,65,98,0,0,96,65,127,106,164,65,233,38,103,65,0,0,168,
  34. 65,0,0,112,65,0,0,168,65,108,0,0,144,65,0,0,168,65,98,139,108,148,65,0,0,168,65,0,0,152,65,139,108,164,
  35. 65,0,0,152,65,0,0,160,65,108,0,0,152,65,0,0,80,65,108,0,0,168,65,0,0,80,65,98,139,108,172,65,0,0,80,65,0,
  36. 0,176,65,23,217,72,65,0,0,176,65,0,0,64,65,98,0,0,176,65,170,241,58,65,156,196,174,65,240,167,54,65,158,239,
  37. 172,65,70,182,51,65,98,213,120,145,65,164,112,5,65,0,0,64,65,0,0,64,64,0,0,64,65,0,0,64,64,99,
  38. 101,0,0 };
  39. static const unsigned char settingsIconPathData[]
  40. = { 110,109,202,111,210,64,243,226,61,64,108,0,0,224,64,0,0,0,0,108,0,0,48,65,0,0,0,0,108,27,200,54,65,243,
  41. 226,61,64,98,91,248,63,65,174,170,76,64,95,130,72,65,231,138,96,64,46,46,80,65,180,163,120,64,108,42,
  42. 181,124,65,20,38,49,64,108,149,90,142,65,246,108,199,64,108,68,249,118,65,2,85,1,65,98,112,166,119,65,
  43. 201,31,6,65,0,0,120,65,111,5,11,65,0,0,120,65,0,0,16,65,98,0,0,120,65,145,250,20,65,108,166,119,65,55,
  44. 224,25,65,72,249,118,65,254,170,30,65,108,151,90,142,65,133,73,60,65,108,46,181,124,65,123,182,115,65,
  45. 108,50,46,80,65,18,215,97,65,98,99,130,72,65,70,221,103,65,96,248,63,65,83,213,108,65,32,200,54,65,66,
  46. 135,112,65,108,0,0,48,65,0,0,144,65,108,0,0,224,64,0,0,144,65,108,202,111,210,64,67,135,112,65,98,74,
  47. 15,192,64,84,213,108,65,65,251,174,64,70,221,103,65,164,163,159,64,19,215,97,65,108,92,43,13,64,123,182,
  48. 115,65,108,187,181,82,62,133,73,60,65,108,244,26,36,64,254,170,30,65,98,64,102,33,64,55,224,25,5,0,0,32,
  49. 64,145,250,20,65,0,0,32,64,0,0,16,65,98,0,0,32,64,111,5,11,65,64,102,33,64,201,31,6,65,244,26,36,64,2,85,
  50. 1,65,108,187,181,82,62,246,108,199,64,108,92,43,13,64,20,38,49,64,108,164,163,159,64,180,163,120,64,98,65,
  51. 251,174,64,231,138,96,64,74,15,192,64,175,170,76,64,202,111,210,64,243,226,61,64,99,109,0,0,16,65,0,0,64,
  52. 65,98,121,130,42,65,0,0,64,65,0,0,64,65,121,130,42,65,0,0,64,65,0,0,16,65,98,0,0,64,65,13,251,234,64,121,
  53. 130,42,65,0,0,192,64,0,0,16,65,0,0,192,64,98,13,251,234,64,0,0,192,64,0,0,192,64,13,251,234,64,0,0,192,64,
  54. 0,0,16,65,98,0,0,192,64,121,130,42,65,13,251,234,64,0,0,64,65,0,0,16,65,0,0,64,65,99,101,0,0 };
  55. Path p;
  56. p.loadPathFromData (homeIconPathData, sizeof (homeIconPathData));
  57. homeButton.setShape (p, true, true, false);
  58. p.clear();
  59. p.loadPathFromData (settingsIconPathData, sizeof (settingsIconPathData));
  60. settingsButton.setShape (p, true, true, false);
  61. titleLabel.setText (owner.getSidePanel().getTitleText(), NotificationType::dontSendNotification);
  62. addAndMakeVisible (titleLabel);
  63. homeButton.onClick = [this] { owner.homeButtonClicked(); };
  64. addAndMakeVisible (homeButton);
  65. addAndMakeVisible (settingsButton);
  66. settingsButton.onClick = [this] { owner.settingsButtonClicked(); };
  67. lookAndFeelChanged();
  68. }
  69. void paint (Graphics& g) override
  70. {
  71. g.fillAll (owner.getSidePanel().findColour (SidePanel::backgroundColour));
  72. }
  73. void resized() override
  74. {
  75. auto bounds = getLocalBounds();
  76. auto buttonWidth = owner.getSidePanel().getTitleBarHeight();
  77. bounds.removeFromLeft (10);
  78. homeButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  79. settingsButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  80. bounds.removeFromLeft (10);
  81. bounds.removeFromRight (10);
  82. titleLabel.setBounds (bounds);
  83. }
  84. void lookAndFeelChanged() override
  85. {
  86. auto& sidePanel = owner.getSidePanel();
  87. auto& lf = sidePanel.getLookAndFeel();
  88. titleLabel.setFont (lf.getSidePanelTitleFont (sidePanel));
  89. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (sidePanel));
  90. titleLabel.setColour (Label::textColourId, owner.findColour (SidePanel::titleTextColour));
  91. auto normal = sidePanel.findColour (SidePanel::dismissButtonNormalColour);
  92. auto over = sidePanel.findColour (SidePanel::dismissButtonOverColour);
  93. auto down = sidePanel.findColour (SidePanel::dismissButtonDownColour);
  94. homeButton.setColours (normal, over, down);
  95. settingsButton.setColours (normal, over, down);
  96. }
  97. MainComponent& owner;
  98. Label titleLabel;
  99. ShapeButton homeButton { "Home", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack },
  100. settingsButton { "Settings", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  101. };
  102. //==============================================================================
  103. class DemoList : public Component,
  104. public ListBoxModel
  105. {
  106. public:
  107. DemoList (DemoContentComponent& holder)
  108. : demoHolder (holder)
  109. {
  110. addAndMakeVisible (demos);
  111. demos.setModel (this);
  112. demos.setRowHeight (40);
  113. }
  114. void resized() override
  115. {
  116. demos.setBounds (getLocalBounds());
  117. }
  118. //==============================================================================
  119. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  120. {
  121. Rectangle<int> bounds (0, 0, width, height);
  122. auto textColour = findColour (Label::textColourId);
  123. g.setColour (textColour.withAlpha (0.4f));
  124. if (rowNumber == 0)
  125. g.fillRect (bounds.removeFromTop (2).reduced (7, 0));
  126. g.fillRect (bounds.removeFromBottom (2).reduced (7, 0));
  127. if (rowIsSelected)
  128. {
  129. g.setColour (findColour (TextEditor::highlightColourId).withAlpha (0.4f));
  130. g.fillRect (bounds);
  131. textColour = findColour (TextEditor::highlightedTextColourId);
  132. }
  133. g.setColour (textColour);
  134. if (selectedCategory.isEmpty())
  135. {
  136. if (isPositiveAndBelow (rowNumber, JUCEDemos::getCategories().size()))
  137. g.drawFittedText (JUCEDemos::getCategories()[(size_t) rowNumber].name,
  138. bounds, Justification::centred, 1);
  139. }
  140. else
  141. {
  142. auto& category = JUCEDemos::getCategory (selectedCategory);
  143. if (isPositiveAndBelow (rowNumber, category.demos.size()))
  144. g.drawFittedText (category.demos[(size_t) rowNumber].demoFile.getFileName(),
  145. bounds, Justification::centred, 1);
  146. }
  147. }
  148. int getNumRows() override
  149. {
  150. return (int) (selectedCategory.isEmpty() ? JUCEDemos::getCategories().size()
  151. : JUCEDemos::getCategory (selectedCategory).demos.size());
  152. }
  153. void selectedRowsChanged (int row) override
  154. {
  155. if (row < 0)
  156. return;
  157. if (selectedCategory.isEmpty())
  158. showCategory (JUCEDemos::getCategories()[(size_t) row].name);
  159. else
  160. demoHolder.setDemo (selectedCategory, row);
  161. }
  162. //==============================================================================
  163. void showCategory (const String& categoryName) noexcept
  164. {
  165. selectedCategory = categoryName;
  166. demos.deselectAllRows();
  167. demos.setHeaderComponent (categoryName.isEmpty() ? nullptr
  168. : new Header (*this));
  169. demos.updateContent();
  170. }
  171. private:
  172. String selectedCategory;
  173. DemoContentComponent& demoHolder;
  174. ListBox demos;
  175. struct Header : public Component
  176. {
  177. Header (DemoList& o)
  178. : owner (o)
  179. {
  180. setSize (0, 30);
  181. }
  182. void paint (Graphics& g) override
  183. {
  184. g.setColour (findColour (Label::textColourId));
  185. g.drawFittedText ("<", getLocalBounds().reduced (20, 0), Justification::centredLeft, 1);
  186. }
  187. void mouseDown (const MouseEvent&) override
  188. {
  189. owner.showCategory ({});
  190. }
  191. DemoList& owner;
  192. };
  193. };
  194. //==============================================================================
  195. MainComponent::MainComponent()
  196. {
  197. contentComponent.reset (new DemoContentComponent (*this, [this] (bool isHeavyweight)
  198. {
  199. demosPanel.showOrHide (false);
  200. if (isHeavyweight)
  201. {
  202. #if JUCE_MAC && USE_COREGRAPHICS_RENDERING
  203. setRenderingEngine (1);
  204. #else
  205. setRenderingEngine (0);
  206. #endif
  207. }
  208. isShowingHeavyweightDemo = isHeavyweight;
  209. resized();
  210. }));
  211. demosPanel.setContent (new DemoList (*contentComponent));
  212. demosPanel.setTitleBarComponent (new SidePanelHeader (*this), true);
  213. addAndMakeVisible (contentComponent.get());
  214. addAndMakeVisible (showDemosButton);
  215. addAndMakeVisible (demosPanel);
  216. showDemosButton.onClick = [this] { demosPanel.showOrHide (true); };
  217. demosPanel.onPanelMove = [this]
  218. {
  219. sidePanelWidth = jmax (0, demosPanel.getRight());
  220. if (isShowingHeavyweightDemo)
  221. resized();
  222. };
  223. demosPanel.onPanelShowHide = [this] (bool isShowing)
  224. {
  225. if (isShowing)
  226. {
  227. sidePanelWidth = jmax (0, demosPanel.getWidth());
  228. if (isShowingHeavyweightDemo)
  229. resized();
  230. }
  231. else
  232. {
  233. sidePanelWidth = 0;
  234. if (isShowingHeavyweightDemo)
  235. Timer::callAfterDelay (250, [this] { resized(); });
  236. }
  237. };
  238. contentComponent->showHomeScreen();
  239. setOpaque (true);
  240. setSize (800, 800);
  241. }
  242. MainComponent::~MainComponent()
  243. {
  244. contentComponent->clearCurrentDemo();
  245. }
  246. //==============================================================================
  247. void MainComponent::paint (Graphics& g)
  248. {
  249. g.fillAll (findColour (ResizableWindow::backgroundColourId));
  250. }
  251. void MainComponent::resized()
  252. {
  253. auto bounds = getLocalBounds();
  254. showDemosButton.setBounds (0, 0, 150, contentComponent->getTabBarDepth());
  255. if (isShowingHeavyweightDemo)
  256. {
  257. bounds.removeFromLeft (sidePanelWidth);
  258. contentComponent->setTabBarIndent (jmax (0, 150 - sidePanelWidth));
  259. }
  260. else
  261. {
  262. contentComponent->setTabBarIndent (150);
  263. }
  264. contentComponent->setBounds (bounds);
  265. }
  266. void MainComponent::homeButtonClicked()
  267. {
  268. if (auto* list = dynamic_cast<DemoList*> (demosPanel.getContent()))
  269. list->showCategory ({});
  270. if (contentComponent != nullptr)
  271. {
  272. if (contentComponent->isShowingHomeScreen())
  273. return;
  274. contentComponent->showHomeScreen();
  275. if (isShowingHeavyweightDemo)
  276. {
  277. isShowingHeavyweightDemo = false;
  278. resized();
  279. }
  280. }
  281. }
  282. void MainComponent::settingsButtonClicked()
  283. {
  284. if (contentComponent != nullptr)
  285. contentComponent->setCurrentTabIndex (2);
  286. }
  287. void MainComponent::setRenderingEngine (int renderingEngineIndex)
  288. {
  289. if (renderingEngineIndex != currentRenderingEngineIdx)
  290. updateRenderingEngine (renderingEngineIndex);
  291. }
  292. void MainComponent::parentHierarchyChanged()
  293. {
  294. auto* newPeer = getPeer();
  295. if (peer != newPeer)
  296. {
  297. peer = newPeer;
  298. auto previousRenderingEngine = renderingEngines[currentRenderingEngineIdx];
  299. renderingEngines.clear();
  300. if (peer != nullptr)
  301. renderingEngines = peer->getAvailableRenderingEngines();
  302. renderingEngines.add ("OpenGL Renderer");
  303. currentRenderingEngineIdx = renderingEngines.indexOf (previousRenderingEngine);
  304. if (currentRenderingEngineIdx < 0)
  305. {
  306. #if JUCE_ANDROID
  307. currentRenderingEngineIdx = (renderingEngines.size() - 1);
  308. #else
  309. currentRenderingEngineIdx = peer->getCurrentRenderingEngine();
  310. #endif
  311. }
  312. updateRenderingEngine (currentRenderingEngineIdx);
  313. }
  314. }
  315. void MainComponent::updateRenderingEngine (int renderingEngineIndex)
  316. {
  317. if (renderingEngineIndex == (renderingEngines.size() - 1))
  318. {
  319. if (isShowingHeavyweightDemo)
  320. return;
  321. openGLContext.attachTo (*getTopLevelComponent());
  322. }
  323. else
  324. {
  325. openGLContext.detach();
  326. peer->setCurrentRenderingEngine (renderingEngineIndex);
  327. }
  328. currentRenderingEngineIdx = renderingEngineIndex;
  329. }