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.

347 lines
7.5KB

  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. json_t *toJson();
  33. void fromJson(json_t *root);
  34. /** Disconnects cables from all ports */
  35. void disconnect();
  36. /** Resets the state of the module */
  37. void initialize();
  38. /** Randomizes the state of the module
  39. This method just randomizes parameters. Override and call this function if your module contains other state information that you wish to randomize.
  40. */
  41. void randomize();
  42. virtual Menu *createContextMenu();
  43. void draw(NVGcontext *vg);
  44. bool requested = false;
  45. Vec requestedPos;
  46. Vec dragPos;
  47. Widget *onMouseMove(Vec pos, Vec mouseRel);
  48. Widget *onHoverKey(Vec pos, int key);
  49. void onDragStart();
  50. void onDragMove(Vec mouseRel);
  51. void onDragEnd();
  52. void onMouseDownOpaque(int button);
  53. };
  54. struct ValueLight;
  55. struct WireWidget : OpaqueWidget {
  56. Port *outputPort = NULL;
  57. Port *inputPort = NULL;
  58. Port *hoveredOutputPort = NULL;
  59. Port *hoveredInputPort = NULL;
  60. ValueLight *inputLight;
  61. ValueLight *outputLight;
  62. Wire *wire = NULL;
  63. NVGcolor color;
  64. WireWidget();
  65. ~WireWidget();
  66. /** Synchronizes the plugged state of the widget to the owned wire */
  67. void updateWire();
  68. Vec getOutputPos();
  69. Vec getInputPos();
  70. void draw(NVGcontext *vg);
  71. void drawPlugs(NVGcontext *vg);
  72. };
  73. struct WireContainer : TransparentWidget {
  74. WireWidget *activeWire = NULL;
  75. /** Takes ownership of `w` and adds it as a child if it isn't already */
  76. void setActiveWire(WireWidget *w);
  77. /** "Drops" the wire onto the port, making an engine connection if successful */
  78. void commitActiveWire();
  79. void removeTopWire(Port *port);
  80. void removeAllWires(Port *port);
  81. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  82. WireWidget *getTopWire(Port *port);
  83. void draw(NVGcontext *vg);
  84. };
  85. struct RackWidget : OpaqueWidget {
  86. FramebufferWidget *rails;
  87. // Only put ModuleWidgets in here
  88. Widget *moduleContainer;
  89. // Only put WireWidgets in here
  90. WireContainer *wireContainer;
  91. std::string lastPath;
  92. RackWidget();
  93. ~RackWidget();
  94. void clear();
  95. void openDialog();
  96. void saveDialog();
  97. void saveAsDialog();
  98. void savePatch(std::string filename);
  99. void loadPatch(std::string filename);
  100. json_t *toJson();
  101. void fromJson(json_t *root);
  102. void addModule(ModuleWidget *m);
  103. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  104. void deleteModule(ModuleWidget *m);
  105. void cloneModule(ModuleWidget *m);
  106. /** Moves a module to the closest non-colliding position */
  107. void repositionModule(ModuleWidget *m);
  108. void step();
  109. void draw(NVGcontext *vg);
  110. void onMouseDownOpaque(int button);
  111. };
  112. struct RackRail : TransparentWidget {
  113. void draw(NVGcontext *vg);
  114. };
  115. struct Panel : TransparentWidget {
  116. NVGcolor backgroundColor;
  117. NVGcolor borderColor;
  118. std::shared_ptr<Image> backgroundImage;
  119. void draw(NVGcontext *vg);
  120. };
  121. struct SVGPanel : FramebufferWidget {
  122. void setBackground(std::shared_ptr<SVG> svg);
  123. };
  124. ////////////////////
  125. // params
  126. ////////////////////
  127. struct CircularShadow : TransparentWidget {
  128. float blur = 0.0;
  129. void draw(NVGcontext *vg);
  130. };
  131. struct Light : TransparentWidget {
  132. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  133. NVGcolor color = nvgRGBf(1, 1, 1);
  134. void draw(NVGcontext *vg);
  135. };
  136. struct ParamWidget : OpaqueWidget, QuantityWidget {
  137. Module *module = NULL;
  138. int paramId;
  139. json_t *toJson();
  140. void fromJson(json_t *root);
  141. virtual void randomize();
  142. void onMouseDownOpaque(int button);
  143. void onChange();
  144. };
  145. /** Implements vertical dragging behavior for ParamWidgets */
  146. struct Knob : ParamWidget {
  147. void onDragStart();
  148. void onDragMove(Vec mouseRel);
  149. void onDragEnd();
  150. /** Tell engine to smoothly vary this parameter */
  151. void onChange();
  152. };
  153. struct SpriteKnob : virtual Knob, SpriteWidget {
  154. int minIndex, maxIndex, spriteCount;
  155. void step();
  156. };
  157. /** A knob which rotates an SVG and caches it in a framebuffer */
  158. struct SVGKnob : virtual Knob, FramebufferWidget {
  159. /** Angles in radians */
  160. float minAngle, maxAngle;
  161. /** Not owned */
  162. TransformWidget *tw;
  163. SVGWidget *sw;
  164. SVGKnob();
  165. void setSVG(std::shared_ptr<SVG> svg);
  166. void step();
  167. void onChange();
  168. };
  169. /** Snaps to the nearest integer value on mouse release */
  170. struct SnapKnob : virtual Knob {
  171. void onDragEnd() {
  172. setValue(roundf(value));
  173. Knob::onDragEnd();
  174. }
  175. };
  176. struct SVGSlider : Knob, FramebufferWidget {
  177. /** Intermediate positions will be interpolated between these positions */
  178. Vec minHandlePos, maxHandlePos;
  179. /** Not owned */
  180. SVGWidget *background;
  181. SVGWidget *handle;
  182. SVGSlider();
  183. void step();
  184. void onChange();
  185. };
  186. struct Switch : ParamWidget {
  187. };
  188. struct SVGSwitch : virtual Switch, FramebufferWidget {
  189. std::vector<std::shared_ptr<SVG>> frames;
  190. /** Not owned */
  191. SVGWidget *sw;
  192. SVGSwitch();
  193. /** Adds an SVG file to represent the next switch position */
  194. void addFrame(std::shared_ptr<SVG> svg);
  195. void step();
  196. void onChange();
  197. };
  198. /** A switch that cycles through each mechanical position */
  199. struct ToggleSwitch : virtual Switch {
  200. void onDragStart() {
  201. // Cycle through values
  202. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  203. if (value >= maxValue)
  204. setValue(minValue);
  205. else
  206. setValue(value + 1.0);
  207. }
  208. };
  209. /** A switch that is turned on when held */
  210. struct MomentarySwitch : virtual Switch {
  211. /** Don't randomize state */
  212. void randomize() {}
  213. void onDragStart() {
  214. setValue(maxValue);
  215. }
  216. void onDragEnd() {
  217. setValue(minValue);
  218. }
  219. };
  220. ////////////////////
  221. // ports
  222. ////////////////////
  223. struct Port : OpaqueWidget {
  224. enum PortType {
  225. INPUT,
  226. OUTPUT
  227. };
  228. Module *module = NULL;
  229. PortType type = INPUT;
  230. int portId;
  231. ~Port();
  232. void draw(NVGcontext *vg);
  233. void onMouseDownOpaque(int button);
  234. void onDragEnd();
  235. void onDragStart();
  236. void onDragDrop(Widget *origin);
  237. void onDragEnter(Widget *origin);
  238. void onDragLeave(Widget *origin);
  239. };
  240. struct SVGPort : Port, FramebufferWidget {
  241. SVGWidget *background;
  242. SVGPort();
  243. void draw(NVGcontext *vg);
  244. };
  245. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  246. struct SVGScrew : FramebufferWidget {
  247. SVGWidget *sw;
  248. SVGScrew();
  249. };
  250. ////////////////////
  251. // scene
  252. ////////////////////
  253. struct Toolbar : OpaqueWidget {
  254. Slider *wireOpacitySlider;
  255. Slider *wireTensionSlider;
  256. RadioButton *cpuUsageButton;
  257. Toolbar();
  258. void draw(NVGcontext *vg);
  259. };
  260. struct PluginManagerWidget : Widget {
  261. Widget *loginWidget;
  262. Widget *manageWidget;
  263. Widget *downloadWidget;
  264. PluginManagerWidget();
  265. void step();
  266. };
  267. struct RackScene : Scene {
  268. Toolbar *toolbar;
  269. ScrollWidget *scrollWidget;
  270. ZoomWidget *zoomWidget;
  271. RackScene();
  272. void step();
  273. void draw(NVGcontext *vg);
  274. Widget *onHoverKey(Vec pos, int key);
  275. };
  276. ////////////////////
  277. // globals
  278. ////////////////////
  279. extern std::string gApplicationName;
  280. extern std::string gApplicationVersion;
  281. extern std::string gApiHost;
  282. extern RackWidget *gRackWidget;
  283. void sceneInit();
  284. void sceneDestroy();
  285. } // namespace rack