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.

407 lines
14KB

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