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.

414 lines
14KB

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