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.

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