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.

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