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.

161 lines
3.9KB

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