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.

365 lines
8.4KB

  1. #include "app.hpp"
  2. #include "engine.hpp"
  3. #include "plugin.hpp"
  4. #include "window.hpp"
  5. namespace rack {
  6. ModuleWidget::ModuleWidget(Module *module) {
  7. if (module) {
  8. engineAddModule(module);
  9. }
  10. this->module = module;
  11. }
  12. ModuleWidget::~ModuleWidget() {
  13. // Make sure WireWidget destructors are called *before* removing `module` from the rack.
  14. disconnect();
  15. // Remove and delete the Module instance
  16. if (module) {
  17. engineRemoveModule(module);
  18. delete module;
  19. module = NULL;
  20. }
  21. }
  22. void ModuleWidget::addInput(Port *input) {
  23. assert(input->type == Port::INPUT);
  24. inputs.push_back(input);
  25. addChild(input);
  26. }
  27. void ModuleWidget::addOutput(Port *output) {
  28. assert(output->type == Port::OUTPUT);
  29. outputs.push_back(output);
  30. addChild(output);
  31. }
  32. void ModuleWidget::addParam(ParamWidget *param) {
  33. params.push_back(param);
  34. addChild(param);
  35. }
  36. void ModuleWidget::setPanel(std::shared_ptr<SVG> svg) {
  37. // Remove old panel
  38. if (panel) {
  39. removeChild(panel);
  40. panel = NULL;
  41. }
  42. panel = new SVGPanel();
  43. panel->setBackground(svg);
  44. addChild(panel);
  45. box.size = panel->box.size;
  46. }
  47. json_t *ModuleWidget::toJson() {
  48. json_t *rootJ = json_object();
  49. // plugin
  50. json_object_set_new(rootJ, "plugin", json_string(model->plugin->slug.c_str()));
  51. // version (of plugin)
  52. if (!model->plugin->version.empty())
  53. json_object_set_new(rootJ, "version", json_string(model->plugin->version.c_str()));
  54. // model
  55. json_object_set_new(rootJ, "model", json_string(model->slug.c_str()));
  56. // pos
  57. Vec pos = box.pos.div(RACK_GRID_SIZE).round();
  58. json_t *posJ = json_pack("[i, i]", (int) pos.x, (int) pos.y);
  59. json_object_set_new(rootJ, "pos", posJ);
  60. // params
  61. json_t *paramsJ = json_array();
  62. for (ParamWidget *paramWidget : params) {
  63. json_t *paramJ = paramWidget->toJson();
  64. json_array_append_new(paramsJ, paramJ);
  65. }
  66. json_object_set_new(rootJ, "params", paramsJ);
  67. // data
  68. if (module) {
  69. json_t *dataJ = module->toJson();
  70. if (dataJ) {
  71. json_object_set_new(rootJ, "data", dataJ);
  72. }
  73. }
  74. return rootJ;
  75. }
  76. void ModuleWidget::fromJson(json_t *rootJ) {
  77. // legacy
  78. int legacy = 0;
  79. json_t *legacyJ = json_object_get(rootJ, "legacy");
  80. if (legacyJ)
  81. legacy = json_integer_value(legacyJ);
  82. // pos
  83. json_t *posJ = json_object_get(rootJ, "pos");
  84. double x, y;
  85. json_unpack(posJ, "[F, F]", &x, &y);
  86. Vec pos = Vec(x, y);
  87. if (legacy && legacy <= 1) {
  88. box.pos = pos;
  89. }
  90. else {
  91. box.pos = pos.mult(RACK_GRID_SIZE);
  92. }
  93. // params
  94. json_t *paramsJ = json_object_get(rootJ, "params");
  95. size_t i;
  96. json_t *paramJ;
  97. json_array_foreach(paramsJ, i, paramJ) {
  98. if (legacy && legacy <= 1) {
  99. // Legacy 1 mode
  100. // The index in the array we're iterating is the index of the ParamWidget in the params vector.
  101. if (i < params.size()) {
  102. // Create upgraded version of param JSON object
  103. json_t *newParamJ = json_object();
  104. json_object_set(newParamJ, "value", paramJ);
  105. params[i]->fromJson(newParamJ);
  106. json_decref(newParamJ);
  107. }
  108. }
  109. else {
  110. // Get paramId
  111. json_t *paramIdJ = json_object_get(paramJ, "paramId");
  112. if (!paramIdJ)
  113. continue;
  114. int paramId = json_integer_value(paramIdJ);
  115. // Find ParamWidget(s) with paramId
  116. for (ParamWidget *paramWidget : params) {
  117. if (paramWidget->paramId == paramId)
  118. paramWidget->fromJson(paramJ);
  119. }
  120. }
  121. }
  122. // data
  123. json_t *dataJ = json_object_get(rootJ, "data");
  124. if (dataJ && module) {
  125. module->fromJson(dataJ);
  126. }
  127. }
  128. void ModuleWidget::disconnect() {
  129. for (Port *input : inputs) {
  130. gRackWidget->wireContainer->removeAllWires(input);
  131. }
  132. for (Port *output : outputs) {
  133. gRackWidget->wireContainer->removeAllWires(output);
  134. }
  135. }
  136. void ModuleWidget::create() {
  137. if (module) {
  138. module->onCreate();
  139. }
  140. }
  141. void ModuleWidget::_delete() {
  142. if (module) {
  143. module->onDelete();
  144. }
  145. }
  146. void ModuleWidget::reset() {
  147. for (ParamWidget *param : params) {
  148. param->reset();
  149. }
  150. if (module) {
  151. module->onReset();
  152. }
  153. }
  154. void ModuleWidget::randomize() {
  155. for (ParamWidget *param : params) {
  156. param->randomize();
  157. }
  158. if (module) {
  159. module->onRandomize();
  160. }
  161. }
  162. void ModuleWidget::draw(NVGcontext *vg) {
  163. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  164. Widget::draw(vg);
  165. nvgResetScissor(vg);
  166. }
  167. void ModuleWidget::drawShadow(NVGcontext *vg) {
  168. nvgBeginPath(vg);
  169. float r = 20; // Blur radius
  170. float c = 20; // Corner radius
  171. Vec b = Vec(-10, 30); // Offset from each corner
  172. nvgRect(vg, b.x - r, b.y - r, box.size.x - 2*b.x + 2*r, box.size.y - 2*b.y + 2*r);
  173. NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
  174. NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
  175. nvgFillPaint(vg, nvgBoxGradient(vg, b.x, b.y, box.size.x - 2*b.x, box.size.y - 2*b.y, c, r, shadowColor, transparentColor));
  176. nvgFill(vg);
  177. }
  178. void ModuleWidget::onMouseDown(EventMouseDown &e) {
  179. Widget::onMouseDown(e);
  180. if (e.consumed)
  181. return;
  182. if (e.button == 1) {
  183. createContextMenu();
  184. }
  185. e.consumed = true;
  186. e.target = this;
  187. }
  188. void ModuleWidget::onMouseMove(EventMouseMove &e) {
  189. OpaqueWidget::onMouseMove(e);
  190. // Don't delete the ModuleWidget if a TextField is focused
  191. if (!gFocusedWidget) {
  192. // Instead of checking key-down events, delete the module even if key-repeat hasn't fired yet and the cursor is hovering over the widget.
  193. if (windowIsKeyPressed(GLFW_KEY_DELETE) || windowIsKeyPressed(GLFW_KEY_BACKSPACE)) {
  194. if (!windowIsModPressed() && !windowIsShiftPressed()) {
  195. gRackWidget->deleteModule(this);
  196. this->finalizeEvents();
  197. delete this;
  198. e.consumed = true;
  199. return;
  200. }
  201. }
  202. }
  203. }
  204. void ModuleWidget::onHoverKey(EventHoverKey &e) {
  205. switch (e.key) {
  206. case GLFW_KEY_I: {
  207. if (windowIsModPressed() && !windowIsShiftPressed()) {
  208. reset();
  209. e.consumed = true;
  210. return;
  211. }
  212. } break;
  213. case GLFW_KEY_R: {
  214. if (windowIsModPressed() && !windowIsShiftPressed()) {
  215. randomize();
  216. e.consumed = true;
  217. return;
  218. }
  219. } break;
  220. case GLFW_KEY_D: {
  221. if (windowIsModPressed() && !windowIsShiftPressed()) {
  222. gRackWidget->cloneModule(this);
  223. e.consumed = true;
  224. return;
  225. }
  226. } break;
  227. case GLFW_KEY_U: {
  228. if (windowIsModPressed() && !windowIsShiftPressed()) {
  229. disconnect();
  230. e.consumed = true;
  231. return;
  232. }
  233. } break;
  234. }
  235. Widget::onHoverKey(e);
  236. }
  237. void ModuleWidget::onDragStart(EventDragStart &e) {
  238. dragPos = gRackWidget->lastMousePos.minus(box.pos);
  239. }
  240. void ModuleWidget::onDragEnd(EventDragEnd &e) {
  241. }
  242. void ModuleWidget::onDragMove(EventDragMove &e) {
  243. Rect newBox = box;
  244. newBox.pos = gRackWidget->lastMousePos.minus(dragPos);
  245. gRackWidget->requestModuleBoxNearest(this, newBox);
  246. }
  247. struct DisconnectMenuItem : MenuItem {
  248. ModuleWidget *moduleWidget;
  249. void onAction(EventAction &e) override {
  250. moduleWidget->disconnect();
  251. }
  252. };
  253. struct ResetMenuItem : MenuItem {
  254. ModuleWidget *moduleWidget;
  255. void onAction(EventAction &e) override {
  256. moduleWidget->reset();
  257. }
  258. };
  259. struct RandomizeMenuItem : MenuItem {
  260. ModuleWidget *moduleWidget;
  261. void onAction(EventAction &e) override {
  262. moduleWidget->randomize();
  263. }
  264. };
  265. struct CloneMenuItem : MenuItem {
  266. ModuleWidget *moduleWidget;
  267. void onAction(EventAction &e) override {
  268. gRackWidget->cloneModule(moduleWidget);
  269. }
  270. };
  271. struct DeleteMenuItem : MenuItem {
  272. ModuleWidget *moduleWidget;
  273. void onAction(EventAction &e) override {
  274. gRackWidget->deleteModule(moduleWidget);
  275. moduleWidget->finalizeEvents();
  276. delete moduleWidget;
  277. }
  278. };
  279. Menu *ModuleWidget::createContextMenu() {
  280. Menu *menu = gScene->createMenu();
  281. MenuLabel *menuLabel = new MenuLabel();
  282. menuLabel->text = model->author + " " + model->name;
  283. menu->addChild(menuLabel);
  284. ResetMenuItem *resetItem = new ResetMenuItem();
  285. resetItem->text = "Initialize";
  286. resetItem->rightText = WINDOW_MOD_KEY_NAME "+I";
  287. resetItem->moduleWidget = this;
  288. menu->addChild(resetItem);
  289. RandomizeMenuItem *randomizeItem = new RandomizeMenuItem();
  290. randomizeItem->text = "Randomize";
  291. randomizeItem->rightText = WINDOW_MOD_KEY_NAME "+R";
  292. randomizeItem->moduleWidget = this;
  293. menu->addChild(randomizeItem);
  294. DisconnectMenuItem *disconnectItem = new DisconnectMenuItem();
  295. disconnectItem->text = "Disconnect cables";
  296. disconnectItem->rightText = WINDOW_MOD_KEY_NAME "+U";
  297. disconnectItem->moduleWidget = this;
  298. menu->addChild(disconnectItem);
  299. CloneMenuItem *cloneItem = new CloneMenuItem();
  300. cloneItem->text = "Duplicate";
  301. cloneItem->rightText = WINDOW_MOD_KEY_NAME "+D";
  302. cloneItem->moduleWidget = this;
  303. menu->addChild(cloneItem);
  304. DeleteMenuItem *deleteItem = new DeleteMenuItem();
  305. deleteItem->text = "Delete";
  306. deleteItem->rightText = "Backspace/Delete";
  307. deleteItem->moduleWidget = this;
  308. menu->addChild(deleteItem);
  309. appendContextMenu(menu);
  310. return menu;
  311. }
  312. } // namespace rack