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.

142 lines
3.6KB

  1. #include "Core.hpp"
  2. #include "app.hpp"
  3. using namespace rack;
  4. struct BlankPanel : virtual 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. }
  13. void draw(NVGcontext *vg) override {
  14. nvgBeginPath(vg);
  15. nvgRect(vg, 0.0, 0.0, box.size.x, box.size.y);
  16. nvgFillColor(vg, nvgRGB(0xe6, 0xe6, 0xe6));
  17. nvgFill(vg);
  18. Widget::draw(vg);
  19. }
  20. };
  21. struct ModuleResizeHandle : virtual Widget {
  22. bool right = false;
  23. float dragX;
  24. Rect originalBox;
  25. ModuleResizeHandle() {
  26. box.size = Vec(RACK_GRID_WIDTH * 1, RACK_GRID_HEIGHT);
  27. }
  28. void onButton(const event::Button &e) override {
  29. if (e.button == GLFW_MOUSE_BUTTON_LEFT) {
  30. e.consume(this);
  31. }
  32. }
  33. void onDragStart(const event::DragStart &e) override {
  34. dragX = app()->scene->rackWidget->lastMousePos.x;
  35. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  36. originalBox = m->box;
  37. }
  38. void onDragMove(const event::DragMove &e) override {
  39. ModuleWidget *m = getAncestorOfType<ModuleWidget>();
  40. float newDragX = app()->scene->rackWidget->lastMousePos.x;
  41. float deltaX = newDragX - dragX;
  42. Rect newBox = originalBox;
  43. const float minWidth = 3 * RACK_GRID_WIDTH;
  44. if (right) {
  45. newBox.size.x += deltaX;
  46. newBox.size.x = fmaxf(newBox.size.x, minWidth);
  47. newBox.size.x = roundf(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  48. }
  49. else {
  50. newBox.size.x -= deltaX;
  51. newBox.size.x = fmaxf(newBox.size.x, minWidth);
  52. newBox.size.x = roundf(newBox.size.x / RACK_GRID_WIDTH) * RACK_GRID_WIDTH;
  53. newBox.pos.x = originalBox.pos.x + originalBox.size.x - newBox.size.x;
  54. }
  55. app()->scene->rackWidget->requestModuleBox(m, newBox);
  56. }
  57. void draw(NVGcontext *vg) override {
  58. for (float x = 5.0; x <= 10.0; x += 5.0) {
  59. nvgBeginPath(vg);
  60. const float margin = 5.0;
  61. nvgMoveTo(vg, x + 0.5, margin + 0.5);
  62. nvgLineTo(vg, x + 0.5, box.size.y - margin + 0.5);
  63. nvgStrokeWidth(vg, 1.0);
  64. nvgStrokeColor(vg, nvgRGBAf(0.5, 0.5, 0.5, 0.5));
  65. nvgStroke(vg);
  66. }
  67. }
  68. };
  69. struct BlankWidget : ModuleWidget {
  70. Widget *topRightScrew;
  71. Widget *bottomRightScrew;
  72. Widget *rightHandle;
  73. BlankWidget(Module *module) : ModuleWidget(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");