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.

402 lines
9.8KB

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