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.

563 lines
15KB

  1. #pragma once
  2. #include <vector>
  3. #include <jansson.h>
  4. #include "widgets.hpp"
  5. #include "ui.hpp"
  6. static const float SVG_DPI = 75.0;
  7. static const float MM_PER_IN = 25.4;
  8. namespace rack {
  9. inline float in2px(float inches) {
  10. return inches * SVG_DPI;
  11. }
  12. inline Vec in2px(Vec inches) {
  13. return inches.mult(SVG_DPI);
  14. }
  15. inline float mm2px(float millimeters) {
  16. return millimeters * (SVG_DPI / MM_PER_IN);
  17. }
  18. inline Vec mm2px(Vec millimeters) {
  19. return millimeters.mult(SVG_DPI / MM_PER_IN);
  20. }
  21. struct Model;
  22. struct Module;
  23. struct Wire;
  24. struct RackWidget;
  25. struct ParamWidget;
  26. struct Port;
  27. struct SVGPanel;
  28. ////////////////////
  29. // module
  30. ////////////////////
  31. // A 1HPx3U module should be 15x380 pixels. Thus the width of a module should be a factor of 15.
  32. static const float RACK_GRID_WIDTH = 15;
  33. static const float RACK_GRID_HEIGHT = 380;
  34. static const Vec RACK_GRID_SIZE = Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  35. struct ModuleWidget : OpaqueWidget {
  36. Model *model = NULL;
  37. /** Owns the module pointer */
  38. Module *module = NULL;
  39. SVGPanel *panel = NULL;
  40. std::vector<Port*> inputs;
  41. std::vector<Port*> outputs;
  42. std::vector<ParamWidget*> params;
  43. ModuleWidget(Module *module);
  44. ~ModuleWidget();
  45. /** Convenience functions for adding special widgets (calls addChild()) */
  46. void addInput(Port *input);
  47. void addOutput(Port *output);
  48. void addParam(ParamWidget *param);
  49. void setPanel(std::shared_ptr<SVG> svg);
  50. virtual json_t *toJson();
  51. virtual void fromJson(json_t *rootJ);
  52. virtual void create();
  53. virtual void _delete();
  54. /** Disconnects cables from all ports
  55. Called when the user clicks Disconnect Cables in the context menu.
  56. */
  57. virtual void disconnect();
  58. /** Resets the parameters of the module and calls the Module's randomize().
  59. Called when the user clicks Initialize in the context menu.
  60. */
  61. virtual void reset();
  62. /** Deprecated */
  63. virtual void initialize() final {}
  64. /** Randomizes the parameters of the module and calls the Module's randomize().
  65. Called when the user clicks Randomize in the context menu.
  66. */
  67. virtual void randomize();
  68. /** Do not subclass this to add context menu entries. Use appendContextMenu() instead */
  69. virtual Menu *createContextMenu();
  70. /** Override to add context menu entries to your subclass.
  71. It is recommended to add a blank MenuEntry first for spacing.
  72. */
  73. virtual void appendContextMenu(Menu *menu) {}
  74. void draw(NVGcontext *vg) override;
  75. void drawShadow(NVGcontext *vg);
  76. Vec dragPos;
  77. void onMouseDown(EventMouseDown &e) override;
  78. void onMouseMove(EventMouseMove &e) override;
  79. void onHoverKey(EventHoverKey &e) override;
  80. void onDragStart(EventDragStart &e) override;
  81. void onDragEnd(EventDragEnd &e) override;
  82. void onDragMove(EventDragMove &e) override;
  83. };
  84. struct WireWidget : OpaqueWidget {
  85. Port *outputPort = NULL;
  86. Port *inputPort = NULL;
  87. Port *hoveredOutputPort = NULL;
  88. Port *hoveredInputPort = NULL;
  89. Wire *wire = NULL;
  90. NVGcolor color;
  91. WireWidget();
  92. ~WireWidget();
  93. /** Synchronizes the plugged state of the widget to the owned wire */
  94. void updateWire();
  95. Vec getOutputPos();
  96. Vec getInputPos();
  97. json_t *toJson();
  98. void fromJson(json_t *rootJ);
  99. void draw(NVGcontext *vg) override;
  100. void drawPlugs(NVGcontext *vg);
  101. };
  102. struct WireContainer : TransparentWidget {
  103. WireWidget *activeWire = NULL;
  104. /** Takes ownership of `w` and adds it as a child if it isn't already */
  105. void setActiveWire(WireWidget *w);
  106. /** "Drops" the wire onto the port, making an engine connection if successful */
  107. void commitActiveWire();
  108. void removeTopWire(Port *port);
  109. void removeAllWires(Port *port);
  110. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  111. WireWidget *getTopWire(Port *port);
  112. void draw(NVGcontext *vg) override;
  113. };
  114. struct RackWidget : OpaqueWidget {
  115. FramebufferWidget *rails;
  116. // Only put ModuleWidgets in here
  117. Widget *moduleContainer;
  118. // Only put WireWidgets in here
  119. WireContainer *wireContainer;
  120. std::string lastPath;
  121. Vec lastMousePos;
  122. bool lockModules = false;
  123. RackWidget();
  124. ~RackWidget();
  125. /** Completely clear the rack's modules and wires */
  126. void clear();
  127. /** Clears the rack and loads the template patch */
  128. void reset();
  129. void openDialog();
  130. void saveDialog();
  131. void saveAsDialog();
  132. /** If `lastPath` is defined, ask the user to reload it */
  133. void revert();
  134. /** Disconnects all wires */
  135. void disconnect();
  136. void savePatch(std::string filename);
  137. void loadPatch(std::string filename);
  138. #ifdef USE_VST2
  139. bool loadPatchFromString (const char *_string);
  140. char *savePatchToString (void);
  141. #endif // USE_VST2
  142. json_t *toJson();
  143. void fromJson(json_t *rootJ);
  144. void addModule(ModuleWidget *m);
  145. /** Removes the module and transfers ownership to the caller */
  146. void deleteModule(ModuleWidget *m);
  147. void cloneModule(ModuleWidget *m);
  148. /** Sets a module's box if non-colliding. Returns true if set */
  149. bool requestModuleBox(ModuleWidget *m, Rect box);
  150. /** Moves a module to the closest non-colliding position */
  151. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  152. void step() override;
  153. void draw(NVGcontext *vg) override;
  154. void onMouseMove(EventMouseMove &e) override;
  155. void onMouseDown(EventMouseDown &e) override;
  156. void onZoom(EventZoom &e) override;
  157. };
  158. struct RackRail : TransparentWidget {
  159. void draw(NVGcontext *vg) override;
  160. };
  161. struct Panel : TransparentWidget {
  162. NVGcolor backgroundColor;
  163. std::shared_ptr<Image> backgroundImage;
  164. void draw(NVGcontext *vg) override;
  165. };
  166. struct SVGPanel : FramebufferWidget {
  167. void step() override;
  168. void setBackground(std::shared_ptr<SVG> svg);
  169. };
  170. ////////////////////
  171. // ParamWidgets and other components
  172. ////////////////////
  173. /** A Widget that exists on a Panel and interacts with a Module */
  174. struct Component : OpaqueWidget {
  175. Module *module = NULL;
  176. template <typename T = Component>
  177. static T *create(Vec pos, Module *module) {
  178. T *o = new T();
  179. o->box.pos = pos;
  180. o->module = module;
  181. return o;
  182. }
  183. };
  184. struct CircularShadow : TransparentWidget {
  185. float blurRadius;
  186. float opacity;
  187. CircularShadow();
  188. void draw(NVGcontext *vg) override;
  189. };
  190. /** A Component which has control over a Param (defined in engine.hpp) */
  191. struct ParamWidget : Component, QuantityWidget {
  192. int paramId;
  193. /** Used to momentarily disable value randomization
  194. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  195. */
  196. bool randomizable = true;
  197. /** Apply per-sample smoothing in the engine */
  198. bool smooth = false;
  199. json_t *toJson();
  200. void fromJson(json_t *rootJ);
  201. virtual void reset();
  202. virtual void randomize();
  203. void onMouseDown(EventMouseDown &e) override;
  204. void onChange(EventChange &e) override;
  205. template <typename T = ParamWidget>
  206. static T *create(Vec pos, Module *module, int paramId, float minValue, float maxValue, float defaultValue) {
  207. T *o = Component::create<T>(pos, module);
  208. o->paramId = paramId;
  209. o->setLimits(minValue, maxValue);
  210. o->setDefaultValue(defaultValue);
  211. return o;
  212. }
  213. };
  214. /** Implements vertical dragging behavior for ParamWidgets */
  215. struct Knob : ParamWidget {
  216. /** Snap to nearest integer while dragging */
  217. bool snap = false;
  218. /** Multiplier for mouse movement to adjust knob value */
  219. float speed = 1.0;
  220. float dragValue;
  221. Knob();
  222. void onDragStart(EventDragStart &e) override;
  223. void onDragMove(EventDragMove &e) override;
  224. void onDragEnd(EventDragEnd &e) override;
  225. };
  226. /** Deprecated */
  227. struct SpriteKnob : Knob, SpriteWidget {
  228. int minIndex, maxIndex, spriteCount;
  229. void step() override;
  230. };
  231. /** A knob which rotates an SVG and caches it in a framebuffer */
  232. struct SVGKnob : Knob, FramebufferWidget {
  233. TransformWidget *tw;
  234. SVGWidget *sw;
  235. CircularShadow *shadow;
  236. /** Angles in radians */
  237. float minAngle, maxAngle;
  238. SVGKnob();
  239. void setSVG(std::shared_ptr<SVG> svg);
  240. void step() override;
  241. void onChange(EventChange &e) override;
  242. };
  243. /** Behaves like a knob but linearly moves an SVGWidget between two points.
  244. Can be used for horizontal or vertical linear faders.
  245. */
  246. struct SVGSlider : Knob, FramebufferWidget {
  247. SVGWidget *background;
  248. SVGWidget *handle;
  249. /** Intermediate positions will be interpolated between these positions */
  250. Vec minHandlePos, maxHandlePos;
  251. SVGSlider();
  252. void setSVGs(std::shared_ptr<SVG> backgroundSVG, std::shared_ptr<SVG> handleSVG);
  253. void step() override;
  254. void onChange(EventChange &e) override;
  255. };
  256. /** Deprecated name for SVGSlider */
  257. typedef SVGSlider SVGFader;
  258. /** A ParamWidget with multiple frames corresponding to its value */
  259. struct SVGSwitch : virtual ParamWidget, FramebufferWidget {
  260. std::vector<std::shared_ptr<SVG>> frames;
  261. SVGWidget *sw;
  262. SVGSwitch();
  263. /** Adds an SVG file to represent the next switch position */
  264. void addFrame(std::shared_ptr<SVG> svg);
  265. void onChange(EventChange &e) override;
  266. };
  267. /** A switch that cycles through each mechanical position */
  268. struct ToggleSwitch : virtual ParamWidget {
  269. void onDragStart(EventDragStart &e) override;
  270. };
  271. /** A switch that is turned on when held and turned off when released.
  272. Consider using SVGButton if the switch simply changes the state of your Module when clicked.
  273. */
  274. struct MomentarySwitch : virtual ParamWidget {
  275. /** Don't randomize state */
  276. void randomize() override {}
  277. void onDragStart(EventDragStart &e) override;
  278. void onDragEnd(EventDragEnd &e) override;
  279. };
  280. /** A Component with a default (up) and active (down) state when clicked.
  281. Does not modify a Param, simply calls onAction() of a subclass.
  282. */
  283. struct SVGButton : Component, FramebufferWidget {
  284. Module *module = NULL;
  285. std::shared_ptr<SVG> defaultSVG;
  286. std::shared_ptr<SVG> activeSVG;
  287. SVGWidget *sw;
  288. SVGButton();
  289. /** If `activeSVG` is NULL, `defaultSVG` is used as the active state instead. */
  290. void setSVGs(std::shared_ptr<SVG> defaultSVG, std::shared_ptr<SVG> activeSVG);
  291. void onDragStart(EventDragStart &e) override;
  292. void onDragEnd(EventDragEnd &e) override;
  293. };
  294. ////////////////////
  295. // IO widgets
  296. ////////////////////
  297. struct LedDisplay : VirtualWidget {
  298. void draw(NVGcontext *vg) override;
  299. };
  300. struct LedDisplaySeparator : TransparentWidget {
  301. LedDisplaySeparator();
  302. void draw(NVGcontext *vg) override;
  303. };
  304. struct LedDisplayChoice : TransparentWidget {
  305. std::string text;
  306. std::shared_ptr<Font> font;
  307. Vec textOffset;
  308. NVGcolor color;
  309. LedDisplayChoice();
  310. void draw(NVGcontext *vg) override;
  311. void onMouseDown(EventMouseDown &e) override;
  312. };
  313. struct LedDisplayTextField : TextField {
  314. std::shared_ptr<Font> font;
  315. Vec textOffset;
  316. NVGcolor color;
  317. LedDisplayTextField();
  318. void draw(NVGcontext *vg) override;
  319. int getTextPosition(Vec mousePos) override;
  320. };
  321. struct AudioIO;
  322. struct MidiIO;
  323. struct AudioWidget : LedDisplay {
  324. /** Not owned */
  325. AudioIO *audioIO = NULL;
  326. LedDisplayChoice *driverChoice;
  327. LedDisplaySeparator *driverSeparator;
  328. LedDisplayChoice *deviceChoice;
  329. LedDisplaySeparator *deviceSeparator;
  330. LedDisplayChoice *sampleRateChoice;
  331. LedDisplaySeparator *sampleRateSeparator;
  332. LedDisplayChoice *bufferSizeChoice;
  333. AudioWidget();
  334. void step() override;
  335. };
  336. struct MidiWidget : LedDisplay {
  337. /** Not owned */
  338. MidiIO *midiIO = NULL;
  339. LedDisplayChoice *driverChoice;
  340. LedDisplaySeparator *driverSeparator;
  341. LedDisplayChoice *deviceChoice;
  342. LedDisplaySeparator *deviceSeparator;
  343. LedDisplayChoice *channelChoice;
  344. MidiWidget();
  345. void step() override;
  346. };
  347. ////////////////////
  348. // lights
  349. ////////////////////
  350. struct LightWidget : TransparentWidget {
  351. NVGcolor bgColor = nvgRGBA(0, 0, 0, 0);
  352. NVGcolor color = nvgRGBA(0, 0, 0, 0);
  353. NVGcolor borderColor = nvgRGBA(0, 0, 0, 0);
  354. void draw(NVGcontext *vg) override;
  355. virtual void drawLight(NVGcontext *vg);
  356. virtual void drawHalo(NVGcontext *vg);
  357. };
  358. /** Mixes a list of colors based on a list of brightness values */
  359. struct MultiLightWidget : LightWidget {
  360. /** Colors of each value state */
  361. std::vector<NVGcolor> baseColors;
  362. void addBaseColor(NVGcolor baseColor);
  363. /** Sets the color to a linear combination of the baseColors with the given weights */
  364. void setValues(const std::vector<float> &values);
  365. };
  366. /** A MultiLightWidget that points to a module's Light or a range of lights
  367. Will access firstLightId, firstLightId + 1, etc. for each added color
  368. */
  369. struct ModuleLightWidget : MultiLightWidget {
  370. Module *module = NULL;
  371. int firstLightId;
  372. void step() override;
  373. template <typename T = ModuleLightWidget>
  374. static T *create(Vec pos, Module *module, int firstLightId) {
  375. T *o = Widget::create<T>(pos);
  376. o->module = module;
  377. o->firstLightId = firstLightId;
  378. return o;
  379. }
  380. };
  381. ////////////////////
  382. // ports
  383. ////////////////////
  384. struct Port : Component {
  385. enum PortType {
  386. INPUT,
  387. OUTPUT
  388. };
  389. PortType type = INPUT;
  390. int portId;
  391. MultiLightWidget *plugLight;
  392. Port();
  393. ~Port();
  394. void step() override;
  395. void draw(NVGcontext *vg) override;
  396. void onMouseDown(EventMouseDown &e) override;
  397. void onDragStart(EventDragStart &e) override;
  398. void onDragEnd(EventDragEnd &e) override;
  399. void onDragDrop(EventDragDrop &e) override;
  400. void onDragEnter(EventDragEnter &e) override;
  401. void onDragLeave(EventDragEnter &e) override;
  402. template <typename T = Port>
  403. static T *create(Vec pos, PortType type, Module *module, int portId) {
  404. T *o = Component::create<T>(pos, module);
  405. o->type = type;
  406. o->portId = portId;
  407. return o;
  408. }
  409. };
  410. struct SVGPort : Port, FramebufferWidget {
  411. SVGWidget *background;
  412. CircularShadow *shadow;
  413. SVGPort();
  414. void setSVG(std::shared_ptr<SVG> svg);
  415. void draw(NVGcontext *vg) override;
  416. };
  417. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  418. struct SVGScrew : FramebufferWidget {
  419. SVGWidget *sw;
  420. SVGScrew();
  421. };
  422. ////////////////////
  423. // scene
  424. ////////////////////
  425. struct Toolbar : OpaqueWidget {
  426. Slider *wireOpacitySlider;
  427. Slider *wireTensionSlider;
  428. Slider *zoomSlider;
  429. RadioButton *cpuUsageButton;
  430. Toolbar();
  431. void draw(NVGcontext *vg) override;
  432. };
  433. struct PluginManagerWidget : VirtualWidget {
  434. Widget *loginWidget;
  435. Widget *manageWidget;
  436. Widget *downloadWidget;
  437. PluginManagerWidget();
  438. void step() override;
  439. };
  440. struct RackScrollWidget : ScrollWidget {
  441. void step() override;
  442. };
  443. struct RackScene : Scene {
  444. ScrollWidget *scrollWidget;
  445. ZoomWidget *zoomWidget;
  446. RackScene();
  447. void step() override;
  448. void draw(NVGcontext *vg) override;
  449. void onHoverKey(EventHoverKey &e) override;
  450. void onPathDrop(EventPathDrop &e) override;
  451. };
  452. ////////////////////
  453. // globals
  454. ////////////////////
  455. extern std::string gApplicationName;
  456. extern std::string gApplicationVersion;
  457. extern std::string gApiHost;
  458. extern std::string gLatestVersion;
  459. extern bool gCheckVersion;
  460. // Easy access to "singleton" widgets
  461. extern RackScene *gRackScene;
  462. extern RackWidget *gRackWidget;
  463. extern Toolbar *gToolbar;
  464. void appInit(bool devMode);
  465. void appDestroy();
  466. void appModuleBrowserCreate();
  467. json_t *appModuleBrowserToJson();
  468. void appModuleBrowserFromJson(json_t *rootJ);
  469. /** Deprecated. Will be removed in v1 */
  470. json_t *colorToJson(NVGcolor color);
  471. /** Deprecated. Will be removed in v1 */
  472. NVGcolor jsonToColor(json_t *colorJ);
  473. } // namespace rack