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.

482 lines
12KB

  1. #pragma once
  2. #include <vector>
  3. #include <jansson.h>
  4. #include "widgets.hpp"
  5. static const float SVG_DPI = 75.0;
  6. static const float MM_PER_IN = 25.4;
  7. #define CHECKMARK_STRING "✔"
  8. #define CHECKMARK(_cond) ((_cond) ? CHECKMARK_STRING : "")
  9. namespace rack {
  10. inline float in2px(float inches) {
  11. return inches * SVG_DPI;
  12. }
  13. inline Vec in2px(Vec inches) {
  14. return inches.mult(SVG_DPI);
  15. }
  16. inline float mm2px(float millimeters) {
  17. return millimeters * (SVG_DPI / MM_PER_IN);
  18. }
  19. inline Vec mm2px(Vec millimeters) {
  20. return millimeters.mult(SVG_DPI / MM_PER_IN);
  21. }
  22. struct Model;
  23. struct Module;
  24. struct Wire;
  25. struct RackWidget;
  26. struct ParamWidget;
  27. struct Port;
  28. struct SVGPanel;
  29. ////////////////////
  30. // module
  31. ////////////////////
  32. // A 1HPx3U module should be 15x380 pixels. Thus the width of a module should be a factor of 15.
  33. static const float RACK_GRID_WIDTH = 15;
  34. static const float RACK_GRID_HEIGHT = 380;
  35. static const Vec RACK_GRID_SIZE = Vec(RACK_GRID_WIDTH, RACK_GRID_HEIGHT);
  36. struct ModuleWidget : OpaqueWidget {
  37. Model *model = NULL;
  38. /** Owns the module pointer */
  39. Module *module = NULL;
  40. SVGPanel *panel = NULL;
  41. std::vector<Port*> inputs;
  42. std::vector<Port*> outputs;
  43. std::vector<ParamWidget*> params;
  44. ~ModuleWidget();
  45. void setModule(Module *module);
  46. /** Convenience functions for adding special widgets (calls addChild()) */
  47. void addInput(Port *input);
  48. void addOutput(Port *output);
  49. void addParam(ParamWidget *param);
  50. void setPanel(std::shared_ptr<SVG> svg);
  51. virtual json_t *toJson();
  52. virtual void fromJson(json_t *rootJ);
  53. virtual void create();
  54. virtual void _delete();
  55. /** Disconnects cables from all ports
  56. Called when the user clicks Disconnect Cables in the context menu.
  57. */
  58. virtual void disconnect();
  59. /** Resets the parameters of the module and calls the Module's randomize().
  60. Called when the user clicks Initialize in the context menu.
  61. */
  62. virtual void reset();
  63. /** Deprecated */
  64. virtual void initialize() final {}
  65. /** Randomizes the parameters of the module and calls the Module's randomize().
  66. Called when the user clicks Randomize in the context menu.
  67. */
  68. virtual void randomize();
  69. virtual Menu *createContextMenu();
  70. void draw(NVGcontext *vg) override;
  71. void drawShadow(NVGcontext *vg);
  72. Vec dragPos;
  73. void onMouseDown(EventMouseDown &e) override;
  74. void onMouseMove(EventMouseMove &e) override;
  75. void onHoverKey(EventHoverKey &e) override;
  76. void onDragStart(EventDragStart &e) override;
  77. void onDragEnd(EventDragEnd &e) override;
  78. void onDragMove(EventDragMove &e) override;
  79. };
  80. struct WireWidget : OpaqueWidget {
  81. Port *outputPort = NULL;
  82. Port *inputPort = NULL;
  83. Port *hoveredOutputPort = NULL;
  84. Port *hoveredInputPort = NULL;
  85. Wire *wire = NULL;
  86. NVGcolor color;
  87. WireWidget();
  88. ~WireWidget();
  89. /** Synchronizes the plugged state of the widget to the owned wire */
  90. void updateWire();
  91. Vec getOutputPos();
  92. Vec getInputPos();
  93. json_t *toJson();
  94. void fromJson(json_t *rootJ);
  95. void draw(NVGcontext *vg) override;
  96. void drawPlugs(NVGcontext *vg);
  97. };
  98. struct WireContainer : TransparentWidget {
  99. WireWidget *activeWire = NULL;
  100. /** Takes ownership of `w` and adds it as a child if it isn't already */
  101. void setActiveWire(WireWidget *w);
  102. /** "Drops" the wire onto the port, making an engine connection if successful */
  103. void commitActiveWire();
  104. void removeTopWire(Port *port);
  105. void removeAllWires(Port *port);
  106. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  107. WireWidget *getTopWire(Port *port);
  108. void draw(NVGcontext *vg) override;
  109. };
  110. struct RackWidget : OpaqueWidget {
  111. FramebufferWidget *rails;
  112. // Only put ModuleWidgets in here
  113. Widget *moduleContainer;
  114. // Only put WireWidgets in here
  115. WireContainer *wireContainer;
  116. std::string lastPath;
  117. Vec lastMousePos;
  118. RackWidget();
  119. ~RackWidget();
  120. /** Completely clear the rack's modules and wires */
  121. void clear();
  122. /** Clears the rack and loads the template patch */
  123. void reset();
  124. void openDialog();
  125. void saveDialog();
  126. void saveAsDialog();
  127. void savePatch(std::string filename);
  128. void loadPatch(std::string filename);
  129. json_t *toJson();
  130. void fromJson(json_t *rootJ);
  131. void addModule(ModuleWidget *m);
  132. /** Removes the module and transfers ownership to the caller */
  133. void deleteModule(ModuleWidget *m);
  134. void cloneModule(ModuleWidget *m);
  135. /** Sets a module's box if non-colliding. Returns true if set */
  136. bool requestModuleBox(ModuleWidget *m, Rect box);
  137. /** Moves a module to the closest non-colliding position */
  138. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  139. void step() override;
  140. void draw(NVGcontext *vg) override;
  141. void onMouseMove(EventMouseMove &e) override;
  142. void onMouseDown(EventMouseDown &e) override;
  143. void onZoom(EventZoom &e) override;
  144. };
  145. struct RackRail : TransparentWidget {
  146. void draw(NVGcontext *vg) override;
  147. };
  148. struct AddModuleWindow : Window {
  149. Vec modulePos;
  150. AddModuleWindow();
  151. void step() override;
  152. };
  153. struct Panel : TransparentWidget {
  154. NVGcolor backgroundColor;
  155. std::shared_ptr<Image> backgroundImage;
  156. void draw(NVGcontext *vg) override;
  157. };
  158. struct SVGPanel : FramebufferWidget {
  159. void step() override;
  160. void setBackground(std::shared_ptr<SVG> svg);
  161. };
  162. ////////////////////
  163. // params
  164. ////////////////////
  165. struct CircularShadow : TransparentWidget {
  166. float blur = 0.0;
  167. void draw(NVGcontext *vg) override;
  168. };
  169. struct ParamWidget : OpaqueWidget, QuantityWidget {
  170. Module *module = NULL;
  171. int paramId;
  172. /** Used to momentarily disable value randomization
  173. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  174. */
  175. bool randomizable = true;
  176. json_t *toJson();
  177. void fromJson(json_t *rootJ);
  178. virtual void reset();
  179. virtual void randomize();
  180. void onMouseDown(EventMouseDown &e) override;
  181. void onChange(EventChange &e) override;
  182. template <typename T = ParamWidget>
  183. static T *create(Vec pos, Module *module, int paramId, float minValue, float maxValue, float defaultValue) {
  184. T *o = Widget::create<T>(pos);
  185. o->module = module;
  186. o->paramId = paramId;
  187. o->setLimits(minValue, maxValue);
  188. o->setDefaultValue(defaultValue);
  189. return o;
  190. }
  191. };
  192. /** Implements vertical dragging behavior for ParamWidgets */
  193. struct Knob : ParamWidget {
  194. /** Snap to nearest integer while dragging */
  195. bool snap = false;
  196. /** Multiplier for mouse movement to adjust knob value */
  197. float speed = 1.0;
  198. float dragValue;
  199. void onDragStart(EventDragStart &e) override;
  200. void onDragMove(EventDragMove &e) override;
  201. void onDragEnd(EventDragEnd &e) override;
  202. /** Tell engine to smoothly vary this parameter */
  203. void onChange(EventChange &e) override;
  204. };
  205. struct SpriteKnob : virtual Knob, SpriteWidget {
  206. int minIndex, maxIndex, spriteCount;
  207. void step() override;
  208. };
  209. /** A knob which rotates an SVG and caches it in a framebuffer */
  210. struct SVGKnob : virtual Knob, FramebufferWidget {
  211. /** Angles in radians */
  212. float minAngle, maxAngle;
  213. /** Not owned */
  214. TransformWidget *tw;
  215. SVGWidget *sw;
  216. SVGKnob();
  217. void setSVG(std::shared_ptr<SVG> svg);
  218. void step() override;
  219. void onChange(EventChange &e) override;
  220. };
  221. struct SVGFader : Knob, FramebufferWidget {
  222. /** Intermediate positions will be interpolated between these positions */
  223. Vec minHandlePos, maxHandlePos;
  224. /** Not owned */
  225. SVGWidget *background;
  226. SVGWidget *handle;
  227. SVGFader();
  228. void step() override;
  229. void onChange(EventChange &e) override;
  230. };
  231. struct Switch : ParamWidget {
  232. };
  233. struct SVGSwitch : virtual Switch, FramebufferWidget {
  234. std::vector<std::shared_ptr<SVG>> frames;
  235. /** Not owned */
  236. SVGWidget *sw;
  237. SVGSwitch();
  238. /** Adds an SVG file to represent the next switch position */
  239. void addFrame(std::shared_ptr<SVG> svg);
  240. void onChange(EventChange &e) override;
  241. };
  242. /** A switch that cycles through each mechanical position */
  243. struct ToggleSwitch : virtual Switch {
  244. void onDragStart(EventDragStart &e) override {
  245. // Cycle through values
  246. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  247. if (value >= maxValue)
  248. setValue(minValue);
  249. else
  250. setValue(value + 1.0);
  251. }
  252. };
  253. /** A switch that is turned on when held */
  254. struct MomentarySwitch : virtual Switch {
  255. /** Don't randomize state */
  256. void randomize() override {}
  257. void onDragStart(EventDragStart &e) override {
  258. setValue(maxValue);
  259. EventAction eAction;
  260. onAction(eAction);
  261. }
  262. void onDragEnd(EventDragEnd &e) override {
  263. setValue(minValue);
  264. }
  265. };
  266. ////////////////////
  267. // IO widgets
  268. ////////////////////
  269. struct AudioIO;
  270. struct MidiIO;
  271. struct AudioWidget : OpaqueWidget {
  272. /** Not owned */
  273. AudioIO *audioIO = NULL;
  274. void onMouseDown(EventMouseDown &e) override;
  275. };
  276. struct MidiWidget : OpaqueWidget {
  277. /** Not owned */
  278. MidiIO *midiIO = NULL;
  279. void onMouseDown(EventMouseDown &e) override;
  280. };
  281. ////////////////////
  282. // lights
  283. ////////////////////
  284. struct LightWidget : TransparentWidget {
  285. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  286. NVGcolor color = nvgRGBf(1, 1, 1);
  287. void draw(NVGcontext *vg) override;
  288. virtual void drawLight(NVGcontext *vg);
  289. virtual void drawHalo(NVGcontext *vg);
  290. };
  291. /** Mixes a list of colors based on a list of brightness values */
  292. struct MultiLightWidget : LightWidget {
  293. std::vector<NVGcolor> baseColors;
  294. void addBaseColor(NVGcolor baseColor);
  295. /** Sets the color to a linear combination of the baseColors with the given weights */
  296. void setValues(const std::vector<float> &values);
  297. };
  298. /** A MultiLightWidget that points to a module's Light or a range of lights
  299. Will access firstLightId, firstLightId + 1, etc. for each added color
  300. */
  301. struct ModuleLightWidget : MultiLightWidget {
  302. Module *module = NULL;
  303. int firstLightId;
  304. void step() override;
  305. template <typename T = ModuleLightWidget>
  306. static T *create(Vec pos, Module *module, int firstLightId) {
  307. T *o = Widget::create<T>(pos);
  308. o->module = module;
  309. o->firstLightId = firstLightId;
  310. return o;
  311. }
  312. };
  313. ////////////////////
  314. // ports
  315. ////////////////////
  316. struct Port : OpaqueWidget {
  317. enum PortType {
  318. INPUT,
  319. OUTPUT
  320. };
  321. Module *module = NULL;
  322. PortType type = INPUT;
  323. int portId;
  324. MultiLightWidget *plugLight;
  325. Port();
  326. ~Port();
  327. void step() override;
  328. void draw(NVGcontext *vg) override;
  329. void onMouseDown(EventMouseDown &e) override;
  330. void onDragStart(EventDragStart &e) override;
  331. void onDragEnd(EventDragEnd &e) override;
  332. void onDragDrop(EventDragDrop &e) override;
  333. void onDragEnter(EventDragEnter &e) override;
  334. void onDragLeave(EventDragEnter &e) override;
  335. template <typename T = Port>
  336. static T *create(Vec pos, PortType type, Module *module, int portId) {
  337. T *o = Widget::create<T>(pos);
  338. o->type = type;
  339. o->module = module;
  340. o->portId = portId;
  341. return o;
  342. }
  343. };
  344. struct SVGPort : Port, FramebufferWidget {
  345. SVGWidget *background;
  346. SVGPort();
  347. void draw(NVGcontext *vg) override;
  348. };
  349. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  350. struct SVGScrew : FramebufferWidget {
  351. SVGWidget *sw;
  352. SVGScrew();
  353. };
  354. ////////////////////
  355. // scene
  356. ////////////////////
  357. struct Toolbar : OpaqueWidget {
  358. Slider *wireOpacitySlider;
  359. Slider *wireTensionSlider;
  360. Slider *zoomSlider;
  361. RadioButton *cpuUsageButton;
  362. Toolbar();
  363. void draw(NVGcontext *vg) override;
  364. };
  365. struct PluginManagerWidget : Widget {
  366. Widget *loginWidget;
  367. Widget *manageWidget;
  368. Widget *downloadWidget;
  369. PluginManagerWidget();
  370. void step() override;
  371. };
  372. struct RackScrollWidget : ScrollWidget {
  373. void step() override;
  374. };
  375. struct RackScene : Scene {
  376. ScrollWidget *scrollWidget;
  377. ZoomWidget *zoomWidget;
  378. RackScene();
  379. void step() override;
  380. void draw(NVGcontext *vg) override;
  381. void onHoverKey(EventHoverKey &e) override;
  382. void onPathDrop(EventPathDrop &e) override;
  383. };
  384. ////////////////////
  385. // globals
  386. ////////////////////
  387. extern std::string gApplicationName;
  388. extern std::string gApplicationVersion;
  389. extern std::string gApiHost;
  390. // Easy access to "singleton" widgets
  391. extern RackScene *gRackScene;
  392. extern RackWidget *gRackWidget;
  393. extern Toolbar *gToolbar;
  394. void sceneInit();
  395. void sceneDestroy();
  396. json_t *colorToJson(NVGcolor color);
  397. NVGcolor jsonToColor(json_t *colorJ);
  398. } // namespace rack