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.

390 lines
9.0KB

  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. delete panel;
  41. panel = NULL;
  42. }
  43. panel = new SVGPanel();
  44. panel->setBackground(svg);
  45. addChild(panel);
  46. box.size = panel->box.size;
  47. }
  48. json_t *ModuleWidget::toJson() {
  49. json_t *rootJ = json_object();
  50. // plugin
  51. json_object_set_new(rootJ, "plugin", json_string(model->plugin->slug.c_str()));
  52. // version (of plugin)
  53. if (!model->plugin->version.empty())
  54. json_object_set_new(rootJ, "version", json_string(model->plugin->version.c_str()));
  55. // model
  56. json_object_set_new(rootJ, "model", json_string(model->slug.c_str()));
  57. // pos
  58. Vec pos = box.pos.div(RACK_GRID_SIZE).round();
  59. json_t *posJ = json_pack("[i, i]", (int) pos.x, (int) pos.y);
  60. json_object_set_new(rootJ, "pos", posJ);
  61. // params
  62. json_t *paramsJ = json_array();
  63. for (ParamWidget *paramWidget : params) {
  64. json_t *paramJ = paramWidget->toJson();
  65. json_array_append_new(paramsJ, paramJ);
  66. }
  67. json_object_set_new(rootJ, "params", paramsJ);
  68. // data
  69. if (module) {
  70. json_t *dataJ = module->toJson();
  71. if (dataJ) {
  72. json_object_set_new(rootJ, "data", dataJ);
  73. }
  74. }
  75. return rootJ;
  76. }
  77. void ModuleWidget::fromJson(json_t *rootJ) {
  78. // legacy
  79. int legacy = 0;
  80. json_t *legacyJ = json_object_get(rootJ, "legacy");
  81. if (legacyJ)
  82. legacy = json_integer_value(legacyJ);
  83. // pos
  84. json_t *posJ = json_object_get(rootJ, "pos");
  85. double x, y;
  86. json_unpack(posJ, "[F, F]", &x, &y);
  87. Vec pos = Vec(x, y);
  88. if (legacy && legacy <= 1) {
  89. box.pos = pos;
  90. }
  91. else {
  92. box.pos = pos.mult(RACK_GRID_SIZE);
  93. }
  94. // params
  95. json_t *paramsJ = json_object_get(rootJ, "params");
  96. size_t i;
  97. json_t *paramJ;
  98. json_array_foreach(paramsJ, i, paramJ) {
  99. if (legacy && legacy <= 1) {
  100. // Legacy 1 mode
  101. // The index in the array we're iterating is the index of the ParamWidget in the params vector.
  102. if (i < params.size()) {
  103. // Create upgraded version of param JSON object
  104. json_t *newParamJ = json_object();
  105. json_object_set(newParamJ, "value", paramJ);
  106. params[i]->fromJson(newParamJ);
  107. json_decref(newParamJ);
  108. }
  109. }
  110. else {
  111. // Get paramId
  112. json_t *paramIdJ = json_object_get(paramJ, "paramId");
  113. if (!paramIdJ)
  114. continue;
  115. int paramId = json_integer_value(paramIdJ);
  116. // Find ParamWidget(s) with paramId
  117. for (ParamWidget *paramWidget : params) {
  118. if (paramWidget->paramId == paramId)
  119. paramWidget->fromJson(paramJ);
  120. }
  121. }
  122. }
  123. // data
  124. json_t *dataJ = json_object_get(rootJ, "data");
  125. if (dataJ && module) {
  126. module->fromJson(dataJ);
  127. }
  128. }
  129. void ModuleWidget::disconnect() {
  130. for (Port *input : inputs) {
  131. gRackWidget->wireContainer->removeAllWires(input);
  132. }
  133. for (Port *output : outputs) {
  134. gRackWidget->wireContainer->removeAllWires(output);
  135. }
  136. }
  137. void ModuleWidget::create() {
  138. if (module) {
  139. module->onCreate();
  140. }
  141. }
  142. void ModuleWidget::_delete() {
  143. if (module) {
  144. module->onDelete();
  145. }
  146. }
  147. void ModuleWidget::reset() {
  148. for (ParamWidget *param : params) {
  149. param->reset();
  150. }
  151. if (module) {
  152. module->onReset();
  153. }
  154. }
  155. void ModuleWidget::randomize() {
  156. for (ParamWidget *param : params) {
  157. param->randomize();
  158. }
  159. if (module) {
  160. module->onRandomize();
  161. }
  162. }
  163. void ModuleWidget::draw(NVGcontext *vg) {
  164. nvgScissor(vg, 0, 0, box.size.x, box.size.y);
  165. Widget::draw(vg);
  166. // CPU meter
  167. if (module && gPowerMeter) {
  168. nvgBeginPath(vg);
  169. nvgRect(vg,
  170. 0, box.size.y - 20,
  171. 55, 20);
  172. nvgFillColor(vg, nvgRGBAf(0, 0, 0, 0.5));
  173. nvgFill(vg);
  174. std::string cpuText = stringf("%.0f mS", module->cpuTime * 1000.f);
  175. nvgFontFaceId(vg, gGuiFont->handle);
  176. nvgFontSize(vg, 12);
  177. nvgFillColor(vg, nvgRGBf(1, 1, 1));
  178. nvgText(vg, 10.0, box.size.y - 6.0, cpuText.c_str(), NULL);
  179. float p = clamp(module->cpuTime, 0.f, 1.f);
  180. nvgBeginPath(vg);
  181. nvgRect(vg,
  182. 0, (1.f - p) * box.size.y,
  183. 5, p * box.size.y);
  184. nvgFillColor(vg, nvgRGBAf(1, 0, 0, 1.0));
  185. nvgFill(vg);
  186. }
  187. nvgResetScissor(vg);
  188. }
  189. void ModuleWidget::drawShadow(NVGcontext *vg) {
  190. nvgBeginPath(vg);
  191. float r = 20; // Blur radius
  192. float c = 20; // Corner radius
  193. Vec b = Vec(-10, 30); // Offset from each corner
  194. nvgRect(vg, b.x - r, b.y - r, box.size.x - 2*b.x + 2*r, box.size.y - 2*b.y + 2*r);
  195. NVGcolor shadowColor = nvgRGBAf(0, 0, 0, 0.2);
  196. NVGcolor transparentColor = nvgRGBAf(0, 0, 0, 0);
  197. nvgFillPaint(vg, nvgBoxGradient(vg, b.x, b.y, box.size.x - 2*b.x, box.size.y - 2*b.y, c, r, shadowColor, transparentColor));
  198. nvgFill(vg);
  199. }
  200. void ModuleWidget::onMouseDown(EventMouseDown &e) {
  201. Widget::onMouseDown(e);
  202. if (e.consumed)
  203. return;
  204. if (e.button == 1) {
  205. createContextMenu();
  206. }
  207. e.consumed = true;
  208. e.target = this;
  209. }
  210. void ModuleWidget::onMouseMove(EventMouseMove &e) {
  211. OpaqueWidget::onMouseMove(e);
  212. // Don't delete the ModuleWidget if a TextField is focused
  213. if (!gFocusedWidget) {
  214. // 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.
  215. if (glfwGetKey(gWindow, GLFW_KEY_DELETE) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_BACKSPACE) == GLFW_PRESS) {
  216. if (!windowIsModPressed() && !windowIsShiftPressed()) {
  217. gRackWidget->deleteModule(this);
  218. this->finalizeEvents();
  219. delete this;
  220. e.consumed = true;
  221. return;
  222. }
  223. }
  224. }
  225. }
  226. void ModuleWidget::onHoverKey(EventHoverKey &e) {
  227. switch (e.key) {
  228. case GLFW_KEY_I: {
  229. if (windowIsModPressed() && !windowIsShiftPressed()) {
  230. reset();
  231. e.consumed = true;
  232. return;
  233. }
  234. } break;
  235. case GLFW_KEY_R: {
  236. if (windowIsModPressed() && !windowIsShiftPressed()) {
  237. randomize();
  238. e.consumed = true;
  239. return;
  240. }
  241. } break;
  242. case GLFW_KEY_D: {
  243. if (windowIsModPressed() && !windowIsShiftPressed()) {
  244. gRackWidget->cloneModule(this);
  245. e.consumed = true;
  246. return;
  247. }
  248. } break;
  249. case GLFW_KEY_U: {
  250. if (windowIsModPressed() && !windowIsShiftPressed()) {
  251. disconnect();
  252. e.consumed = true;
  253. return;
  254. }
  255. } break;
  256. }
  257. Widget::onHoverKey(e);
  258. }
  259. void ModuleWidget::onDragStart(EventDragStart &e) {
  260. dragPos = gRackWidget->lastMousePos.minus(box.pos);
  261. }
  262. void ModuleWidget::onDragEnd(EventDragEnd &e) {
  263. }
  264. void ModuleWidget::onDragMove(EventDragMove &e) {
  265. Rect newBox = box;
  266. newBox.pos = gRackWidget->lastMousePos.minus(dragPos);
  267. gRackWidget->requestModuleBoxNearest(this, newBox);
  268. }
  269. struct DisconnectMenuItem : MenuItem {
  270. ModuleWidget *moduleWidget;
  271. void onAction(EventAction &e) override {
  272. moduleWidget->disconnect();
  273. }
  274. };
  275. struct ResetMenuItem : MenuItem {
  276. ModuleWidget *moduleWidget;
  277. void onAction(EventAction &e) override {
  278. moduleWidget->reset();
  279. }
  280. };
  281. struct RandomizeMenuItem : MenuItem {
  282. ModuleWidget *moduleWidget;
  283. void onAction(EventAction &e) override {
  284. moduleWidget->randomize();
  285. }
  286. };
  287. struct CloneMenuItem : MenuItem {
  288. ModuleWidget *moduleWidget;
  289. void onAction(EventAction &e) override {
  290. gRackWidget->cloneModule(moduleWidget);
  291. }
  292. };
  293. struct DeleteMenuItem : MenuItem {
  294. ModuleWidget *moduleWidget;
  295. void onAction(EventAction &e) override {
  296. gRackWidget->deleteModule(moduleWidget);
  297. moduleWidget->finalizeEvents();
  298. delete moduleWidget;
  299. }
  300. };
  301. Menu *ModuleWidget::createContextMenu() {
  302. Menu *menu = gScene->createMenu();
  303. MenuLabel *menuLabel = new MenuLabel();
  304. menuLabel->text = model->author + " " + model->name + " " + model->plugin->version;
  305. menu->addChild(menuLabel);
  306. ResetMenuItem *resetItem = new ResetMenuItem();
  307. resetItem->text = "Initialize";
  308. resetItem->rightText = WINDOW_MOD_KEY_NAME "+I";
  309. resetItem->moduleWidget = this;
  310. menu->addChild(resetItem);
  311. RandomizeMenuItem *randomizeItem = new RandomizeMenuItem();
  312. randomizeItem->text = "Randomize";
  313. randomizeItem->rightText = WINDOW_MOD_KEY_NAME "+R";
  314. randomizeItem->moduleWidget = this;
  315. menu->addChild(randomizeItem);
  316. DisconnectMenuItem *disconnectItem = new DisconnectMenuItem();
  317. disconnectItem->text = "Disconnect cables";
  318. disconnectItem->rightText = WINDOW_MOD_KEY_NAME "+U";
  319. disconnectItem->moduleWidget = this;
  320. menu->addChild(disconnectItem);
  321. CloneMenuItem *cloneItem = new CloneMenuItem();
  322. cloneItem->text = "Duplicate";
  323. cloneItem->rightText = WINDOW_MOD_KEY_NAME "+D";
  324. cloneItem->moduleWidget = this;
  325. menu->addChild(cloneItem);
  326. DeleteMenuItem *deleteItem = new DeleteMenuItem();
  327. deleteItem->text = "Delete";
  328. deleteItem->rightText = "Backspace/Delete";
  329. deleteItem->moduleWidget = this;
  330. menu->addChild(deleteItem);
  331. appendContextMenu(menu);
  332. return menu;
  333. }
  334. } // namespace rack