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.

236 lines
4.4KB

  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, SpriteWidget {
  87. NVGcolor color;
  88. void draw(NVGcontext *vg);
  89. };
  90. // If you don't add these to your ModuleWidget, it will fall out of the RackWidget
  91. struct Screw : TransparentWidget {
  92. Screw();
  93. };
  94. struct ParamWidget : OpaqueWidget, QuantityWidget {
  95. Module *module = NULL;
  96. int paramId;
  97. json_t *toJson();
  98. void fromJson(json_t *root);
  99. void onMouseDown(int button);
  100. void onChange();
  101. };
  102. struct Knob : ParamWidget {
  103. void onDragStart();
  104. void onDragMove(Vec mouseRel);
  105. void onDragEnd();
  106. };
  107. struct SpriteKnob : Knob, SpriteWidget {
  108. int minIndex, maxIndex, spriteCount;
  109. void step();
  110. };
  111. struct Switch : ParamWidget, SpriteWidget {
  112. };
  113. struct ToggleSwitch : virtual Switch {
  114. void onDragStart() {
  115. index = 1;
  116. }
  117. void onDragEnd() {
  118. index = 0;
  119. }
  120. void onDragDrop(Widget *origin) {
  121. if (origin != this)
  122. return;
  123. // Cycle through modes
  124. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  125. float v = value + 1.0;
  126. setValue(v > maxValue ? minValue : v);
  127. }
  128. };
  129. struct MomentarySwitch : virtual Switch {
  130. void onDragStart() {
  131. setValue(maxValue);
  132. index = 1;
  133. }
  134. void onDragEnd() {
  135. setValue(minValue);
  136. index = 0;
  137. }
  138. };
  139. ////////////////////
  140. // ports
  141. ////////////////////
  142. struct Port : OpaqueWidget {
  143. Module *module = NULL;
  144. WireWidget *connectedWire = NULL;
  145. Port();
  146. ~Port();
  147. void disconnect();
  148. void onMouseDown(int button);
  149. void onDragEnd();
  150. };
  151. struct InputPort : Port {
  152. int inputId;
  153. void onDragStart();
  154. void onDragDrop(Widget *origin);
  155. };
  156. struct OutputPort : Port {
  157. int outputId;
  158. void onDragStart();
  159. void onDragDrop(Widget *origin);
  160. };
  161. ////////////////////
  162. // scene
  163. ////////////////////
  164. struct Toolbar : OpaqueWidget {
  165. Slider *wireOpacitySlider;
  166. Slider *wireTensionSlider;
  167. RadioButton *cpuUsageButton;
  168. Toolbar();
  169. void draw(NVGcontext *vg);
  170. };
  171. struct RackScene : Scene {
  172. Toolbar *toolbar;
  173. ScrollWidget *scrollWidget;
  174. RackScene();
  175. void step();
  176. void draw(NVGcontext *vg);
  177. };
  178. ////////////////////
  179. // globals
  180. ////////////////////
  181. extern std::string gApplicationName;
  182. extern std::string gApplicationVersion;
  183. extern Scene *gScene;
  184. extern RackWidget *gRackWidget;
  185. void sceneInit();
  186. void sceneDestroy();
  187. } // namespace rack