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.

523 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. NVGcolor color;
  286. LedDisplayChoice();
  287. void draw(NVGcontext *vg) override;
  288. void onMouseDown(EventMouseDown &e) override;
  289. };
  290. struct LedDisplayTextField : TextField {
  291. std::shared_ptr<Font> font;
  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. struct Internal;
  303. Internal *internal;
  304. AudioWidget();
  305. ~AudioWidget();
  306. void step() override;
  307. };
  308. struct MidiWidget : LedDisplay {
  309. /** Not owned */
  310. MidiIO *midiIO = NULL;
  311. struct Internal;
  312. Internal *internal;
  313. MidiWidget();
  314. ~MidiWidget();
  315. void step() override;
  316. };
  317. ////////////////////
  318. // lights
  319. ////////////////////
  320. struct LightWidget : TransparentWidget {
  321. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  322. NVGcolor color = nvgRGBf(1, 1, 1);
  323. void draw(NVGcontext *vg) override;
  324. virtual void drawLight(NVGcontext *vg);
  325. virtual void drawHalo(NVGcontext *vg);
  326. };
  327. /** Mixes a list of colors based on a list of brightness values */
  328. struct MultiLightWidget : LightWidget {
  329. std::vector<NVGcolor> baseColors;
  330. void addBaseColor(NVGcolor baseColor);
  331. /** Sets the color to a linear combination of the baseColors with the given weights */
  332. void setValues(const std::vector<float> &values);
  333. };
  334. /** A MultiLightWidget that points to a module's Light or a range of lights
  335. Will access firstLightId, firstLightId + 1, etc. for each added color
  336. */
  337. struct ModuleLightWidget : MultiLightWidget {
  338. Module *module = NULL;
  339. int firstLightId;
  340. void step() override;
  341. template <typename T = ModuleLightWidget>
  342. static T *create(Vec pos, Module *module, int firstLightId) {
  343. T *o = Widget::create<T>(pos);
  344. o->module = module;
  345. o->firstLightId = firstLightId;
  346. return o;
  347. }
  348. };
  349. ////////////////////
  350. // ports
  351. ////////////////////
  352. struct Port : OpaqueWidget {
  353. enum PortType {
  354. INPUT,
  355. OUTPUT
  356. };
  357. Module *module = NULL;
  358. PortType type = INPUT;
  359. int portId;
  360. MultiLightWidget *plugLight;
  361. Port();
  362. ~Port();
  363. void step() override;
  364. void draw(NVGcontext *vg) override;
  365. void onMouseDown(EventMouseDown &e) override;
  366. void onDragStart(EventDragStart &e) override;
  367. void onDragEnd(EventDragEnd &e) override;
  368. void onDragDrop(EventDragDrop &e) override;
  369. void onDragEnter(EventDragEnter &e) override;
  370. void onDragLeave(EventDragEnter &e) override;
  371. template <typename T = Port>
  372. static T *create(Vec pos, PortType type, Module *module, int portId) {
  373. T *o = Widget::create<T>(pos);
  374. o->type = type;
  375. o->module = module;
  376. o->portId = portId;
  377. return o;
  378. }
  379. };
  380. struct SVGPort : Port, FramebufferWidget {
  381. SVGWidget *background;
  382. SVGPort();
  383. void draw(NVGcontext *vg) override;
  384. };
  385. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  386. struct SVGScrew : FramebufferWidget {
  387. SVGWidget *sw;
  388. SVGScrew();
  389. };
  390. ////////////////////
  391. // scene
  392. ////////////////////
  393. struct Toolbar : OpaqueWidget {
  394. Slider *wireOpacitySlider;
  395. Slider *wireTensionSlider;
  396. Slider *zoomSlider;
  397. RadioButton *cpuUsageButton;
  398. Toolbar();
  399. void draw(NVGcontext *vg) override;
  400. };
  401. struct PluginManagerWidget : Widget {
  402. Widget *loginWidget;
  403. Widget *manageWidget;
  404. Widget *downloadWidget;
  405. PluginManagerWidget();
  406. void step() override;
  407. };
  408. struct RackScrollWidget : ScrollWidget {
  409. void step() override;
  410. };
  411. struct RackScene : Scene {
  412. ScrollWidget *scrollWidget;
  413. ZoomWidget *zoomWidget;
  414. RackScene();
  415. void step() override;
  416. void draw(NVGcontext *vg) override;
  417. void onHoverKey(EventHoverKey &e) override;
  418. void onPathDrop(EventPathDrop &e) override;
  419. };
  420. ////////////////////
  421. // globals
  422. ////////////////////
  423. extern std::string gApplicationName;
  424. extern std::string gApplicationVersion;
  425. extern std::string gApiHost;
  426. // Easy access to "singleton" widgets
  427. extern RackScene *gRackScene;
  428. extern RackWidget *gRackWidget;
  429. extern Toolbar *gToolbar;
  430. void sceneInit();
  431. void sceneDestroy();
  432. json_t *colorToJson(NVGcolor color);
  433. NVGcolor jsonToColor(json_t *colorJ);
  434. } // namespace rack