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.

252 lines
4.7KB

  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. /** Owns the module pointer */
  21. Module *module = NULL;
  22. std::vector<InputPort*> inputs;
  23. std::vector<OutputPort*> outputs;
  24. std::vector<ParamWidget*> params;
  25. ~ModuleWidget();
  26. void setModule(Module *module);
  27. // Convenience functions for adding special widgets (calls addChild())
  28. void addInput(InputPort *input);
  29. void addOutput(OutputPort *output);
  30. void addParam(ParamWidget *param);
  31. json_t *toJson();
  32. void fromJson(json_t *root);
  33. void disconnectPorts();
  34. void resetParams();
  35. void cloneParams(ModuleWidget *source);
  36. void draw(NVGcontext *vg);
  37. bool requested = false;
  38. Vec requestedPos;
  39. Vec dragPos;
  40. void onDragStart();
  41. void onDragMove(Vec mouseRel);
  42. void onDragEnd();
  43. void onMouseDown(int button);
  44. };
  45. struct WireWidget : OpaqueWidget {
  46. OutputPort *outputPort = NULL;
  47. InputPort *inputPort = NULL;
  48. OutputPort *hoveredOutputPort = NULL;
  49. InputPort *hoveredInputPort = NULL;
  50. Wire *wire = NULL;
  51. NVGcolor color;
  52. WireWidget();
  53. ~WireWidget();
  54. void updateWire();
  55. void draw(NVGcontext *vg);
  56. void drawOutputPlug(NVGcontext *vg);
  57. void drawInputPlug(NVGcontext *vg);
  58. };
  59. struct RackWidget : OpaqueWidget {
  60. // Only put ModuleWidgets in here
  61. Widget *moduleContainer;
  62. // Only put WireWidgets in here
  63. Widget *wireContainer;
  64. WireWidget *activeWire = NULL;
  65. std::shared_ptr<Image> railsImage;
  66. RackWidget();
  67. ~RackWidget();
  68. void clear();
  69. void savePatch(std::string filename);
  70. void loadPatch(std::string filename);
  71. json_t *toJson();
  72. void fromJson(json_t *root);
  73. void repositionModule(ModuleWidget *module);
  74. void step();
  75. void draw(NVGcontext *vg);
  76. void onMouseDown(int button);
  77. };
  78. struct Panel : TransparentWidget {
  79. NVGcolor backgroundColor;
  80. NVGcolor borderColor;
  81. std::shared_ptr<Image> backgroundImage;
  82. void draw(NVGcontext *vg);
  83. };
  84. ////////////////////
  85. // params
  86. ////////////////////
  87. struct Light : TransparentWidget {
  88. NVGcolor color;
  89. void draw(NVGcontext *vg);
  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 {
  100. void onDragStart();
  101. void onDragMove(Vec mouseRel);
  102. void onDragEnd();
  103. };
  104. struct SpriteKnob : Knob, SpriteWidget {
  105. int minIndex, maxIndex, spriteCount;
  106. void step();
  107. };
  108. /** A knob which rotates an SVG and caches it in a framebuffer */
  109. struct SVGKnob : Knob, FramebufferWidget {
  110. /** Angles in radians */
  111. float minAngle, maxAngle;
  112. /** Not owned */
  113. TransformWidget *tw;
  114. SVGWidget *sw;
  115. SVGKnob();
  116. void setSVG(std::shared_ptr<SVG> svg);
  117. void step();
  118. void onChange();
  119. };
  120. struct Switch : ParamWidget, SpriteWidget {
  121. };
  122. struct ToggleSwitch : virtual Switch {
  123. void onDragStart() {
  124. index = 1;
  125. }
  126. void onDragEnd() {
  127. index = 0;
  128. }
  129. void onDragDrop(Widget *origin) {
  130. if (origin != this)
  131. return;
  132. // Cycle through modes
  133. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  134. float v = value + 1.0;
  135. setValue(v > maxValue ? minValue : v);
  136. }
  137. };
  138. struct MomentarySwitch : virtual Switch {
  139. void onDragStart() {
  140. setValue(maxValue);
  141. index = 1;
  142. }
  143. void onDragEnd() {
  144. setValue(minValue);
  145. index = 0;
  146. }
  147. };
  148. ////////////////////
  149. // ports
  150. ////////////////////
  151. struct Port : OpaqueWidget, SpriteWidget {
  152. Module *module = NULL;
  153. WireWidget *connectedWire = NULL;
  154. Port();
  155. ~Port();
  156. void disconnect();
  157. void onMouseDown(int button);
  158. void onDragEnd();
  159. };
  160. struct InputPort : Port {
  161. int inputId;
  162. void onDragStart();
  163. void onDragDrop(Widget *origin);
  164. void onDragEnter(Widget *origin);
  165. void onDragLeave(Widget *origin);
  166. void draw(NVGcontext *vg);
  167. };
  168. struct OutputPort : Port {
  169. int outputId;
  170. void onDragStart();
  171. void onDragDrop(Widget *origin);
  172. void onDragEnter(Widget *origin);
  173. void onDragLeave(Widget *origin);
  174. void draw(NVGcontext *vg);
  175. };
  176. ////////////////////
  177. // scene
  178. ////////////////////
  179. struct Toolbar : OpaqueWidget {
  180. Slider *wireOpacitySlider;
  181. Slider *wireTensionSlider;
  182. RadioButton *cpuUsageButton;
  183. Toolbar();
  184. void draw(NVGcontext *vg);
  185. };
  186. struct RackScene : Scene {
  187. Toolbar *toolbar;
  188. ScrollWidget *scrollWidget;
  189. RackScene();
  190. void step();
  191. void draw(NVGcontext *vg);
  192. };
  193. ////////////////////
  194. // globals
  195. ////////////////////
  196. extern std::string gApplicationName;
  197. extern std::string gApplicationVersion;
  198. extern Scene *gScene;
  199. extern RackWidget *gRackWidget;
  200. void sceneInit();
  201. void sceneDestroy();
  202. } // namespace rack