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.

246 lines
8.7KB

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