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.

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