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.

256 lines
5.0KB

  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. std::string imageFilename;
  77. void draw(NVGcontext *vg);
  78. };
  79. ////////////////////
  80. // params
  81. ////////////////////
  82. struct Light : TransparentWidget, SpriteWidget {
  83. NVGcolor color;
  84. void draw(NVGcontext *vg);
  85. };
  86. // If you don't add these to your ModuleWidget, it will fall out of the RackWidget
  87. struct Screw : TransparentWidget, SpriteWidget {
  88. Screw();
  89. };
  90. struct ParamWidget : OpaqueWidget, QuantityWidget {
  91. Module *module = NULL;
  92. int paramId;
  93. json_t *toJson();
  94. void fromJson(json_t *root);
  95. void onMouseDown(int button);
  96. void onChange();
  97. };
  98. struct Knob : ParamWidget, SpriteWidget {
  99. int minIndex, maxIndex, spriteCount;
  100. void step();
  101. void onDragStart();
  102. void onDragMove(Vec mouseRel);
  103. void onDragEnd();
  104. };
  105. struct Switch : ParamWidget, SpriteWidget {
  106. };
  107. struct ToggleSwitch : virtual Switch {
  108. void onDragStart() {
  109. index = 1;
  110. }
  111. void onDragEnd() {
  112. index = 0;
  113. }
  114. void onDragDrop(Widget *origin) {
  115. if (origin != this)
  116. return;
  117. // Cycle through modes
  118. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  119. float v = value + 1.0;
  120. setValue(v > maxValue ? minValue : v);
  121. }
  122. };
  123. struct MomentarySwitch : virtual Switch {
  124. void onDragStart() {
  125. setValue(maxValue);
  126. index = 1;
  127. }
  128. void onDragEnd() {
  129. setValue(minValue);
  130. index = 0;
  131. }
  132. };
  133. ////////////////////
  134. // ports
  135. ////////////////////
  136. struct Port : OpaqueWidget {
  137. Module *module = NULL;
  138. WireWidget *connectedWire = NULL;
  139. Port();
  140. ~Port();
  141. void disconnect();
  142. void onMouseDown(int button);
  143. void onDragEnd();
  144. };
  145. struct InputPort : Port {
  146. int inputId;
  147. void onDragStart();
  148. void onDragDrop(Widget *origin);
  149. };
  150. struct OutputPort : Port {
  151. int outputId;
  152. void onDragStart();
  153. void onDragDrop(Widget *origin);
  154. };
  155. ////////////////////
  156. // scene
  157. ////////////////////
  158. struct Toolbar : OpaqueWidget {
  159. Slider *wireOpacitySlider;
  160. Slider *wireTensionSlider;
  161. RadioButton *cpuUsageButton;
  162. Toolbar();
  163. void draw(NVGcontext *vg);
  164. };
  165. struct Scene : OpaqueWidget {
  166. Toolbar *toolbar;
  167. ScrollWidget *scrollWidget;
  168. Widget *overlay = NULL;
  169. Scene();
  170. void setOverlay(Widget *w);
  171. void step();
  172. };
  173. ////////////////////
  174. // component library
  175. ////////////////////
  176. struct PJ301M : SpriteWidget {
  177. PJ301M() {
  178. box.size = Vec(24, 24);
  179. spriteOffset = Vec(-10, -10);
  180. spriteSize = Vec(48, 48);
  181. spriteFilename = "res/ComponentLibrary/PJ301M.png";
  182. }
  183. };
  184. struct InputPortPJ301M : InputPort, PJ301M {};
  185. struct OutputPortPJ301M: OutputPort, PJ301M {};
  186. struct PJ3410 : SpriteWidget {
  187. PJ3410() {
  188. box.size = Vec(31, 31);
  189. spriteOffset = Vec(-9, -9);
  190. spriteSize = Vec(54, 54);
  191. spriteFilename = "res/ComponentLibrary/PJ3410.png";
  192. }
  193. };
  194. struct InputPortPJ3410 : InputPort, PJ3410 {};
  195. struct OutputPortPJ3410: OutputPort, PJ3410 {};
  196. struct CL1362 : SpriteWidget {
  197. CL1362() {
  198. box.size = Vec(33, 29);
  199. spriteOffset = Vec(-10, -10);
  200. spriteSize = Vec(57, 54);
  201. spriteFilename = "res/ComponentLibrary/CL1362.png";
  202. }
  203. };
  204. struct InputPortCL1362 : InputPort, CL1362 {};
  205. struct OutputPortCL1362 : OutputPort, CL1362 {};
  206. } // namespace rack