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.

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