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.

342 lines
7.3KB

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