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.

376 lines
8.7KB

  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. // CPU meter
  166. if (gCpuMeters && module) {
  167. float p = clamp(module->cpuTime, 0.f, 1.f);
  168. nvgBeginPath(vg);
  169. nvgRect(vg,
  170. 0, (1.f - p) * box.size.y,
  171. 5, p * box.size.y);
  172. nvgFillColor(vg, nvgRGBAf(1, 0, 0, 1.0));
  173. nvgFill(vg);
  174. }
  175. nvgResetScissor(vg);
  176. }
  177. void ModuleWidget::drawShadow(NVGcontext *vg) {
  178. nvgBeginPath(vg);
  179. float r = 20; // Blur radius
  180. float c = 20; // Corner radius
  181. Vec b = Vec(-10, 30); // Offset from each corner
  182. nvgRect(vg, b.x - r, b.y - r, box.size.x - 2*b.x + 2*r, box.size.y - 2*b.y + 2*r);
  183. NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
  184. NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
  185. nvgFillPaint(vg, nvgBoxGradient(vg, b.x, b.y, box.size.x - 2*b.x, box.size.y - 2*b.y, c, r, shadowColor, transparentColor));
  186. nvgFill(vg);
  187. }
  188. void ModuleWidget::onMouseDown(EventMouseDown &e) {
  189. Widget::onMouseDown(e);
  190. if (e.consumed)
  191. return;
  192. if (e.button == 1) {
  193. createContextMenu();
  194. }
  195. e.consumed = true;
  196. e.target = this;
  197. }
  198. void ModuleWidget::onMouseMove(EventMouseMove &e) {
  199. OpaqueWidget::onMouseMove(e);
  200. // Don't delete the ModuleWidget if a TextField is focused
  201. if (!gFocusedWidget) {
  202. // 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.
  203. if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
  204. if (!windowIsModPressed() && !windowIsShiftPressed()) {
  205. gRackWidget->deleteModule(this);
  206. this->finalizeEvents();
  207. delete this;
  208. e.consumed = true;
  209. return;
  210. }
  211. }
  212. }
  213. }
  214. void ModuleWidget::onHoverKey(EventHoverKey &e) {
  215. switch (e.key) {
  216. case GLFW_KEY_I: {
  217. if (windowIsModPressed() && !windowIsShiftPressed()) {
  218. reset();
  219. e.consumed = true;
  220. return;
  221. }
  222. } break;
  223. case GLFW_KEY_R: {
  224. if (windowIsModPressed() && !windowIsShiftPressed()) {
  225. randomize();
  226. e.consumed = true;
  227. return;
  228. }
  229. } break;
  230. case GLFW_KEY_D: {
  231. if (windowIsModPressed() && !windowIsShiftPressed()) {
  232. gRackWidget->cloneModule(this);
  233. e.consumed = true;
  234. return;
  235. }
  236. } break;
  237. case GLFW_KEY_U: {
  238. if (windowIsModPressed() && !windowIsShiftPressed()) {
  239. disconnect();
  240. e.consumed = true;
  241. return;
  242. }
  243. } break;
  244. }
  245. Widget::onHoverKey(e);
  246. }
  247. void ModuleWidget::onDragStart(EventDragStart &e) {
  248. dragPos = gRackWidget->lastMousePos.minus(box.pos);
  249. }
  250. void ModuleWidget::onDragEnd(EventDragEnd &e) {
  251. }
  252. void ModuleWidget::onDragMove(EventDragMove &e) {
  253. Rect newBox = box;
  254. newBox.pos = gRackWidget->lastMousePos.minus(dragPos);
  255. gRackWidget->requestModuleBoxNearest(this, newBox);
  256. }
  257. struct DisconnectMenuItem : MenuItem {
  258. ModuleWidget *moduleWidget;
  259. void onAction(EventAction &e) override {
  260. moduleWidget->disconnect();
  261. }
  262. };
  263. struct ResetMenuItem : MenuItem {
  264. ModuleWidget *moduleWidget;
  265. void onAction(EventAction &e) override {
  266. moduleWidget->reset();
  267. }
  268. };
  269. struct RandomizeMenuItem : MenuItem {
  270. ModuleWidget *moduleWidget;
  271. void onAction(EventAction &e) override {
  272. moduleWidget->randomize();
  273. }
  274. };
  275. struct CloneMenuItem : MenuItem {
  276. ModuleWidget *moduleWidget;
  277. void onAction(EventAction &e) override {
  278. gRackWidget->cloneModule(moduleWidget);
  279. }
  280. };
  281. struct DeleteMenuItem : MenuItem {
  282. ModuleWidget *moduleWidget;
  283. void onAction(EventAction &e) override {
  284. gRackWidget->deleteModule(moduleWidget);
  285. moduleWidget->finalizeEvents();
  286. delete moduleWidget;
  287. }
  288. };
  289. Menu *ModuleWidget::createContextMenu() {
  290. Menu *menu = gScene->createMenu();
  291. MenuLabel *menuLabel = new MenuLabel();
  292. menuLabel->text = model->author + " " + model->name + " " + model->plugin->version;
  293. menu->addChild(menuLabel);
  294. ResetMenuItem *resetItem = new ResetMenuItem();
  295. resetItem->text = "Initialize";
  296. resetItem->rightText = WINDOW_MOD_KEY_NAME "+I";
  297. resetItem->moduleWidget = this;
  298. menu->addChild(resetItem);
  299. RandomizeMenuItem *randomizeItem = new RandomizeMenuItem();
  300. randomizeItem->text = "Randomize";
  301. randomizeItem->rightText = WINDOW_MOD_KEY_NAME "+R";
  302. randomizeItem->moduleWidget = this;
  303. menu->addChild(randomizeItem);
  304. DisconnectMenuItem *disconnectItem = new DisconnectMenuItem();
  305. disconnectItem->text = "Disconnect cables";
  306. disconnectItem->rightText = WINDOW_MOD_KEY_NAME "+U";
  307. disconnectItem->moduleWidget = this;
  308. menu->addChild(disconnectItem);
  309. CloneMenuItem *cloneItem = new CloneMenuItem();
  310. cloneItem->text = "Duplicate";
  311. cloneItem->rightText = WINDOW_MOD_KEY_NAME "+D";
  312. cloneItem->moduleWidget = this;
  313. menu->addChild(cloneItem);
  314. DeleteMenuItem *deleteItem = new DeleteMenuItem();
  315. deleteItem->text = "Delete";
  316. deleteItem->rightText = "Backspace/Delete";
  317. deleteItem->moduleWidget = this;
  318. menu->addChild(deleteItem);
  319. appendContextMenu(menu);
  320. return menu;
  321. }
  322. } // namespace rack