QuickPick.cpp 10 KB

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