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.

244 lines
8.6KB

  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. #include "../JuceDemoHeader.h"
  18. #if JUCE_MAC || JUCE_DIRECTSHOW
  19. //==============================================================================
  20. // so that we can easily have two video windows each with a file browser, wrap this up as a class..
  21. class MovieComponentWithFileBrowser : public Component,
  22. public DragAndDropTarget,
  23. private FilenameComponentListener
  24. {
  25. public:
  26. MovieComponentWithFileBrowser()
  27. : isDragOver (false),
  28. fileChooser ("movie", File(), true, false, false,
  29. "*", String(), "(choose a video file to play)")
  30. {
  31. addAndMakeVisible (videoComp);
  32. addAndMakeVisible (fileChooser);
  33. fileChooser.addListener (this);
  34. fileChooser.setBrowseButtonText ("browse");
  35. }
  36. void setFile (const File& file)
  37. {
  38. fileChooser.setCurrentFile (file, true);
  39. }
  40. void paintOverChildren (Graphics& g) override
  41. {
  42. if (isDragOver)
  43. {
  44. g.setColour (Colours::red);
  45. g.drawRect (fileChooser.getBounds(), 2);
  46. }
  47. }
  48. void resized() override
  49. {
  50. videoComp.setBoundsWithCorrectAspectRatio (Rectangle<int> (0, 0, getWidth(), getHeight() - 30),
  51. Justification::centred);
  52. fileChooser.setBounds (0, getHeight() - 24, getWidth(), 24);
  53. }
  54. bool isInterestedInDragSource (const SourceDetails&) override { return true; }
  55. void itemDragEnter (const SourceDetails&) override
  56. {
  57. isDragOver = true;
  58. repaint();
  59. }
  60. void itemDragExit (const SourceDetails&) override
  61. {
  62. isDragOver = false;
  63. repaint();
  64. }
  65. void itemDropped (const SourceDetails& dragSourceDetails) override
  66. {
  67. setFile (dragSourceDetails.description.toString());
  68. isDragOver = false;
  69. repaint();
  70. }
  71. private:
  72. #if JUCE_MAC
  73. MovieComponent videoComp;
  74. #elif JUCE_DIRECTSHOW
  75. DirectShowComponent videoComp;
  76. #endif
  77. bool isDragOver;
  78. FilenameComponent fileChooser;
  79. void filenameComponentChanged (FilenameComponent*) override
  80. {
  81. // this is called when the user changes the filename in the file chooser box
  82. if (videoComp.loadMovie (fileChooser.getCurrentFile()))
  83. {
  84. // loaded the file ok, so let's start it playing..
  85. videoComp.play();
  86. resized(); // update to reflect the video's aspect ratio
  87. }
  88. else
  89. {
  90. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  91. "Couldn't load the file!", String());
  92. }
  93. }
  94. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  95. };
  96. //==============================================================================
  97. class VideoDemo : public Component,
  98. public DragAndDropContainer,
  99. private Button::Listener,
  100. private FileBrowserListener
  101. {
  102. public:
  103. VideoDemo()
  104. : moviesWildcardFilter ("*", "*", "Movies File Filter"),
  105. directoryThread ("Movie File Scanner Thread"),
  106. movieList (&moviesWildcardFilter, directoryThread),
  107. fileTree (movieList),
  108. resizerBar (&stretchableManager, 1, false)
  109. {
  110. setOpaque (true);
  111. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  112. directoryThread.startThread (1);
  113. fileTree.addListener (this);
  114. fileTree.setColour (TreeView::backgroundColourId, Colours::lightgrey);
  115. addAndMakeVisible (fileTree);
  116. addAndMakeVisible (resizerBar);
  117. loadLeftButton.setButtonText ("Load Left");
  118. loadRightButton.setButtonText ("Load Right");
  119. loadLeftButton.addListener (this);
  120. loadRightButton.addListener (this);
  121. addAndMakeVisible (loadLeftButton);
  122. addAndMakeVisible (loadRightButton);
  123. addAndMakeVisible (movieCompLeft);
  124. addAndMakeVisible (movieCompRight);
  125. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  126. stretchableManager.setItemLayout (0, // for the fileTree
  127. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  128. -0.3); // and its preferred size is 30% of the total available space
  129. stretchableManager.setItemLayout (1, // for the resize bar
  130. 5, 5, 5); // hard limit to 5 pixels
  131. stretchableManager.setItemLayout (2, // for the movie components
  132. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  133. -0.7); // and its preferred size is 70% of the total available space
  134. }
  135. ~VideoDemo()
  136. {
  137. loadLeftButton.removeListener (this);
  138. loadRightButton.removeListener (this);
  139. fileTree.removeListener (this);
  140. }
  141. void paint (Graphics& g) override
  142. {
  143. fillStandardDemoBackground (g);
  144. }
  145. void resized() override
  146. {
  147. // make a list of two of our child components that we want to reposition
  148. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  149. // this will position the 3 components, one above the other, to fit
  150. // vertically into the rectangle provided.
  151. stretchableManager.layOutComponents (comps, 3,
  152. 0, 0, getWidth(), getHeight(),
  153. true, true);
  154. // now position out two video components in the space that's left
  155. Rectangle<int> area (getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom()));
  156. {
  157. Rectangle<int> buttonArea (area.removeFromTop (30));
  158. loadLeftButton.setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  159. loadRightButton.setBounds (buttonArea.reduced (5));
  160. }
  161. movieCompLeft.setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  162. movieCompRight.setBounds (area.reduced (5));
  163. }
  164. private:
  165. WildcardFileFilter moviesWildcardFilter;
  166. TimeSliceThread directoryThread;
  167. DirectoryContentsList movieList;
  168. FileTreeComponent fileTree;
  169. StretchableLayoutManager stretchableManager;
  170. StretchableLayoutResizerBar resizerBar;
  171. TextButton loadLeftButton, loadRightButton;
  172. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  173. void buttonClicked (Button* button) override
  174. {
  175. if (button == &loadLeftButton)
  176. movieCompLeft.setFile (fileTree.getSelectedFile (0));
  177. else if (button == &loadRightButton)
  178. movieCompRight.setFile (fileTree.getSelectedFile (0));
  179. }
  180. void selectionChanged() override
  181. {
  182. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  183. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  184. }
  185. void fileClicked (const File&, const MouseEvent&) override {}
  186. void fileDoubleClicked (const File&) override {}
  187. void browserRootChanged (const File&) override {}
  188. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  189. };
  190. // This static object will register this demo type in a global list of demos..
  191. static JuceDemoType<VideoDemo> demo ("29 Graphics: Video Playback");
  192. #endif