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.

236 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: VideoDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Plays video files.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra, juce_video
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: VideoDemo
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. // so that we can easily have two video windows each with a file browser, wrap this up as a class..
  36. class MovieComponentWithFileBrowser : public Component,
  37. public DragAndDropTarget,
  38. private FilenameComponentListener
  39. {
  40. public:
  41. MovieComponentWithFileBrowser()
  42. {
  43. addAndMakeVisible (videoComp);
  44. addAndMakeVisible (fileChooser);
  45. fileChooser.addListener (this);
  46. fileChooser.setBrowseButtonText ("browse");
  47. }
  48. void setFile (const File& file)
  49. {
  50. fileChooser.setCurrentFile (file, true);
  51. }
  52. void paintOverChildren (Graphics& g) override
  53. {
  54. if (isDragOver)
  55. {
  56. g.setColour (Colours::red);
  57. g.drawRect (fileChooser.getBounds(), 2);
  58. }
  59. }
  60. void resized() override
  61. {
  62. videoComp.setBounds (getLocalBounds().reduced (10));
  63. }
  64. bool isInterestedInDragSource (const SourceDetails&) override { return true; }
  65. void itemDragEnter (const SourceDetails&) override
  66. {
  67. isDragOver = true;
  68. repaint();
  69. }
  70. void itemDragExit (const SourceDetails&) override
  71. {
  72. isDragOver = false;
  73. repaint();
  74. }
  75. void itemDropped (const SourceDetails& dragSourceDetails) override
  76. {
  77. setFile (dragSourceDetails.description.toString());
  78. isDragOver = false;
  79. repaint();
  80. }
  81. private:
  82. VideoComponent videoComp;
  83. bool isDragOver = false;
  84. FilenameComponent fileChooser { "movie", {}, true, false, false, "*", {}, "(choose a video file to play)"};
  85. void filenameComponentChanged (FilenameComponent*) override
  86. {
  87. // this is called when the user changes the filename in the file chooser box
  88. auto result = videoComp.load (fileChooser.getCurrentFile());
  89. if (result.wasOk())
  90. {
  91. // loaded the file ok, so let's start it playing..
  92. videoComp.play();
  93. resized(); // update to reflect the video's aspect ratio
  94. }
  95. else
  96. {
  97. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  98. "Couldn't load the file!",
  99. result.getErrorMessage());
  100. }
  101. }
  102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MovieComponentWithFileBrowser)
  103. };
  104. //==============================================================================
  105. class VideoDemo : public Component,
  106. public DragAndDropContainer,
  107. private FileBrowserListener
  108. {
  109. public:
  110. VideoDemo()
  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 .onClick = [this] { movieCompLeft .setFile (fileTree.getSelectedFile (0)); };
  120. loadRightButton.onClick = [this] { movieCompRight.setFile (fileTree.getSelectedFile (0)); };
  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. setSize (500, 500);
  135. }
  136. ~VideoDemo()
  137. {
  138. fileTree.removeListener (this);
  139. }
  140. void paint (Graphics& g) override
  141. {
  142. g.fillAll (getUIColourIfAvailable (LookAndFeel_V4::ColourScheme::UIColour::windowBackground));
  143. }
  144. void resized() override
  145. {
  146. // make a list of two of our child components that we want to reposition
  147. Component* comps[] = { &fileTree, &resizerBar, nullptr };
  148. // this will position the 3 components, one above the other, to fit
  149. // vertically into the rectangle provided.
  150. stretchableManager.layOutComponents (comps, 3,
  151. 0, 0, getWidth(), getHeight(),
  152. true, true);
  153. // now position out two video components in the space that's left
  154. auto area = getLocalBounds().removeFromBottom (getHeight() - resizerBar.getBottom());
  155. {
  156. auto buttonArea = area.removeFromTop (30);
  157. loadLeftButton .setBounds (buttonArea.removeFromLeft (buttonArea.getWidth() / 2).reduced (5));
  158. loadRightButton.setBounds (buttonArea.reduced (5));
  159. }
  160. movieCompLeft .setBounds (area.removeFromLeft (area.getWidth() / 2).reduced (5));
  161. movieCompRight.setBounds (area.reduced (5));
  162. }
  163. private:
  164. WildcardFileFilter moviesWildcardFilter { "*", "*", "Movies File Filter" };
  165. TimeSliceThread directoryThread { "Movie File Scanner Thread" };
  166. DirectoryContentsList movieList { &moviesWildcardFilter, directoryThread };
  167. FileTreeComponent fileTree { movieList };
  168. StretchableLayoutManager stretchableManager;
  169. StretchableLayoutResizerBar resizerBar { &stretchableManager, 1, false };
  170. TextButton loadLeftButton { "Load Left" },
  171. loadRightButton { "Load Right" };
  172. MovieComponentWithFileBrowser movieCompLeft, movieCompRight;
  173. void selectionChanged() override
  174. {
  175. // we're just going to update the drag description of out tree so that rows can be dragged onto the file players
  176. fileTree.setDragAndDropDescription (fileTree.getSelectedFile().getFullPathName());
  177. }
  178. void fileClicked (const File&, const MouseEvent&) override {}
  179. void fileDoubleClicked (const File&) override {}
  180. void browserRootChanged (const File&) override {}
  181. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (VideoDemo)
  182. };