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.

352 lines
7.9KB

  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 initialize();
  98. void openDialog();
  99. void saveDialog();
  100. void saveAsDialog();
  101. void savePatch(std::string filename);
  102. void loadPatch(std::string filename);
  103. json_t *toJson();
  104. void fromJson(json_t *rootJ);
  105. void addModule(ModuleWidget *m);
  106. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  107. void deleteModule(ModuleWidget *m);
  108. void cloneModule(ModuleWidget *m);
  109. /** Sets a module's box if non-colliding. Returns true if set */
  110. bool requestModuleBox(ModuleWidget *m, Rect box);
  111. /** Moves a module to the closest non-colliding position */
  112. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  113. void step();
  114. void draw(NVGcontext *vg);
  115. void onMouseDownOpaque(int button);
  116. };
  117. struct RackRail : TransparentWidget {
  118. void draw(NVGcontext *vg);
  119. };
  120. struct Panel : TransparentWidget {
  121. NVGcolor backgroundColor;
  122. std::shared_ptr<Image> backgroundImage;
  123. void draw(NVGcontext *vg);
  124. };
  125. struct SVGPanel : FramebufferWidget {
  126. void setBackground(std::shared_ptr<SVG> svg);
  127. };
  128. ////////////////////
  129. // params
  130. ////////////////////
  131. struct CircularShadow : TransparentWidget {
  132. float blur = 0.0;
  133. void draw(NVGcontext *vg);
  134. };
  135. struct Light : TransparentWidget {
  136. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  137. NVGcolor color = nvgRGBf(1, 1, 1);
  138. void draw(NVGcontext *vg);
  139. };
  140. struct ParamWidget : OpaqueWidget, QuantityWidget {
  141. Module *module = NULL;
  142. int paramId;
  143. /** Used to momentarily disable value randomization
  144. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  145. */
  146. bool randomizable = true;
  147. json_t *toJson();
  148. void fromJson(json_t *rootJ);
  149. virtual void randomize();
  150. void onMouseDownOpaque(int button);
  151. void onChange();
  152. };
  153. /** Implements vertical dragging behavior for ParamWidgets */
  154. struct Knob : ParamWidget {
  155. /** Snap to nearest integer while dragging */
  156. bool snap = false;
  157. float dragValue;
  158. void onDragStart();
  159. void onDragMove(Vec mouseRel);
  160. void onDragEnd();
  161. /** Tell engine to smoothly vary this parameter */
  162. void onChange();
  163. };
  164. struct SpriteKnob : virtual Knob, SpriteWidget {
  165. int minIndex, maxIndex, spriteCount;
  166. void step();
  167. };
  168. /** A knob which rotates an SVG and caches it in a framebuffer */
  169. struct SVGKnob : virtual Knob, FramebufferWidget {
  170. /** Angles in radians */
  171. float minAngle, maxAngle;
  172. /** Not owned */
  173. TransformWidget *tw;
  174. SVGWidget *sw;
  175. SVGKnob();
  176. void setSVG(std::shared_ptr<SVG> svg);
  177. void step();
  178. void onChange();
  179. };
  180. struct SVGSlider : Knob, FramebufferWidget {
  181. /** Intermediate positions will be interpolated between these positions */
  182. Vec minHandlePos, maxHandlePos;
  183. /** Not owned */
  184. SVGWidget *background;
  185. SVGWidget *handle;
  186. SVGSlider();
  187. void step();
  188. void onChange();
  189. };
  190. struct Switch : ParamWidget {
  191. };
  192. struct SVGSwitch : virtual Switch, FramebufferWidget {
  193. std::vector<std::shared_ptr<SVG>> frames;
  194. /** Not owned */
  195. SVGWidget *sw;
  196. SVGSwitch();
  197. /** Adds an SVG file to represent the next switch position */
  198. void addFrame(std::shared_ptr<SVG> svg);
  199. void step();
  200. void onChange();
  201. };
  202. /** A switch that cycles through each mechanical position */
  203. struct ToggleSwitch : virtual Switch {
  204. void onDragStart() {
  205. // Cycle through values
  206. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  207. if (value >= maxValue)
  208. setValue(minValue);
  209. else
  210. setValue(value + 1.0);
  211. }
  212. };
  213. /** A switch that is turned on when held */
  214. struct MomentarySwitch : virtual Switch {
  215. /** Don't randomize state */
  216. void randomize() {}
  217. void onDragStart() {
  218. setValue(maxValue);
  219. }
  220. void onDragEnd() {
  221. setValue(minValue);
  222. }
  223. };
  224. ////////////////////
  225. // ports
  226. ////////////////////
  227. struct Port : OpaqueWidget {
  228. enum PortType {
  229. INPUT,
  230. OUTPUT
  231. };
  232. Module *module = NULL;
  233. PortType type = INPUT;
  234. int portId;
  235. ~Port();
  236. void draw(NVGcontext *vg);
  237. void onMouseDownOpaque(int button);
  238. void onDragEnd();
  239. void onDragStart();
  240. void onDragDrop(Widget *origin);
  241. void onDragEnter(Widget *origin);
  242. void onDragLeave(Widget *origin);
  243. };
  244. struct SVGPort : Port, FramebufferWidget {
  245. SVGWidget *background;
  246. SVGPort();
  247. void draw(NVGcontext *vg);
  248. };
  249. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  250. struct SVGScrew : FramebufferWidget {
  251. SVGWidget *sw;
  252. SVGScrew();
  253. };
  254. ////////////////////
  255. // scene
  256. ////////////////////
  257. struct Toolbar : OpaqueWidget {
  258. Slider *wireOpacitySlider;
  259. Slider *wireTensionSlider;
  260. RadioButton *cpuUsageButton;
  261. RadioButton *plugLightButton;
  262. Toolbar();
  263. void draw(NVGcontext *vg);
  264. };
  265. struct PluginManagerWidget : Widget {
  266. Widget *loginWidget;
  267. Widget *manageWidget;
  268. Widget *downloadWidget;
  269. PluginManagerWidget();
  270. void step();
  271. };
  272. struct RackScene : Scene {
  273. ScrollWidget *scrollWidget;
  274. ZoomWidget *zoomWidget;
  275. RackScene();
  276. void step();
  277. void draw(NVGcontext *vg);
  278. Widget *onHoverKey(Vec pos, int key);
  279. };
  280. ////////////////////
  281. // globals
  282. ////////////////////
  283. extern std::string gApplicationName;
  284. extern std::string gApplicationVersion;
  285. extern std::string gApiHost;
  286. // Easy access to "singleton" widgets
  287. extern RackWidget *gRackWidget;
  288. extern Toolbar *gToolbar;
  289. void sceneInit();
  290. void sceneDestroy();
  291. } // namespace rack