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.

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