EditorTools.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. #include "ui/FlMods.h"
  2. #include "ui/EditorTools.h"
  3. #include "core/NoteEdit.h"
  4. #include "ui/HintBar.h"
  5. #include "ui/I18n.h"
  6. #include "ui/Theme.h"
  7. namespace lynx
  8. {
  9. // --- edit tools ------------------------------------------------------------------------
  10. const std::vector<EditToolInfo>& editTools()
  11. {
  12. static const std::vector<EditToolInfo> list {
  13. { EditTool::pencil, Icon::draw, TR_NOOP ("Pencil"), TR_NOOP ("Click adds, drag moves, drag an edge resizes"), 'P' },
  14. { EditTool::brush, Icon::brush, TR_NOOP ("Brush"), TR_NOOP ("Drag to paint a run of notes or clips"), 'B' },
  15. { EditTool::drumPaint, Icon::drumBrush, TR_NOOP ("Drum brush"),
  16. TR_NOOP ("Drag to paint one short hit per grid cell, on whatever key is under the mouse"), 'N' },
  17. { EditTool::eraser, Icon::erase, TR_NOOP ("Eraser"), TR_NOOP ("Click or drag to delete"), 'D' },
  18. { EditTool::mute, Icon::mute, TR_NOOP ("Mute"), TR_NOOP ("Click or drag to mute or unmute"), 'T' },
  19. { EditTool::knife, Icon::knife, TR_NOOP ("Knife"), TR_NOOP ("Drag a line to cut; a click cuts at the cursor"), 'C' },
  20. { EditTool::lasso, Icon::select, TR_NOOP ("Lasso"), TR_NOOP ("Click or drag to select (Shift adds)"), 'E' },
  21. { EditTool::zoom, Icon::zoom, TR_NOOP ("Zoom"), TR_NOOP ("Click zooms in, drag a range to zoom to it; right-click zooms out"), 'Z' },
  22. { EditTool::listen, Icon::listen, TR_NOOP ("Listen"), TR_NOOP ("Click or drag to hear what is under the cursor"), 'Y' },
  23. { EditTool::slip, Icon::slip, TR_NOOP ("Slip"), TR_NOOP ("Drag inside a clip to move its content under it"), 'S' },
  24. };
  25. return list;
  26. }
  27. const char* editToolId (EditTool tool)
  28. {
  29. switch (tool)
  30. {
  31. case EditTool::pencil: return "pencil";
  32. case EditTool::brush: return "brush";
  33. case EditTool::drumPaint: return "drumPaint";
  34. case EditTool::eraser: return "eraser";
  35. case EditTool::mute: return "mute";
  36. case EditTool::knife: return "knife";
  37. case EditTool::lasso: return "lasso";
  38. case EditTool::zoom: return "zoom";
  39. case EditTool::listen: return "listen";
  40. case EditTool::slip: return "slip";
  41. }
  42. return "pencil";
  43. }
  44. const EditToolInfo& editToolInfo (EditTool tool)
  45. {
  46. for (const auto& t : editTools())
  47. if (t.tool == tool)
  48. return t;
  49. return editTools().front();
  50. }
  51. namespace
  52. {
  53. void setKeyTip (IconButton& button, const EditToolInfo& info, const juce::String& key)
  54. {
  55. button.setTooltip (tr (info.name) + (key.isNotEmpty() ? " (" + key + ")" : juce::String()) + "\n" + tr (info.description) + "\n"
  56. + tr ("Every tool: right-click deletes, Alt ignores the grid, Ctrl+drag selects"));
  57. ui::setQuickTip (button, tr (info.name), key);
  58. }
  59. } // namespace
  60. void ToolStrip::refreshKeys (const std::function<juce::String (EditTool)>& keyOf)
  61. {
  62. for (std::size_t i = 0; i < tools.size() && i < buttons.size(); ++i)
  63. setKeyTip (*buttons[i], editToolInfo (tools[i]), keyOf (tools[i]));
  64. }
  65. ToolStrip::ToolStrip (bool includeSlip)
  66. {
  67. for (const auto& info : editTools())
  68. {
  69. // the Playlist has Slip, the Piano Roll the drum brush
  70. if ((info.tool == EditTool::slip && ! includeSlip) || (info.tool == EditTool::drumPaint && includeSlip))
  71. continue;
  72. auto button = std::make_unique<IconButton> (info.name, info.icon);
  73. button->setRadioGroupId (0x7001);
  74. button->setClickingTogglesState (true);
  75. button->setFlat (true);
  76. button->setWantsKeyboardFocus (false);
  77. button->setMouseClickGrabsKeyboardFocus (false);
  78. button->setOnColour (theme::colour::accent.withAlpha (0.16f)); // the light header style: a faint accent wash
  79. button->setIconColourWhenOn (theme::colour::accentHover);
  80. setKeyTip (*button, info, juce::String::charToString ((juce::juce_wchar) info.key));
  81. button->setToggleState (info.tool == tool, juce::dontSendNotification);
  82. button->onClick = [this, t = info.tool] {
  83. if (tool != t)
  84. {
  85. tool = t;
  86. if (onChange)
  87. onChange (t);
  88. }
  89. };
  90. addAndMakeVisible (*button);
  91. buttons.push_back (std::move (button));
  92. tools.push_back (info.tool);
  93. }
  94. }
  95. void ToolStrip::setTool (EditTool newTool)
  96. {
  97. for (std::size_t i = 0; i < tools.size(); ++i)
  98. if (tools[i] == newTool)
  99. {
  100. tool = newTool;
  101. buttons[i]->setToggleState (true, juce::dontSendNotification);
  102. for (std::size_t j = 0; j < buttons.size(); ++j)
  103. if (j != i)
  104. buttons[j]->setToggleState (false, juce::dontSendNotification);
  105. if (onChange)
  106. onChange (tool);
  107. return;
  108. }
  109. }
  110. int ToolStrip::getIdealWidth (int height) const
  111. {
  112. return (int) buttons.size() * toolbarlayout::toolButtonWidth (iconSize, height);
  113. }
  114. void ToolStrip::resized()
  115. {
  116. const auto size = getHeight();
  117. const auto width = toolbarlayout::toolButtonWidth (iconSize, size);
  118. const auto icon = toolbarlayout::toolIconPx (iconSize);
  119. for (std::size_t i = 0; i < buttons.size(); ++i)
  120. {
  121. buttons[i]->setIconSizing (1.0f, icon);
  122. buttons[i]->setBounds ((int) i * width, 0, width, size);
  123. }
  124. }
  125. void ToolStrip::mouseWheelMove (const juce::MouseEvent&, const juce::MouseWheelDetails& wheel)
  126. {
  127. if (wheel.deltaY == 0.0f || tools.empty())
  128. return;
  129. const auto at = (int) (std::find (tools.begin(), tools.end(), tool) - tools.begin());
  130. const auto next = juce::jlimit (0, (int) tools.size() - 1, at + (wheel.deltaY < 0.0f ? 1 : -1));
  131. setTool (tools[(std::size_t) next]);
  132. }
  133. // --- cursors ---------------------------------------------------------------------------
  134. namespace
  135. {
  136. // A tool icon as a cursor: drawn at 2x, light strokes with a dark outline so it reads on the dark grid
  137. // and on bright notes alike. `hot` is the hotspot on the icon's 24-unit grid.
  138. juce::MouseCursor iconCursor (Icon icon, juce::Point<float> hot)
  139. {
  140. constexpr int size = 24, scale = 2;
  141. juce::Image image (juce::Image::ARGB, size * scale, size * scale, true);
  142. {
  143. juce::Graphics g (image);
  144. g.addTransform (juce::AffineTransform::scale ((float) scale));
  145. const auto area = juce::Rectangle<float> (0.0f, 0.0f, (float) size, (float) size);
  146. for (auto [dx, dy] : { std::pair { -1.0f, 0.0f }, { 1.0f, 0.0f }, { 0.0f, -1.0f }, { 0.0f, 1.0f },
  147. { -0.7f, -0.7f }, { 0.7f, 0.7f }, { -0.7f, 0.7f }, { 0.7f, -0.7f } })
  148. icons::draw (g, icon, area.translated (dx, dy), theme::colour::bg0);
  149. icons::draw (g, icon, area, theme::colour::textPrimary);
  150. }
  151. return juce::MouseCursor (juce::ScaledImage (image, (double) scale), hot.roundToInt());
  152. }
  153. } // namespace
  154. juce::MouseCursor editorCursor (gesture::Cursor cursor)
  155. {
  156. using C = gesture::Cursor;
  157. // icons are drawn with a margin (fitted into the 24x24 box), so the hotspots are the icon's tips
  158. static const auto pencil = iconCursor (Icon::draw, { 4.0f, 20.0f });
  159. static const auto brush = iconCursor (Icon::brush, { 4.0f, 20.0f });
  160. static const auto eraser = iconCursor (Icon::erase, { 6.0f, 18.0f });
  161. static const auto mute = iconCursor (Icon::mute, { 12.0f, 12.0f });
  162. static const auto knife = iconCursor (Icon::knife, { 4.0f, 20.0f });
  163. static const auto zoom = iconCursor (Icon::zoom, { 10.0f, 10.0f });
  164. static const auto listen = iconCursor (Icon::listen, { 12.0f, 12.0f });
  165. static const auto slip = iconCursor (Icon::slip, { 12.0f, 12.0f });
  166. switch (cursor)
  167. {
  168. case C::draw: return pencil;
  169. case C::paint: return brush;
  170. case C::erase: return eraser;
  171. case C::mute: return mute;
  172. case C::slice: return knife;
  173. case C::zoom: return zoom;
  174. case C::listen: return listen;
  175. case C::slip: return slip;
  176. case C::move: return juce::MouseCursor::UpDownLeftRightResizeCursor;
  177. case C::copy: return juce::MouseCursor::CopyingCursor;
  178. case C::resizeH: return juce::MouseCursor::LeftRightResizeCursor;
  179. case C::resizeV: return juce::MouseCursor::UpDownResizeCursor;
  180. case C::select: return juce::MouseCursor::CrosshairCursor;
  181. case C::point: return juce::MouseCursor::PointingHandCursor;
  182. case C::pan: return juce::MouseCursor::DraggingHandCursor;
  183. case C::normal: break;
  184. }
  185. return juce::MouseCursor::NormalCursor;
  186. }
  187. juce::String actionHint (gesture::Action action, bool playlist)
  188. {
  189. using A = gesture::Action;
  190. const auto common = tr ("[RMB] delete [Ctrl][LMB] select [Alt] no snap");
  191. switch (action)
  192. {
  193. case A::create:
  194. return (playlist ? tr ("[LMB] place the Picker's clip, drag to position it")
  195. : tr ("[LMB] add a note, drag to position it"))
  196. + " " + common;
  197. case A::paint:
  198. return (playlist ? tr ("[LMB] drag paints clips end to end") : tr ("[LMB] drag paints a run of notes [Shift] one at a time"))
  199. + " " + common;
  200. case A::move:
  201. return (playlist ? tr ("Clip: [LMB] drag moves [Shift][LMB] copies [2xLMB] opens [RMB] deletes")
  202. : tr ("Note: [LMB] drag moves [Shift][LMB] copies [2xLMB] properties [RMB] deletes [Alt][Wheel] velocity"));
  203. case A::copyMove: return playlist ? tr ("[Shift][LMB] drags a copy of the clip (selection)") : tr ("[Shift][LMB] drags a copy of the note (selection)");
  204. case A::resizeRight:
  205. return (playlist ? tr ("Clip end: [LMB] drag resizes") : tr ("Note end: [LMB] drag resizes")) + " " + tr ("[Alt] no snap");
  206. case A::resizeLeft:
  207. return (playlist ? tr ("Clip start: [LMB] drag trims, the content stays in place") : tr ("Note start: [LMB] drag resizes, the end stays"))
  208. + " " + tr ("[Alt] no snap");
  209. case A::slipContent: return tr ("[LMB] drag slides the clip's content inside it (the clip stays)");
  210. case A::toggleSelect: return tr ("[Ctrl][LMB] adds to / removes from the selection");
  211. case A::boxSelect: return tr ("[LMB] drag draws a selection box [Shift] adds");
  212. case A::erase: return tr ("[LMB] click or drag deletes");
  213. case A::toggleMute: return tr ("[LMB] click or drag mutes / unmutes");
  214. case A::slice: return tr ("[LMB] drag a line to cut across, a click cuts here") + " " + tr ("[Alt] no snap");
  215. case A::zoomRange: return tr ("[LMB] click zooms in, drag a range to zoom to it [RMB] zooms out");
  216. case A::listen: return playlist ? tr ("[LMB] click or drag plays from here") : tr ("[LMB] click or drag plays the notes under the cursor");
  217. case A::fade: return tr ("Fade: [LMB] drag sets its length");
  218. case A::clipGain: return tr ("Clip gain: [LMB] drag up / down (the selected clips together) [RMB] menu");
  219. case A::movePoint: return tr ("Point: [LMB] drag moves, then [Shift] keeps time, [Ctrl] keeps value [Alt] no snap [RMB] menu");
  220. case A::addPoint: return tr ("Curve: [LMB] or [RMB] adds a point (hold and drag to place it)");
  221. case A::bendSegment: return tr ("Tension: [LMB] drag up / down bends the segment ([Ctrl] fine) [RMB] straight");
  222. case A::openMenu: return tr ("[LMB] clip menu");
  223. case A::none: break;
  224. }
  225. return {};
  226. }
  227. // --- grid --------------------------------------------------------------------------------
  228. namespace grid
  229. {
  230. const std::vector<Option>& options()
  231. {
  232. static const std::vector<Option> list {
  233. { Kind::none, 1, TR_NOOP ("No snap") },
  234. { Kind::screen, 0, TR_NOOP ("Line (follows zoom)") },
  235. { Kind::nearest, 0, TR_NOOP ("Events (edges)") },
  236. { Kind::ticks, ticksPerStep / 6, TR_NOOP ("1/6 step") },
  237. { Kind::ticks, ticksPerStep / 4, TR_NOOP ("1/4 step") },
  238. { Kind::ticks, ticksPerStep / 3, TR_NOOP ("1/3 step") },
  239. { Kind::ticks, ticksPerStep / 2, TR_NOOP ("1/2 step") },
  240. { Kind::ticks, ticksPerStep, TR_NOOP ("Step") },
  241. { Kind::ticks, ppq / 3, TR_NOOP ("1/3 beat") },
  242. { Kind::ticks, ppq / 2, TR_NOOP ("1/2 beat") },
  243. { Kind::ticks, ppq, TR_NOOP ("Beat") },
  244. { Kind::bar, 0, TR_NOOP ("Bar") },
  245. };
  246. return list;
  247. }
  248. void fillEditorBox (juce::ComboBox& box)
  249. {
  250. box.clear (juce::dontSendNotification);
  251. box.addItem (tr ("Main (toolbar snap)"), followId);
  252. box.addSeparator();
  253. for (int i = 0; i < (int) options().size(); ++i)
  254. box.addItem (tr (options()[(std::size_t) i].name), i + 2);
  255. box.setSelectedId (followId, juce::dontSendNotification);
  256. }
  257. void fillGlobalBox (juce::ComboBox& box)
  258. {
  259. box.clear (juce::dontSendNotification);
  260. for (int i = 0; i < (int) options().size(); ++i)
  261. box.addItem (tr (options()[(std::size_t) i].name), i + 1);
  262. box.setSelectedId (EditorGlobals::get().getGridOption() + 1, juce::dontSendNotification);
  263. }
  264. const Option& resolve (const juce::ComboBox& editorBox)
  265. {
  266. const auto id = editorBox.getSelectedId();
  267. const auto index = id <= followId ? EditorGlobals::get().getGridOption() : id - 2;
  268. return options()[(std::size_t) juce::jlimit (0, (int) options().size() - 1, index)];
  269. }
  270. juce::String describe (const Option& option)
  271. {
  272. switch (option.kind)
  273. {
  274. case Kind::none: return tr ("free: notes and clips move by single ticks");
  275. case Kind::screen: return tr ("snaps to the grid lines drawn at this zoom (zoom in for a finer grid)");
  276. case Kind::nearest: return tr ("snaps to the nearest start or end of another note or clip (ghost notes too); elsewhere to the lines");
  277. case Kind::bar: return tr ("snaps to whole bars");
  278. case Kind::ticks: break;
  279. }
  280. return tr ("a fixed grid of {0}", { tr (option.name) });
  281. }
  282. } // namespace grid
  283. SnapBox::SnapBox (bool isEditor) : editor (isEditor)
  284. {
  285. setScrollWheelEnabled (true);
  286. }
  287. ui::Hint SnapBox::hintAt (juce::Point<int>)
  288. {
  289. ui::Hint h;
  290. h.text = getProperties().contains ("lynxHint") ? getProperties()["lynxHint"].toString() : getTooltip();
  291. h.hasMenu = true;
  292. if (getSelectedId() <= 0)
  293. return h;
  294. const auto followsMain = editor && getSelectedId() == grid::followId;
  295. const auto& option = followsMain ? grid::options()[(std::size_t) juce::jlimit (0, (int) grid::options().size() - 1,
  296. EditorGlobals::get().getGridOption())]
  297. : editor ? grid::resolve (*this)
  298. : grid::options()[(std::size_t) juce::jlimit (0, (int) grid::options().size() - 1, getSelectedId() - 1)];
  299. const auto what = followsMain ? tr ("Main: follows the toolbar snap, now {0}", { tr (option.name) }) + " - " + grid::describe (option)
  300. : tr (option.name) + ": " + grid::describe (option);
  301. h.text = h.text.isEmpty() ? what : what + " | " + h.text;
  302. h.value = tr (option.name).upToFirstOccurrenceOf (" (", false, false);
  303. return h;
  304. }
  305. EditorGlobals& EditorGlobals::get()
  306. {
  307. static EditorGlobals globals;
  308. return globals;
  309. }
  310. void EditorGlobals::setGridOption (int option)
  311. {
  312. option = juce::jlimit (0, (int) grid::options().size() - 1, option);
  313. if (option != gridOption)
  314. {
  315. if (gridOption != grid::noneOption)
  316. lastGridOption = gridOption;
  317. gridOption = option;
  318. sendChangeMessage();
  319. }
  320. }
  321. void EditorGlobals::toggleGrid()
  322. {
  323. setGridOption (gridOption == grid::noneOption ? lastGridOption : grid::noneOption);
  324. }
  325. void EditorGlobals::setDefaultNoteLength (std::int64_t ticks)
  326. {
  327. if (ticks != defaultNoteLength)
  328. {
  329. defaultNoteLength = juce::jmax<std::int64_t> (0, ticks);
  330. sendChangeMessage();
  331. }
  332. }
  333. void EditorGlobals::setFollowing (bool pianoRoll, bool on, bool save)
  334. {
  335. auto& flag = pianoRoll ? followPianoRoll : followPlaylist;
  336. if (flag != on)
  337. {
  338. flag = on;
  339. sendChangeMessage();
  340. if (save && onOptionsChanged)
  341. onOptionsChanged();
  342. }
  343. }
  344. void EditorGlobals::setFollowingView (FollowView v, bool on, bool save)
  345. {
  346. if (v == FollowView::playlist || v == FollowView::pianoRoll)
  347. return setFollowing (v == FollowView::pianoRoll, on, save);
  348. auto& flag = v == FollowView::channelRack ? followRack : followAudioEditor;
  349. if (flag != on)
  350. {
  351. flag = on;
  352. sendChangeMessage();
  353. if (save && onOptionsChanged)
  354. onOptionsChanged();
  355. }
  356. }
  357. GlobalGridBox::GlobalGridBox() : SnapBox (false)
  358. {
  359. grid::fillGlobalBox (*this);
  360. setTooltip (tr ("Global grid: used by the Piano Roll and Playlist when their grid follows it. Backspace turns it off and on; "
  361. "right-click sets the length of new notes"));
  362. setWantsKeyboardFocus (false);
  363. setMouseClickGrabsKeyboardFocus (false);
  364. onChange = [this] {
  365. if (getSelectedId() > 0)
  366. EditorGlobals::get().setGridOption (getSelectedId() - 1);
  367. };
  368. EditorGlobals::get().addChangeListener (this);
  369. }
  370. GlobalGridBox::~GlobalGridBox()
  371. {
  372. EditorGlobals::get().removeChangeListener (this);
  373. }
  374. void GlobalGridBox::mouseDown (const juce::MouseEvent& e)
  375. {
  376. if (flmods::popup (e.mods))
  377. {
  378. showDefaultLengthMenu (this);
  379. return;
  380. }
  381. juce::ComboBox::mouseDown (e);
  382. }
  383. void GlobalGridBox::showDefaultLengthMenu (juce::Component* target)
  384. {
  385. struct Length
  386. {
  387. const char* name;
  388. std::int64_t ticks;
  389. };
  390. static const Length lengths[] = {
  391. { TR_NOOP ("Last clicked note"), 0 }, { TR_NOOP ("1/4 step"), ticksPerStep / 4 }, { TR_NOOP ("1/2 step"), ticksPerStep / 2 },
  392. { TR_NOOP ("Step"), ticksPerStep }, { TR_NOOP ("1/2 beat"), ppq / 2 }, { TR_NOOP ("Beat"), ppq },
  393. { TR_NOOP ("2 beats"), 2 * ppq }, { TR_NOOP ("Bar"), 4 * ppq },
  394. };
  395. juce::PopupMenu menu;
  396. menu.addSectionHeader (tr ("Default note length"));
  397. const auto current = EditorGlobals::get().getDefaultNoteLength();
  398. for (const auto& l : lengths)
  399. menu.addItem (tr (l.name), true, current == l.ticks, [ticks = l.ticks] { EditorGlobals::get().setDefaultNoteLength (ticks); });
  400. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (target));
  401. }
  402. void GlobalGridBox::changeListenerCallback (juce::ChangeBroadcaster*)
  403. {
  404. setSelectedId (EditorGlobals::get().getGridOption() + 1, juce::dontSendNotification);
  405. }
  406. // --- overview strip ------------------------------------------------------------------------
  407. OverviewBar::OverviewBar()
  408. {
  409. setOpaque (true);
  410. ui::setHint (*this, tr ("Overview: [LMB] drag the window to scroll, drag its edges to zoom, click elsewhere to jump there "
  411. "[2xLMB] show everything"));
  412. }
  413. void OverviewBar::setRange (double newTotal, double newStart, double newLength)
  414. {
  415. newTotal = juce::jmax (1.0, newTotal);
  416. if (newTotal != total || newStart != start || newLength != length)
  417. {
  418. total = newTotal;
  419. start = newStart;
  420. length = newLength;
  421. repaint();
  422. }
  423. }
  424. juce::Rectangle<float> OverviewBar::windowArea() const
  425. {
  426. const auto w = (double) getWidth();
  427. const auto x0 = (float) (start / total * w), x1 = (float) ((start + length) / total * w);
  428. return { x0, 0.0f, juce::jmax (3.0f, x1 - x0), (float) getHeight() - 1.0f };
  429. }
  430. gesture::OverviewGrab OverviewBar::grabAt (int x) const
  431. {
  432. return gesture::overviewGrabAt (x, getWidth(), total, { start, length });
  433. }
  434. void OverviewBar::updateCursor (gesture::OverviewGrab g)
  435. {
  436. using G = gesture::OverviewGrab;
  437. setMouseCursor (g == G::left || g == G::right ? juce::MouseCursor::LeftRightResizeCursor
  438. : g == G::move ? juce::MouseCursor::DraggingHandCursor
  439. : juce::MouseCursor::PointingHandCursor);
  440. if (g != hoverGrab)
  441. {
  442. hoverGrab = g;
  443. repaint();
  444. }
  445. }
  446. void OverviewBar::paint (juce::Graphics& g)
  447. {
  448. using namespace theme::colour;
  449. const auto area = getLocalBounds().toFloat();
  450. g.fillAll (bg0);
  451. if (paintContent)
  452. {
  453. juce::Graphics::ScopedSaveState state (g);
  454. g.reduceClipRegion (getLocalBounds().withTrimmedBottom (1));
  455. paintContent (g, area.withTrimmedBottom (1.0f), total);
  456. }
  457. // outside the window: dimmed; the window: a light frame, its edges lit while hovered or dragged
  458. const auto win = windowArea();
  459. g.setColour (bg0.withAlpha (0.5f));
  460. g.fillRect (area.withRight (win.getX()));
  461. g.fillRect (area.withLeft (win.getRight()));
  462. g.setColour (textPrimary.withAlpha (hoverGrab == gesture::OverviewGrab::move || dragging ? 0.08f : 0.04f));
  463. g.fillRect (win);
  464. g.setColour (textSecondary.withAlpha (0.55f));
  465. g.drawRect (win, 1.0f);
  466. const auto lit = dragging ? grab : hoverGrab;
  467. g.setColour (accent);
  468. if (lit == gesture::OverviewGrab::left)
  469. g.fillRect (win.withWidth (2.0f));
  470. if (lit == gesture::OverviewGrab::right)
  471. g.fillRect (win.withLeft (win.getRight() - 2.0f));
  472. g.setColour (border);
  473. g.fillRect (area.withTop (area.getBottom() - 1.0f));
  474. }
  475. void OverviewBar::mouseMove (const juce::MouseEvent& e)
  476. {
  477. updateCursor (grabAt (e.x));
  478. }
  479. void OverviewBar::mouseExit (const juce::MouseEvent&)
  480. {
  481. if (! dragging && hoverGrab != gesture::OverviewGrab::none)
  482. {
  483. hoverGrab = gesture::OverviewGrab::none;
  484. repaint();
  485. }
  486. }
  487. void OverviewBar::mouseDown (const juce::MouseEvent& e)
  488. {
  489. if (! e.mods.isLeftButtonDown())
  490. return;
  491. grab = grabAt (e.x);
  492. grabFrom = { start, length };
  493. dragging = true;
  494. if (grab == gesture::OverviewGrab::none) // jump: centre the window on the click, then drag it
  495. {
  496. grabFrom = gesture::overviewCentredOn (e.x / juce::jmax (1.0, (double) getWidth()) * total, grabFrom, total);
  497. if (onChange)
  498. onChange (grabFrom.start, grabFrom.length);
  499. }
  500. repaint();
  501. }
  502. void OverviewBar::mouseDrag (const juce::MouseEvent& e)
  503. {
  504. if (! dragging)
  505. return;
  506. const auto w = gesture::overviewDrag (grab, grabFrom, e.getDistanceFromDragStartX(), getWidth(), total, minLength);
  507. if (onChange)
  508. onChange (w.start, w.length);
  509. }
  510. void OverviewBar::mouseUp (const juce::MouseEvent& e)
  511. {
  512. dragging = false;
  513. grab = gesture::OverviewGrab::none;
  514. updateCursor (grabAt (e.x));
  515. repaint();
  516. }
  517. void OverviewBar::mouseDoubleClick (const juce::MouseEvent&)
  518. {
  519. if (onShowAll)
  520. onShowAll();
  521. }
  522. // --- Note inspector ------------------------------------------------------------------------
  523. namespace
  524. {
  525. class NoteInspector final : public juce::Component
  526. {
  527. public:
  528. NoteInspector (AudioEngine& e, std::vector<int> i, int f)
  529. : engine (e), indices (std::move (i)), focus (f)
  530. {
  531. const auto& song = engine.getSong();
  532. patternIndex = song.currentPattern;
  533. const auto& notes = song.patterns[(std::size_t) patternIndex].notes;
  534. for (auto index : indices)
  535. originals.push_back (notes[(std::size_t) index]);
  536. if (std::find (indices.begin(), indices.end(), focus) == indices.end())
  537. focus = indices.front();
  538. shown = notes[(std::size_t) focus];
  539. title.setText (indices.size() > 1 ? tr ("Note inspector: {0} notes", { juce::String ((int) indices.size()) }) : tr ("Note inspector"),
  540. juce::dontSendNotification);
  541. title.setFont (theme::font (theme::type::title, true));
  542. title.setColour (juce::Label::textColourId, theme::colour::textPrimary);
  543. addAndMakeVisible (title);
  544. addRow (velocity, TR_NOOP ("Velocity"), 0.0, 100.0, "%");
  545. addRow (pan, TR_NOOP ("Pan"), -100.0, 100.0, "");
  546. addRow (release, TR_NOOP ("Release"), 0.0, 100.0, "%");
  547. addRow (finePitch, TR_NOOP ("Fine pitch"), -120.0, 120.0, " ct");
  548. addRow (modX, TR_NOOP ("Mod X"), 0.0, 100.0, "%");
  549. addRow (modY, TR_NOOP ("Mod Y"), 0.0, 100.0, "%");
  550. release.slider.setTooltip (tr ("Release velocity (kept with the note; instruments do not use it yet)"));
  551. modX.slider.setTooltip (tr ("Free modulation value (kept with the note; not routed yet)"));
  552. modY.slider.setTooltip (modX.slider.getTooltip());
  553. for (auto* t : { &slide, &porta })
  554. {
  555. t->setColour (juce::ToggleButton::tickColourId, theme::colour::accent);
  556. t->onClick = [this, t] { (t == &slide ? slideTouched : portaTouched) = true; };
  557. addAndMakeVisible (*t);
  558. }
  559. slide.setButtonText (tr ("Slide"));
  560. porta.setButtonText (tr ("Porta"));
  561. single = indices.size() == 1;
  562. for (auto* row : { &time, &length })
  563. {
  564. row->label.setColour (juce::Label::textColourId, theme::colour::textSecondary);
  565. row->label.setFont (theme::font (theme::type::small));
  566. row->editor.setFont (theme::monoFont (theme::type::body));
  567. row->editor.setJustification (juce::Justification::centredLeft);
  568. row->editor.onTextChange = [row] { row->touched = true; };
  569. addChildComponent (row->label);
  570. addChildComponent (row->editor);
  571. row->label.setVisible (single);
  572. row->editor.setVisible (single);
  573. }
  574. time.label.setText (tr ("Time (bar:step:tick)"), juce::dontSendNotification);
  575. length.label.setText (tr ("Length (bars:steps:ticks)"), juce::dontSendNotification);
  576. applyButton.setButtonText (tr ("Apply"));
  577. revertButton.setButtonText (tr ("Revert"));
  578. applyButton.setColour (juce::TextButton::buttonColourId, theme::colour::accent);
  579. applyButton.onClick = [this] { apply(); };
  580. revertButton.onClick = [this] { load(); };
  581. addAndMakeVisible (applyButton);
  582. addAndMakeVisible (revertButton);
  583. load();
  584. setSize (320, 34 + 6 * rowHeight + rowHeight + (single ? 2 * rowHeight : 0) + 44);
  585. }
  586. void paint (juce::Graphics& g) override
  587. {
  588. g.fillAll (theme::colour::bg3);
  589. }
  590. void resized() override
  591. {
  592. auto area = getLocalBounds().reduced (12, 8);
  593. title.setBounds (area.removeFromTop (26));
  594. for (auto* row : { &velocity, &pan, &release, &finePitch, &modX, &modY })
  595. {
  596. auto r = area.removeFromTop (rowHeight);
  597. row->label.setBounds (r.removeFromLeft (80));
  598. row->slider.setBounds (r);
  599. }
  600. auto toggles = area.removeFromTop (rowHeight);
  601. slide.setBounds (toggles.removeFromLeft (toggles.getWidth() / 2));
  602. porta.setBounds (toggles);
  603. if (single)
  604. for (auto* row : { &time, &length })
  605. {
  606. auto r = area.removeFromTop (rowHeight);
  607. row->label.setBounds (r.removeFromLeft (160));
  608. row->editor.setBounds (r.reduced (0, 2));
  609. }
  610. auto buttons = area.removeFromBottom (30);
  611. applyButton.setBounds (buttons.removeFromRight (90));
  612. buttons.removeFromRight (8);
  613. revertButton.setBounds (buttons.removeFromRight (90));
  614. }
  615. private:
  616. static constexpr int rowHeight = 26;
  617. struct SliderRow
  618. {
  619. juce::Label label;
  620. juce::Slider slider { juce::Slider::LinearHorizontal, juce::Slider::TextBoxRight };
  621. bool touched = false;
  622. };
  623. struct TextRow
  624. {
  625. juce::Label label;
  626. juce::TextEditor editor;
  627. bool touched = false;
  628. };
  629. void addRow (SliderRow& row, const char* name, double min, double max, const juce::String& suffix)
  630. {
  631. row.label.setText (tr (name), juce::dontSendNotification);
  632. row.label.setFont (theme::font (theme::type::small));
  633. row.label.setColour (juce::Label::textColourId, theme::colour::textSecondary);
  634. row.slider.setRange (min, max, 1.0);
  635. row.slider.setTextValueSuffix (suffix);
  636. row.slider.setTextBoxStyle (juce::Slider::TextBoxRight, false, 64, 20);
  637. row.slider.setColour (juce::Slider::trackColourId, theme::colour::accent);
  638. row.slider.onValueChange = [&row] { row.touched = true; };
  639. addAndMakeVisible (row.label);
  640. addAndMakeVisible (row.slider);
  641. }
  642. void load()
  643. {
  644. const auto set = [] (SliderRow& row, double v) {
  645. row.slider.setValue (v, juce::dontSendNotification);
  646. row.touched = false;
  647. };
  648. set (velocity, std::round (shown.velocity * 100.0f));
  649. set (pan, std::round (shown.pan * 100.0f));
  650. set (release, std::round (shown.release * 100.0f));
  651. set (finePitch, std::round (shown.finePitch));
  652. set (modX, std::round (shown.modX * 100.0f));
  653. set (modY, std::round (shown.modY * 100.0f));
  654. slide.setToggleState (shown.slide, juce::dontSendNotification);
  655. porta.setToggleState (shown.porta, juce::dontSendNotification);
  656. slideTouched = portaTouched = false;
  657. const auto bpb = engine.getSong().beatsPerBar;
  658. time.editor.setText (noteedit::formatTime (shown.start, bpb), juce::dontSendNotification);
  659. length.editor.setText (noteedit::formatTime (shown.length, bpb, true), juce::dontSendNotification);
  660. time.touched = length.touched = false;
  661. time.editor.setColour (juce::TextEditor::outlineColourId, theme::colour::borderStrong);
  662. length.editor.setColour (juce::TextEditor::outlineColourId, theme::colour::borderStrong);
  663. }
  664. void apply()
  665. {
  666. const auto bpb = engine.getSong().beatsPerBar;
  667. std::int64_t start = shown.start, len = shown.length;
  668. auto ok = true;
  669. if (single && time.touched && ! noteedit::parseTime (time.editor.getText().toStdString(), bpb, start))
  670. {
  671. time.editor.setColour (juce::TextEditor::outlineColourId, theme::colour::warning);
  672. ok = false;
  673. }
  674. if (single && length.touched && (! noteedit::parseTime (length.editor.getText().toStdString(), bpb, len, true) || len <= 0))
  675. {
  676. length.editor.setColour (juce::TextEditor::outlineColourId, theme::colour::warning);
  677. ok = false;
  678. }
  679. if (! ok)
  680. {
  681. repaint();
  682. return;
  683. }
  684. // only what was changed is written, so notes with different values keep theirs
  685. engine.editSong ([&] (Song& s) {
  686. if (patternIndex >= (int) s.patterns.size())
  687. return;
  688. auto& notes = s.patterns[(std::size_t) patternIndex].notes;
  689. for (std::size_t k = 0; k < indices.size(); ++k)
  690. {
  691. const auto i = indices[k];
  692. if (i >= (int) notes.size() || notes[(std::size_t) i].start != originals[k].start || notes[(std::size_t) i].key != originals[k].key)
  693. continue; // the pattern changed under the inspector
  694. auto& n = notes[(std::size_t) i];
  695. if (velocity.touched) n.velocity = (float) velocity.slider.getValue() / 100.0f;
  696. if (pan.touched) n.pan = (float) pan.slider.getValue() / 100.0f;
  697. if (release.touched) n.release = (float) release.slider.getValue() / 100.0f;
  698. if (finePitch.touched) n.finePitch = (float) finePitch.slider.getValue();
  699. if (modX.touched) n.modX = (float) modX.slider.getValue() / 100.0f;
  700. if (modY.touched) n.modY = (float) modY.slider.getValue() / 100.0f;
  701. if (slideTouched) n.slide = slide.getToggleState();
  702. if (portaTouched) n.porta = porta.getToggleState();
  703. if (single)
  704. {
  705. n.start = start;
  706. n.length = len;
  707. }
  708. }
  709. if (single)
  710. s.patterns[(std::size_t) patternIndex].sortNotes();
  711. });
  712. if (auto* box = findParentComponentOfClass<juce::CallOutBox>())
  713. box->dismiss();
  714. }
  715. AudioEngine& engine;
  716. std::vector<int> indices;
  717. std::vector<Note> originals;
  718. int focus = 0, patternIndex = 0;
  719. Note shown;
  720. bool single = false, slideTouched = false, portaTouched = false;
  721. juce::Label title;
  722. SliderRow velocity, pan, release, finePitch, modX, modY;
  723. juce::ToggleButton slide, porta;
  724. TextRow time, length;
  725. juce::TextButton applyButton, revertButton;
  726. };
  727. } // namespace
  728. std::unique_ptr<juce::Component> makeNoteInspector (AudioEngine& engine, std::vector<int> indices, int focus)
  729. {
  730. const auto& song = engine.getSong();
  731. const auto count = (int) song.patterns[(std::size_t) song.currentPattern].notes.size();
  732. indices.erase (std::remove_if (indices.begin(), indices.end(), [count] (int i) { return i < 0 || i >= count; }), indices.end());
  733. if (indices.empty())
  734. return nullptr;
  735. return std::make_unique<NoteInspector> (engine, std::move (indices), focus);
  736. }
  737. void showNoteInspector (AudioEngine& engine, std::vector<int> indices, int focus, juce::Rectangle<int> screenArea)
  738. {
  739. if (auto content = makeNoteInspector (engine, std::move (indices), focus))
  740. juce::CallOutBox::launchAsynchronously (std::move (content), screenArea, nullptr);
  741. }
  742. } // namespace lynx