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 10KB

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