ChannelRackComponent.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #pragma once
  2. #include <functional>
  3. #include <juce_gui_basics/juce_gui_basics.h>
  4. #include "core/HeldNotes.h"
  5. #include "core/RackOps.h"
  6. #include "engine/AudioEngine.h"
  7. #include "ui/ValueControl.h"
  8. namespace lynx
  9. {
  10. // The menu name of an FL instrument engine (core/NativeEngines.h, round 142, issue #5; the generator menus list them
  11. // through quickpick::generatorEntries) ("(beta)", "(requires FL Studio)")
  12. juce::String flEngineName (const natives::InstrumentEngine& e);
  13. // FL-style Channel Rack: one row per channel with mute, pan, volume, mixer insert, name, selector
  14. // and the step grid of the current pattern.
  15. // - steps: left click turns on (drag paints), right click erases
  16. // - name (channel button): click opens/closes the channel's editor (plugin window or settings),
  17. // closing the previously opened one unless Alt is held (or "keep editors open" is on);
  18. // Ctrl+click auditions; double click opens the Piano Roll; right click opens the channel menu;
  19. // dragging it up/down reorders the selected channels
  20. // - selector: click selects one row, Ctrl+click or right click toggles, Shift+click selects a range,
  21. // double click selects all; left-drag selects a range, right-drag adds the rows it crosses.
  22. // The selection receives MIDI input and is what the keys act on
  23. // - knobs, route box: the shared control contract (ui/ValueControl.h), with a value read-out while
  24. // dragging; double-click on the route box opens the Mixer at that insert
  25. // - everything under the mouse is highlighted and described in the hint bar
  26. // - dropping an audio file onto a row turns the channel into a sampler
  27. class ChannelRackComponent final : public juce::Component,
  28. public juce::FileDragAndDropTarget,
  29. public juce::DragAndDropTarget,
  30. public ui::HintSource,
  31. private juce::ChangeListener,
  32. private juce::Timer
  33. {
  34. public:
  35. explicit ChannelRackComponent (AudioEngine& engine);
  36. ~ChannelRackComponent() override;
  37. void visibilityChanged() override;
  38. std::function<void()> onOpenPianoRoll;
  39. std::function<void (int)> onOpenMixerInsert; // double-click on a route box: show the Mixer at that insert
  40. // Settings > "Keep channel editors open": opening an editor never closes the previous one.
  41. static bool keepEditorsOpen;
  42. // FL's "Select first channel with notes when the pattern changes" (Settings › General; REA round 167j §5)
  43. static bool selectFirstChannelWithNotes;
  44. void paint (juce::Graphics& g) override;
  45. void mouseDown (const juce::MouseEvent& e) override;
  46. void mouseDrag (const juce::MouseEvent& e) override;
  47. void mouseUp (const juce::MouseEvent& e) override;
  48. void mouseDoubleClick (const juce::MouseEvent& e) override;
  49. void mouseMove (const juce::MouseEvent& e) override;
  50. void mouseExit (const juce::MouseEvent& e) override;
  51. void mouseWheelMove (const juce::MouseEvent& e, const juce::MouseWheelDetails& wheel) override;
  52. ui::Hint hintAt (juce::Point<int> position) override;
  53. bool isInterestedInFileDrag (const juce::StringArray& files) override;
  54. void filesDropped (const juce::StringArray& files, int x, int y) override;
  55. // files dragged from the LYNX browser
  56. bool isInterestedInDragSource (const SourceDetails& details) override;
  57. void itemDropped (const SourceDetails& details) override;
  58. // Keyboard actions (the key map's rack.* actions)
  59. void selectRelative (int delta, bool extend);
  60. void selectAll();
  61. bool moveSelected (int delta);
  62. void cloneSelected();
  63. void deleteSelected();
  64. // FL's Cut / Copy / Paste of the selected channels' steps and notes in the current pattern (Ctrl+X / C / V)
  65. void cutSelected();
  66. void copySelected();
  67. void pasteSelected();
  68. void sendSelectedToInserts();
  69. void toggleMute (int channel);
  70. void solo (int channel);
  71. void setSwingMixOfSelected (int channel); // "Swing mix..." (type a value for the selected channels)
  72. // Workflow P2: step graph lanes, fills, rotation and organisation
  73. void toggleGraphLanes();
  74. // Piano-roll view: every row shows its notes as a miniature (a click opens them in the Piano Roll) instead of
  75. // the step buttons; FL's "step sequencer / piano roll overview" switch in the rack's title bar.
  76. void setPianoRollView (bool on);
  77. bool isPianoRollView() const noexcept { return pianoRollView; }
  78. bool areGraphLanesShown() const noexcept { return graphLanes; }
  79. juce::PopupMenu getChannelMenuForSnapshot (int channel) { return buildChannelMenu (channel); }
  80. void setGraphLevelForSnapshot (int level) { graphLevel = level; graphLanes = true; updateSize(); repaint(); }
  81. void rotateSelected (int steps);
  82. void fillSelected (int every);
  83. void showEuclideanFill (int channel);
  84. std::unique_ptr<juce::Component> makeEuclideanPanel (int channel); // the window's content (also for --snapshot)
  85. void sortChannels (ChannelSort by);
  86. void selectUnused();
  87. // FL's Channel Rack looping (round 165f-rack): the title bar's Loop button switches the current pattern's looping
  88. // on or off; its menu picks the loop type and burns the loops. Burn MIDI writes what a channel plays as notes.
  89. void toggleLooping();
  90. bool isLoopingOn() const;
  91. void showLoopMenu (juce::Component* target);
  92. void burnLoops (bool selectedOnly);
  93. void burnMidi (int channel, bool toNewPattern);
  94. // Adds a channel at the end (routed to a free insert), selects it and, for plugins, opens its window.
  95. int addChannel (ChannelDef channel, bool openEditor, int insert = -1); // insert: see addChannelRouted
  96. // Loads `file` into the selected sampler channel (Browser: Enter, Shift+Up/Down). Returns false when
  97. // the selected channel cannot take a sample.
  98. bool loadSampleIntoSelected (const juce::File& file);
  99. // Opens or closes the channel's editor (plugin window or settings window; built-in drums and audio
  100. // clips have none). `keepOthers`: don't close the previously opened editor.
  101. void toggleEditor (int channel, bool keepOthers);
  102. void updateSize();
  103. // Width that shows the controls and at most `maxSteps` steps (the panel scrolls for longer patterns).
  104. int widthForSteps (int maxSteps) const { return gridLeft() + juce::jmin (numSteps(), maxSteps) * stepWidth + 8; }
  105. int naturalWidth() const { return gridLeft() + numSteps() * stepWidth + 8; } // without the ghost steps
  106. // The menu of instruments and plugins to add as a new channel (also the toolbar's plugin picker).
  107. void showAddChannelMenu (int insertAt = -1);
  108. // The same items into `menu` (also the main window's Add menu, REA round 139).
  109. void addInstrumentItems (juce::PopupMenu& menu, int insertAt);
  110. // Channel filter groups (FL's "All" dropdown in the rack's title, REA round 104): -1 shows every channel,
  111. // otherwise only the channels of that group (Song::channelGroups). New channels join the group shown.
  112. int getFilterGroup() const noexcept { return filterGroup; }
  113. void setFilterGroup (int group);
  114. juce::String filterName() const;
  115. void showFilterMenu (juce::Component* target);
  116. void groupSelected (); // "Group selected..." (Alt+G): the selected channels go into a new, named group
  117. void moveSelectedToGroup (int group); // -1 = no group
  118. bool isShown (int row) const; // not hidden by the filter
  119. std::function<void()> onFilterChanged; // the title shows the filter's name and the graph editor's state
  120. static constexpr int rowHeight = 30;
  121. static constexpr int muteWidth = 26;
  122. static constexpr int knobWidth = 28;
  123. static constexpr int routeWidth = 30;
  124. static constexpr int nameWidth = 150;
  125. static constexpr int selectWidth = 12;
  126. static constexpr int stepWidth = 22;
  127. private:
  128. enum class Area { none, mute, pan, volume, route, name, select, grid, addRow, laneLevels, lane, loop };
  129. void changeListenerCallback (juce::ChangeBroadcaster*) override;
  130. void timerCallback() override;
  131. void followPlayhead();
  132. Area areaAt (int x, int y, int& row) const;
  133. void moved() override;
  134. int stickyX = 0; // how far the viewport scrolled the rack sideways (the controls column stays in view)
  135. int numSteps() const; // the pattern's steps
  136. int shownSteps() const; // plus FL's ghost steps: the free width after the pattern's end (issue #11)
  137. int ghostSteps = 0;
  138. // channel looping: the row's loop point in steps (0: the row doesn't loop), the advanced controls' column (FL's
  139. // LoopWP: at the right edge of the visible rack) and its loop point menu
  140. int loopStepsOf (int row) const;
  141. bool advancedLoopShown() const;
  142. static constexpr int loopColumnWidth = 44;
  143. juce::Rectangle<int> loopBoxBounds (int row) const;
  144. void paintLoopColumn (juce::Graphics& g);
  145. void showLoopPointMenu (int row);
  146. void setLoopPoint (int row, int setting);
  147. int loopDragRow = -1, loopDragStart = 0;
  148. float loopDragY = 0.0f;
  149. void parentSizeChanged() override { updateSize(); }
  150. int nameLeft() const noexcept { return muteWidth + 2 * knobWidth + routeWidth + 2; }
  151. int selectLeft() const noexcept { return nameLeft() + nameWidth + 4; }
  152. int gridLeft() const noexcept { return selectLeft() + selectWidth + 8; }
  153. juce::Rectangle<int> rowBounds (int row) const;
  154. int rowY (int row) const; // top of a row (rows below the graph lane shift down)
  155. int filterGroup = -1;
  156. static inline ChannelStepsClipboard stepClipboard; // Cut / Copy / Paste (Ctrl+X / C / V, the channel menu)
  157. int visibleIndex (int row) const; // the row's place among the shown rows
  158. int shownCount() const;
  159. int rowAtY (int y) const; // row under y (the lane counts as its channel's row)
  160. int graphRow() const; // the row the lane belongs to (the primary selection), -1 when hidden
  161. juce::Rectangle<int> laneBounds() const;
  162. juce::Rectangle<int> levelButtonBounds (int level) const;
  163. void paintLane (juce::Graphics& g);
  164. void paintNotePreview (juce::Graphics& g, int row, const Pattern& pattern, std::uint32_t channelColour);
  165. void setLaneValue (int step, float value);
  166. void applyLane (const juce::MouseEvent& e);
  167. float laneValueAt (int y) const;
  168. bool graphLanes = false;
  169. bool pianoRollView = false;
  170. int graphLevel = 0; // notetools::Level
  171. int laneDownStep = -1;
  172. float laneDownValue = 0.0f;
  173. static constexpr int laneHeight = 72;
  174. juce::Rectangle<int> stepBounds (int row, int step) const;
  175. bool cellHasNote (int channel, int step) const;
  176. void applyPaint (const juce::MouseEvent& e);
  177. juce::PopupMenu buildChannelMenu (int channel);
  178. void showChannelMenu (int channel);
  179. void chooseSample (int channel, int insertAt = -1); // channel < 0 = new channel (at the end, or at row insertAt)
  180. void openNewGenerator (int row, InstrumentKind kind); // the window a new / replaced generator opens with
  181. void assignSample (int channel, const juce::File& file);
  182. void assignSampleNow (int channel, const juce::File& file); // (no Speech properties step)
  183. void rename (int channel);
  184. void openPluginWindow (int channel);
  185. void sliceChannel (int channel);
  186. bool isEditorOpen (int channel);
  187. void closeEditor (int channel);
  188. // selection
  189. void selectRows (std::vector<int> rows, int primary);
  190. void clickSelector (int row, const juce::ModifierKeys& mods);
  191. int selectDownRow = -1; // a drag over the selector column: where it started
  192. bool selectDragAdds = false; // right-drag: add the crossed rows to the selection
  193. std::vector<int> selectDragBase; // the selection before a right-drag
  194. // channel button: a click opens the editor on release; dragging reorders the selection
  195. int nameDownRow = -1;
  196. bool nameDragged = false, nameDownAlt = false;
  197. float nameDragY = 0.0f;
  198. // hover feedback
  199. void setHover (Area area, int row, int step);
  200. juce::Rectangle<int> controlBounds (Area area, int row) const; // knob / route box under the mouse (value tip)
  201. std::uint32_t shownAutomation = 0; // AutomationDisplay generation last drawn (issue #12)
  202. std::vector<int> automatedRows;
  203. void showTip (Area area, int row, bool transient);
  204. Area hoverArea = Area::none;
  205. int hoverRow = -1, hoverStep = -1;
  206. // the control contract for the row's knobs and route box
  207. ui::ValueSpec specFor (Area area, int row) const;
  208. double valueOf (Area area, int row) const;
  209. void setValue (Area area, int row, double value);
  210. AudioEngine& engine;
  211. int playheadStep = -1;
  212. static constexpr int positionStripHeight = 9; // FL's "Playing step" LED strip, at the top of the add row
  213. bool channelHitsNow (int row) const;
  214. static void paintStepHit (juce::Graphics&, juce::Rectangle<float>);
  215. enum class PaintMode { none, add, remove } paintMode = PaintMode::none;
  216. int lastPaintedRow = -1, lastPaintedStep = -1;
  217. Area knobDrag = Area::none;
  218. int knobRow = -1;
  219. ui::ValueDrag drag;
  220. HeldNotes previews; // Ctrl+click audition and step-click previews: they stop on mouse up
  221. int openEditorRow = -1; // the editor opened last by a click on a channel button
  222. int selectionAnchor = 0;
  223. std::unique_ptr<juce::FileChooser> fileChooser;
  224. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChannelRackComponent)
  225. };
  226. // The Channel Rack panel's viewport: a sample, SoundFont or generator dropped on the free space under the channels
  227. // becomes a new channel too, as anywhere in FL's Channel Rack (issue #11); the drop goes to the rack.
  228. class ChannelRackViewport final : public juce::Viewport,
  229. public juce::FileDragAndDropTarget,
  230. public juce::DragAndDropTarget
  231. {
  232. public:
  233. explicit ChannelRackViewport (ChannelRackComponent& r) : rack (r)
  234. {
  235. // juce::Viewport makes itself transparent to clicks, so drag-and-drop would skip it and the empty space under
  236. // the last channel would not accept drops. Children still get their own clicks.
  237. setInterceptsMouseClicks (true, true);
  238. }
  239. bool isInterestedInFileDrag (const juce::StringArray& files) override { return rack.isInterestedInFileDrag (files); }
  240. void filesDropped (const juce::StringArray& files, int x, int y) override
  241. {
  242. const auto p = rack.getLocalPoint (this, juce::Point<int> (x, y));
  243. rack.filesDropped (files, p.x, p.y);
  244. }
  245. bool isInterestedInDragSource (const SourceDetails& details) override { return rack.isInterestedInDragSource (details); }
  246. void itemDropped (const SourceDetails& details) override
  247. {
  248. auto moved = details;
  249. moved.localPosition = rack.getLocalPoint (this, details.localPosition);
  250. rack.itemDropped (moved);
  251. }
  252. private:
  253. ChannelRackComponent& rack;
  254. };
  255. } // namespace lynx