PluginHeader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. #include "ui/PluginHeader.h"
  2. #include <map>
  3. #include "core/EffectPresets.h"
  4. #include "core/Effects.h"
  5. #include "core/FlEffects.h"
  6. #include "core/ParamHelp.h"
  7. #include "engine/AudioEngine.h"
  8. #include "engine/Text.h"
  9. #include "platform/Paths.h"
  10. #include "ui/I18n.h"
  11. #include "ui/PluginSkin.h"
  12. #include "ui/Theme.h"
  13. #include "ui/ValueControl.h"
  14. namespace lynx::pluginheader
  15. {
  16. namespace
  17. {
  18. struct Snapshot
  19. {
  20. EffectParams params {};
  21. std::vector<std::uint8_t> state;
  22. std::string presetName;
  23. bool operator== (const Snapshot& o) const { return params == o.params && state == o.state && presetName == o.presetName; }
  24. };
  25. struct SlotHistory
  26. {
  27. EffectKind kind = EffectKind::eq;
  28. std::vector<Snapshot> undo, redo;
  29. };
  30. constexpr std::size_t maxSteps = 100;
  31. std::map<std::pair<int, int>, SlotHistory>& histories()
  32. {
  33. static std::map<std::pair<int, int>, SlotHistory> h;
  34. return h;
  35. }
  36. SlotHistory& historyOf (int track, int slot, EffectKind kind)
  37. {
  38. auto& h = histories()[{ track, slot }];
  39. if (h.kind != kind) // another effect now sits in the slot
  40. {
  41. h = {};
  42. h.kind = kind;
  43. }
  44. return h;
  45. }
  46. Snapshot snapshotOf (const EffectSlot& fx) { return { fx.params, fx.state, fx.presetName }; }
  47. void drawArrow (juce::Graphics& g, juce::Rectangle<float> r, bool left, juce::Colour c)
  48. {
  49. juce::Path p;
  50. p.addTriangle (-3.0f, -5.0f, -3.0f, 5.0f, 4.0f, 0.0f);
  51. p.applyTransform (juce::AffineTransform::rotation (left ? juce::MathConstants<float>::pi : 0.0f).translated (r.getCentreX(), r.getCentreY()));
  52. g.setColour (c);
  53. g.fillPath (p);
  54. }
  55. // a curved arrow (undo: pointing left, redo: right)
  56. void drawTurn (juce::Graphics& g, juce::Rectangle<float> r, bool redo, juce::Colour c)
  57. {
  58. const auto cx = r.getCentreX(), cy = r.getCentreY() + 1.5f;
  59. juce::Path arc;
  60. arc.addCentredArc (cx, cy, 5.0f, 5.0f, 0.0f, -juce::MathConstants<float>::halfPi, juce::MathConstants<float>::halfPi, true);
  61. if (redo)
  62. arc.applyTransform (juce::AffineTransform::scale (-1.0f, 1.0f, cx, cy));
  63. g.setColour (c);
  64. g.strokePath (arc, juce::PathStrokeType (1.6f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  65. // the arrow head at the arc's upper end
  66. const auto tip = juce::Point<float> (redo ? cx + 0.5f : cx - 0.5f, cy - 5.0f);
  67. juce::Path head;
  68. const auto d = redo ? 1.0f : -1.0f;
  69. head.addTriangle (tip.x + d * 3.5f, tip.y, tip.x - d * 1.0f, tip.y - 3.5f, tip.x - d * 1.0f, tip.y + 3.5f);
  70. g.fillPath (head);
  71. }
  72. bool isLynxEngine (EffectKind kind)
  73. {
  74. return flPluginsForEffect (kind).empty();
  75. }
  76. // "Keep the Mix when loading presets" (the reference reverbs' mix lock): per engine, a marker file in its presets folder
  77. juce::File mixLockFile (EffectKind kind) { return userFolder (kind).getChildFile (".mixlock"); }
  78. bool mixLocked (EffectKind kind) { return mixLockFile (kind).existsAsFile(); }
  79. void setMixLocked (EffectKind kind, bool on)
  80. {
  81. if (on)
  82. {
  83. userFolder (kind).createDirectory();
  84. mixLockFile (kind).create();
  85. }
  86. else
  87. mixLockFile (kind).deleteFile();
  88. }
  89. // the preset's parameters with the slot's Mix kept when the mix lock is on
  90. EffectParams withMixLock (EffectKind kind, EffectParams params, const EffectParams& current)
  91. {
  92. if (mixLocked (kind))
  93. if (const auto p = presets::paramByName (kind, "Mix"); p >= 0)
  94. params[(std::size_t) p] = current[(std::size_t) p];
  95. return params;
  96. }
  97. } // namespace
  98. juce::File userFolder (EffectKind kind)
  99. {
  100. return platform::settingsFile ("Presets").getChildFile (juce::File::createLegalFileName (utf8 (effectInfo (kind).name)));
  101. }
  102. juce::Array<juce::File> userPresets (EffectKind kind)
  103. {
  104. auto files = userFolder (kind).findChildFiles (juce::File::findFiles, false, "*.lynxpreset");
  105. std::sort (files.begin(), files.end(), [] (const juce::File& a, const juce::File& b) {
  106. return a.getFileNameWithoutExtension().compareNatural (b.getFileNameWithoutExtension()) < 0;
  107. });
  108. return files;
  109. }
  110. void clearHistory() { histories().clear(); }
  111. juce::String describe (EffectKind kind, const char* param)
  112. {
  113. // round 135: the texts moved to core/ParamHelp (tested; every LYNX engine has its own wording)
  114. const auto* text = paramhelp::describe (kind, param);
  115. return text != nullptr ? tr (text) : juce::String();
  116. }
  117. juce::String modeLine (EffectKind kind, const EffectParams& params, double bpm)
  118. {
  119. const auto s = paramhelp::summary (kind, params);
  120. if (s.param < 0)
  121. return {};
  122. const auto& info = effectInfo (kind);
  123. auto line = tr (info.params[(std::size_t) s.param].name) + ": " + tr (juce::String::fromUTF8 (formatEffectParam (kind, s.param, params[(std::size_t) s.param], bpm, params).c_str()));
  124. if (s.text != nullptr)
  125. line << juce::String::fromUTF8 (" \xe2\x80\x94 ") << tr (s.text);
  126. if (s.detail != nullptr)
  127. line << ", " << tr (s.detail);
  128. return line;
  129. }
  130. bool isMixLocked (EffectKind kind) { return mixLocked (kind); }
  131. void toggleMixLock (EffectKind kind) { setMixLocked (kind, ! mixLocked (kind)); }
  132. PresetBar::PresetBar (AudioEngine& e, int t, int s) : engine (e), track (t), slot (s)
  133. {
  134. const auto& mixer = engine.getSong().mixer;
  135. if (track >= 0 && track < (int) mixer.size() && slot >= 0 && slot < (int) mixer[(std::size_t) track].effects.size())
  136. kind = mixer[(std::size_t) track].effects[(std::size_t) slot].kind; // (current() checks the kind: read it directly)
  137. }
  138. const EffectSlot* PresetBar::current() const
  139. {
  140. const auto& mixer = engine.getSong().mixer;
  141. if (track < 0 || track >= (int) mixer.size() || slot < 0 || slot >= (int) mixer[(std::size_t) track].effects.size())
  142. return nullptr;
  143. const auto& fx = mixer[(std::size_t) track].effects[(std::size_t) slot];
  144. return fx.kind == kind ? &fx : nullptr;
  145. }
  146. bool PresetBar::hasLynxPresets() const
  147. {
  148. return ! presets::factory (kind).empty() || isLynxEngine (kind);
  149. }
  150. juce::Rectangle<float> PresetBar::place (juce::Rectangle<float> area, bool withPresets)
  151. {
  152. presetsShown = withPresets;
  153. const auto h = juce::jmin (20.0f, area.getHeight() - 4.0f);
  154. const auto y = area.getCentreY() - h * 0.5f;
  155. auto x = area.getRight() - 4.0f;
  156. auto take = [&] (float w) {
  157. x -= w;
  158. return juce::Rectangle<float> (x, y, w, h);
  159. };
  160. if (withPresets)
  161. {
  162. rNext = take (18.0f);
  163. rPrev = take (18.0f);
  164. x -= 2.0f;
  165. rPreset = take (juce::jlimit (70.0f, 180.0f, area.getWidth() * 0.26f));
  166. x -= 8.0f;
  167. }
  168. else
  169. rNext = rPrev = rPreset = {};
  170. if (area.getWidth() >= 420.0f) // narrow windows (Quell) keep the title readable: A / B without the copy button
  171. {
  172. rCopy = take (34.0f);
  173. x -= 3.0f;
  174. }
  175. else
  176. rCopy = {};
  177. rB = take (18.0f);
  178. rA = take (18.0f);
  179. x -= 8.0f;
  180. rRedo = take (20.0f);
  181. rUndo = take (20.0f);
  182. x -= 6.0f;
  183. return area.withRight (juce::jmax (area.getX(), x));
  184. }
  185. void PresetBar::paint (juce::Graphics& g, juce::Colour accent) const
  186. {
  187. using namespace theme;
  188. const auto* fx = current();
  189. if (fx == nullptr)
  190. return;
  191. const auto& h = histories();
  192. const auto it = h.find ({ track, slot });
  193. const auto canUndo = it != h.end() && it->second.kind == kind && ! it->second.undo.empty();
  194. const auto canRedo = it != h.end() && it->second.kind == kind && ! it->second.redo.empty();
  195. auto tone = [&] (Part p, bool enabled) {
  196. return ! enabled ? colour::textDisabled : hovered == p ? accent : colour::textSecondary;
  197. };
  198. drawTurn (g, rUndo, false, tone (Part::undo, canUndo));
  199. drawTurn (g, rRedo, true, tone (Part::redo, canRedo));
  200. // A / B: the live setting lit; the copy button names the direction
  201. const auto showingB = fx->abShowingB;
  202. for (const auto side : { 0, 1 })
  203. {
  204. const auto r = side == 0 ? rA : rB;
  205. const auto lit = (side == 1) == showingB;
  206. skin::button (g, r, lit, hovered == (side == 0 ? Part::a : Part::b), accent, radius::sm);
  207. g.setColour (lit ? colour::textPrimary : colour::textSecondary);
  208. g.setFont (font (type::small, true));
  209. g.drawText (side == 0 ? "A" : "B", r, juce::Justification::centred, false);
  210. }
  211. if (! rCopy.isEmpty())
  212. {
  213. skin::button (g, rCopy, false, hovered == Part::copy, accent, radius::sm);
  214. g.setColour (hovered == Part::copy ? colour::textPrimary : colour::textSecondary);
  215. g.setFont (font (type::caption, true));
  216. g.drawText (showingB ? juce::String::fromUTF8 ("B\xe2\x86\x92" "A") : juce::String::fromUTF8 ("A\xe2\x86\x92" "B"), rCopy,
  217. juce::Justification::centred, false);
  218. }
  219. if (! presetsShown)
  220. return;
  221. skin::valuePlate (g, rPreset, radius::md);
  222. if (hovered == Part::preset)
  223. {
  224. g.setColour (accent.withAlpha (0.5f));
  225. g.drawRoundedRectangle (rPreset.reduced (0.5f), radius::md, 1.0f);
  226. }
  227. const auto label = presetLabel();
  228. const auto named = fx->presetName.size() > 0;
  229. g.setColour (named ? colour::textPrimary : colour::textSecondary);
  230. ui::drawFittingText (g, label, rPreset.reduced (6.0f, 0.0f), juce::Justification::centred, font (type::small, named), 7.5f, 0.0f, 0.7f); // (round 165)
  231. drawArrow (g, rPrev, true, hovered == Part::prev ? accent : colour::textSecondary);
  232. drawArrow (g, rNext, false, hovered == Part::next ? accent : colour::textSecondary);
  233. }
  234. juce::String PresetBar::presetLabel (const FlPresets* flPresets) const
  235. {
  236. if (flPresets != nullptr && ! hasLynxPresets() && flPresets->name)
  237. {
  238. const auto n = flPresets->name();
  239. return n.isNotEmpty() ? n : tr ("(no preset)");
  240. }
  241. const auto* fx = current();
  242. if (fx != nullptr && ! fx->presetName.empty())
  243. return utf8 (fx->presetName.c_str());
  244. return tr ("(no preset)");
  245. }
  246. Part PresetBar::partAt (juce::Point<float> p) const
  247. {
  248. if (rUndo.contains (p)) return Part::undo;
  249. if (rRedo.contains (p)) return Part::redo;
  250. if (rA.contains (p)) return Part::a;
  251. if (rB.contains (p)) return Part::b;
  252. if (rCopy.contains (p)) return Part::copy;
  253. if (presetsShown)
  254. {
  255. if (rPreset.contains (p)) return Part::preset;
  256. if (rPrev.contains (p)) return Part::prev;
  257. if (rNext.contains (p)) return Part::next;
  258. }
  259. return Part::none;
  260. }
  261. bool PresetBar::hover (juce::Point<float> p)
  262. {
  263. const auto part = partAt (p);
  264. if (part == hovered)
  265. return false;
  266. hovered = part;
  267. return true;
  268. }
  269. juce::String PresetBar::hintFor (Part part) const
  270. {
  271. switch (part)
  272. {
  273. case Part::undo: return tr ("Undo this plugin's last change");
  274. case Part::redo: return tr ("Redo this plugin's change");
  275. case Part::a: return tr ("Setting A: compare two settings (A / B)");
  276. case Part::b: return tr ("Setting B: compare two settings (A / B)");
  277. case Part::copy: return tr ("Copy the setting you hear to the other one (A / B)");
  278. case Part::preset: return tr ("Presets: factory presets, your presets, save");
  279. case Part::prev: return tr ("Previous preset");
  280. case Part::next: return tr ("Next preset");
  281. case Part::none: break;
  282. }
  283. return {};
  284. }
  285. void PresetBar::willEdit()
  286. {
  287. const auto* fx = current();
  288. if (fx == nullptr)
  289. return;
  290. const auto now = juce::Time::getMillisecondCounter();
  291. if (gesture || now - lastEdit > 600)
  292. {
  293. auto& h = historyOf (track, slot, kind);
  294. const auto snap = snapshotOf (*fx);
  295. if (h.undo.empty() || ! (h.undo.back() == snap))
  296. {
  297. h.undo.push_back (snap);
  298. if (h.undo.size() > maxSteps)
  299. h.undo.erase (h.undo.begin());
  300. }
  301. h.redo.clear();
  302. }
  303. gesture = false;
  304. lastEdit = now;
  305. }
  306. void PresetBar::restore (const EffectParams& params, const std::vector<std::uint8_t>& state, const std::string& presetName, bool undoable)
  307. {
  308. engine.editSong ([t = track, s = slot, k = kind, params, state, presetName] (Song& song) {
  309. if (t >= (int) song.mixer.size() || s >= (int) song.mixer[(std::size_t) t].effects.size())
  310. return;
  311. auto& fx = song.mixer[(std::size_t) t].effects[(std::size_t) s];
  312. if (fx.kind != k)
  313. return;
  314. fx.params = params;
  315. fx.state = state;
  316. fx.presetName = presetName;
  317. }, undoable);
  318. }
  319. void PresetBar::click (Part part, juce::Component& owner, const FlPresets* flPresets)
  320. {
  321. const auto* fx = current();
  322. if (fx == nullptr)
  323. return;
  324. auto& h = historyOf (track, slot, kind);
  325. switch (part)
  326. {
  327. case Part::undo:
  328. case Part::redo:
  329. {
  330. auto& from = part == Part::undo ? h.undo : h.redo;
  331. auto& to = part == Part::undo ? h.redo : h.undo;
  332. if (from.empty())
  333. return;
  334. to.push_back (snapshotOf (*fx));
  335. const auto snap = from.back();
  336. from.pop_back();
  337. restore (snap.params, snap.state, snap.presetName);
  338. gesture = true;
  339. break;
  340. }
  341. case Part::a:
  342. case Part::b:
  343. {
  344. const auto wantB = part == Part::b;
  345. if (wantB == fx->abShowingB)
  346. return;
  347. engine.editSong ([t = track, s = slot] (Song& song) {
  348. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) s];
  349. std::vector<float> live (target.params.begin(), target.params.end());
  350. if (target.abOther.empty()) // B used for the first time: it starts as a copy of A
  351. target.abOther = live;
  352. for (std::size_t i = 0; i < target.params.size() && i < target.abOther.size(); ++i)
  353. target.params[i] = target.abOther[i];
  354. target.abOther = live;
  355. target.abShowingB = ! target.abShowingB;
  356. });
  357. gesture = true;
  358. break;
  359. }
  360. case Part::copy:
  361. engine.editSong ([t = track, s = slot] (Song& song) {
  362. auto& target = song.mixer[(std::size_t) t].effects[(std::size_t) s];
  363. target.abOther.assign (target.params.begin(), target.params.end());
  364. });
  365. break;
  366. case Part::preset: showPresetMenu (owner, flPresets); break;
  367. case Part::prev: stepPreset (-1, flPresets); break;
  368. case Part::next: stepPreset (1, flPresets); break;
  369. case Part::none: break;
  370. }
  371. owner.repaint();
  372. }
  373. void PresetBar::loadFactory (int index)
  374. {
  375. const auto& list = presets::factory (kind);
  376. const auto* fx = current();
  377. if (fx == nullptr || index < 0 || index >= (int) list.size())
  378. return;
  379. gesture = true;
  380. willEdit();
  381. restore (withMixLock (kind, presets::apply (kind, list[(std::size_t) index], engine.getSong().bpm, nullptr, &fx->params), fx->params), fx->state,
  382. list[(std::size_t) index].name);
  383. gesture = true;
  384. }
  385. void PresetBar::loadUser (const juce::File& file)
  386. {
  387. const auto* fx = current();
  388. if (fx == nullptr)
  389. return;
  390. const auto loaded = presets::fromText (kind, file.loadFileAsString().toStdString());
  391. if (! loaded)
  392. return;
  393. gesture = true;
  394. willEdit();
  395. restore (withMixLock (kind, loaded->params, fx->params), fx->state, loaded->name.empty() ? file.getFileNameWithoutExtension().toStdString() : loaded->name);
  396. gesture = true;
  397. }
  398. void PresetBar::stepPreset (int delta, const FlPresets* flPresets)
  399. {
  400. if (! hasLynxPresets())
  401. {
  402. if (flPresets != nullptr && flPresets->step)
  403. flPresets->step (delta);
  404. return;
  405. }
  406. const auto* fx = current();
  407. if (fx == nullptr)
  408. return;
  409. const auto& factory = presets::factory (kind);
  410. const auto users = userPresets (kind);
  411. const auto own = (int) factory.size() + users.size();
  412. const auto fl = flPresets != nullptr && flPresets->count && flPresets->load ? flPresets->count() : 0; // round 168r
  413. const auto count = own + fl;
  414. if (count == 0)
  415. return;
  416. int at = -1;
  417. if (fl > 0 && flPresets->index)
  418. if (const auto i = flPresets->index(); i >= 0 && i < fl)
  419. at = own + i;
  420. for (int i = 0; i < (int) factory.size() && at < 0; ++i)
  421. if (factory[(std::size_t) i].name == fx->presetName)
  422. at = i;
  423. for (int i = 0; i < users.size() && at < 0; ++i)
  424. if (users[i].getFileNameWithoutExtension().toStdString() == presets::fileNameFor (fx->presetName))
  425. at = (int) factory.size() + i;
  426. at = at < 0 ? (delta > 0 ? 0 : count - 1) : (at + delta + count) % count;
  427. if (at < (int) factory.size())
  428. loadFactory (at);
  429. else if (at < own)
  430. loadUser (users[at - (int) factory.size()]);
  431. else
  432. flPresets->load (at - own);
  433. }
  434. void PresetBar::showPresetMenu (juce::Component& owner, const FlPresets* flPresets)
  435. {
  436. const auto* fx = current();
  437. if (fx == nullptr)
  438. return;
  439. juce::PopupMenu menu;
  440. const auto& factory = presets::factory (kind);
  441. if (! factory.empty())
  442. {
  443. std::map<std::string, juce::PopupMenu> folders;
  444. std::vector<std::string> order;
  445. for (int i = 0; i < (int) factory.size(); ++i)
  446. {
  447. const auto& p = factory[(std::size_t) i];
  448. if (folders.find (p.folder) == folders.end())
  449. order.push_back (p.folder);
  450. folders[p.folder].addItem (utf8 (p.name.c_str()), true, p.name == fx->presetName, [this, i] { loadFactory (i); });
  451. }
  452. menu.addSectionHeader (tr ("LYNX SHARE presets"));
  453. for (const auto& f : order)
  454. if (f.empty())
  455. for (juce::PopupMenu::MenuItemIterator it (folders[f]); it.next();)
  456. menu.addItem (it.getItem());
  457. else
  458. menu.addSubMenu (tr (f.c_str()), folders[f]);
  459. }
  460. const auto users = userPresets (kind);
  461. if (! users.isEmpty())
  462. {
  463. menu.addSectionHeader (tr ("Your presets"));
  464. for (const auto& f : users)
  465. menu.addItem (f.getFileNameWithoutExtension(), true, f.getFileNameWithoutExtension().toStdString() == presets::fileNameFor (fx->presetName),
  466. [this, f] { loadUser (f); });
  467. }
  468. if (flPresets != nullptr && flPresets->addTo)
  469. {
  470. juce::PopupMenu fl;
  471. flPresets->addTo (fl);
  472. if (fl.getNumItems() > 0)
  473. {
  474. if (menu.getNumItems() == 0)
  475. menu = fl;
  476. else
  477. menu.addSubMenu (tr ("FL Studio presets"), fl);
  478. }
  479. }
  480. menu.addSeparator();
  481. juce::Component::SafePointer<juce::Component> safe (&owner);
  482. menu.addItem (tr ("Copy the setting you hear to the other one (A / B)"), [this, safe] {
  483. if (safe != nullptr)
  484. click (Part::copy, *safe);
  485. });
  486. menu.addItem (tr ("Save preset..."), [this, safe] {
  487. if (safe != nullptr)
  488. saveAs (*safe);
  489. });
  490. for (const auto& f : users)
  491. if (f.getFileNameWithoutExtension().toStdString() == presets::fileNameFor (fx->presetName) && ! fx->presetName.empty())
  492. {
  493. menu.addItem (tr ("Delete preset \"{0}\"", { f.getFileNameWithoutExtension() }), [this, f, safe] {
  494. f.deleteFile();
  495. engine.editSong ([t = track, s = slot] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) s].presetName.clear(); }, false);
  496. if (safe != nullptr)
  497. safe->repaint();
  498. });
  499. break;
  500. }
  501. if (presets::paramByName (kind, "Mix") >= 0)
  502. menu.addItem (tr ("Keep the Mix when loading presets"), true, mixLocked (kind), [this] { setMixLocked (kind, ! mixLocked (kind)); });
  503. menu.addItem (tr ("Show the presets folder"), [this] {
  504. const auto folder = userFolder (kind);
  505. folder.createDirectory();
  506. folder.revealToUser();
  507. });
  508. menu.showMenuAsync (juce::PopupMenu::Options().withTargetComponent (&owner).withTargetScreenArea (owner.localAreaToGlobal (rPreset.toNearestInt())));
  509. }
  510. void PresetBar::saveAs (juce::Component& owner)
  511. {
  512. const auto* fx = current();
  513. if (fx == nullptr)
  514. return;
  515. auto* window = new juce::AlertWindow (tr ("Save preset"), tr ("Name of the preset:"), juce::MessageBoxIconType::NoIcon, &owner);
  516. window->addTextEditor ("name", fx->presetName.empty() ? tr ("My preset") : utf8 (fx->presetName.c_str()));
  517. window->addButton (tr ("Save"), 1, juce::KeyPress (juce::KeyPress::returnKey));
  518. window->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  519. juce::Component::SafePointer<juce::Component> safe (&owner);
  520. window->enterModalState (true, juce::ModalCallbackFunction::create ([this, window, safe] (int result) {
  521. const auto name = window->getTextEditorContents ("name").trim();
  522. if (result != 1 || name.isEmpty() || safe == nullptr)
  523. return;
  524. const auto* slotNow = current();
  525. if (slotNow == nullptr)
  526. return;
  527. const auto folder = userFolder (kind);
  528. folder.createDirectory();
  529. const auto file = folder.getChildFile (utf8 (presets::fileNameFor (name.toStdString()).c_str()) + ".lynxpreset");
  530. file.replaceWithText (utf8 (presets::toText (kind, name.toStdString(), slotNow->params).c_str()));
  531. engine.editSong ([t = track, s = slot, n = name.toStdString()] (Song& song) { song.mixer[(std::size_t) t].effects[(std::size_t) s].presetName = n; }, false);
  532. safe->repaint();
  533. }), true);
  534. }
  535. } // namespace lynx::pluginheader