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.

455 lines
11KB

  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. };
  183. /** Implements vertical dragging behavior for ParamWidgets */
  184. struct Knob : ParamWidget {
  185. /** Snap to nearest integer while dragging */
  186. bool snap = false;
  187. /** Multiplier for mouse movement to adjust knob value */
  188. float speed = 1.0;
  189. float dragValue;
  190. void onDragStart(EventDragStart &e) override;
  191. void onDragMove(EventDragMove &e) override;
  192. void onDragEnd(EventDragEnd &e) override;
  193. /** Tell engine to smoothly vary this parameter */
  194. void onChange(EventChange &e) override;
  195. };
  196. struct SpriteKnob : virtual Knob, SpriteWidget {
  197. int minIndex, maxIndex, spriteCount;
  198. void step() override;
  199. };
  200. /** A knob which rotates an SVG and caches it in a framebuffer */
  201. struct SVGKnob : virtual Knob, FramebufferWidget {
  202. /** Angles in radians */
  203. float minAngle, maxAngle;
  204. /** Not owned */
  205. TransformWidget *tw;
  206. SVGWidget *sw;
  207. SVGKnob();
  208. void setSVG(std::shared_ptr<SVG> svg);
  209. void step() override;
  210. void onChange(EventChange &e) override;
  211. };
  212. struct SVGFader : Knob, FramebufferWidget {
  213. /** Intermediate positions will be interpolated between these positions */
  214. Vec minHandlePos, maxHandlePos;
  215. /** Not owned */
  216. SVGWidget *background;
  217. SVGWidget *handle;
  218. SVGFader();
  219. void step() override;
  220. void onChange(EventChange &e) override;
  221. };
  222. struct Switch : ParamWidget {
  223. };
  224. struct SVGSwitch : virtual Switch, FramebufferWidget {
  225. std::vector<std::shared_ptr<SVG>> frames;
  226. /** Not owned */
  227. SVGWidget *sw;
  228. SVGSwitch();
  229. /** Adds an SVG file to represent the next switch position */
  230. void addFrame(std::shared_ptr<SVG> svg);
  231. void onChange(EventChange &e) override;
  232. };
  233. /** A switch that cycles through each mechanical position */
  234. struct ToggleSwitch : virtual Switch {
  235. void onDragStart(EventDragStart &e) override {
  236. // Cycle through values
  237. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  238. if (value >= maxValue)
  239. setValue(minValue);
  240. else
  241. setValue(value + 1.0);
  242. }
  243. };
  244. /** A switch that is turned on when held */
  245. struct MomentarySwitch : virtual Switch {
  246. /** Don't randomize state */
  247. void randomize() override {}
  248. void onDragStart(EventDragStart &e) override {
  249. setValue(maxValue);
  250. EventAction eAction;
  251. onAction(eAction);
  252. }
  253. void onDragEnd(EventDragEnd &e) override {
  254. setValue(minValue);
  255. }
  256. };
  257. ////////////////////
  258. // IO widgets
  259. ////////////////////
  260. struct AudioIO;
  261. struct MidiIO;
  262. struct AudioWidget : OpaqueWidget {
  263. /** Not owned */
  264. AudioIO *audioIO = NULL;
  265. void onMouseDown(EventMouseDown &e) override;
  266. };
  267. struct MidiWidget : OpaqueWidget {
  268. /** Not owned */
  269. MidiIO *midiIO = NULL;
  270. void onMouseDown(EventMouseDown &e) override;
  271. };
  272. ////////////////////
  273. // lights
  274. ////////////////////
  275. struct LightWidget : TransparentWidget {
  276. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  277. NVGcolor color = nvgRGBf(1, 1, 1);
  278. void draw(NVGcontext *vg) override;
  279. virtual void drawLight(NVGcontext *vg);
  280. virtual void drawHalo(NVGcontext *vg);
  281. };
  282. /** Mixes a list of colors based on a list of brightness values */
  283. struct MultiLightWidget : LightWidget {
  284. std::vector<NVGcolor> baseColors;
  285. void addBaseColor(NVGcolor baseColor);
  286. /** Sets the color to a linear combination of the baseColors with the given weights */
  287. void setValues(const std::vector<float> &values);
  288. };
  289. /** A MultiLightWidget that points to a module's Light or a range of lights
  290. Will access firstLightId, firstLightId + 1, etc. for each added color
  291. */
  292. struct ModuleLightWidget : MultiLightWidget {
  293. Module *module = NULL;
  294. int firstLightId;
  295. void step() override;
  296. };
  297. ////////////////////
  298. // ports
  299. ////////////////////
  300. struct Port : OpaqueWidget {
  301. enum PortType {
  302. INPUT,
  303. OUTPUT
  304. };
  305. Module *module = NULL;
  306. PortType type = INPUT;
  307. int portId;
  308. MultiLightWidget *plugLight;
  309. Port();
  310. ~Port();
  311. void step() override;
  312. void draw(NVGcontext *vg) override;
  313. void onMouseDown(EventMouseDown &e) override;
  314. void onDragStart(EventDragStart &e) override;
  315. void onDragEnd(EventDragEnd &e) override;
  316. void onDragDrop(EventDragDrop &e) override;
  317. void onDragEnter(EventDragEnter &e) override;
  318. void onDragLeave(EventDragEnter &e) override;
  319. };
  320. struct SVGPort : Port, FramebufferWidget {
  321. SVGWidget *background;
  322. SVGPort();
  323. void draw(NVGcontext *vg) override;
  324. };
  325. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  326. struct SVGScrew : FramebufferWidget {
  327. SVGWidget *sw;
  328. SVGScrew();
  329. };
  330. ////////////////////
  331. // scene
  332. ////////////////////
  333. struct Toolbar : OpaqueWidget {
  334. Slider *wireOpacitySlider;
  335. Slider *wireTensionSlider;
  336. Slider *zoomSlider;
  337. RadioButton *cpuUsageButton;
  338. Toolbar();
  339. void draw(NVGcontext *vg) override;
  340. };
  341. struct PluginManagerWidget : Widget {
  342. Widget *loginWidget;
  343. Widget *manageWidget;
  344. Widget *downloadWidget;
  345. PluginManagerWidget();
  346. void step() override;
  347. };
  348. struct RackScrollWidget : ScrollWidget {
  349. void step() override;
  350. };
  351. struct RackScene : Scene {
  352. ScrollWidget *scrollWidget;
  353. ZoomWidget *zoomWidget;
  354. RackScene();
  355. void step() override;
  356. void draw(NVGcontext *vg) override;
  357. void onHoverKey(EventHoverKey &e) override;
  358. void onPathDrop(EventPathDrop &e) override;
  359. };
  360. ////////////////////
  361. // globals
  362. ////////////////////
  363. extern std::string gApplicationName;
  364. extern std::string gApplicationVersion;
  365. extern std::string gApiHost;
  366. // Easy access to "singleton" widgets
  367. extern RackScene *gRackScene;
  368. extern RackWidget *gRackWidget;
  369. extern Toolbar *gToolbar;
  370. void sceneInit();
  371. void sceneDestroy();
  372. json_t *colorToJson(NVGcolor color);
  373. NVGcolor jsonToColor(json_t *colorJ);
  374. } // namespace rack