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.

531 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. json_t *toJson();
  183. void fromJson(json_t *rootJ);
  184. virtual void reset();
  185. virtual void randomize();
  186. void onMouseDown(EventMouseDown &e) override;
  187. void onChange(EventChange &e) override;
  188. template <typename T = ParamWidget>
  189. static T *create(Vec pos, Module *module, int paramId, float minValue, float maxValue, float defaultValue) {
  190. T *o = Widget::create<T>(pos);
  191. o->module = module;
  192. o->paramId = paramId;
  193. o->setLimits(minValue, maxValue);
  194. o->setDefaultValue(defaultValue);
  195. return o;
  196. }
  197. };
  198. /** Implements vertical dragging behavior for ParamWidgets */
  199. struct Knob : ParamWidget {
  200. /** Snap to nearest integer while dragging */
  201. bool snap = false;
  202. /** Multiplier for mouse movement to adjust knob value */
  203. float speed = 1.0;
  204. float dragValue;
  205. void onDragStart(EventDragStart &e) override;
  206. void onDragMove(EventDragMove &e) override;
  207. void onDragEnd(EventDragEnd &e) override;
  208. /** Tell engine to smoothly vary this parameter */
  209. void onChange(EventChange &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 : Widget {
  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 bgColor = nvgRGBf(0, 0, 0);
  330. NVGcolor color = nvgRGBf(1, 1, 1);
  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. std::vector<NVGcolor> baseColors;
  338. void addBaseColor(NVGcolor baseColor);
  339. /** Sets the color to a linear combination of the baseColors with the given weights */
  340. void setValues(const std::vector<float> &values);
  341. };
  342. /** A MultiLightWidget that points to a module's Light or a range of lights
  343. Will access firstLightId, firstLightId + 1, etc. for each added color
  344. */
  345. struct ModuleLightWidget : MultiLightWidget {
  346. Module *module = NULL;
  347. int firstLightId;
  348. void step() override;
  349. template <typename T = ModuleLightWidget>
  350. static T *create(Vec pos, Module *module, int firstLightId) {
  351. T *o = Widget::create<T>(pos);
  352. o->module = module;
  353. o->firstLightId = firstLightId;
  354. return o;
  355. }
  356. };
  357. ////////////////////
  358. // ports
  359. ////////////////////
  360. struct Port : OpaqueWidget {
  361. enum PortType {
  362. INPUT,
  363. OUTPUT
  364. };
  365. Module *module = NULL;
  366. PortType type = INPUT;
  367. int portId;
  368. MultiLightWidget *plugLight;
  369. Port();
  370. ~Port();
  371. void step() override;
  372. void draw(NVGcontext *vg) override;
  373. void onMouseDown(EventMouseDown &e) override;
  374. void onDragStart(EventDragStart &e) override;
  375. void onDragEnd(EventDragEnd &e) override;
  376. void onDragDrop(EventDragDrop &e) override;
  377. void onDragEnter(EventDragEnter &e) override;
  378. void onDragLeave(EventDragEnter &e) override;
  379. template <typename T = Port>
  380. static T *create(Vec pos, PortType type, Module *module, int portId) {
  381. T *o = Widget::create<T>(pos);
  382. o->type = type;
  383. o->module = module;
  384. o->portId = portId;
  385. return o;
  386. }
  387. };
  388. struct SVGPort : Port, FramebufferWidget {
  389. SVGWidget *background;
  390. SVGPort();
  391. void draw(NVGcontext *vg) override;
  392. };
  393. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  394. struct SVGScrew : FramebufferWidget {
  395. SVGWidget *sw;
  396. SVGScrew();
  397. };
  398. ////////////////////
  399. // scene
  400. ////////////////////
  401. struct Toolbar : OpaqueWidget {
  402. Slider *wireOpacitySlider;
  403. Slider *wireTensionSlider;
  404. Slider *zoomSlider;
  405. RadioButton *cpuUsageButton;
  406. Toolbar();
  407. void draw(NVGcontext *vg) override;
  408. };
  409. struct PluginManagerWidget : Widget {
  410. Widget *loginWidget;
  411. Widget *manageWidget;
  412. Widget *downloadWidget;
  413. PluginManagerWidget();
  414. void step() override;
  415. };
  416. struct RackScrollWidget : ScrollWidget {
  417. void step() override;
  418. };
  419. struct RackScene : Scene {
  420. ScrollWidget *scrollWidget;
  421. ZoomWidget *zoomWidget;
  422. RackScene();
  423. void step() override;
  424. void draw(NVGcontext *vg) override;
  425. void onHoverKey(EventHoverKey &e) override;
  426. void onPathDrop(EventPathDrop &e) override;
  427. };
  428. ////////////////////
  429. // globals
  430. ////////////////////
  431. extern std::string gApplicationName;
  432. extern std::string gApplicationVersion;
  433. extern std::string gApiHost;
  434. // Easy access to "singleton" widgets
  435. extern RackScene *gRackScene;
  436. extern RackWidget *gRackWidget;
  437. extern Toolbar *gToolbar;
  438. void sceneInit();
  439. void sceneDestroy();
  440. json_t *colorToJson(NVGcolor color);
  441. NVGcolor jsonToColor(json_t *colorJ);
  442. } // namespace rack