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.

382 lines
9.1KB

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