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.

556 lines
14KB

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