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.

558 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 math::Vec in2px(math::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 math::Vec mm2px(math::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 math::Vec RACK_GRID_SIZE = math::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. math::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. math::Vec getOutputPos();
  104. math::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. math::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, math::Rect box);
  157. /** Moves a module to the closest non-colliding position */
  158. bool requestModuleBoxNearest(ModuleWidget *m, math::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 SVGPanel : FramebufferWidget {
  169. void step() override;
  170. void setBackground(std::shared_ptr<SVG> svg);
  171. };
  172. ////////////////////
  173. // ParamWidgets and other components
  174. ////////////////////
  175. /** A Widget that exists on a Panel and interacts with a Module */
  176. struct Component : OpaqueWidget {
  177. Module *module = NULL;
  178. template <typename T = Component>
  179. static T *create(math::Vec pos, Module *module) {
  180. T *o = new T();
  181. o->box.pos = pos;
  182. o->module = module;
  183. return o;
  184. }
  185. };
  186. struct CircularShadow : TransparentWidget {
  187. float blurRadius;
  188. float opacity;
  189. CircularShadow();
  190. void draw(NVGcontext *vg) override;
  191. };
  192. /** A Component which has control over a Param (defined in engine.hpp) */
  193. struct ParamWidget : Component, QuantityWidget {
  194. int paramId;
  195. /** Used to momentarily disable value randomization
  196. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  197. */
  198. bool randomizable = true;
  199. /** Apply per-sample smoothing in the engine */
  200. bool smooth = false;
  201. json_t *toJson();
  202. void fromJson(json_t *rootJ);
  203. virtual void reset();
  204. virtual void randomize();
  205. void onMouseDown(EventMouseDown &e) override;
  206. void onChange(EventChange &e) override;
  207. template <typename T = ParamWidget>
  208. static T *create(math::Vec pos, Module *module, int paramId, float minValue, float maxValue, float defaultValue) {
  209. T *o = Component::create<T>(pos, module);
  210. o->paramId = paramId;
  211. o->setLimits(minValue, maxValue);
  212. o->setDefaultValue(defaultValue);
  213. return o;
  214. }
  215. };
  216. /** Implements vertical dragging behavior for ParamWidgets */
  217. struct Knob : ParamWidget {
  218. /** Snap to nearest integer while dragging */
  219. bool snap = false;
  220. /** Multiplier for mouse movement to adjust knob value */
  221. float speed = 1.0;
  222. float dragValue;
  223. Knob();
  224. void onDragStart(EventDragStart &e) override;
  225. void onDragMove(EventDragMove &e) override;
  226. void onDragEnd(EventDragEnd &e) override;
  227. };
  228. /** A knob which rotates an SVG and caches it in a framebuffer */
  229. struct SVGKnob : Knob, FramebufferWidget {
  230. TransformWidget *tw;
  231. SVGWidget *sw;
  232. CircularShadow *shadow;
  233. /** Angles in radians */
  234. float minAngle, maxAngle;
  235. SVGKnob();
  236. void setSVG(std::shared_ptr<SVG> svg);
  237. void step() override;
  238. void onChange(EventChange &e) override;
  239. };
  240. /** Behaves like a knob but linearly moves an SVGWidget between two points.
  241. Can be used for horizontal or vertical linear faders.
  242. */
  243. struct SVGSlider : Knob, FramebufferWidget {
  244. SVGWidget *background;
  245. SVGWidget *handle;
  246. /** Intermediate positions will be interpolated between these positions */
  247. math::Vec minHandlePos, maxHandlePos;
  248. SVGSlider();
  249. void setSVGs(std::shared_ptr<SVG> backgroundSVG, std::shared_ptr<SVG> handleSVG);
  250. void step() override;
  251. void onChange(EventChange &e) override;
  252. };
  253. /** Deprecated name for SVGSlider */
  254. typedef SVGSlider SVGFader;
  255. /** A ParamWidget with multiple frames corresponding to its value */
  256. struct SVGSwitch : virtual ParamWidget, FramebufferWidget {
  257. std::vector<std::shared_ptr<SVG>> frames;
  258. SVGWidget *sw;
  259. SVGSwitch();
  260. /** Adds an SVG file to represent the next switch position */
  261. void addFrame(std::shared_ptr<SVG> svg);
  262. void onChange(EventChange &e) override;
  263. };
  264. /** A switch that cycles through each mechanical position */
  265. struct ToggleSwitch : virtual ParamWidget {
  266. void onDragStart(EventDragStart &e) override;
  267. };
  268. /** A switch that is turned on when held and turned off when released.
  269. Consider using SVGButton if the switch simply changes the state of your Module when clicked.
  270. */
  271. struct MomentarySwitch : virtual ParamWidget {
  272. /** Don't randomize state */
  273. void randomize() override {}
  274. void onDragStart(EventDragStart &e) override;
  275. void onDragEnd(EventDragEnd &e) override;
  276. };
  277. /** A Component with a default (up) and active (down) state when clicked.
  278. Does not modify a Param, simply calls onAction() of a subclass.
  279. */
  280. struct SVGButton : Component, FramebufferWidget {
  281. Module *module = NULL;
  282. std::shared_ptr<SVG> defaultSVG;
  283. std::shared_ptr<SVG> activeSVG;
  284. SVGWidget *sw;
  285. SVGButton();
  286. /** If `activeSVG` is NULL, `defaultSVG` is used as the active state instead. */
  287. void setSVGs(std::shared_ptr<SVG> defaultSVG, std::shared_ptr<SVG> activeSVG);
  288. void onDragStart(EventDragStart &e) override;
  289. void onDragEnd(EventDragEnd &e) override;
  290. };
  291. ////////////////////
  292. // IO widgets
  293. ////////////////////
  294. struct LedDisplay : VirtualWidget {
  295. void draw(NVGcontext *vg) override;
  296. };
  297. struct LedDisplaySeparator : TransparentWidget {
  298. LedDisplaySeparator();
  299. void draw(NVGcontext *vg) override;
  300. };
  301. struct LedDisplayChoice : TransparentWidget {
  302. std::string text;
  303. std::shared_ptr<Font> font;
  304. math::Vec textOffset;
  305. NVGcolor color;
  306. LedDisplayChoice();
  307. void draw(NVGcontext *vg) override;
  308. void onMouseDown(EventMouseDown &e) override;
  309. };
  310. struct LedDisplayTextField : TextField {
  311. std::shared_ptr<Font> font;
  312. math::Vec textOffset;
  313. NVGcolor color;
  314. LedDisplayTextField();
  315. void draw(NVGcontext *vg) override;
  316. int getTextPosition(math::Vec mousePos) override;
  317. };
  318. struct AudioIO;
  319. struct MidiIO;
  320. struct AudioWidget : LedDisplay {
  321. /** Not owned */
  322. AudioIO *audioIO = NULL;
  323. LedDisplayChoice *driverChoice;
  324. LedDisplaySeparator *driverSeparator;
  325. LedDisplayChoice *deviceChoice;
  326. LedDisplaySeparator *deviceSeparator;
  327. LedDisplayChoice *sampleRateChoice;
  328. LedDisplaySeparator *sampleRateSeparator;
  329. LedDisplayChoice *bufferSizeChoice;
  330. AudioWidget();
  331. void step() override;
  332. };
  333. struct MidiWidget : LedDisplay {
  334. /** Not owned */
  335. MidiIO *midiIO = NULL;
  336. LedDisplayChoice *driverChoice;
  337. LedDisplaySeparator *driverSeparator;
  338. LedDisplayChoice *deviceChoice;
  339. LedDisplaySeparator *deviceSeparator;
  340. LedDisplayChoice *channelChoice;
  341. MidiWidget();
  342. void step() override;
  343. };
  344. ////////////////////
  345. // lights
  346. ////////////////////
  347. struct LightWidget : TransparentWidget {
  348. NVGcolor bgColor = nvgRGBA(0, 0, 0, 0);
  349. NVGcolor color = nvgRGBA(0, 0, 0, 0);
  350. NVGcolor borderColor = nvgRGBA(0, 0, 0, 0);
  351. void draw(NVGcontext *vg) override;
  352. virtual void drawLight(NVGcontext *vg);
  353. virtual void drawHalo(NVGcontext *vg);
  354. };
  355. /** Mixes a list of colors based on a list of brightness values */
  356. struct MultiLightWidget : LightWidget {
  357. /** Colors of each value state */
  358. std::vector<NVGcolor> baseColors;
  359. void addBaseColor(NVGcolor baseColor);
  360. /** Sets the color to a linear combination of the baseColors with the given weights */
  361. void setValues(const std::vector<float> &values);
  362. };
  363. /** A MultiLightWidget that points to a module's Light or a range of lights
  364. Will access firstLightId, firstLightId + 1, etc. for each added color
  365. */
  366. struct ModuleLightWidget : MultiLightWidget {
  367. Module *module = NULL;
  368. int firstLightId;
  369. void step() override;
  370. template <typename T = ModuleLightWidget>
  371. static T *create(math::Vec pos, Module *module, int firstLightId) {
  372. T *o = Widget::create<T>(pos);
  373. o->module = module;
  374. o->firstLightId = firstLightId;
  375. return o;
  376. }
  377. };
  378. ////////////////////
  379. // ports
  380. ////////////////////
  381. struct Port : Component {
  382. enum PortType {
  383. INPUT,
  384. OUTPUT
  385. };
  386. PortType type = INPUT;
  387. int portId;
  388. MultiLightWidget *plugLight;
  389. Port();
  390. ~Port();
  391. void step() override;
  392. void draw(NVGcontext *vg) override;
  393. void onMouseDown(EventMouseDown &e) override;
  394. void onDragStart(EventDragStart &e) override;
  395. void onDragEnd(EventDragEnd &e) override;
  396. void onDragDrop(EventDragDrop &e) override;
  397. void onDragEnter(EventDragEnter &e) override;
  398. void onDragLeave(EventDragEnter &e) override;
  399. template <typename T = Port>
  400. static T *create(math::Vec pos, PortType type, Module *module, int portId) {
  401. T *o = Component::create<T>(pos, module);
  402. o->type = type;
  403. o->portId = portId;
  404. return o;
  405. }
  406. };
  407. struct SVGPort : Port, FramebufferWidget {
  408. SVGWidget *background;
  409. CircularShadow *shadow;
  410. SVGPort();
  411. void setSVG(std::shared_ptr<SVG> svg);
  412. void draw(NVGcontext *vg) override;
  413. };
  414. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  415. struct SVGScrew : FramebufferWidget {
  416. SVGWidget *sw;
  417. SVGScrew();
  418. };
  419. ////////////////////
  420. // scene
  421. ////////////////////
  422. struct Toolbar : OpaqueWidget {
  423. Slider *wireOpacitySlider;
  424. Slider *wireTensionSlider;
  425. Slider *zoomSlider;
  426. RadioButton *cpuUsageButton;
  427. Toolbar();
  428. void draw(NVGcontext *vg) override;
  429. };
  430. struct PluginManagerWidget : VirtualWidget {
  431. Widget *loginWidget;
  432. Widget *manageWidget;
  433. Widget *downloadWidget;
  434. PluginManagerWidget();
  435. void step() override;
  436. };
  437. struct RackScrollWidget : ScrollWidget {
  438. void step() override;
  439. };
  440. struct RackScene : Scene {
  441. ScrollWidget *scrollWidget;
  442. ZoomWidget *zoomWidget;
  443. RackScene();
  444. void step() override;
  445. void draw(NVGcontext *vg) override;
  446. void onHoverKey(EventHoverKey &e) override;
  447. void onPathDrop(EventPathDrop &e) override;
  448. };
  449. ////////////////////
  450. // globals
  451. ////////////////////
  452. extern std::string gApplicationName;
  453. extern std::string gApplicationVersion;
  454. extern std::string gApiHost;
  455. extern std::string gLatestVersion;
  456. extern bool gCheckVersion;
  457. // Easy access to "singleton" widgets
  458. extern RackScene *gRackScene;
  459. extern RackWidget *gRackWidget;
  460. extern Toolbar *gToolbar;
  461. void appInit(bool devMode);
  462. void appDestroy();
  463. void appModuleBrowserCreate();
  464. json_t *appModuleBrowserToJson();
  465. void appModuleBrowserFromJson(json_t *rootJ);
  466. /** Deprecated. Will be removed in v1 */
  467. json_t *colorToJson(NVGcolor color);
  468. /** Deprecated. Will be removed in v1 */
  469. NVGcolor jsonToColor(json_t *colorJ);
  470. } // namespace rack