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.

150 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 : Widget {
  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 onButton(const event::Button &e) override {
  30. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  31. e.consume(this);
  32. }
  33. }
  34. void onDragStart(const event::DragStart &e) override {
  35. dragX = APP->scene->rackWidget->mousePos.x;
  36. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  37. originalBox = m->box;
  38. e.consume(this);
  39. }
  40. void onDragMove(const event::DragMove &e) override {
  41. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  42. float newDragX = APP->scene->rackWidget->mousePos.x;
  43. float deltaX = newDragX - dragX;
  44. Rect newBox = originalBox;
  45. const float minWidth = 3 * RACK_GRID_WIDTH;
  46. if (right) {
  47. newBox.size.x += deltaX;
  48. newBox.size.x = fmaxf(newBox.size.x, minWidth);
  49. newBox.size.x = roundf(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  50. }
  51. else {
  52. newBox.size.x -= deltaX;
  53. newBox.size.x = fmaxf(newBox.size.x, minWidth);
  54. newBox.size.x = roundf(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  55. newBox.pos.x = originalBox.pos.x + originalBox.size.x - newBox.size.x;
  56. }
  57. APP->scene->rackWidget->requestModuleBox(m, newBox);
  58. }
  59. void draw(const DrawArgs &args) override {
  60. for (float x = 5.0; x <= 10.0; x += 5.0) {
  61. nvgBeginPath(args.vg);
  62. const float margin = 5.0;
  63. nvgMoveTo(args.vg, x + 0.5, margin + 0.5);
  64. nvgLineTo(args.vg, x + 0.5, box.size.y - margin + 0.5);
  65. nvgStrokeWidth(args.vg, 1.0);
  66. nvgStrokeColor(args.vg, nvgRGBAf(0.5, 0.5, 0.5, 0.5));
  67. nvgStroke(args.vg);
  68. }
  69. }
  70. };
  71. struct BlankWidget : ModuleWidget {
  72. Widget *topRightScrew;
  73. Widget *bottomRightScrew;
  74. Widget *rightHandle;
  75. BlankWidget(Module *module) {
  76. setModule(module);
  77. box.size = Vec(RACK_GRID_WIDTH * 10, RACK_GRID_HEIGHT);
  78. panel = new BlankPanel;
  79. addChild(panel);
  80. ModuleResizeHandle *leftHandle = new ModuleResizeHandle;
  81. ModuleResizeHandle *rightHandle = new ModuleResizeHandle;
  82. rightHandle->right = true;
  83. this->rightHandle = rightHandle;
  84. addChild(leftHandle);
  85. addChild(rightHandle);
  86. addChild(createWidget<ScrewSilver>(Vec(15, 0)));
  87. addChild(createWidget<ScrewSilver>(Vec(15, 365)));
  88. topRightScrew = createWidget<ScrewSilver>(Vec(box.size.x - 30, 0));
  89. bottomRightScrew = createWidget<ScrewSilver>(Vec(box.size.x - 30, 365));
  90. addChild(topRightScrew);
  91. addChild(bottomRightScrew);
  92. }
  93. void step() override {
  94. panel->box.size = box.size;
  95. topRightScrew->box.pos.x = box.size.x - 30;
  96. bottomRightScrew->box.pos.x = box.size.x - 30;
  97. if (box.size.x < RACK_GRID_WIDTH * 6) {
  98. topRightScrew->visible = bottomRightScrew->visible = false;
  99. }
  100. else {
  101. topRightScrew->visible = bottomRightScrew->visible = true;
  102. }
  103. rightHandle->box.pos.x = box.size.x - rightHandle->box.size.x;
  104. ModuleWidget::step();
  105. }
  106. json_t *toJson() override {
  107. json_t *rootJ = ModuleWidget::toJson();
  108. // width
  109. json_object_set_new(rootJ, "width", json_real(box.size.x));
  110. return rootJ;
  111. }
  112. void fromJson(json_t *rootJ) override {
  113. ModuleWidget::fromJson(rootJ);
  114. // width
  115. json_t *widthJ = json_object_get(rootJ, "width");
  116. if (widthJ)
  117. box.size.x = json_number_value(widthJ);
  118. }
  119. };
  120. Model *modelBlank = createModel<Module, BlankWidget>("Blank");