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.

453 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. Vec dragPos;
  72. void onMouseDown(EventMouseDown &e) override;
  73. void onMouseMove(EventMouseMove &e) override;
  74. void onHoverKey(EventHoverKey &e) override;
  75. void onDragStart(EventDragStart &e) override;
  76. void onDragEnd(EventDragEnd &e) override;
  77. void onDragMove(EventDragMove &e) override;
  78. };
  79. struct WireWidget : OpaqueWidget {
  80. Port *outputPort = NULL;
  81. Port *inputPort = NULL;
  82. Port *hoveredOutputPort = NULL;
  83. Port *hoveredInputPort = NULL;
  84. Wire *wire = NULL;
  85. NVGcolor color;
  86. WireWidget();
  87. ~WireWidget();
  88. /** Synchronizes the plugged state of the widget to the owned wire */
  89. void updateWire();
  90. Vec getOutputPos();
  91. Vec getInputPos();
  92. json_t *toJson();
  93. void fromJson(json_t *rootJ);
  94. void draw(NVGcontext *vg) override;
  95. void drawPlugs(NVGcontext *vg);
  96. };
  97. struct WireContainer : TransparentWidget {
  98. WireWidget *activeWire = NULL;
  99. /** Takes ownership of `w` and adds it as a child if it isn't already */
  100. void setActiveWire(WireWidget *w);
  101. /** "Drops" the wire onto the port, making an engine connection if successful */
  102. void commitActiveWire();
  103. void removeTopWire(Port *port);
  104. void removeAllWires(Port *port);
  105. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  106. WireWidget *getTopWire(Port *port);
  107. void draw(NVGcontext *vg) override;
  108. };
  109. struct RackWidget : OpaqueWidget {
  110. FramebufferWidget *rails;
  111. // Only put ModuleWidgets in here
  112. Widget *moduleContainer;
  113. // Only put WireWidgets in here
  114. WireContainer *wireContainer;
  115. std::string lastPath;
  116. Vec lastMousePos;
  117. RackWidget();
  118. ~RackWidget();
  119. /** Completely clear the rack's modules and wires */
  120. void clear();
  121. /** Clears the rack and loads the template patch */
  122. void reset();
  123. void openDialog();
  124. void saveDialog();
  125. void saveAsDialog();
  126. void savePatch(std::string filename);
  127. void loadPatch(std::string filename);
  128. json_t *toJson();
  129. void fromJson(json_t *rootJ);
  130. void addModule(ModuleWidget *m);
  131. /** Removes the module and transfers ownership to the caller */
  132. void deleteModule(ModuleWidget *m);
  133. void cloneModule(ModuleWidget *m);
  134. /** Sets a module's box if non-colliding. Returns true if set */
  135. bool requestModuleBox(ModuleWidget *m, Rect box);
  136. /** Moves a module to the closest non-colliding position */
  137. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  138. void step() override;
  139. void draw(NVGcontext *vg) override;
  140. void onMouseMove(EventMouseMove &e) override;
  141. void onMouseDown(EventMouseDown &e) override;
  142. void onZoom(EventZoom &e) override;
  143. };
  144. struct RackRail : TransparentWidget {
  145. void draw(NVGcontext *vg) override;
  146. };
  147. struct AddModuleWindow : Window {
  148. Vec modulePos;
  149. AddModuleWindow();
  150. void step() override;
  151. };
  152. struct Panel : TransparentWidget {
  153. NVGcolor backgroundColor;
  154. std::shared_ptr<Image> backgroundImage;
  155. void draw(NVGcontext *vg) override;
  156. };
  157. struct SVGPanel : FramebufferWidget {
  158. void step() override;
  159. void setBackground(std::shared_ptr<SVG> svg);
  160. };
  161. ////////////////////
  162. // params
  163. ////////////////////
  164. struct CircularShadow : TransparentWidget {
  165. float blur = 0.0;
  166. void draw(NVGcontext *vg) override;
  167. };
  168. struct ParamWidget : OpaqueWidget, QuantityWidget {
  169. Module *module = NULL;
  170. int paramId;
  171. /** Used to momentarily disable value randomization
  172. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  173. */
  174. bool randomizable = true;
  175. json_t *toJson();
  176. void fromJson(json_t *rootJ);
  177. virtual void randomize();
  178. void onMouseDown(EventMouseDown &e) override;
  179. void onChange(EventChange &e) override;
  180. };
  181. /** Implements vertical dragging behavior for ParamWidgets */
  182. struct Knob : ParamWidget {
  183. /** Snap to nearest integer while dragging */
  184. bool snap = false;
  185. /** Multiplier for mouse movement to adjust knob value */
  186. float speed = 1.0;
  187. float dragValue;
  188. void onDragStart(EventDragStart &e) override;
  189. void onDragMove(EventDragMove &e) override;
  190. void onDragEnd(EventDragEnd &e) override;
  191. /** Tell engine to smoothly vary this parameter */
  192. void onChange(EventChange &e) override;
  193. };
  194. struct SpriteKnob : virtual Knob, SpriteWidget {
  195. int minIndex, maxIndex, spriteCount;
  196. void step() override;
  197. };
  198. /** A knob which rotates an SVG and caches it in a framebuffer */
  199. struct SVGKnob : virtual Knob, FramebufferWidget {
  200. /** Angles in radians */
  201. float minAngle, maxAngle;
  202. /** Not owned */
  203. TransformWidget *tw;
  204. SVGWidget *sw;
  205. SVGKnob();
  206. void setSVG(std::shared_ptr<SVG> svg);
  207. void step() override;
  208. void onChange(EventChange &e) override;
  209. };
  210. struct SVGFader : Knob, FramebufferWidget {
  211. /** Intermediate positions will be interpolated between these positions */
  212. Vec minHandlePos, maxHandlePos;
  213. /** Not owned */
  214. SVGWidget *background;
  215. SVGWidget *handle;
  216. SVGFader();
  217. void step() override;
  218. void onChange(EventChange &e) override;
  219. };
  220. struct Switch : ParamWidget {
  221. };
  222. struct SVGSwitch : virtual Switch, FramebufferWidget {
  223. std::vector<std::shared_ptr<SVG>> frames;
  224. /** Not owned */
  225. SVGWidget *sw;
  226. SVGSwitch();
  227. /** Adds an SVG file to represent the next switch position */
  228. void addFrame(std::shared_ptr<SVG> svg);
  229. void onChange(EventChange &e) override;
  230. };
  231. /** A switch that cycles through each mechanical position */
  232. struct ToggleSwitch : virtual Switch {
  233. void onDragStart(EventDragStart &e) override {
  234. // Cycle through values
  235. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  236. if (value >= maxValue)
  237. setValue(minValue);
  238. else
  239. setValue(value + 1.0);
  240. }
  241. };
  242. /** A switch that is turned on when held */
  243. struct MomentarySwitch : virtual Switch {
  244. /** Don't randomize state */
  245. void randomize() override {}
  246. void onDragStart(EventDragStart &e) override {
  247. setValue(maxValue);
  248. EventAction eAction;
  249. onAction(eAction);
  250. }
  251. void onDragEnd(EventDragEnd &e) override {
  252. setValue(minValue);
  253. }
  254. };
  255. ////////////////////
  256. // IO widgets
  257. ////////////////////
  258. struct AudioIO;
  259. struct MidiIO;
  260. struct AudioWidget : OpaqueWidget {
  261. /** Not owned */
  262. AudioIO *audioIO = NULL;
  263. void onMouseDown(EventMouseDown &e) override;
  264. };
  265. struct MidiWidget : OpaqueWidget {
  266. /** Not owned */
  267. MidiIO *midiIO = NULL;
  268. void onMouseDown(EventMouseDown &e) override;
  269. };
  270. ////////////////////
  271. // lights
  272. ////////////////////
  273. struct LightWidget : TransparentWidget {
  274. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  275. NVGcolor color = nvgRGBf(1, 1, 1);
  276. void draw(NVGcontext *vg) override;
  277. virtual void drawLight(NVGcontext *vg);
  278. virtual void drawHalo(NVGcontext *vg);
  279. };
  280. /** Mixes a list of colors based on a list of brightness values */
  281. struct MultiLightWidget : LightWidget {
  282. std::vector<NVGcolor> baseColors;
  283. void addBaseColor(NVGcolor baseColor);
  284. /** Sets the color to a linear combination of the baseColors with the given weights */
  285. void setValues(const std::vector<float> &values);
  286. };
  287. /** A MultiLightWidget that points to a module's Light or a range of lights
  288. Will access firstLightId, firstLightId + 1, etc. for each added color
  289. */
  290. struct ModuleLightWidget : MultiLightWidget {
  291. Module *module = NULL;
  292. int firstLightId;
  293. void step() override;
  294. };
  295. ////////////////////
  296. // ports
  297. ////////////////////
  298. struct Port : OpaqueWidget {
  299. enum PortType {
  300. INPUT,
  301. OUTPUT
  302. };
  303. Module *module = NULL;
  304. PortType type = INPUT;
  305. int portId;
  306. MultiLightWidget *plugLight;
  307. Port();
  308. ~Port();
  309. void step() override;
  310. void draw(NVGcontext *vg) override;
  311. void onMouseDown(EventMouseDown &e) override;
  312. void onDragStart(EventDragStart &e) override;
  313. void onDragEnd(EventDragEnd &e) override;
  314. void onDragDrop(EventDragDrop &e) override;
  315. void onDragEnter(EventDragEnter &e) override;
  316. void onDragLeave(EventDragEnter &e) override;
  317. };
  318. struct SVGPort : Port, FramebufferWidget {
  319. SVGWidget *background;
  320. SVGPort();
  321. void draw(NVGcontext *vg) override;
  322. };
  323. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  324. struct SVGScrew : FramebufferWidget {
  325. SVGWidget *sw;
  326. SVGScrew();
  327. };
  328. ////////////////////
  329. // scene
  330. ////////////////////
  331. struct Toolbar : OpaqueWidget {
  332. Slider *wireOpacitySlider;
  333. Slider *wireTensionSlider;
  334. Slider *zoomSlider;
  335. RadioButton *cpuUsageButton;
  336. Toolbar();
  337. void draw(NVGcontext *vg) override;
  338. };
  339. struct PluginManagerWidget : Widget {
  340. Widget *loginWidget;
  341. Widget *manageWidget;
  342. Widget *downloadWidget;
  343. PluginManagerWidget();
  344. void step() override;
  345. };
  346. struct RackScrollWidget : ScrollWidget {
  347. void step() override;
  348. };
  349. struct RackScene : Scene {
  350. ScrollWidget *scrollWidget;
  351. ZoomWidget *zoomWidget;
  352. RackScene();
  353. void step() override;
  354. void draw(NVGcontext *vg) override;
  355. void onHoverKey(EventHoverKey &e) override;
  356. void onPathDrop(EventPathDrop &e) override;
  357. };
  358. ////////////////////
  359. // globals
  360. ////////////////////
  361. extern std::string gApplicationName;
  362. extern std::string gApplicationVersion;
  363. extern std::string gApiHost;
  364. // Easy access to "singleton" widgets
  365. extern RackScene *gRackScene;
  366. extern RackWidget *gRackWidget;
  367. extern Toolbar *gToolbar;
  368. void sceneInit();
  369. void sceneDestroy();
  370. json_t *colorToJson(NVGcolor color);
  371. NVGcolor jsonToColor(json_t *colorJ);
  372. } // namespace rack