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.

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