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.

564 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. 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. /** A knob which rotates an SVG and caches it in a framebuffer */
  234. struct SVGKnob : Knob, FramebufferWidget {
  235. TransformWidget *tw;
  236. SVGWidget *sw;
  237. CircularShadow *shadow;
  238. /** Angles in radians */
  239. float minAngle, maxAngle;
  240. SVGKnob();
  241. void setSVG(std::shared_ptr<SVG> svg);
  242. void step() override;
  243. void onChange(EventChange &e) override;
  244. };
  245. /** Behaves like a knob but linearly moves an SVGWidget between two points.
  246. Can be used for horizontal or vertical linear faders.
  247. */
  248. struct SVGSlider : Knob, FramebufferWidget {
  249. SVGWidget *background;
  250. SVGWidget *handle;
  251. /** Intermediate positions will be interpolated between these positions */
  252. Vec minHandlePos, maxHandlePos;
  253. SVGSlider();
  254. void setSVGs(std::shared_ptr<SVG> backgroundSVG, std::shared_ptr<SVG> handleSVG);
  255. void step() override;
  256. void onChange(EventChange &e) override;
  257. };
  258. /** Deprecated name for SVGSlider */
  259. typedef SVGSlider SVGFader;
  260. /** A ParamWidget with multiple frames corresponding to its value */
  261. struct SVGSwitch : virtual ParamWidget, FramebufferWidget {
  262. std::vector<std::shared_ptr<SVG>> frames;
  263. SVGWidget *sw;
  264. SVGSwitch();
  265. /** Adds an SVG file to represent the next switch position */
  266. void addFrame(std::shared_ptr<SVG> svg);
  267. void onChange(EventChange &e) override;
  268. };
  269. /** A switch that cycles through each mechanical position */
  270. struct ToggleSwitch : virtual ParamWidget {
  271. void onDragStart(EventDragStart &e) override;
  272. };
  273. /** A switch that is turned on when held and turned off when released.
  274. Consider using SVGButton if the switch simply changes the state of your Module when clicked.
  275. */
  276. struct MomentarySwitch : virtual ParamWidget {
  277. /** Don't randomize state */
  278. void randomize() override {}
  279. void onDragStart(EventDragStart &e) override;
  280. void onDragEnd(EventDragEnd &e) override;
  281. };
  282. /** A Component with a default (up) and active (down) state when clicked.
  283. Does not modify a Param, simply calls onAction() of a subclass.
  284. */
  285. struct SVGButton : Component, FramebufferWidget {
  286. Module *module = NULL;
  287. std::shared_ptr<SVG> defaultSVG;
  288. std::shared_ptr<SVG> activeSVG;
  289. SVGWidget *sw;
  290. SVGButton();
  291. /** If `activeSVG` is NULL, `defaultSVG` is used as the active state instead. */
  292. void setSVGs(std::shared_ptr<SVG> defaultSVG, std::shared_ptr<SVG> activeSVG);
  293. void onDragStart(EventDragStart &e) override;
  294. void onDragEnd(EventDragEnd &e) override;
  295. };
  296. ////////////////////
  297. // IO widgets
  298. ////////////////////
  299. struct LedDisplay : VirtualWidget {
  300. void draw(NVGcontext *vg) override;
  301. };
  302. struct LedDisplaySeparator : TransparentWidget {
  303. LedDisplaySeparator();
  304. void draw(NVGcontext *vg) override;
  305. };
  306. struct LedDisplayChoice : TransparentWidget {
  307. std::string text;
  308. std::shared_ptr<Font> font;
  309. Vec textOffset;
  310. NVGcolor color;
  311. LedDisplayChoice();
  312. void draw(NVGcontext *vg) override;
  313. void onMouseDown(EventMouseDown &e) override;
  314. };
  315. struct LedDisplayTextField : TextField {
  316. std::shared_ptr<Font> font;
  317. Vec textOffset;
  318. NVGcolor color;
  319. LedDisplayTextField();
  320. void draw(NVGcontext *vg) override;
  321. int getTextPosition(Vec mousePos) override;
  322. };
  323. struct AudioIO;
  324. struct MidiIO;
  325. struct AudioWidget : LedDisplay {
  326. /** Not owned */
  327. AudioIO *audioIO = NULL;
  328. LedDisplayChoice *driverChoice;
  329. LedDisplaySeparator *driverSeparator;
  330. LedDisplayChoice *deviceChoice;
  331. LedDisplaySeparator *deviceSeparator;
  332. LedDisplayChoice *sampleRateChoice;
  333. LedDisplaySeparator *sampleRateSeparator;
  334. LedDisplayChoice *bufferSizeChoice;
  335. AudioWidget();
  336. void step() override;
  337. };
  338. struct MidiWidget : LedDisplay {
  339. /** Not owned */
  340. MidiIO *midiIO = NULL;
  341. LedDisplayChoice *driverChoice;
  342. LedDisplaySeparator *driverSeparator;
  343. LedDisplayChoice *deviceChoice;
  344. LedDisplaySeparator *deviceSeparator;
  345. LedDisplayChoice *channelChoice;
  346. MidiWidget();
  347. void step() override;
  348. };
  349. ////////////////////
  350. // lights
  351. ////////////////////
  352. struct LightWidget : TransparentWidget {
  353. NVGcolor bgColor = nvgRGBA(0, 0, 0, 0);
  354. NVGcolor color = nvgRGBA(0, 0, 0, 0);
  355. NVGcolor borderColor = nvgRGBA(0, 0, 0, 0);
  356. void draw(NVGcontext *vg) override;
  357. virtual void drawLight(NVGcontext *vg);
  358. virtual void drawHalo(NVGcontext *vg);
  359. };
  360. /** Mixes a list of colors based on a list of brightness values */
  361. struct MultiLightWidget : LightWidget {
  362. /** Colors of each value state */
  363. std::vector<NVGcolor> baseColors;
  364. void addBaseColor(NVGcolor baseColor);
  365. /** Sets the color to a linear combination of the baseColors with the given weights */
  366. void setValues(const std::vector<float> &values);
  367. };
  368. /** A MultiLightWidget that points to a module's Light or a range of lights
  369. Will access firstLightId, firstLightId + 1, etc. for each added color
  370. */
  371. struct ModuleLightWidget : MultiLightWidget {
  372. Module *module = NULL;
  373. int firstLightId;
  374. void step() override;
  375. template <typename T = ModuleLightWidget>
  376. static T *create(Vec pos, Module *module, int firstLightId) {
  377. T *o = Widget::create<T>(pos);
  378. o->module = module;
  379. o->firstLightId = firstLightId;
  380. return o;
  381. }
  382. };
  383. ////////////////////
  384. // ports
  385. ////////////////////
  386. struct Port : Component {
  387. enum PortType {
  388. INPUT,
  389. OUTPUT
  390. };
  391. PortType type = INPUT;
  392. int portId;
  393. MultiLightWidget *plugLight;
  394. Port();
  395. ~Port();
  396. void step() override;
  397. void draw(NVGcontext *vg) override;
  398. void onMouseDown(EventMouseDown &e) override;
  399. void onDragStart(EventDragStart &e) override;
  400. void onDragEnd(EventDragEnd &e) override;
  401. void onDragDrop(EventDragDrop &e) override;
  402. void onDragEnter(EventDragEnter &e) override;
  403. void onDragLeave(EventDragEnter &e) override;
  404. template <typename T = Port>
  405. static T *create(Vec pos, PortType type, Module *module, int portId) {
  406. T *o = Component::create<T>(pos, module);
  407. o->type = type;
  408. o->portId = portId;
  409. return o;
  410. }
  411. };
  412. struct SVGPort : Port, FramebufferWidget {
  413. SVGWidget *background;
  414. CircularShadow *shadow;
  415. SVGPort();
  416. void setSVG(std::shared_ptr<SVG> svg);
  417. void draw(NVGcontext *vg) override;
  418. };
  419. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  420. struct SVGScrew : FramebufferWidget {
  421. SVGWidget *sw;
  422. SVGScrew();
  423. };
  424. ////////////////////
  425. // scene
  426. ////////////////////
  427. struct Toolbar : OpaqueWidget {
  428. Slider *wireOpacitySlider;
  429. Slider *wireTensionSlider;
  430. Slider *zoomSlider;
  431. RadioButton *cpuUsageButton;
  432. Toolbar();
  433. void draw(NVGcontext *vg) override;
  434. };
  435. struct PluginManagerWidget : VirtualWidget {
  436. Widget *loginWidget;
  437. Widget *manageWidget;
  438. Widget *downloadWidget;
  439. PluginManagerWidget();
  440. void step() override;
  441. };
  442. struct RackScrollWidget : ScrollWidget {
  443. void step() override;
  444. };
  445. struct RackScene : Scene {
  446. ScrollWidget *scrollWidget;
  447. ZoomWidget *zoomWidget;
  448. RackScene();
  449. void step() override;
  450. void draw(NVGcontext *vg) override;
  451. void onHoverKey(EventHoverKey &e) override;
  452. void onPathDrop(EventPathDrop &e) override;
  453. };
  454. ////////////////////
  455. // globals
  456. ////////////////////
  457. extern std::string gApplicationName;
  458. extern std::string gApplicationVersion;
  459. extern std::string gApiHost;
  460. extern std::string gLatestVersion;
  461. extern bool gCheckVersion;
  462. // Easy access to "singleton" widgets
  463. extern RackScene *gRackScene;
  464. extern RackWidget *gRackWidget;
  465. extern Toolbar *gToolbar;
  466. void appInit(bool devMode);
  467. void appDestroy();
  468. void appModuleBrowserCreate();
  469. json_t *appModuleBrowserToJson();
  470. void appModuleBrowserFromJson(json_t *rootJ);
  471. /** Deprecated. Will be removed in v1 */
  472. json_t *colorToJson(NVGcolor color);
  473. /** Deprecated. Will be removed in v1 */
  474. NVGcolor jsonToColor(json_t *colorJ);
  475. } // namespace rack