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.

232 lines
8.2KB

  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 && ! JUCE_MINGW)
  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 FileBrowserListener
  98. {
  99. public:
  100. VideoDemo()
  101. : moviesWildcardFilter ("*", "*", "Movies File Filter"),
  102. directoryThread ("Movie File Scanner Thread"),
  103. movieList (&moviesWildcardFilter, directoryThread),
  104. fileTree (movieList),
  105. resizerBar (&stretchableManager, 1, false)
  106. {
  107. setOpaque (true);
  108. movieList.setDirectory (File::getSpecialLocation (File::userMoviesDirectory), true, true);
  109. directoryThread.startThread (1);
  110. fileTree.addListener (this);
  111. fileTree.setColour (FileTreeComponent::backgroundColourId, Colours::lightgrey.withAlpha (0.6f));
  112. addAndMakeVisible (fileTree);
  113. addAndMakeVisible (resizerBar);
  114. loadLeftButton.setButtonText ("Load Left");
  115. loadRightButton.setButtonText ("Load Right");
  116. loadLeftButton .onClick = [this] { movieCompLeft .setFile (fileTree.getSelectedFile (0)); };
  117. loadRightButton.onClick = [this] { movieCompRight.setFile (fileTree.getSelectedFile (0)); };
  118. addAndMakeVisible (loadLeftButton);
  119. addAndMakeVisible (loadRightButton);
  120. addAndMakeVisible (movieCompLeft);
  121. addAndMakeVisible (movieCompRight);
  122. // we have to set up our StretchableLayoutManager so it know the limits and preferred sizes of it's contents
  123. stretchableManager.setItemLayout (0, // for the fileTree
  124. -0.1, -0.9, // must be between 50 pixels and 90% of the available space
  125. -0.3); // and its preferred size is 30% of the total available space
  126. stretchableManager.setItemLayout (1, // for the resize bar
  127. 5, 5, 5); // hard limit to 5 pixels
  128. stretchableManager.setItemLayout (2, // for the movie components
  129. -0.1, -0.9, // size must be between 50 pixels and 90% of the available space
  130. -0.7); // and its preferred size is 70% of the total available space
  131. }
  132. ~VideoDemo()
  133. {
  134. fileTree.removeListener (this);
  135. }
  136. void paint (Graphics& g) override
  137. {
  138. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  139. }
  140. void resized() override
  141. {
  142. // make a list of two of our child components that we want to reposition
  143. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  144. // this will position the 3 components, one above the other, to fit
  145. // vertically into the rectangle provided.
  146. stretchableManager.layOutComponents (comps, 3,
  147. 0, 0, getWidth(), getHeight(),
  148. true, true);
  149. // now position out two video components in the space that's left
  150. Rectangle<int> area (getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom()));
  151. {
  152. Rectangle<int> buttonArea (area.removeFromTop (30));
  153. loadLeftButton.setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  154. loadRightButton.setBounds (buttonArea.reduced (5));
  155. }
  156. movieCompLeft.setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  157. movieCompRight.setBounds (area.reduced (5));
  158. }
  159. private:
  160. WildcardFileFilter moviesWildcardFilter;
  161. TimeSliceThread directoryThread;
  162. DirectoryContentsList movieList;
  163. FileTreeComponent fileTree;
  164. StretchableLayoutManager stretchableManager;
  165. StretchableLayoutResizerBar resizerBar;
  166. TextButton loadLeftButton, loadRightButton;
  167. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  168. void selectionChanged() override
  169. {
  170. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  171. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  172. }
  173. void fileClicked (const File&, const MouseEvent&) override {}
  174. void fileDoubleClicked (const File&) override {}
  175. void browserRootChanged (const File&) override {}
  176. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  177. };
  178. // This static object will register this demo type in a global list of demos..
  179. static JuceDemoType<VideoDemo> demo ("29 Graphics: Video Playback");
  180. #endif