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.

147 lines
3.7KB

  1. #include "plugin.hpp"
  2. #include "app.hpp"
  3. using namespace rack;
  4. struct BlankPanel : Widget {
  5. Widget *panelBorder;
  6. BlankPanel() {
  7. panelBorder = new PanelBorder;
  8. addChild(panelBorder);
  9. }
  10. void step() override {
  11. panelBorder->box.size = box.size;
  12. Widget::step();
  13. }
  14. void draw(const DrawArgs &args) override {
  15. nvgBeginPath(args.vg);
  16. nvgRect(args.vg, 0.0, 0.0, box.size.x, box.size.y);
  17. nvgFillColor(args.vg, nvgRGB(0xe6, 0xe6, 0xe6));
  18. nvgFill(args.vg);
  19. Widget::draw(args);
  20. }
  21. };
  22. struct ModuleResizeHandle : OpaqueWidget {
  23. bool right = false;
  24. float dragX;
  25. Rect originalBox;
  26. ModuleResizeHandle() {
  27. box.size = Vec(RACK_GRID_WIDTH * 1, RACK_GRID_HEIGHT);
  28. }
  29. void onDragStart(const event::DragStart &e) override {
  30. if (e.button != GLFW_MOUSE_BUTTON_LEFT)
  31. return;
  32. dragX = APP->scene->rack->mousePos.x;
  33. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  34. originalBox = m->box;
  35. e.consume(this);
  36. }
  37. void onDragMove(const event::DragMove &e) override {
  38. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  39. float newDragX = APP->scene->rack->mousePos.x;
  40. float deltaX = newDragX - dragX;
  41. Rect newBox = originalBox;
  42. const float minWidth = 3 * RACK_GRID_WIDTH;
  43. if (right) {
  44. newBox.size.x += deltaX;
  45. newBox.size.x = std::fmax(newBox.size.x, minWidth);
  46. newBox.size.x = std::round(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  47. }
  48. else {
  49. newBox.size.x -= deltaX;
  50. newBox.size.x = std::fmax(newBox.size.x, minWidth);
  51. newBox.size.x = std::round(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  52. newBox.pos.x = originalBox.pos.x + originalBox.size.x - newBox.size.x;
  53. }
  54. APP->scene->rack->requestModuleBox(m, newBox);
  55. }
  56. void draw(const DrawArgs &args) override {
  57. for (float x = 5.0; x <= 10.0; x += 5.0) {
  58. nvgBeginPath(args.vg);
  59. const float margin = 5.0;
  60. nvgMoveTo(args.vg, x + 0.5, margin + 0.5);
  61. nvgLineTo(args.vg, x + 0.5, box.size.y - margin + 0.5);
  62. nvgStrokeWidth(args.vg, 1.0);
  63. nvgStrokeColor(args.vg, nvgRGBAf(0.5, 0.5, 0.5, 0.5));
  64. nvgStroke(args.vg);
  65. }
  66. }
  67. };
  68. struct BlankWidget : ModuleWidget {
  69. Widget *topRightScrew;
  70. Widget *bottomRightScrew;
  71. Widget *rightHandle;
  72. BlankWidget(Module *module) {
  73. setModule(module);
  74. box.size = Vec(RACK_GRID_WIDTH * 10, RACK_GRID_HEIGHT);
  75. panel = new BlankPanel;
  76. addChild(panel);
  77. ModuleResizeHandle *leftHandle = new ModuleResizeHandle;
  78. ModuleResizeHandle *rightHandle = new ModuleResizeHandle;
  79. rightHandle->right = true;
  80. this->rightHandle = rightHandle;
  81. addChild(leftHandle);
  82. addChild(rightHandle);
  83. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  84. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  85. topRightScrew = createWidget<ScrewSilver>(Vec(box.size.x - 30, 0));
  86. bottomRightScrew = createWidget<ScrewSilver>(Vec(box.size.x - 30, 365));
  87. addChild(topRightScrew);
  88. addChild(bottomRightScrew);
  89. }
  90. void step() override {
  91. panel->box.size = box.size;
  92. topRightScrew->box.pos.x = box.size.x - 30;
  93. bottomRightScrew->box.pos.x = box.size.x - 30;
  94. if (box.size.x < RACK_GRID_WIDTH * 6) {
  95. topRightScrew->visible = bottomRightScrew->visible = false;
  96. }
  97. else {
  98. topRightScrew->visible = bottomRightScrew->visible = true;
  99. }
  100. rightHandle->box.pos.x = box.size.x - rightHandle->box.size.x;
  101. ModuleWidget::step();
  102. }
  103. json_t *toJson() override {
  104. json_t *rootJ = ModuleWidget::toJson();
  105. // width
  106. json_object_set_new(rootJ, "width", json_real(box.size.x));
  107. return rootJ;
  108. }
  109. void fromJson(json_t *rootJ) override {
  110. ModuleWidget::fromJson(rootJ);
  111. // width
  112. json_t *widthJ = json_object_get(rootJ, "width");
  113. if (widthJ)
  114. box.size.x = json_number_value(widthJ);
  115. }
  116. };
  117. Model *modelBlank = createModel<Module, BlankWidget>("Blank");