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

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