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.

243 lines
8.5KB

  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_WINDOWS
  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.setBounds (getLocalBounds().reduced (10));
  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. VideoComponent videoComp;
  73. bool isDragOver;
  74. FilenameComponent fileChooser;
  75. void filenameComponentChanged (FilenameComponent*) override
  76. {
  77. // this is called when the user changes the filename in the file chooser box
  78. auto result = videoComp.load (fileChooser.getCurrentFile());
  79. if (result.wasOk())
  80. {
  81. // loaded the file ok, so let's start it playing..
  82. videoComp.play();
  83. resized(); // update to reflect the video's aspect ratio
  84. }
  85. else
  86. {
  87. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  88. "Couldn't load the file!",
  89. result.getErrorMessage());
  90. }
  91. }
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  93. };
  94. //==============================================================================
  95. class VideoDemo : public Component,
  96. public DragAndDropContainer,
  97. private Button::Listener,
  98. private FileBrowserListener
  99. {
  100. public:
  101. VideoDemo()
  102. : moviesWildcardFilter ("*", "*", "Movies File Filter"),
  103. directoryThread ("Movie File Scanner Thread"),
  104. movieList (&moviesWildcardFilter, directoryThread),
  105. fileTree (movieList),
  106. resizerBar (&stretchableManager, 1, false)
  107. {
  108. setOpaque (true);
  109. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  110. directoryThread.startThread (1);
  111. fileTree.addListener (this);
  112. fileTree.setColour (FileTreeComponent::backgroundColourId, Colours::lightgrey.withAlpha (0.6f));
  113. addAndMakeVisible (fileTree);
  114. addAndMakeVisible (resizerBar);
  115. loadLeftButton.setButtonText ("Load Left");
  116. loadRightButton.setButtonText ("Load Right");
  117. loadLeftButton.addListener (this);
  118. loadRightButton.addListener (this);
  119. addAndMakeVisible (loadLeftButton);
  120. addAndMakeVisible (loadRightButton);
  121. addAndMakeVisible (movieCompLeft);
  122. addAndMakeVisible (movieCompRight);
  123. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  124. stretchableManager.setItemLayout (0, // for the fileTree
  125. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  126. -0.3); // and its preferred size is 30% of the total available space
  127. stretchableManager.setItemLayout (1, // for the resize bar
  128. 5, 5, 5); // hard limit to 5 pixels
  129. stretchableManager.setItemLayout (2, // for the movie components
  130. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  131. -0.7); // and its preferred size is 70% of the total available space
  132. }
  133. ~VideoDemo()
  134. {
  135. loadLeftButton.removeListener (this);
  136. loadRightButton.removeListener (this);
  137. fileTree.removeListener (this);
  138. }
  139. void paint (Graphics& g) override
  140. {
  141. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  142. }
  143. void resized() override
  144. {
  145. // make a list of two of our child components that we want to reposition
  146. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  147. // this will position the 3 components, one above the other, to fit
  148. // vertically into the rectangle provided.
  149. stretchableManager.layOutComponents (comps, 3,
  150. 0, 0, getWidth(), getHeight(),
  151. true, true);
  152. // now position out two video components in the space that's left
  153. Rectangle<int> area (getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom()));
  154. {
  155. Rectangle<int> buttonArea (area.removeFromTop (30));
  156. loadLeftButton.setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  157. loadRightButton.setBounds (buttonArea.reduced (5));
  158. }
  159. movieCompLeft.setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  160. movieCompRight.setBounds (area.reduced (5));
  161. }
  162. private:
  163. WildcardFileFilter moviesWildcardFilter;
  164. TimeSliceThread directoryThread;
  165. DirectoryContentsList movieList;
  166. FileTreeComponent fileTree;
  167. StretchableLayoutManager stretchableManager;
  168. StretchableLayoutResizerBar resizerBar;
  169. TextButton loadLeftButton, loadRightButton;
  170. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  171. void buttonClicked (Button* button) override
  172. {
  173. if (button == &loadLeftButton)
  174. movieCompLeft.setFile (fileTree.getSelectedFile (0));
  175. else if (button == &loadRightButton)
  176. movieCompRight.setFile (fileTree.getSelectedFile (0));
  177. }
  178. void selectionChanged() override
  179. {
  180. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  181. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  182. }
  183. void fileClicked (const File&, const MouseEvent&) override {}
  184. void fileDoubleClicked (const File&) override {}
  185. void browserRootChanged (const File&) override {}
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  187. };
  188. // This static object will register this demo type in a global list of demos..
  189. static JuceDemoType<VideoDemo> demo ("29 Graphics: Video Playback");
  190. #endif