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.

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