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.9KB

  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. /** Multiplier for mouse movement to adjust knob value */
  169. float speed = 1.0;
  170. float dragValue;
  171. void onDragStart(EventDragStart &e) override;
  172. void onDragMove(EventDragMove &e) override;
  173. void onDragEnd(EventDragEnd &e) override;
  174. /** Tell engine to smoothly vary this parameter */
  175. void onChange(EventChange &e) override;
  176. };
  177. struct SpriteKnob : virtual Knob, SpriteWidget {
  178. int minIndex, maxIndex, spriteCount;
  179. void step() override;
  180. };
  181. /** A knob which rotates an SVG and caches it in a framebuffer */
  182. struct SVGKnob : virtual Knob, FramebufferWidget {
  183. /** Angles in radians */
  184. float minAngle, maxAngle;
  185. /** Not owned */
  186. TransformWidget *tw;
  187. SVGWidget *sw;
  188. SVGKnob();
  189. void setSVG(std::shared_ptr<SVG> svg);
  190. void step() override;
  191. void onChange(EventChange &e) override;
  192. };
  193. struct SVGSlider : Knob, FramebufferWidget {
  194. /** Intermediate positions will be interpolated between these positions */
  195. Vec minHandlePos, maxHandlePos;
  196. /** Not owned */
  197. SVGWidget *background;
  198. SVGWidget *handle;
  199. SVGSlider();
  200. void step() override;
  201. void onChange(EventChange &e) override;
  202. };
  203. struct Switch : ParamWidget {
  204. };
  205. struct SVGSwitch : virtual Switch, FramebufferWidget {
  206. std::vector<std::shared_ptr<SVG>> frames;
  207. /** Not owned */
  208. SVGWidget *sw;
  209. SVGSwitch();
  210. /** Adds an SVG file to represent the next switch position */
  211. void addFrame(std::shared_ptr<SVG> svg);
  212. void onChange(EventChange &e) override;
  213. };
  214. /** A switch that cycles through each mechanical position */
  215. struct ToggleSwitch : virtual Switch {
  216. void onDragStart(EventDragStart &e) override {
  217. // Cycle through values
  218. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  219. if (value >= maxValue)
  220. setValue(minValue);
  221. else
  222. setValue(value + 1.0);
  223. }
  224. };
  225. /** A switch that is turned on when held */
  226. struct MomentarySwitch : virtual Switch {
  227. /** Don't randomize state */
  228. void randomize() override {}
  229. void onDragStart(EventDragStart &e) override {
  230. setValue(maxValue);
  231. }
  232. void onDragEnd(EventDragEnd &e) override {
  233. setValue(minValue);
  234. }
  235. };
  236. ////////////////////
  237. // lights
  238. ////////////////////
  239. struct LightWidget : TransparentWidget {
  240. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  241. NVGcolor color = nvgRGBf(1, 1, 1);
  242. void draw(NVGcontext *vg) override;
  243. };
  244. /** Mixes a list of colors based on a list of brightness values */
  245. struct MultiLightWidget : LightWidget {
  246. std::vector<NVGcolor> baseColors;
  247. void addBaseColor(NVGcolor baseColor);
  248. /** Sets the color to a linear combination of the baseColors with the given weights */
  249. void setValues(const std::vector<float> &values);
  250. };
  251. /** A MultiLightWidget that points to a module's Light or a range of lights
  252. Will access firstLightId, firstLightId + 1, etc. for each added color
  253. */
  254. struct ModuleLightWidget : MultiLightWidget {
  255. Module *module = NULL;
  256. int firstLightId;
  257. void step() override;
  258. };
  259. ////////////////////
  260. // ports
  261. ////////////////////
  262. struct Port : OpaqueWidget {
  263. enum PortType {
  264. INPUT,
  265. OUTPUT
  266. };
  267. Module *module = NULL;
  268. PortType type = INPUT;
  269. int portId;
  270. MultiLightWidget *plugLight;
  271. Port();
  272. ~Port();
  273. void step() override;
  274. void draw(NVGcontext *vg) override;
  275. void onMouseDown(EventMouseDown &e) override;
  276. void onDragStart(EventDragStart &e) override;
  277. void onDragEnd(EventDragEnd &e) override;
  278. void onDragDrop(EventDragDrop &e) override;
  279. void onDragEnter(EventDragEnter &e) override;
  280. void onDragLeave(EventDragEnter &e) override;
  281. };
  282. struct SVGPort : Port, FramebufferWidget {
  283. SVGWidget *background;
  284. SVGPort();
  285. void draw(NVGcontext *vg) override;
  286. };
  287. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  288. struct SVGScrew : FramebufferWidget {
  289. SVGWidget *sw;
  290. SVGScrew();
  291. };
  292. ////////////////////
  293. // scene
  294. ////////////////////
  295. struct Toolbar : OpaqueWidget {
  296. Slider *wireOpacitySlider;
  297. Slider *wireTensionSlider;
  298. Slider *zoomSlider;
  299. RadioButton *cpuUsageButton;
  300. Toolbar();
  301. void draw(NVGcontext *vg) override;
  302. };
  303. struct PluginManagerWidget : Widget {
  304. Widget *loginWidget;
  305. Widget *manageWidget;
  306. Widget *downloadWidget;
  307. PluginManagerWidget();
  308. void step() override;
  309. };
  310. struct RackScrollWidget : ScrollWidget {
  311. void step() override;
  312. };
  313. struct RackScene : Scene {
  314. ScrollWidget *scrollWidget;
  315. ZoomWidget *zoomWidget;
  316. RackScene();
  317. void step() override;
  318. void draw(NVGcontext *vg) override;
  319. void onHoverKey(EventHoverKey &e) override;
  320. void onPathDrop(EventPathDrop &e) override;
  321. };
  322. ////////////////////
  323. // globals
  324. ////////////////////
  325. extern std::string gApplicationName;
  326. extern std::string gApplicationVersion;
  327. extern std::string gApiHost;
  328. // Easy access to "singleton" widgets
  329. extern RackScene *gRackScene;
  330. extern RackWidget *gRackWidget;
  331. extern Toolbar *gToolbar;
  332. void sceneInit();
  333. void sceneDestroy();
  334. } // namespace rack