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.

225 lines
4.2KB

  1. #pragma once
  2. #include "widgets.hpp"
  3. #include <jansson.h>
  4. namespace rack {
  5. struct Module;
  6. struct Wire;
  7. struct RackWidget;
  8. struct ParamWidget;
  9. struct InputPort;
  10. struct OutputPort;
  11. ////////////////////
  12. // module
  13. ////////////////////
  14. // A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
  15. struct Model;
  16. struct ModuleWidget : OpaqueWidget {
  17. Model *model = NULL;
  18. // Eventually this should be replaced with a `moduleId` which will be used for inter-process communication between the gui world and the audio world.
  19. Module *module = NULL;
  20. // int moduleId;
  21. std::vector<InputPort*> inputs;
  22. std::vector<OutputPort*> outputs;
  23. std::vector<ParamWidget*> params;
  24. ModuleWidget(Module *module);
  25. ~ModuleWidget();
  26. // Convenience functions for adding special widgets (calls addChild())
  27. void addInput(InputPort *input);
  28. void addOutput(OutputPort *output);
  29. void addParam(ParamWidget *param);
  30. json_t *toJson();
  31. void fromJson(json_t *root);
  32. void disconnectPorts();
  33. void resetParams();
  34. void cloneParams(ModuleWidget *source);
  35. void draw(NVGcontext *vg);
  36. bool requested = false;
  37. Vec requestedPos;
  38. Vec dragPos;
  39. void onDragStart();
  40. void onDragMove(Vec mouseRel);
  41. void onDragEnd();
  42. void onMouseDown(int button);
  43. };
  44. struct WireWidget : OpaqueWidget {
  45. OutputPort *outputPort = NULL;
  46. InputPort *inputPort = NULL;
  47. Wire *wire = NULL;
  48. NVGcolor color;
  49. WireWidget();
  50. ~WireWidget();
  51. void updateWire();
  52. void draw(NVGcontext *vg);
  53. void drawOutputPlug(NVGcontext *vg);
  54. void drawInputPlug(NVGcontext *vg);
  55. };
  56. struct RackWidget : OpaqueWidget {
  57. // Only put ModuleWidgets in here
  58. Widget *moduleContainer;
  59. // Only put WireWidgets in here
  60. Widget *wireContainer;
  61. WireWidget *activeWire = NULL;
  62. RackWidget();
  63. ~RackWidget();
  64. void clear();
  65. void savePatch(std::string filename);
  66. void loadPatch(std::string filename);
  67. json_t *toJson();
  68. void fromJson(json_t *root);
  69. void repositionModule(ModuleWidget *module);
  70. void step();
  71. void draw(NVGcontext *vg);
  72. void onMouseDown(int button);
  73. };
  74. struct ModulePanel : TransparentWidget {
  75. NVGcolor backgroundColor;
  76. NVGcolor highlightColor;
  77. std::string imageFilename;
  78. void draw(NVGcontext *vg);
  79. };
  80. ////////////////////
  81. // params
  82. ////////////////////
  83. struct Light : TransparentWidget, SpriteWidget {
  84. NVGcolor color;
  85. void draw(NVGcontext *vg);
  86. };
  87. // If you don't add these to your ModuleWidget, it will fall out of the RackWidget
  88. struct Screw : TransparentWidget, SpriteWidget {
  89. Screw();
  90. };
  91. struct ParamWidget : OpaqueWidget, QuantityWidget {
  92. Module *module = NULL;
  93. int paramId;
  94. json_t *toJson();
  95. void fromJson(json_t *root);
  96. void onMouseDown(int button);
  97. void onChange();
  98. };
  99. struct Knob : ParamWidget, SpriteWidget {
  100. int minIndex, maxIndex, spriteCount;
  101. void step();
  102. void onDragStart();
  103. void onDragMove(Vec mouseRel);
  104. void onDragEnd();
  105. };
  106. struct Switch : ParamWidget, SpriteWidget {
  107. };
  108. struct ToggleSwitch : virtual Switch {
  109. void onDragStart() {
  110. index = 1;
  111. }
  112. void onDragEnd() {
  113. index = 0;
  114. }
  115. void onDragDrop(Widget *origin) {
  116. if (origin != this)
  117. return;
  118. // Cycle through modes
  119. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  120. float v = value + 1.0;
  121. setValue(v > maxValue ? minValue : v);
  122. }
  123. };
  124. struct MomentarySwitch : virtual Switch {
  125. void onDragStart() {
  126. setValue(maxValue);
  127. index = 1;
  128. }
  129. void onDragEnd() {
  130. setValue(minValue);
  131. index = 0;
  132. }
  133. };
  134. ////////////////////
  135. // ports
  136. ////////////////////
  137. struct Port : OpaqueWidget, SpriteWidget {
  138. Module *module = NULL;
  139. WireWidget *connectedWire = NULL;
  140. Port();
  141. ~Port();
  142. void disconnect();
  143. int type;
  144. void drawGlow(NVGcontext *vg);
  145. void onMouseDown(int button);
  146. void onDragEnd();
  147. };
  148. struct InputPort : Port {
  149. int inputId;
  150. void draw(NVGcontext *vg);
  151. void onDragStart();
  152. void onDragDrop(Widget *origin);
  153. };
  154. struct OutputPort : Port {
  155. int outputId;
  156. void draw(NVGcontext *vg);
  157. void onDragStart();
  158. void onDragDrop(Widget *origin);
  159. };
  160. ////////////////////
  161. // scene
  162. ////////////////////
  163. struct Toolbar : OpaqueWidget {
  164. Slider *wireOpacitySlider;
  165. Slider *wireTensionSlider;
  166. RadioButton *cpuUsageButton;
  167. Toolbar();
  168. void draw(NVGcontext *vg);
  169. };
  170. struct Scene : OpaqueWidget {
  171. Toolbar *toolbar;
  172. ScrollWidget *scrollWidget;
  173. Widget *overlay = NULL;
  174. Scene();
  175. void setOverlay(Widget *w);
  176. void step();
  177. };
  178. } // namespace rack