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.

231 lines
4.3KB

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