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.

110 lines
2.8KB

  1. #include "core.hpp"
  2. using namespace rack;
  3. struct ModuleResizeHandle : Widget {
  4. bool right = false;
  5. float originalWidth;
  6. float totalX;
  7. ModuleResizeHandle() {
  8. box.size = Vec(RACK_GRID_WIDTH * 1, RACK_GRID_HEIGHT);
  9. }
  10. Widget *onMouseDown(Vec pos, int button) override {
  11. if (button == 0)
  12. return this;
  13. return NULL;
  14. }
  15. void onDragStart() override {
  16. assert(parent);
  17. originalWidth = parent->box.size.x;
  18. totalX = 0.0;
  19. }
  20. void onDragMove(Vec mouseRel) override {
  21. ModuleWidget *m = dynamic_cast<ModuleWidget*>(parent);
  22. assert(m);
  23. totalX += mouseRel.x;
  24. float targetWidth = originalWidth;
  25. if (right)
  26. targetWidth += totalX;
  27. else
  28. targetWidth -= totalX;
  29. targetWidth = RACK_GRID_WIDTH * roundf(targetWidth / RACK_GRID_WIDTH);
  30. targetWidth = fmaxf(targetWidth, RACK_GRID_WIDTH * 3);
  31. Rect newBox = m->box;
  32. newBox.size.x = targetWidth;
  33. if (!right) {
  34. newBox.pos.x = m->box.pos.x + m->box.size.x - newBox.size.x;
  35. }
  36. gRackWidget->requestModuleBox(m, newBox);
  37. }
  38. void draw(NVGcontext *vg) override {
  39. for (float x = 5.0; x <= 10.0; x += 5.0) {
  40. nvgBeginPath(vg);
  41. const float margin = 5.0;
  42. nvgMoveTo(vg, x + 0.5, margin + 0.5);
  43. nvgLineTo(vg, x + 0.5, box.size.y - margin + 0.5);
  44. nvgStrokeWidth(vg, 1.0);
  45. nvgStrokeColor(vg, nvgRGBAf(0.5, 0.5, 0.5, 0.5));
  46. nvgStroke(vg);
  47. }
  48. }
  49. };
  50. BlankWidget::BlankWidget() {
  51. box.size = Vec(RACK_GRID_WIDTH * 10, RACK_GRID_HEIGHT);
  52. {
  53. panel = new LightPanel();
  54. panel->box.size = box.size;
  55. addChild(panel);
  56. }
  57. ModuleResizeHandle *leftHandle = new ModuleResizeHandle();
  58. ModuleResizeHandle *rightHandle = new ModuleResizeHandle();
  59. rightHandle->right = true;
  60. this->rightHandle = rightHandle;
  61. addChild(leftHandle);
  62. addChild(rightHandle);
  63. addChild(createScrew<ScrewSilver>(Vec(15, 0)));
  64. addChild(createScrew<ScrewSilver>(Vec(15, 365)));
  65. topRightScrew = createScrew<ScrewSilver>(Vec(box.size.x - 30, 0));
  66. bottomRightScrew = createScrew<ScrewSilver>(Vec(box.size.x - 30, 365));
  67. addChild(topRightScrew);
  68. addChild(bottomRightScrew);
  69. }
  70. void BlankWidget::step() {
  71. panel->box.size = box.size;
  72. topRightScrew->box.pos.x = box.size.x - 30;
  73. bottomRightScrew->box.pos.x = box.size.x - 30;
  74. if (box.size.x < RACK_GRID_WIDTH * 6) {
  75. topRightScrew->visible = bottomRightScrew->visible = false;
  76. }
  77. else {
  78. topRightScrew->visible = bottomRightScrew->visible = true;
  79. }
  80. rightHandle->box.pos.x = box.size.x - rightHandle->box.size.x;
  81. ModuleWidget::step();
  82. }
  83. json_t *BlankWidget::toJson() {
  84. json_t *rootJ = ModuleWidget::toJson();
  85. // // width
  86. json_object_set_new(rootJ, "width", json_real(box.size.x));
  87. return rootJ;
  88. }
  89. void BlankWidget::fromJson(json_t *rootJ) {
  90. ModuleWidget::fromJson(rootJ);
  91. // width
  92. json_t *widthJ = json_object_get(rootJ, "width");
  93. if (widthJ)
  94. box.size.x = json_number_value(widthJ);
  95. }