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.

386 lines
9.2KB

  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. ////////////////////
  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. static const Vec RACK_GRID_SIZE = Vec(15, 380);
  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. void setPanel(std::shared_ptr<SVG> svg);
  33. virtual json_t *toJson();
  34. virtual void fromJson(json_t *rootJ);
  35. /** Disconnects cables from all ports
  36. Called when the user clicks Disconnect Cables in the context menu.
  37. */
  38. virtual void disconnect();
  39. /** Resets the parameters of the module and calls the Module's randomize().
  40. Called when the user clicks Initialize in the context menu.
  41. */
  42. virtual void reset();
  43. /** Deprecated */
  44. virtual void initialize() final {}
  45. /** Randomizes the parameters of the module and calls the Module's randomize().
  46. Called when the user clicks Randomize in the context menu.
  47. */
  48. virtual void randomize();
  49. virtual Menu *createContextMenu();
  50. void draw(NVGcontext *vg) override;
  51. Vec dragPos;
  52. Widget *onMouseMove(Vec pos, Vec mouseRel) override;
  53. Widget *onHoverKey(Vec pos, int key) override;
  54. void onDragStart() override;
  55. void onDragMove(Vec mouseRel) override;
  56. void onDragEnd() override;
  57. void onMouseDownOpaque(int button) override;
  58. };
  59. struct ValueLight;
  60. struct WireWidget : OpaqueWidget {
  61. Port *outputPort = NULL;
  62. Port *inputPort = NULL;
  63. Port *hoveredOutputPort = NULL;
  64. Port *hoveredInputPort = NULL;
  65. ValueLight *inputLight;
  66. ValueLight *outputLight;
  67. Wire *wire = NULL;
  68. NVGcolor color;
  69. WireWidget();
  70. ~WireWidget();
  71. /** Synchronizes the plugged state of the widget to the owned wire */
  72. void updateWire();
  73. Vec getOutputPos();
  74. Vec getInputPos();
  75. void draw(NVGcontext *vg) override;
  76. void drawPlugs(NVGcontext *vg);
  77. };
  78. struct WireContainer : TransparentWidget {
  79. WireWidget *activeWire = NULL;
  80. /** Takes ownership of `w` and adds it as a child if it isn't already */
  81. void setActiveWire(WireWidget *w);
  82. /** "Drops" the wire onto the port, making an engine connection if successful */
  83. void commitActiveWire();
  84. void removeTopWire(Port *port);
  85. void removeAllWires(Port *port);
  86. /** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
  87. WireWidget *getTopWire(Port *port);
  88. void draw(NVGcontext *vg) override;
  89. };
  90. struct RackWidget : OpaqueWidget {
  91. FramebufferWidget *rails;
  92. // Only put ModuleWidgets in here
  93. Widget *moduleContainer;
  94. // Only put WireWidgets in here
  95. WireContainer *wireContainer;
  96. std::string lastPath;
  97. Vec lastMousePos;
  98. RackWidget();
  99. ~RackWidget();
  100. /** Completely clear the rack's modules and wires */
  101. void clear();
  102. /** Clears the rack and loads the template patch */
  103. void reset();
  104. void openDialog();
  105. void saveDialog();
  106. void saveAsDialog();
  107. void savePatch(std::string filename);
  108. void loadPatch(std::string filename);
  109. json_t *toJson();
  110. void fromJson(json_t *rootJ);
  111. void addModule(ModuleWidget *m);
  112. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  113. void deleteModule(ModuleWidget *m);
  114. void cloneModule(ModuleWidget *m);
  115. /** Sets a module's box if non-colliding. Returns true if set */
  116. bool requestModuleBox(ModuleWidget *m, Rect box);
  117. /** Moves a module to the closest non-colliding position */
  118. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  119. void step() override;
  120. void draw(NVGcontext *vg) override;
  121. Widget *onMouseMove(Vec pos, Vec mouseRel) override;
  122. void onMouseDownOpaque(int button) override;
  123. void onZoom() override;
  124. };
  125. struct RackRail : TransparentWidget {
  126. void draw(NVGcontext *vg) override;
  127. };
  128. struct Panel : TransparentWidget {
  129. NVGcolor backgroundColor;
  130. std::shared_ptr<Image> backgroundImage;
  131. void draw(NVGcontext *vg) override;
  132. };
  133. struct SVGPanel : FramebufferWidget {
  134. void step() override;
  135. void setBackground(std::shared_ptr<SVG> svg);
  136. };
  137. ////////////////////
  138. // params
  139. ////////////////////
  140. struct CircularShadow : TransparentWidget {
  141. float blur = 0.0;
  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. // lights
  260. ////////////////////
  261. struct LightWidget : TransparentWidget {
  262. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  263. NVGcolor color = nvgRGBf(1, 1, 1);
  264. void draw(NVGcontext *vg) override;
  265. };
  266. /** A LightWidget that points to a module's Light or a range of lights */
  267. struct ModuleLightWidget : LightWidget {
  268. Module *module = NULL;
  269. int lightId;
  270. };
  271. /** Mixes colors based on the brightness of the module light at lightId, lightId + 1, etc */
  272. struct ColorLightWidget : ModuleLightWidget {
  273. std::vector<NVGcolor> colors;
  274. void addColor(NVGcolor c);
  275. void step() override;
  276. };
  277. ////////////////////
  278. // scene
  279. ////////////////////
  280. struct Toolbar : OpaqueWidget {
  281. Slider *wireOpacitySlider;
  282. Slider *wireTensionSlider;
  283. Slider *zoomSlider;
  284. RadioButton *cpuUsageButton;
  285. RadioButton *plugLightButton;
  286. Toolbar();
  287. void draw(NVGcontext *vg) override;
  288. };
  289. struct PluginManagerWidget : Widget {
  290. Widget *loginWidget;
  291. Widget *manageWidget;
  292. Widget *downloadWidget;
  293. PluginManagerWidget();
  294. void step() override;
  295. };
  296. struct RackScrollWidget : ScrollWidget {
  297. void step() override;
  298. };
  299. struct RackScene : Scene {
  300. ScrollWidget *scrollWidget;
  301. ZoomWidget *zoomWidget;
  302. RackScene();
  303. void step() override;
  304. void draw(NVGcontext *vg) override;
  305. Widget *onHoverKey(Vec pos, int key) override;
  306. };
  307. ////////////////////
  308. // globals
  309. ////////////////////
  310. extern std::string gApplicationName;
  311. extern std::string gApplicationVersion;
  312. extern std::string gApiHost;
  313. // Easy access to "singleton" widgets
  314. extern RackScene *gRackScene;
  315. extern RackWidget *gRackWidget;
  316. extern Toolbar *gToolbar;
  317. void sceneInit();
  318. void sceneDestroy();
  319. } // namespace rack