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.

363 lines
8.6KB

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