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.

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