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.

app.hpp 11KB

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