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.

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