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.

537 lines
13KB

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