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.

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