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.

404 lines
9.9KB

  1. #pragma once
  2. #include <vector>
  3. #include <jansson.h>
  4. #include "widgets.hpp"
  5. namespace rack {
  6. struct Model;
  7. struct Module;
  8. struct Wire;
  9. struct RackWidget;
  10. struct ParamWidget;
  11. struct Port;
  12. struct SVGPanel;
  13. ////////////////////
  14. // module
  15. ////////////////////
  16. // A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
  17. #define RACK_GRID_WIDTH 15
  18. #define RACK_GRID_HEIGHT 380
  19. #define KNOB_SENSITIVITY 0.0015
  20. static const Vec RACK_GRID_SIZE = Vec(15, 380);
  21. struct ModuleWidget : OpaqueWidget {
  22. Model *model = NULL;
  23. /** Owns the module pointer */
  24. Module *module = NULL;
  25. SVGPanel *panel = NULL;
  26. std::vector<Port*> inputs;
  27. std::vector<Port*> outputs;
  28. std::vector<ParamWidget*> params;
  29. ~ModuleWidget();
  30. void setModule(Module *module);
  31. /** Convenience functions for adding special widgets (calls addChild()) */
  32. void addInput(Port *input);
  33. void addOutput(Port *output);
  34. void addParam(ParamWidget *param);
  35. void setPanel(std::shared_ptr<SVG> svg);
  36. virtual json_t *toJson();
  37. virtual void fromJson(json_t *rootJ);
  38. /** Disconnects cables from all ports
  39. Called when the user clicks Disconnect Cables in the context menu.
  40. */
  41. virtual void disconnect();
  42. /** Resets the parameters of the module and calls the Module's randomize().
  43. Called when the user clicks Initialize in the context menu.
  44. */
  45. virtual void reset();
  46. /** Deprecated */
  47. virtual void initialize() final {}
  48. /** Randomizes the parameters of the module and calls the Module's randomize().
  49. Called when the user clicks Randomize in the context menu.
  50. */
  51. virtual void randomize();
  52. virtual Menu *createContextMenu();
  53. void draw(NVGcontext *vg) override;
  54. Vec dragPos;
  55. void onMouseDown(EventMouseDown &e) override;
  56. void onMouseMove(EventMouseMove &e) override;
  57. void onHoverKey(EventHoverKey &e) override;
  58. void onDragStart(EventDragStart &e) override;
  59. void onDragEnd(EventDragEnd &e) override;
  60. void onDragMove(EventDragMove &e) override;
  61. };
  62. struct ValueLight;
  63. struct WireWidget : OpaqueWidget {
  64. Port *outputPort = NULL;
  65. Port *inputPort = NULL;
  66. Port *hoveredOutputPort = NULL;
  67. Port *hoveredInputPort = NULL;
  68. ValueLight *inputLight;
  69. ValueLight *outputLight;
  70. Wire *wire = NULL;
  71. NVGcolor color;
  72. WireWidget();
  73. ~WireWidget();
  74. /** Synchronizes the plugged state of the widget to the owned wire */
  75. void updateWire();
  76. Vec getOutputPos();
  77. Vec getInputPos();
  78. void draw(NVGcontext *vg) override;
  79. void drawPlugs(NVGcontext *vg);
  80. };
  81. struct WireContainer : TransparentWidget {
  82. WireWidget *activeWire = NULL;
  83. /** Takes ownership of `w` and adds it as a child if it isn't already */
  84. void setActiveWire(WireWidget *w);
  85. /** "Drops" the wire onto the port, making an engine connection if successful */
  86. void commitActiveWire();
  87. void removeTopWire(Port *port);
  88. void removeAllWires(Port *port);
  89. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  90. WireWidget *getTopWire(Port *port);
  91. void draw(NVGcontext *vg) override;
  92. };
  93. struct RackWidget : OpaqueWidget {
  94. FramebufferWidget *rails;
  95. // Only put ModuleWidgets in here
  96. Widget *moduleContainer;
  97. // Only put WireWidgets in here
  98. WireContainer *wireContainer;
  99. std::string lastPath;
  100. Vec lastMousePos;
  101. RackWidget();
  102. ~RackWidget();
  103. /** Completely clear the rack's modules and wires */
  104. void clear();
  105. /** Clears the rack and loads the template patch */
  106. void reset();
  107. void openDialog();
  108. void saveDialog();
  109. void saveAsDialog();
  110. void savePatch(std::string filename);
  111. void loadPatch(std::string filename);
  112. json_t *toJson();
  113. void fromJson(json_t *rootJ);
  114. void addModule(ModuleWidget *m);
  115. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  116. void deleteModule(ModuleWidget *m);
  117. void cloneModule(ModuleWidget *m);
  118. /** Sets a module's box if non-colliding. Returns true if set */
  119. bool requestModuleBox(ModuleWidget *m, Rect box);
  120. /** Moves a module to the closest non-colliding position */
  121. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  122. void step() override;
  123. void draw(NVGcontext *vg) override;
  124. void onMouseMove(EventMouseMove &e) override;
  125. void onMouseDown(EventMouseDown &e) override;
  126. void onZoom(EventZoom &e) override;
  127. };
  128. struct RackRail : TransparentWidget {
  129. void draw(NVGcontext *vg) override;
  130. };
  131. struct AddModuleWindow : Window {
  132. Vec modulePos;
  133. AddModuleWindow();
  134. void step() override;
  135. };
  136. struct Panel : TransparentWidget {
  137. NVGcolor backgroundColor;
  138. std::shared_ptr<Image> backgroundImage;
  139. void draw(NVGcontext *vg) override;
  140. };
  141. struct SVGPanel : FramebufferWidget {
  142. void step() override;
  143. void setBackground(std::shared_ptr<SVG> svg);
  144. };
  145. ////////////////////
  146. // params
  147. ////////////////////
  148. struct CircularShadow : TransparentWidget {
  149. float blur = 0.0;
  150. void draw(NVGcontext *vg) override;
  151. };
  152. struct ParamWidget : OpaqueWidget, QuantityWidget {
  153. Module *module = NULL;
  154. int paramId;
  155. /** Used to momentarily disable value randomization
  156. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  157. */
  158. bool randomizable = true;
  159. json_t *toJson();
  160. void fromJson(json_t *rootJ);
  161. virtual void randomize();
  162. void onMouseDown(EventMouseDown &e) override;
  163. void onChange(EventChange &e) override;
  164. };
  165. /** Implements vertical dragging behavior for ParamWidgets */
  166. struct Knob : ParamWidget {
  167. /** Snap to nearest integer while dragging */
  168. bool snap = false;
  169. float dragValue;
  170. float sensitivity = KNOB_SENSITIVITY;
  171. void onDragStart(EventDragStart &e) override;
  172. void onDragMove(EventDragMove &e) override;
  173. void onDragEnd(EventDragEnd &e) override;
  174. /** Tell engine to smoothly vary this parameter */
  175. void onChange(EventChange &e) override;
  176. };
  177. struct SpriteKnob : virtual Knob, SpriteWidget {
  178. int minIndex, maxIndex, spriteCount;
  179. void step() override;
  180. };
  181. /** A knob which rotates an SVG and caches it in a framebuffer */
  182. struct SVGKnob : virtual Knob, FramebufferWidget {
  183. /** Angles in radians */
  184. float minAngle, maxAngle;
  185. /** Not owned */
  186. TransformWidget *tw;
  187. SVGWidget *sw;
  188. SVGKnob();
  189. void setSVG(std::shared_ptr<SVG> svg);
  190. void step() override;
  191. void onChange(EventChange &e) override;
  192. };
  193. struct SVGSlider : Knob, FramebufferWidget {
  194. /** Intermediate positions will be interpolated between these positions */
  195. Vec minHandlePos, maxHandlePos;
  196. /** Not owned */
  197. SVGWidget *background;
  198. SVGWidget *handle;
  199. SVGSlider();
  200. void step() override;
  201. void onChange(EventChange &e) override;
  202. };
  203. struct Switch : ParamWidget {
  204. };
  205. struct SVGSwitch : virtual Switch, FramebufferWidget {
  206. std::vector<std::shared_ptr<SVG>> frames;
  207. /** Not owned */
  208. SVGWidget *sw;
  209. SVGSwitch();
  210. /** Adds an SVG file to represent the next switch position */
  211. void addFrame(std::shared_ptr<SVG> svg);
  212. void step() override;
  213. void onChange(EventChange &e) override;
  214. };
  215. /** A switch that cycles through each mechanical position */
  216. struct ToggleSwitch : virtual Switch {
  217. void onDragStart(EventDragStart &e) override {
  218. // Cycle through values
  219. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  220. if (value >= maxValue)
  221. setValue(minValue);
  222. else
  223. setValue(value + 1.0);
  224. }
  225. };
  226. /** A switch that is turned on when held */
  227. struct MomentarySwitch : virtual Switch {
  228. /** Don't randomize state */
  229. void randomize() override {}
  230. void onDragStart(EventDragStart &e) override {
  231. setValue(maxValue);
  232. }
  233. void onDragEnd(EventDragEnd &e) override {
  234. setValue(minValue);
  235. }
  236. };
  237. ////////////////////
  238. // lights
  239. ////////////////////
  240. struct LightWidget : TransparentWidget {
  241. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  242. NVGcolor color = nvgRGBf(1, 1, 1);
  243. void draw(NVGcontext *vg) override;
  244. };
  245. /** Mixes a list of colors based on a list of brightness values */
  246. struct MultiLightWidget : LightWidget {
  247. std::vector<NVGcolor> baseColors;
  248. void addBaseColor(NVGcolor baseColor);
  249. /** Sets the color to a linear combination of the baseColors with the given weights */
  250. void setValues(const std::vector<float> &values);
  251. };
  252. /** A MultiLightWidget that points to a module's Light or a range of lights
  253. Will access firstLightId, firstLightId + 1, etc. for each added color
  254. */
  255. struct ModuleLightWidget : MultiLightWidget {
  256. Module *module = NULL;
  257. int firstLightId;
  258. void step() override;
  259. };
  260. ////////////////////
  261. // ports
  262. ////////////////////
  263. struct Port : OpaqueWidget {
  264. enum PortType {
  265. INPUT,
  266. OUTPUT
  267. };
  268. Module *module = NULL;
  269. PortType type = INPUT;
  270. int portId;
  271. MultiLightWidget *plugLight;
  272. Port();
  273. ~Port();
  274. void step() override;
  275. void draw(NVGcontext *vg) override;
  276. void onMouseDown(EventMouseDown &e) override;
  277. void onDragStart(EventDragStart &e) override;
  278. void onDragEnd(EventDragEnd &e) override;
  279. void onDragDrop(EventDragDrop &e) override;
  280. void onDragEnter(EventDragEnter &e) override;
  281. void onDragLeave(EventDragEnter &e) override;
  282. };
  283. struct SVGPort : Port, FramebufferWidget {
  284. SVGWidget *background;
  285. SVGPort();
  286. void draw(NVGcontext *vg) override;
  287. };
  288. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  289. struct SVGScrew : FramebufferWidget {
  290. SVGWidget *sw;
  291. SVGScrew();
  292. };
  293. ////////////////////
  294. // scene
  295. ////////////////////
  296. struct Toolbar : OpaqueWidget {
  297. Slider *wireOpacitySlider;
  298. Slider *wireTensionSlider;
  299. Slider *zoomSlider;
  300. RadioButton *cpuUsageButton;
  301. Toolbar();
  302. void draw(NVGcontext *vg) override;
  303. };
  304. struct PluginManagerWidget : Widget {
  305. Widget *loginWidget;
  306. Widget *manageWidget;
  307. Widget *downloadWidget;
  308. PluginManagerWidget();
  309. void step() override;
  310. };
  311. struct RackScrollWidget : ScrollWidget {
  312. void step() override;
  313. };
  314. struct RackScene : Scene {
  315. ScrollWidget *scrollWidget;
  316. ZoomWidget *zoomWidget;
  317. RackScene();
  318. void step() override;
  319. void draw(NVGcontext *vg) override;
  320. void onHoverKey(EventHoverKey &e) override;
  321. void onPathDrop(EventPathDrop &e) override;
  322. };
  323. ////////////////////
  324. // globals
  325. ////////////////////
  326. extern std::string gApplicationName;
  327. extern std::string gApplicationVersion;
  328. extern std::string gApiHost;
  329. // Easy access to "singleton" widgets
  330. extern RackScene *gRackScene;
  331. extern RackWidget *gRackWidget;
  332. extern Toolbar *gToolbar;
  333. void sceneInit();
  334. void sceneDestroy();
  335. } // namespace rack