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.

237 lines
4.5KB

  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. OutputPort *hoveredOutputPort = NULL;
  50. InputPort *hoveredInputPort = NULL;
  51. Wire *wire = NULL;
  52. NVGcolor color;
  53. WireWidget();
  54. ~WireWidget();
  55. void updateWire();
  56. void draw(NVGcontext *vg);
  57. void drawOutputPlug(NVGcontext *vg);
  58. void drawInputPlug(NVGcontext *vg);
  59. };
  60. struct RackWidget : OpaqueWidget {
  61. // Only put ModuleWidgets in here
  62. Widget *moduleContainer;
  63. // Only put WireWidgets in here
  64. Widget *wireContainer;
  65. WireWidget *activeWire = NULL;
  66. std::shared_ptr<Image> railsImage;
  67. RackWidget();
  68. ~RackWidget();
  69. void clear();
  70. void savePatch(std::string filename);
  71. void loadPatch(std::string filename);
  72. json_t *toJson();
  73. void fromJson(json_t *root);
  74. void repositionModule(ModuleWidget *module);
  75. void step();
  76. void draw(NVGcontext *vg);
  77. void onMouseDown(int button);
  78. };
  79. struct Panel : TransparentWidget {
  80. NVGcolor backgroundColor;
  81. NVGcolor borderColor;
  82. std::shared_ptr<Image> backgroundImage;
  83. void draw(NVGcontext *vg);
  84. };
  85. ////////////////////
  86. // params
  87. ////////////////////
  88. struct Light : TransparentWidget {
  89. NVGcolor color;
  90. void draw(NVGcontext *vg);
  91. };
  92. struct ParamWidget : OpaqueWidget, QuantityWidget {
  93. Module *module = NULL;
  94. int paramId;
  95. json_t *toJson();
  96. void fromJson(json_t *root);
  97. void onMouseDown(int button);
  98. void onChange();
  99. };
  100. struct Knob : ParamWidget {
  101. void onDragStart();
  102. void onDragMove(Vec mouseRel);
  103. void onDragEnd();
  104. };
  105. struct SpriteKnob : Knob, SpriteWidget {
  106. int minIndex, maxIndex, spriteCount;
  107. void step();
  108. };
  109. struct Switch : ParamWidget, SpriteWidget {
  110. };
  111. struct ToggleSwitch : virtual Switch {
  112. void onDragStart() {
  113. index = 1;
  114. }
  115. void onDragEnd() {
  116. index = 0;
  117. }
  118. void onDragDrop(Widget *origin) {
  119. if (origin != this)
  120. return;
  121. // Cycle through modes
  122. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  123. float v = value + 1.0;
  124. setValue(v > maxValue ? minValue : v);
  125. }
  126. };
  127. struct MomentarySwitch : virtual Switch {
  128. void onDragStart() {
  129. setValue(maxValue);
  130. index = 1;
  131. }
  132. void onDragEnd() {
  133. setValue(minValue);
  134. index = 0;
  135. }
  136. };
  137. ////////////////////
  138. // ports
  139. ////////////////////
  140. struct Port : OpaqueWidget {
  141. Module *module = NULL;
  142. WireWidget *connectedWire = NULL;
  143. Port();
  144. ~Port();
  145. void disconnect();
  146. void onMouseDown(int button);
  147. void onDragEnd();
  148. };
  149. struct InputPort : Port {
  150. int inputId;
  151. void onDragStart();
  152. void onDragDrop(Widget *origin);
  153. void onDragEnter(Widget *origin);
  154. void onDragLeave(Widget *origin);
  155. };
  156. struct OutputPort : Port {
  157. int outputId;
  158. void onDragStart();
  159. void onDragDrop(Widget *origin);
  160. void onDragEnter(Widget *origin);
  161. void onDragLeave(Widget *origin);
  162. };
  163. ////////////////////
  164. // scene
  165. ////////////////////
  166. struct Toolbar : OpaqueWidget {
  167. Slider *wireOpacitySlider;
  168. Slider *wireTensionSlider;
  169. RadioButton *cpuUsageButton;
  170. Toolbar();
  171. void draw(NVGcontext *vg);
  172. };
  173. struct RackScene : Scene {
  174. Toolbar *toolbar;
  175. ScrollWidget *scrollWidget;
  176. RackScene();
  177. void step();
  178. void draw(NVGcontext *vg);
  179. };
  180. ////////////////////
  181. // globals
  182. ////////////////////
  183. extern std::string gApplicationName;
  184. extern std::string gApplicationVersion;
  185. extern Scene *gScene;
  186. extern RackWidget *gRackWidget;
  187. void sceneInit();
  188. void sceneDestroy();
  189. } // namespace rack