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.

356 lines
8.0KB

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