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