QuickPick.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "ui/QuickPick.h"
  2. #include <algorithm>
  3. #include "core/ControlSurface.h"
  4. #include "core/NativeEngines.h"
  5. #include "core/Preferences.h"
  6. #include "core/VideoPlayer.h"
  7. #include "core/VoltageController.h"
  8. #include "core/Vfx.h"
  9. #include "engine/PluginHost.h"
  10. #include "ui/ChannelRackComponent.h"
  11. #include "ui/I18n.h"
  12. #include "ui/MixerComponent.h"
  13. namespace lynx::quickpick
  14. {
  15. std::vector<GeneratorEntry> generatorEntries (AudioEngine& engine)
  16. {
  17. std::vector<GeneratorEntry> out;
  18. auto add = [&out] (std::string english, juce::String label, juce::String detail, int folder, std::function<std::optional<ChannelDef>()> make) {
  19. out.push_back ({ "lynx:gen:" + english, std::move (label), std::move (detail), folder, std::move (make) });
  20. };
  21. const std::pair<InstrumentKind, const char*> builtIn[] = { { InstrumentKind::synth, TR_NOOP ("Synth") }, { InstrumentKind::kick, TR_NOOP ("Kick") },
  22. { InstrumentKind::snare, TR_NOOP ("Snare") }, { InstrumentKind::hat, TR_NOOP ("Hat") },
  23. { InstrumentKind::clap, TR_NOOP ("Clap") } };
  24. for (const auto& [kind, name] : builtIn)
  25. add (name, tr (name), tr ("built-in"), 0, [kind = kind, english = std::string (name)] {
  26. ChannelDef c;
  27. c.name = english;
  28. c.instrument = kind;
  29. return std::optional<ChannelDef> (c);
  30. });
  31. // FL's Layer and Dashboard (round 164)
  32. add ("Layer", tr ("Layer"), tr ("built-in"), 0, [] {
  33. ChannelDef c;
  34. c.name = "Layer";
  35. c.instrument = InstrumentKind::layer;
  36. return std::optional<ChannelDef> (c);
  37. });
  38. add ("Dashboard", "Dashboard", tr ("built-in"), 0, [] {
  39. ChannelDef c;
  40. c.name = "Dashboard";
  41. c.instrument = InstrumentKind::dashboard;
  42. c.patcher.surface = surface::starterControls();
  43. return std::optional<ChannelDef> (c);
  44. });
  45. add ("Voltage Controller", "Voltage Controller", tr ("built-in"), 0, [] {
  46. ChannelDef c;
  47. c.name = "Voltage Controller";
  48. c.instrument = InstrumentKind::voltage;
  49. c.pluginState = voltage::write (voltage::Settings {});
  50. return std::optional<ChannelDef> (c);
  51. });
  52. add ("Fruity Video Player", "Fruity Video Player", tr ("built-in"), 0, [] {
  53. ChannelDef c;
  54. c.name = "Fruity Video Player";
  55. c.instrument = InstrumentKind::videoplayer;
  56. c.pluginState = video::write (video::Settings {});
  57. return std::optional<ChannelDef> (c);
  58. });
  59. const auto& fl = flStudioAccess();
  60. for (const auto& e : natives::instrumentEngines())
  61. if (natives::available (e, fl))
  62. add (e.name, flEngineName (e), tr ("built-in FL engine"), 1, [kind = e.kind] { return natives::makeChannel (kind, flStudioAccess()); });
  63. // FL's VFX note processors (round 164b): channels with no sound, they play other channels / Patcher nodes
  64. for (const auto kind : vfx::allKinds)
  65. add (vfx::flName (kind), vfx::flName (kind), tr ("built-in"), 2, [kind] {
  66. ChannelDef c;
  67. vfx::makeChannel (c, kind);
  68. return std::optional<ChannelDef> (c);
  69. });
  70. auto types = engine.getPluginHost().getKnownPlugins().getTypes();
  71. std::sort (types.begin(), types.end(), [] (const auto& a, const auto& b) { return a.name.compareIgnoreCase (b.name) < 0; });
  72. for (const auto& t : types)
  73. if (t.isInstrument)
  74. {
  75. const auto id = t.createIdentifierString().toStdString();
  76. out.push_back ({ id, t.name, t.manufacturerName + " · " + t.pluginFormatName, 3, [id, name = t.name.toStdString()] {
  77. ChannelDef c;
  78. c.name = name;
  79. c.instrument = InstrumentKind::plugin;
  80. c.plugin.id = id;
  81. c.plugin.name = name;
  82. return std::optional<ChannelDef> (c);
  83. } });
  84. }
  85. return out;
  86. }
  87. std::vector<EffectEntry> effectEntries (AudioEngine& engine)
  88. {
  89. std::vector<EffectEntry> out;
  90. const auto bpm = engine.getSong().bpm;
  91. for (const auto kind : addableEffects())
  92. if (addableEffectAvailable (kind))
  93. {
  94. const auto flEngine = natives::effectEngine (kind) != nullptr;
  95. out.push_back ({ std::string ("lynx:fx:") + effectInfo (kind).name, addableEffectName (kind),
  96. flEngine ? tr ("built-in FL engine") : tr ("built-in"), flEngine ? 1 : 0,
  97. [kind, bpm] { return std::optional<EffectSlot> (newAddableEffect (kind, bpm)); } });
  98. }
  99. auto types = engine.getPluginHost().getKnownPlugins().getTypes();
  100. std::sort (types.begin(), types.end(), [] (const auto& a, const auto& b) { return a.name.compareIgnoreCase (b.name) < 0; });
  101. for (const auto& t : types)
  102. if (! t.isInstrument)
  103. {
  104. const auto id = t.createIdentifierString().toStdString();
  105. out.push_back ({ id, t.name, t.manufacturerName + " · " + t.pluginFormatName, 3, [id, name = t.name.toStdString()] {
  106. EffectSlot fx;
  107. fx.kind = EffectKind::plugin;
  108. fx.plugin.id = id;
  109. fx.plugin.name = name;
  110. return std::optional<EffectSlot> (fx);
  111. } });
  112. }
  113. return out;
  114. }
  115. juce::String shownName (AudioEngine& engine, const std::string& key, const juce::String& original)
  116. {
  117. const auto& names = engine.getPluginHost().pluginNames();
  118. if (const auto it = names.find (key); it != names.end())
  119. return juce::String::fromUTF8 (it->second.c_str());
  120. return original;
  121. }
  122. void rename (AudioEngine& engine, const std::string& key, const juce::String& original, std::function<void()> done)
  123. {
  124. auto* window = new juce::AlertWindow (tr ("Rename plugin"),
  125. tr ("A name for \"{0}\" in the Plugin database and the menus. Start it with a digit or letter of its own "
  126. "(\"1_EQ\", \"2_Limiter\"): on an empty mixer slot (or in the Channel Rack's add menu) typing it adds "
  127. "the plugin. Empty: the original name.",
  128. { original }),
  129. juce::MessageBoxIconType::NoIcon);
  130. window->addTextEditor ("name", shownName (engine, key, original) == original ? juce::String() : shownName (engine, key, original));
  131. window->addButton (tr ("OK"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  132. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  133. window->enterModalState (true, juce::ModalCallbackFunction::create ([window, &engine, key, done] (int result) {
  134. if (result == 1)
  135. {
  136. engine.getPluginHost().setPluginName (key, window->getTextEditorContents ("name").toStdString());
  137. if (done)
  138. done();
  139. }
  140. }),
  141. true);
  142. }
  143. namespace
  144. {
  145. template <typename Entry, typename Thing>
  146. std::vector<Pick> renamed (AudioEngine& engine, const std::vector<Entry>& entries, std::function<void (Thing)> add)
  147. {
  148. std::vector<Pick> out;
  149. const auto& names = engine.getPluginHost().pluginNames();
  150. for (const auto& e : entries)
  151. if (const auto it = names.find (e.key); it != names.end())
  152. out.push_back ({ juce::String::fromUTF8 (it->second.c_str()), [make = e.make, add] {
  153. if (auto thing = make())
  154. add (std::move (*thing));
  155. } });
  156. std::sort (out.begin(), out.end(), [] (const Pick& a, const Pick& b) { return a.label.compareNatural (b.label) < 0; });
  157. return out;
  158. }
  159. // Listens to the keys of an open menu (its window is the modal component while it shows).
  160. struct MenuKeys final : public juce::KeyListener
  161. {
  162. std::vector<Pick> picks;
  163. std::function<bool (const juce::KeyPress&)> onKey;
  164. bool keyPressed (const juce::KeyPress& key, juce::Component*) override
  165. {
  166. if (onKey && onKey (key))
  167. {
  168. juce::PopupMenu::dismissAllActiveMenus();
  169. return true;
  170. }
  171. const auto typed = key.getTextCharacter();
  172. if (typed <= ' ' || typed > 0x7e || key.getModifiers().isCtrlDown() || key.getModifiers().isAltDown() || key.getModifiers().isCommandDown())
  173. return false;
  174. std::vector<std::string> labels;
  175. for (const auto& p : picks)
  176. labels.push_back (p.label.toStdString());
  177. const auto index = prefs::uniqueTypeAhead (labels, (char) typed);
  178. if (index < 0)
  179. return false;
  180. auto action = picks[(std::size_t) index].action;
  181. juce::PopupMenu::dismissAllActiveMenus();
  182. juce::MessageManager::callAsync ([action] {
  183. if (action)
  184. action();
  185. });
  186. return true;
  187. }
  188. };
  189. } // namespace
  190. std::vector<Pick> renamedEffects (AudioEngine& engine, std::function<void (EffectSlot)> add)
  191. {
  192. return renamed<EffectEntry, EffectSlot> (engine, effectEntries (engine), std::move (add));
  193. }
  194. std::vector<Pick> renamedGenerators (AudioEngine& engine, std::function<void (ChannelDef)> add)
  195. {
  196. return renamed<GeneratorEntry, ChannelDef> (engine, generatorEntries (engine), std::move (add));
  197. }
  198. void addPicks (juce::PopupMenu& menu, const std::vector<Pick>& picks)
  199. {
  200. if (picks.empty())
  201. return;
  202. menu.addSectionHeader (tr ("Renamed plugins (type the first character)"));
  203. for (const auto& p : picks)
  204. menu.addItem (p.label, p.action);
  205. menu.addSeparator();
  206. }
  207. void showMenu (juce::PopupMenu& menu, const juce::PopupMenu::Options& options, std::vector<Pick> picks,
  208. std::function<bool (const juce::KeyPress&)> onKey)
  209. {
  210. auto keys = std::make_shared<MenuKeys>();
  211. keys->picks = std::move (picks);
  212. keys->onKey = std::move (onKey);
  213. auto holder = std::make_shared<juce::Component::SafePointer<juce::Component>>();
  214. menu.showMenuAsync (options, [keys, holder] (int) {
  215. if (auto* w = holder->getComponent())
  216. w->removeKeyListener (keys.get());
  217. });
  218. // the menu's window is the modal component now (JUCE enters its modal state while showing it)
  219. if (auto* modal = juce::Component::getCurrentlyModalComponent())
  220. {
  221. *holder = modal;
  222. modal->addKeyListener (keys.get());
  223. }
  224. }
  225. } // namespace lynx::quickpick