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.

345 lines
7.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 Port;
  11. struct Scene;
  12. ////////////////////
  13. // module
  14. ////////////////////
  15. // A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
  16. #define RACK_GRID_WIDTH 15
  17. #define RACK_GRID_HEIGHT 380
  18. struct Model;
  19. struct ModuleWidget : OpaqueWidget {
  20. Model *model = NULL;
  21. /** Owns the module pointer */
  22. Module *module = NULL;
  23. std::vector<Port*> inputs;
  24. std::vector<Port*> outputs;
  25. std::vector<ParamWidget*> params;
  26. ~ModuleWidget();
  27. void setModule(Module *module);
  28. // Convenience functions for adding special widgets (calls addChild())
  29. void addInput(Port *input);
  30. void addOutput(Port *output);
  31. void addParam(ParamWidget *param);
  32. json_t *toJson();
  33. void fromJson(json_t *root);
  34. /** Disconnects cables from all ports */
  35. void disconnect();
  36. /** Resets the state of the module */
  37. void initialize();
  38. /** Randomizes the state of the module
  39. This method just randomizes parameters. Override and call this function if your module contains other state information that you wish to randomize.
  40. */
  41. void randomize();
  42. virtual Menu *createContextMenu();
  43. void draw(NVGcontext *vg);
  44. bool requested = false;
  45. Vec requestedPos;
  46. Vec dragPos;
  47. Widget *onMouseMove(Vec pos, Vec mouseRel);
  48. Widget *onHoverKey(Vec pos, int key);
  49. void onDragStart();
  50. void onDragMove(Vec mouseRel);
  51. void onDragEnd();
  52. void onMouseDownOpaque(int button);
  53. };
  54. struct ValueLight;
  55. struct WireWidget : OpaqueWidget {
  56. Port *outputPort = NULL;
  57. Port *inputPort = NULL;
  58. Port *hoveredOutputPort = NULL;
  59. Port *hoveredInputPort = NULL;
  60. ValueLight *inputLight;
  61. ValueLight *outputLight;
  62. Wire *wire = NULL;
  63. NVGcolor color;
  64. WireWidget();
  65. ~WireWidget();
  66. /** Synchronizes the plugged state of the widget to the owned wire */
  67. void updateWire();
  68. Vec getOutputPos();
  69. Vec getInputPos();
  70. void draw(NVGcontext *vg);
  71. void drawPlugs(NVGcontext *vg);
  72. };
  73. struct WireContainer : TransparentWidget {
  74. WireWidget *activeWire = NULL;
  75. /** Takes ownership of `w` and adds it as a child if it isn't already */
  76. void setActiveWire(WireWidget *w);
  77. /** "Drops" the wire onto the port, making an engine connection if successful */
  78. void commitActiveWire();
  79. void removeTopWire(Port *port);
  80. void removeAllWires(Port *port);
  81. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  82. WireWidget *getTopWire(Port *port);
  83. void draw(NVGcontext *vg);
  84. };
  85. struct RackWidget : OpaqueWidget {
  86. FramebufferWidget *rails;
  87. // Only put ModuleWidgets in here
  88. Widget *moduleContainer;
  89. // Only put WireWidgets in here
  90. WireContainer *wireContainer;
  91. std::string lastPath;
  92. RackWidget();
  93. ~RackWidget();
  94. void clear();
  95. void openDialog();
  96. void saveDialog();
  97. void saveAsDialog();
  98. void savePatch(std::string filename);
  99. void loadPatch(std::string filename);
  100. json_t *toJson();
  101. void fromJson(json_t *root);
  102. void addModule(ModuleWidget *m);
  103. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  104. void deleteModule(ModuleWidget *m);
  105. void cloneModule(ModuleWidget *m);
  106. /** Moves a module to the closest non-colliding position */
  107. void repositionModule(ModuleWidget *m);
  108. void step();
  109. void draw(NVGcontext *vg);
  110. void onMouseDownOpaque(int button);
  111. };
  112. struct RackRail : TransparentWidget {
  113. void draw(NVGcontext *vg);
  114. };
  115. struct Panel : TransparentWidget {
  116. NVGcolor backgroundColor;
  117. NVGcolor borderColor;
  118. std::shared_ptr<Image> backgroundImage;
  119. void draw(NVGcontext *vg);
  120. };
  121. struct SVGPanel : FramebufferWidget {
  122. void setBackground(std::shared_ptr<SVG> svg);
  123. };
  124. ////////////////////
  125. // params
  126. ////////////////////
  127. struct CircularShadow : TransparentWidget {
  128. float blur = 0.0;
  129. void draw(NVGcontext *vg);
  130. };
  131. struct Light : TransparentWidget {
  132. NVGcolor color;
  133. void draw(NVGcontext *vg);
  134. };
  135. struct ParamWidget : OpaqueWidget, QuantityWidget {
  136. Module *module = NULL;
  137. int paramId;
  138. json_t *toJson();
  139. void fromJson(json_t *root);
  140. virtual void randomize();
  141. void onMouseDownOpaque(int button);
  142. void onChange();
  143. };
  144. /** Implements vertical dragging behavior for ParamWidgets */
  145. struct Knob : ParamWidget {
  146. void onDragStart();
  147. void onDragMove(Vec mouseRel);
  148. void onDragEnd();
  149. /** Tell engine to smoothly vary this parameter */
  150. void onChange();
  151. };
  152. struct SpriteKnob : virtual Knob, SpriteWidget {
  153. int minIndex, maxIndex, spriteCount;
  154. void step();
  155. };
  156. /** A knob which rotates an SVG and caches it in a framebuffer */
  157. struct SVGKnob : virtual Knob, FramebufferWidget {
  158. /** Angles in radians */
  159. float minAngle, maxAngle;
  160. /** Not owned */
  161. TransformWidget *tw;
  162. SVGWidget *sw;
  163. SVGKnob();
  164. void setSVG(std::shared_ptr<SVG> svg);
  165. void step();
  166. void onChange();
  167. };
  168. /** Snaps to the nearest integer value on mouse release */
  169. struct SnapKnob : virtual Knob {
  170. void onDragEnd() {
  171. setValue(roundf(value));
  172. Knob::onDragEnd();
  173. }
  174. };
  175. struct SVGSlider : Knob, FramebufferWidget {
  176. /** Intermediate positions will be interpolated between these positions */
  177. Vec minHandlePos, maxHandlePos;
  178. /** Not owned */
  179. SVGWidget *background;
  180. SVGWidget *handle;
  181. SVGSlider();
  182. void step();
  183. void onChange();
  184. };
  185. struct Switch : ParamWidget {
  186. };
  187. struct SVGSwitch : virtual Switch, FramebufferWidget {
  188. std::vector<std::shared_ptr<SVG>> frames;
  189. /** Not owned */
  190. SVGWidget *sw;
  191. SVGSwitch();
  192. /** Adds an SVG file to represent the next switch position */
  193. void addFrame(std::shared_ptr<SVG> svg);
  194. void step();
  195. void onChange();
  196. };
  197. /** A switch that cycles through each mechanical position */
  198. struct ToggleSwitch : virtual Switch {
  199. void onDragStart() {
  200. // Cycle through values
  201. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  202. if (value >= maxValue)
  203. setValue(minValue);
  204. else
  205. setValue(value + 1.0);
  206. }
  207. };
  208. /** A switch that is turned on when held */
  209. struct MomentarySwitch : virtual Switch {
  210. /** Don't randomize state */
  211. void randomize() {}
  212. void onDragStart() {
  213. setValue(maxValue);
  214. }
  215. void onDragEnd() {
  216. setValue(minValue);
  217. }
  218. };
  219. ////////////////////
  220. // ports
  221. ////////////////////
  222. struct Port : OpaqueWidget {
  223. enum PortType {
  224. INPUT,
  225. OUTPUT
  226. };
  227. Module *module = NULL;
  228. PortType type = INPUT;
  229. int portId;
  230. ~Port();
  231. void draw(NVGcontext *vg);
  232. void onMouseDownOpaque(int button);
  233. void onDragEnd();
  234. void onDragStart();
  235. void onDragDrop(Widget *origin);
  236. void onDragEnter(Widget *origin);
  237. void onDragLeave(Widget *origin);
  238. };
  239. struct SVGPort : Port, FramebufferWidget {
  240. SVGWidget *background;
  241. SVGPort();
  242. void draw(NVGcontext *vg);
  243. };
  244. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  245. struct SVGScrew : FramebufferWidget {
  246. SVGWidget *sw;
  247. SVGScrew();
  248. };
  249. ////////////////////
  250. // scene
  251. ////////////////////
  252. struct Toolbar : OpaqueWidget {
  253. Slider *wireOpacitySlider;
  254. Slider *wireTensionSlider;
  255. RadioButton *cpuUsageButton;
  256. Toolbar();
  257. void draw(NVGcontext *vg);
  258. };
  259. struct PluginManagerWidget : Widget {
  260. Widget *loginWidget;
  261. Widget *manageWidget;
  262. Widget *downloadWidget;
  263. PluginManagerWidget();
  264. void step();
  265. };
  266. struct RackScene : Scene {
  267. Toolbar *toolbar;
  268. ScrollWidget *scrollWidget;
  269. RackScene();
  270. void step();
  271. void draw(NVGcontext *vg);
  272. Widget *onHoverKey(Vec pos, int key);
  273. };
  274. ////////////////////
  275. // globals
  276. ////////////////////
  277. extern std::string gApplicationName;
  278. extern std::string gApplicationVersion;
  279. extern std::string gApiHost;
  280. extern RackWidget *gRackWidget;
  281. void sceneInit();
  282. void sceneDestroy();
  283. } // namespace rack