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.

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