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.

124 lines
3.3KB

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