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.

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