You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1132 lines
29KB

  1. #include <thread>
  2. #include <regex>
  3. #include <osdialog.h>
  4. #include <app/ModuleWidget.hpp>
  5. #include <app/Scene.hpp>
  6. #include <engine/Engine.hpp>
  7. #include <plugin/Plugin.hpp>
  8. #include <app/SvgPanel.hpp>
  9. #include <ui/MenuSeparator.hpp>
  10. #include <system.hpp>
  11. #include <asset.hpp>
  12. #include <helpers.hpp>
  13. #include <context.hpp>
  14. #include <settings.hpp>
  15. #include <history.hpp>
  16. #include <string.hpp>
  17. #include <componentlibrary.hpp>
  18. namespace rack {
  19. namespace app {
  20. static const char PRESET_FILTERS[] = "VCV Rack module preset (.vcvm):vcvm";
  21. struct ModuleWidget::Internal {
  22. /** The module position clicked on to start dragging in the rack.
  23. */
  24. math::Vec dragOffset;
  25. /** Global rack position the user clicked on.
  26. */
  27. math::Vec dragRackPos;
  28. bool dragEnabled = true;
  29. widget::Widget* panel = NULL;
  30. };
  31. ModuleWidget::ModuleWidget() {
  32. internal = new Internal;
  33. box.size = math::Vec(0, RACK_GRID_HEIGHT);
  34. }
  35. ModuleWidget::~ModuleWidget() {
  36. clearChildren();
  37. setModule(NULL);
  38. delete internal;
  39. }
  40. plugin::Model* ModuleWidget::getModel() {
  41. return model;
  42. }
  43. void ModuleWidget::setModel(plugin::Model* model) {
  44. assert(!this->model);
  45. this->model = model;
  46. }
  47. engine::Module* ModuleWidget::getModule() {
  48. return module;
  49. }
  50. void ModuleWidget::setModule(engine::Module* module) {
  51. if (this->module) {
  52. APP->engine->removeModule(this->module);
  53. delete this->module;
  54. this->module = NULL;
  55. }
  56. this->module = module;
  57. }
  58. widget::Widget* ModuleWidget::getPanel() {
  59. return internal->panel;
  60. }
  61. void ModuleWidget::setPanel(widget::Widget* panel) {
  62. // Remove existing panel
  63. if (internal->panel) {
  64. removeChild(internal->panel);
  65. delete internal->panel;
  66. internal->panel = NULL;
  67. }
  68. if (panel) {
  69. addChildBottom(panel);
  70. internal->panel = panel;
  71. box.size.x = std::round(panel->box.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  72. // If width is zero, set it to 12HP for sanity
  73. if (box.size.x == 0.0)
  74. box.size.x = 12 * RACK_GRID_WIDTH;
  75. }
  76. }
  77. void ModuleWidget::setPanel(std::shared_ptr<window::Svg> svg) {
  78. // Create SvgPanel
  79. SvgPanel* panel = new SvgPanel;
  80. panel->setBackground(svg);
  81. setPanel(panel);
  82. }
  83. void ModuleWidget::addParam(ParamWidget* param) {
  84. addChild(param);
  85. }
  86. void ModuleWidget::addInput(PortWidget* input) {
  87. // Check that the port is an input
  88. assert(input->type == engine::Port::INPUT);
  89. // Check that the port doesn't have a duplicate ID
  90. PortWidget* input2 = getInput(input->portId);
  91. assert(!input2);
  92. // Add port
  93. addChild(input);
  94. }
  95. void ModuleWidget::addOutput(PortWidget* output) {
  96. // Check that the port is an output
  97. assert(output->type == engine::Port::OUTPUT);
  98. // Check that the port doesn't have a duplicate ID
  99. PortWidget* output2 = getOutput(output->portId);
  100. assert(!output2);
  101. // Add port
  102. addChild(output);
  103. }
  104. template <class T, typename F>
  105. T* getFirstDescendantOfTypeWithCondition(widget::Widget* w, F f) {
  106. T* t = dynamic_cast<T*>(w);
  107. if (t && f(t))
  108. return t;
  109. for (widget::Widget* child : w->children) {
  110. T* foundT = getFirstDescendantOfTypeWithCondition<T>(child, f);
  111. if (foundT)
  112. return foundT;
  113. }
  114. return NULL;
  115. }
  116. ParamWidget* ModuleWidget::getParam(int paramId) {
  117. return getFirstDescendantOfTypeWithCondition<ParamWidget>(this, [&](ParamWidget* pw) -> bool {
  118. return pw->paramId == paramId;
  119. });
  120. }
  121. PortWidget* ModuleWidget::getInput(int portId) {
  122. return getFirstDescendantOfTypeWithCondition<PortWidget>(this, [&](PortWidget* pw) -> bool {
  123. return pw->type == engine::Port::INPUT && pw->portId == portId;
  124. });
  125. }
  126. PortWidget* ModuleWidget::getOutput(int portId) {
  127. return getFirstDescendantOfTypeWithCondition<PortWidget>(this, [&](PortWidget* pw) -> bool {
  128. return pw->type == engine::Port::OUTPUT && pw->portId == portId;
  129. });
  130. }
  131. template <class T, typename F>
  132. void doIfTypeRecursive(widget::Widget* w, F f) {
  133. T* t = dynamic_cast<T*>(w);
  134. if (t)
  135. f(t);
  136. for (widget::Widget* child : w->children) {
  137. doIfTypeRecursive<T>(child, f);
  138. }
  139. }
  140. std::vector<ParamWidget*> ModuleWidget::getParams() {
  141. std::vector<ParamWidget*> pws;
  142. doIfTypeRecursive<ParamWidget>(this, [&](ParamWidget* pw) {
  143. pws.push_back(pw);
  144. });
  145. return pws;
  146. }
  147. std::vector<PortWidget*> ModuleWidget::getPorts() {
  148. std::vector<PortWidget*> pws;
  149. doIfTypeRecursive<PortWidget>(this, [&](PortWidget* pw) {
  150. pws.push_back(pw);
  151. });
  152. return pws;
  153. }
  154. std::vector<PortWidget*> ModuleWidget::getInputs() {
  155. std::vector<PortWidget*> pws;
  156. doIfTypeRecursive<PortWidget>(this, [&](PortWidget* pw) {
  157. if (pw->type == engine::Port::INPUT)
  158. pws.push_back(pw);
  159. });
  160. return pws;
  161. }
  162. std::vector<PortWidget*> ModuleWidget::getOutputs() {
  163. std::vector<PortWidget*> pws;
  164. doIfTypeRecursive<PortWidget>(this, [&](PortWidget* pw) {
  165. if (pw->type == engine::Port::OUTPUT)
  166. pws.push_back(pw);
  167. });
  168. return pws;
  169. }
  170. void ModuleWidget::draw(const DrawArgs& args) {
  171. nvgScissor(args.vg, RECT_ARGS(args.clipBox));
  172. if (module && module->isBypassed()) {
  173. nvgAlpha(args.vg, 0.33);
  174. }
  175. Widget::draw(args);
  176. // Meter
  177. if (module && settings::cpuMeter) {
  178. float sampleRate = APP->engine->getSampleRate();
  179. const float* meterBuffer = module->meterBuffer();
  180. int meterLength = module->meterLength();
  181. int meterIndex = module->meterIndex();
  182. // // Text background
  183. // nvgBeginPath(args.vg);
  184. // nvgRect(args.vg, 0.0, box.size.y - infoHeight, box.size.x, infoHeight);
  185. // nvgFillColor(args.vg, nvgRGBAf(0, 0, 0, 0.75));
  186. // nvgFill(args.vg);
  187. // Draw time plot
  188. const float plotHeight = box.size.y - BND_WIDGET_HEIGHT;
  189. nvgBeginPath(args.vg);
  190. nvgMoveTo(args.vg, 0.0, plotHeight);
  191. math::Vec p1;
  192. for (int i = 0; i < meterLength; i++) {
  193. int index = math::eucMod(meterIndex + i + 1, meterLength);
  194. float meter = math::clamp(meterBuffer[index] * sampleRate, 0.f, 1.f);
  195. meter = std::max(0.f, meter);
  196. math::Vec p;
  197. p.x = (float) i / (meterLength - 1) * box.size.x;
  198. p.y = (1.f - meter) * plotHeight;
  199. if (i == 0) {
  200. nvgLineTo(args.vg, VEC_ARGS(p));
  201. }
  202. else {
  203. math::Vec p2 = p;
  204. p2.x -= 0.5f / (meterLength - 1) * box.size.x;
  205. nvgBezierTo(args.vg, VEC_ARGS(p1), VEC_ARGS(p2), VEC_ARGS(p));
  206. }
  207. p1 = p;
  208. p1.x += 0.5f / (meterLength - 1) * box.size.x;
  209. }
  210. nvgLineTo(args.vg, box.size.x, plotHeight);
  211. nvgClosePath(args.vg);
  212. NVGcolor color = componentlibrary::SCHEME_ORANGE;
  213. nvgFillColor(args.vg, color::alpha(color, 0.75));
  214. nvgFill(args.vg);
  215. nvgStrokeWidth(args.vg, 2.0);
  216. nvgStrokeColor(args.vg, color);
  217. nvgStroke(args.vg);
  218. // Text background
  219. bndMenuBackground(args.vg, 0.0, plotHeight, box.size.x, BND_WIDGET_HEIGHT, BND_CORNER_ALL);
  220. // Text
  221. float percent = meterBuffer[meterIndex] * sampleRate * 100.f;
  222. // float microseconds = meterBuffer[meterIndex] * 1e6f;
  223. std::string meterText = string::f("%.1f", percent);
  224. // Only append "%" if wider than 2 HP
  225. if (box.getWidth() > RACK_GRID_WIDTH * 2)
  226. meterText += "%";
  227. math::Vec pt;
  228. pt.x = box.size.x - bndLabelWidth(args.vg, -1, meterText.c_str()) + 3;
  229. pt.y = plotHeight + 0.5;
  230. bndMenuLabel(args.vg, VEC_ARGS(pt), INFINITY, BND_WIDGET_HEIGHT, -1, meterText.c_str());
  231. }
  232. // Selection
  233. if (APP->scene->rack->isSelected(this)) {
  234. nvgBeginPath(args.vg);
  235. nvgRect(args.vg, 0.0, 0.0, VEC_ARGS(box.size));
  236. nvgFillColor(args.vg, nvgRGBAf(1, 0, 0, 0.25));
  237. nvgFill(args.vg);
  238. nvgStrokeWidth(args.vg, 2.0);
  239. nvgStrokeColor(args.vg, nvgRGBAf(1, 0, 0, 0.5));
  240. nvgStroke(args.vg);
  241. }
  242. nvgResetScissor(args.vg);
  243. }
  244. void ModuleWidget::drawLayer(const DrawArgs& args, int layer) {
  245. if (layer == -1) {
  246. nvgBeginPath(args.vg);
  247. float r = 20; // Blur radius
  248. float c = 20; // Corner radius
  249. math::Rect shadowBox = box.zeroPos().grow(math::Vec(10, -30));
  250. math::Rect shadowOutsideBox = shadowBox.grow(math::Vec(r, r));
  251. nvgRect(args.vg, RECT_ARGS(shadowOutsideBox));
  252. NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
  253. NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
  254. nvgFillPaint(args.vg, nvgBoxGradient(args.vg, RECT_ARGS(shadowBox), c, r, shadowColor, transparentColor));
  255. nvgFill(args.vg);
  256. }
  257. else {
  258. Widget::drawLayer(args, layer);
  259. }
  260. }
  261. void ModuleWidget::onHover(const HoverEvent& e) {
  262. if (APP->scene->rack->isSelected(this)) {
  263. e.consume(this);
  264. }
  265. OpaqueWidget::onHover(e);
  266. }
  267. void ModuleWidget::onHoverKey(const HoverKeyEvent& e) {
  268. if (e.action == GLFW_PRESS || e.action == GLFW_REPEAT) {
  269. if (e.keyName == "c" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  270. copyClipboard();
  271. e.consume(this);
  272. }
  273. if (e.keyName == "v" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  274. if (pasteClipboardAction()) {
  275. e.consume(this);
  276. }
  277. }
  278. if (e.keyName == "d" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  279. cloneAction(false);
  280. e.consume(this);
  281. }
  282. if (e.keyName == "d" && (e.mods & RACK_MOD_MASK) == (RACK_MOD_CTRL | GLFW_MOD_SHIFT)) {
  283. cloneAction(true);
  284. e.consume(this);
  285. }
  286. if (e.keyName == "i" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  287. resetAction();
  288. e.consume(this);
  289. }
  290. if (e.keyName == "r" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  291. randomizeAction();
  292. e.consume(this);
  293. }
  294. if (e.keyName == "u" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  295. disconnectAction();
  296. e.consume(this);
  297. }
  298. if (e.keyName == "e" && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  299. bypassAction(!module->isBypassed());
  300. e.consume(this);
  301. }
  302. if ((e.key == GLFW_KEY_DELETE || e.key == GLFW_KEY_BACKSPACE) && (e.mods & RACK_MOD_MASK) == 0) {
  303. // Deletes `this`
  304. removeAction();
  305. e.consume(NULL);
  306. return;
  307. }
  308. if (e.key == GLFW_KEY_F1 && (e.mods & RACK_MOD_MASK) == RACK_MOD_CTRL) {
  309. std::string manualUrl = model->getManualUrl();
  310. if (!manualUrl.empty())
  311. system::openBrowser(manualUrl);
  312. e.consume(this);
  313. }
  314. }
  315. if (e.isConsumed())
  316. return;
  317. OpaqueWidget::onHoverKey(e);
  318. }
  319. void ModuleWidget::onButton(const ButtonEvent& e) {
  320. bool selected = APP->scene->rack->isSelected(this);
  321. if (selected) {
  322. if (e.button == GLFW_MOUSE_BUTTON_RIGHT) {
  323. if (e.action == GLFW_PRESS) {
  324. // Open selection context menu on right-click
  325. ui::Menu* menu = createMenu();
  326. APP->scene->rack->appendSelectionContextMenu(menu);
  327. }
  328. e.consume(this);
  329. }
  330. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  331. if (e.action == GLFW_PRESS) {
  332. // Toggle selection on Shift-click
  333. if ((e.mods & RACK_MOD_MASK) == GLFW_MOD_SHIFT) {
  334. APP->scene->rack->select(this, false);
  335. e.consume(NULL);
  336. return;
  337. }
  338. // If module positions are locked, don't consume left-click
  339. if (settings::lockModules) {
  340. e.consume(NULL);
  341. return;
  342. }
  343. internal->dragOffset = e.pos;
  344. }
  345. e.consume(this);
  346. }
  347. return;
  348. }
  349. // Dispatch event to children
  350. Widget::onButton(e);
  351. e.stopPropagating();
  352. if (e.isConsumed())
  353. return;
  354. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  355. if (e.action == GLFW_PRESS) {
  356. // Toggle selection on Shift-click
  357. if ((e.mods & RACK_MOD_MASK) == GLFW_MOD_SHIFT) {
  358. APP->scene->rack->select(this, true);
  359. e.consume(NULL);
  360. return;
  361. }
  362. // If module positions are locked, don't consume left-click
  363. if (settings::lockModules) {
  364. e.consume(NULL);
  365. return;
  366. }
  367. internal->dragOffset = e.pos;
  368. }
  369. e.consume(this);
  370. }
  371. // Open context menu on right-click
  372. if (e.button == GLFW_MOUSE_BUTTON_RIGHT && e.action == GLFW_PRESS) {
  373. createContextMenu();
  374. e.consume(this);
  375. }
  376. }
  377. void ModuleWidget::onDragStart(const DragStartEvent& e) {
  378. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  379. // HACK Disable FramebufferWidget redrawing subpixels while dragging
  380. APP->window->fbDirtyOnSubpixelChange() = false;
  381. // Clear dragRack so dragging in not enabled until mouse is moved a bit.
  382. internal->dragRackPos = math::Vec(NAN, NAN);
  383. // Prepare initial position of modules for history.
  384. APP->scene->rack->updateModuleOldPositions();
  385. }
  386. }
  387. void ModuleWidget::onDragEnd(const DragEndEvent& e) {
  388. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  389. APP->window->fbDirtyOnSubpixelChange() = true;
  390. // The next time the module is dragged, it should always move immediately
  391. internal->dragEnabled = true;
  392. history::ComplexAction* h = APP->scene->rack->getModuleDragAction();
  393. if (!h->isEmpty())
  394. APP->history->push(h);
  395. else
  396. delete h;
  397. }
  398. }
  399. void ModuleWidget::onDragMove(const DragMoveEvent& e) {
  400. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  401. math::Vec mousePos = APP->scene->rack->getMousePos();
  402. if (!internal->dragEnabled) {
  403. // Set dragRackPos on the first time after dragging
  404. if (!internal->dragRackPos.isFinite())
  405. internal->dragRackPos = mousePos;
  406. // Check if the mouse has moved enough to start dragging the module.
  407. const float minDist = RACK_GRID_WIDTH;
  408. if (internal->dragRackPos.minus(mousePos).square() >= std::pow(minDist, 2))
  409. internal->dragEnabled = true;
  410. }
  411. // Move module
  412. if (internal->dragEnabled) {
  413. // Round y coordinate to nearest rack height
  414. math::Vec pos = mousePos;
  415. pos.x -= internal->dragOffset.x;
  416. pos.y -= RACK_GRID_HEIGHT / 2;
  417. if (APP->scene->rack->isSelected(this)) {
  418. pos = (pos / RACK_GRID_SIZE).round() * RACK_GRID_SIZE;
  419. math::Vec delta = pos.minus(box.pos);
  420. APP->scene->rack->setSelectionPosNearest(delta);
  421. }
  422. else {
  423. if (settings::squeezeModules) {
  424. APP->scene->rack->setModulePosSqueeze(this, pos);
  425. }
  426. else {
  427. if ((APP->window->getMods() & RACK_MOD_MASK) == RACK_MOD_CTRL)
  428. APP->scene->rack->setModulePosForce(this, pos);
  429. else
  430. APP->scene->rack->setModulePosNearest(this, pos);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. void ModuleWidget::onDragHover(const DragHoverEvent& e) {
  437. if (APP->scene->rack->isSelected(this)) {
  438. e.consume(this);
  439. }
  440. OpaqueWidget::onDragHover(e);
  441. }
  442. json_t* ModuleWidget::toJson() {
  443. json_t* moduleJ = APP->engine->moduleToJson(module);
  444. return moduleJ;
  445. }
  446. void ModuleWidget::fromJson(json_t* moduleJ) {
  447. APP->engine->moduleFromJson(module, moduleJ);
  448. }
  449. bool ModuleWidget::pasteJsonAction(json_t* moduleJ) {
  450. engine::Module::jsonStripIds(moduleJ);
  451. json_t* oldModuleJ = toJson();
  452. DEFER({json_decref(oldModuleJ);});
  453. try {
  454. fromJson(moduleJ);
  455. }
  456. catch (Exception& e) {
  457. WARN("%s", e.what());
  458. return false;
  459. }
  460. // history::ModuleChange
  461. history::ModuleChange* h = new history::ModuleChange;
  462. h->name = "paste module preset";
  463. h->moduleId = module->id;
  464. json_incref(oldModuleJ);
  465. h->oldModuleJ = oldModuleJ;
  466. json_incref(moduleJ);
  467. h->newModuleJ = moduleJ;
  468. APP->history->push(h);
  469. return true;
  470. }
  471. void ModuleWidget::copyClipboard() {
  472. json_t* moduleJ = toJson();
  473. engine::Module::jsonStripIds(moduleJ);
  474. DEFER({json_decref(moduleJ);});
  475. char* json = json_dumps(moduleJ, JSON_INDENT(2));
  476. DEFER({std::free(json);});
  477. glfwSetClipboardString(APP->window->win, json);
  478. }
  479. bool ModuleWidget::pasteClipboardAction() {
  480. const char* json = glfwGetClipboardString(APP->window->win);
  481. if (!json) {
  482. WARN("Could not get text from clipboard.");
  483. return false;
  484. }
  485. json_error_t error;
  486. json_t* moduleJ = json_loads(json, 0, &error);
  487. if (!moduleJ) {
  488. WARN("JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text);
  489. return false;
  490. }
  491. DEFER({json_decref(moduleJ);});
  492. return pasteJsonAction(moduleJ);
  493. }
  494. void ModuleWidget::load(std::string filename) {
  495. FILE* file = std::fopen(filename.c_str(), "r");
  496. if (!file)
  497. throw Exception("Could not load patch file %s", filename.c_str());
  498. DEFER({std::fclose(file);});
  499. INFO("Loading preset %s", filename.c_str());
  500. json_error_t error;
  501. json_t* moduleJ = json_loadf(file, 0, &error);
  502. if (!moduleJ)
  503. throw Exception("File is not a valid patch file. JSON parsing error at %s %d:%d %s", error.source, error.line, error.column, error.text);
  504. DEFER({json_decref(moduleJ);});
  505. engine::Module::jsonStripIds(moduleJ);
  506. fromJson(moduleJ);
  507. }
  508. void ModuleWidget::loadAction(std::string filename) {
  509. // history::ModuleChange
  510. history::ModuleChange* h = new history::ModuleChange;
  511. h->name = "load module preset";
  512. h->moduleId = module->id;
  513. h->oldModuleJ = toJson();
  514. try {
  515. load(filename);
  516. }
  517. catch (Exception& e) {
  518. delete h;
  519. throw;
  520. }
  521. // TODO We can use `moduleJ` here instead to save a toJson() call.
  522. h->newModuleJ = toJson();
  523. APP->history->push(h);
  524. }
  525. void ModuleWidget::loadTemplate() {
  526. std::string templatePath = system::join(model->getUserPresetDirectory(), "template.vcvm");
  527. try {
  528. load(templatePath);
  529. }
  530. catch (Exception& e) {
  531. // Do nothing
  532. }
  533. }
  534. void ModuleWidget::loadDialog() {
  535. std::string presetDir = model->getUserPresetDirectory();
  536. system::createDirectories(presetDir);
  537. // Delete directories if empty
  538. DEFER({
  539. try {
  540. system::remove(presetDir);
  541. system::remove(system::getDirectory(presetDir));
  542. }
  543. catch (Exception& e) {
  544. // Ignore exceptions if directory cannot be removed.
  545. }
  546. });
  547. osdialog_filters* filters = osdialog_filters_parse(PRESET_FILTERS);
  548. DEFER({osdialog_filters_free(filters);});
  549. char* pathC = osdialog_file(OSDIALOG_OPEN, presetDir.c_str(), NULL, filters);
  550. if (!pathC) {
  551. // No path selected
  552. return;
  553. }
  554. DEFER({std::free(pathC);});
  555. try {
  556. loadAction(pathC);
  557. }
  558. catch (Exception& e) {
  559. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, e.what());
  560. }
  561. }
  562. void ModuleWidget::save(std::string filename) {
  563. INFO("Saving preset %s", filename.c_str());
  564. json_t* moduleJ = toJson();
  565. assert(moduleJ);
  566. DEFER({json_decref(moduleJ);});
  567. engine::Module::jsonStripIds(moduleJ);
  568. FILE* file = std::fopen(filename.c_str(), "w");
  569. if (!file) {
  570. std::string message = string::f("Could not save preset to file %s", filename.c_str());
  571. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, message.c_str());
  572. return;
  573. }
  574. DEFER({std::fclose(file);});
  575. json_dumpf(moduleJ, file, JSON_INDENT(2));
  576. }
  577. void ModuleWidget::saveTemplate() {
  578. std::string presetDir = model->getUserPresetDirectory();
  579. system::createDirectories(presetDir);
  580. std::string templatePath = system::join(presetDir, "template.vcvm");
  581. save(templatePath);
  582. }
  583. void ModuleWidget::saveTemplateDialog() {
  584. if (hasTemplate()) {
  585. std::string message = string::f("Overwrite default preset for %s?", model->getFullName().c_str());
  586. if (!osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, message.c_str()))
  587. return;
  588. }
  589. saveTemplate();
  590. }
  591. bool ModuleWidget::hasTemplate() {
  592. std::string presetDir = model->getUserPresetDirectory();
  593. std::string templatePath = system::join(presetDir, "template.vcvm");
  594. return system::exists(templatePath);;
  595. }
  596. void ModuleWidget::clearTemplate() {
  597. std::string presetDir = model->getUserPresetDirectory();
  598. std::string templatePath = system::join(presetDir, "template.vcvm");
  599. system::remove(templatePath);
  600. }
  601. void ModuleWidget::clearTemplateDialog() {
  602. std::string message = string::f("Delete default preset for %s?", model->getFullName().c_str());
  603. if (!osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, message.c_str()))
  604. return;
  605. clearTemplate();
  606. }
  607. void ModuleWidget::saveDialog() {
  608. std::string presetDir = model->getUserPresetDirectory();
  609. system::createDirectories(presetDir);
  610. // Delete directories if empty
  611. DEFER({
  612. try {
  613. system::remove(presetDir);
  614. system::remove(system::getDirectory(presetDir));
  615. }
  616. catch (Exception& e) {
  617. // Ignore exceptions if directory cannot be removed.
  618. }
  619. });
  620. osdialog_filters* filters = osdialog_filters_parse(PRESET_FILTERS);
  621. DEFER({osdialog_filters_free(filters);});
  622. char* pathC = osdialog_file(OSDIALOG_SAVE, presetDir.c_str(), "Untitled.vcvm", filters);
  623. if (!pathC) {
  624. // No path selected
  625. return;
  626. }
  627. DEFER({std::free(pathC);});
  628. std::string path = pathC;
  629. // Automatically append .vcvm extension
  630. if (system::getExtension(path) != ".vcvm")
  631. path += ".vcvm";
  632. save(path);
  633. }
  634. void ModuleWidget::disconnect() {
  635. for (PortWidget* pw : getPorts()) {
  636. APP->scene->rack->clearCablesOnPort(pw);
  637. }
  638. }
  639. void ModuleWidget::resetAction() {
  640. assert(module);
  641. // history::ModuleChange
  642. history::ModuleChange* h = new history::ModuleChange;
  643. h->name = "reset module";
  644. h->moduleId = module->id;
  645. h->oldModuleJ = toJson();
  646. APP->engine->resetModule(module);
  647. h->newModuleJ = toJson();
  648. APP->history->push(h);
  649. }
  650. void ModuleWidget::randomizeAction() {
  651. assert(module);
  652. // history::ModuleChange
  653. history::ModuleChange* h = new history::ModuleChange;
  654. h->name = "randomize module";
  655. h->moduleId = module->id;
  656. h->oldModuleJ = toJson();
  657. APP->engine->randomizeModule(module);
  658. h->newModuleJ = toJson();
  659. APP->history->push(h);
  660. }
  661. void ModuleWidget::appendDisconnectActions(history::ComplexAction* complexAction) {
  662. for (PortWidget* pw : getPorts()) {
  663. for (CableWidget* cw : APP->scene->rack->getCompleteCablesOnPort(pw)) {
  664. // history::CableRemove
  665. history::CableRemove* h = new history::CableRemove;
  666. h->setCable(cw);
  667. complexAction->push(h);
  668. // Delete cable
  669. APP->scene->rack->removeCable(cw);
  670. delete cw;
  671. }
  672. };
  673. }
  674. void ModuleWidget::disconnectAction() {
  675. history::ComplexAction* complexAction = new history::ComplexAction;
  676. complexAction->name = "disconnect cables";
  677. appendDisconnectActions(complexAction);
  678. if (!complexAction->isEmpty())
  679. APP->history->push(complexAction);
  680. else
  681. delete complexAction;
  682. }
  683. void ModuleWidget::cloneAction(bool cloneCables) {
  684. // history::ComplexAction
  685. history::ComplexAction* h = new history::ComplexAction;
  686. h->name = "duplicate module";
  687. // Save patch store in this module so we can copy it below
  688. APP->engine->prepareSaveModule(module);
  689. // JSON serialization is the obvious way to do this
  690. json_t* moduleJ = toJson();
  691. DEFER({
  692. json_decref(moduleJ);
  693. });
  694. engine::Module::jsonStripIds(moduleJ);
  695. // Clone Module
  696. INFO("Creating module %s", model->getFullName().c_str());
  697. engine::Module* clonedModule = model->createModule();
  698. // Set ID here so we can copy module storage dir
  699. clonedModule->id = random::u64() % (1ull << 53);
  700. system::copy(module->getPatchStorageDirectory(), clonedModule->getPatchStorageDirectory());
  701. // This doesn't need a lock (via Engine::moduleFromJson()) because the Module is not added to the Engine yet.
  702. try {
  703. clonedModule->fromJson(moduleJ);
  704. }
  705. catch (Exception& e) {
  706. WARN("%s", e.what());
  707. }
  708. APP->engine->addModule(clonedModule);
  709. // Clone ModuleWidget
  710. INFO("Creating module widget %s", model->getFullName().c_str());
  711. ModuleWidget* clonedModuleWidget = model->createModuleWidget(clonedModule);
  712. APP->scene->rack->updateModuleOldPositions();
  713. APP->scene->rack->addModule(clonedModuleWidget);
  714. // Place module to the right of `this` module, by forcing it to 1 HP to the right.
  715. math::Vec clonedPos = box.pos;
  716. clonedPos.x += clonedModuleWidget->box.getWidth();
  717. if (settings::squeezeModules)
  718. APP->scene->rack->squeezeModulePos(clonedModuleWidget, clonedPos);
  719. else
  720. APP->scene->rack->setModulePosNearest(clonedModuleWidget, clonedPos);
  721. h->push(APP->scene->rack->getModuleDragAction());
  722. APP->scene->rack->updateExpanders();
  723. // history::ModuleAdd
  724. history::ModuleAdd* hma = new history::ModuleAdd;
  725. hma->setModule(clonedModuleWidget);
  726. h->push(hma);
  727. if (cloneCables) {
  728. // Clone cables attached to input ports
  729. for (PortWidget* pw : getInputs()) {
  730. for (CableWidget* cw : APP->scene->rack->getCompleteCablesOnPort(pw)) {
  731. // Create cable attached to cloned ModuleWidget's input
  732. engine::Cable* clonedCable = new engine::Cable;
  733. clonedCable->inputModule = clonedModule;
  734. clonedCable->inputId = cw->cable->inputId;
  735. // If cable is self-patched, attach to cloned module instead
  736. if (cw->cable->outputModule == module)
  737. clonedCable->outputModule = clonedModule;
  738. else
  739. clonedCable->outputModule = cw->cable->outputModule;
  740. clonedCable->outputId = cw->cable->outputId;
  741. APP->engine->addCable(clonedCable);
  742. app::CableWidget* clonedCw = new app::CableWidget;
  743. clonedCw->setCable(clonedCable);
  744. clonedCw->color = cw->color;
  745. APP->scene->rack->addCable(clonedCw);
  746. // history::CableAdd
  747. history::CableAdd* hca = new history::CableAdd;
  748. hca->setCable(clonedCw);
  749. h->push(hca);
  750. }
  751. }
  752. }
  753. APP->history->push(h);
  754. }
  755. void ModuleWidget::bypassAction(bool bypassed) {
  756. assert(module);
  757. // history::ModuleBypass
  758. history::ModuleBypass* h = new history::ModuleBypass;
  759. h->moduleId = module->id;
  760. h->bypassed = bypassed;
  761. if (!bypassed)
  762. h->name = "un-bypass module";
  763. APP->history->push(h);
  764. APP->engine->bypassModule(module, bypassed);
  765. }
  766. void ModuleWidget::removeAction() {
  767. history::ComplexAction* h = new history::ComplexAction;
  768. h->name = "delete module";
  769. // Disconnect cables
  770. appendDisconnectActions(h);
  771. // Unset module position from rack.
  772. APP->scene->rack->updateModuleOldPositions();
  773. if (settings::squeezeModules)
  774. APP->scene->rack->unsqueezeModulePos(this);
  775. h->push(APP->scene->rack->getModuleDragAction());
  776. // history::ModuleRemove
  777. history::ModuleRemove* moduleRemove = new history::ModuleRemove;
  778. moduleRemove->setModule(this);
  779. h->push(moduleRemove);
  780. APP->history->push(h);
  781. // This removes the module and transfers ownership to caller
  782. APP->scene->rack->removeModule(this);
  783. delete this;
  784. APP->scene->rack->updateExpanders();
  785. }
  786. // Create ModulePresetPathItems for each patch in a directory.
  787. static void appendPresetItems(ui::Menu* menu, WeakPtr<ModuleWidget> moduleWidget, std::string presetDir) {
  788. bool hasPresets = false;
  789. if (system::isDirectory(presetDir)) {
  790. // Note: This is not cached, so opening this menu each time might have a bit of latency.
  791. std::vector<std::string> entries = system::getEntries(presetDir);
  792. std::sort(entries.begin(), entries.end());
  793. for (std::string path : entries) {
  794. std::string name = system::getStem(path);
  795. // Remove "1_", "42_", "001_", etc at the beginning of preset filenames
  796. std::regex r("^\\d+_");
  797. name = std::regex_replace(name, r, "");
  798. if (system::isDirectory(path)) {
  799. hasPresets = true;
  800. menu->addChild(createSubmenuItem(name, "", [=](ui::Menu* menu) {
  801. if (!moduleWidget)
  802. return;
  803. appendPresetItems(menu, moduleWidget, path);
  804. }));
  805. }
  806. else if (system::getExtension(path) == ".vcvm" && name != "template") {
  807. hasPresets = true;
  808. menu->addChild(createMenuItem(name, "", [=]() {
  809. if (!moduleWidget)
  810. return;
  811. try {
  812. moduleWidget->loadAction(path);
  813. }
  814. catch (Exception& e) {
  815. osdialog_message(OSDIALOG_WARNING, OSDIALOG_OK, e.what());
  816. }
  817. }));
  818. }
  819. }
  820. }
  821. if (!hasPresets) {
  822. menu->addChild(createMenuLabel("(None)"));
  823. }
  824. };
  825. void ModuleWidget::createContextMenu() {
  826. ui::Menu* menu = createMenu();
  827. assert(model);
  828. WeakPtr<ModuleWidget> weakThis = this;
  829. // Brand and module name
  830. menu->addChild(createMenuLabel(model->name));
  831. menu->addChild(createMenuLabel(model->plugin->brand));
  832. // Info
  833. menu->addChild(createSubmenuItem("Info", "", [=](ui::Menu* menu) {
  834. model->appendContextMenu(menu);
  835. }));
  836. // Preset
  837. menu->addChild(createSubmenuItem("Preset", "", [=](ui::Menu* menu) {
  838. menu->addChild(createMenuItem("Copy", RACK_MOD_CTRL_NAME "+C", [=]() {
  839. if (!weakThis)
  840. return;
  841. weakThis->copyClipboard();
  842. }));
  843. menu->addChild(createMenuItem("Paste", RACK_MOD_CTRL_NAME "+V", [=]() {
  844. if (!weakThis)
  845. return;
  846. weakThis->pasteClipboardAction();
  847. }));
  848. menu->addChild(createMenuItem("Open", "", [=]() {
  849. if (!weakThis)
  850. return;
  851. weakThis->loadDialog();
  852. }));
  853. menu->addChild(createMenuItem("Save as", "", [=]() {
  854. if (!weakThis)
  855. return;
  856. weakThis->saveDialog();
  857. }));
  858. menu->addChild(createMenuItem("Save default", "", [=]() {
  859. if (!weakThis)
  860. return;
  861. weakThis->saveTemplateDialog();
  862. }));
  863. menu->addChild(createMenuItem("Clear default", "", [=]() {
  864. if (!weakThis)
  865. return;
  866. weakThis->clearTemplateDialog();
  867. }, !weakThis->hasTemplate()));
  868. // Scan `<user dir>/presets/<plugin slug>/<module slug>` for presets.
  869. menu->addChild(new ui::MenuSeparator);
  870. menu->addChild(createMenuLabel("User presets"));
  871. appendPresetItems(menu, weakThis, weakThis->model->getUserPresetDirectory());
  872. // Scan `<plugin dir>/presets/<module slug>` for presets.
  873. menu->addChild(new ui::MenuSeparator);
  874. menu->addChild(createMenuLabel("Factory presets"));
  875. appendPresetItems(menu, weakThis, weakThis->model->getFactoryPresetDirectory());
  876. }));
  877. // Initialize
  878. menu->addChild(createMenuItem("Initialize", RACK_MOD_CTRL_NAME "+I", [=]() {
  879. if (!weakThis)
  880. return;
  881. weakThis->resetAction();
  882. }));
  883. // Randomize
  884. menu->addChild(createMenuItem("Randomize", RACK_MOD_CTRL_NAME "+R", [=]() {
  885. if (!weakThis)
  886. return;
  887. weakThis->randomizeAction();
  888. }));
  889. // Disconnect cables
  890. menu->addChild(createMenuItem("Disconnect cables", RACK_MOD_CTRL_NAME "+U", [=]() {
  891. if (!weakThis)
  892. return;
  893. weakThis->disconnectAction();
  894. }));
  895. // Bypass
  896. std::string bypassText = RACK_MOD_CTRL_NAME "+E";
  897. bool bypassed = module && module->isBypassed();
  898. if (bypassed)
  899. bypassText += " " CHECKMARK_STRING;
  900. menu->addChild(createMenuItem("Bypass", bypassText, [=]() {
  901. if (!weakThis)
  902. return;
  903. weakThis->bypassAction(!bypassed);
  904. }));
  905. // Duplicate
  906. menu->addChild(createMenuItem("Duplicate", RACK_MOD_CTRL_NAME "+D", [=]() {
  907. if (!weakThis)
  908. return;
  909. weakThis->cloneAction(false);
  910. }));
  911. // Duplicate with cables
  912. menu->addChild(createMenuItem("└ with cables", RACK_MOD_SHIFT_NAME "+" RACK_MOD_CTRL_NAME "+D", [=]() {
  913. if (!weakThis)
  914. return;
  915. weakThis->cloneAction(true);
  916. }));
  917. // Delete
  918. menu->addChild(createMenuItem("Delete", "Backspace/Delete", [=]() {
  919. if (!weakThis)
  920. return;
  921. weakThis->removeAction();
  922. }, false, true));
  923. appendContextMenu(menu);
  924. }
  925. math::Vec ModuleWidget::getGridPosition() {
  926. return ((getPosition() - RACK_OFFSET) / RACK_GRID_SIZE).round();
  927. }
  928. void ModuleWidget::setGridPosition(math::Vec pos) {
  929. setPosition(pos * RACK_GRID_SIZE + RACK_OFFSET);
  930. }
  931. math::Vec ModuleWidget::getGridSize() {
  932. return (getSize() / RACK_GRID_SIZE).round();
  933. }
  934. math::Rect ModuleWidget::getGridBox() {
  935. return math::Rect(getGridPosition(), getGridSize());
  936. }
  937. math::Vec& ModuleWidget::dragOffset() {
  938. return internal->dragOffset;
  939. }
  940. bool& ModuleWidget::dragEnabled() {
  941. return internal->dragEnabled;
  942. }
  943. engine::Module* ModuleWidget::releaseModule() {
  944. engine::Module* module = this->module;
  945. this->module = NULL;
  946. return module;
  947. }
  948. } // namespace app
  949. } // namespace rack