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