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.

403 lines
10.0KB

  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 WireWidget : OpaqueWidget {
  62. Port *outputPort = NULL;
  63. Port *inputPort = NULL;
  64. Port *hoveredOutputPort = NULL;
  65. Port *hoveredInputPort = NULL;
  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. Vec lastMousePos;
  97. RackWidget();
  98. ~RackWidget();
  99. /** Completely clear the rack's modules and wires */
  100. void clear();
  101. /** Clears the rack and loads the template patch */
  102. void reset();
  103. void openDialog();
  104. void saveDialog();
  105. void saveAsDialog();
  106. void savePatch(std::string filename);
  107. void loadPatch(std::string filename);
  108. json_t *toJson();
  109. void fromJson(json_t *rootJ);
  110. void addModule(ModuleWidget *m);
  111. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  112. void deleteModule(ModuleWidget *m);
  113. void cloneModule(ModuleWidget *m);
  114. /** Sets a module's box if non-colliding. Returns true if set */
  115. bool requestModuleBox(ModuleWidget *m, Rect box);
  116. /** Moves a module to the closest non-colliding position */
  117. bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
  118. void step() override;
  119. void draw(NVGcontext *vg) override;
  120. void onMouseMove(EventMouseMove &e) override;
  121. void onMouseDown(EventMouseDown &e) override;
  122. void onZoom(EventZoom &e) override;
  123. };
  124. struct RackRail : TransparentWidget {
  125. void draw(NVGcontext *vg) override;
  126. };
  127. struct AddModuleWindow : Window {
  128. Vec modulePos;
  129. AddModuleWindow();
  130. void step() override;
  131. };
  132. struct Panel : TransparentWidget {
  133. NVGcolor backgroundColor;
  134. std::shared_ptr<Image> backgroundImage;
  135. void draw(NVGcontext *vg) override;
  136. };
  137. struct SVGPanel : FramebufferWidget {
  138. void step() override;
  139. void setBackground(std::shared_ptr<SVG> svg);
  140. };
  141. ////////////////////
  142. // params
  143. ////////////////////
  144. struct CircularShadow : TransparentWidget {
  145. float blur = 0.0;
  146. void draw(NVGcontext *vg) override;
  147. };
  148. struct ParamWidget : OpaqueWidget, QuantityWidget {
  149. Module *module = NULL;
  150. int paramId;
  151. /** Used to momentarily disable value randomization
  152. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  153. */
  154. bool randomizable = true;
  155. json_t *toJson();
  156. void fromJson(json_t *rootJ);
  157. virtual void randomize();
  158. void onMouseDown(EventMouseDown &e) override;
  159. void onChange(EventChange &e) override;
  160. };
  161. /** Implements vertical dragging behavior for ParamWidgets */
  162. struct Knob : ParamWidget {
  163. /** Snap to nearest integer while dragging */
  164. bool snap = false;
  165. /** Multiplier for mouse movement to adjust knob value */
  166. float speed = 1.0;
  167. float dragValue;
  168. void onDragStart(EventDragStart &e) override;
  169. void onDragMove(EventDragMove &e) override;
  170. void onDragEnd(EventDragEnd &e) override;
  171. /** Tell engine to smoothly vary this parameter */
  172. void onChange(EventChange &e) override;
  173. };
  174. struct SpriteKnob : virtual Knob, SpriteWidget {
  175. int minIndex, maxIndex, spriteCount;
  176. void step() override;
  177. };
  178. /** A knob which rotates an SVG and caches it in a framebuffer */
  179. struct SVGKnob : virtual Knob, FramebufferWidget {
  180. /** Angles in radians */
  181. float minAngle, maxAngle;
  182. /** Not owned */
  183. TransformWidget *tw;
  184. SVGWidget *sw;
  185. SVGKnob();
  186. void setSVG(std::shared_ptr<SVG> svg);
  187. void step() override;
  188. void onChange(EventChange &e) override;
  189. };
  190. struct SVGFader : Knob, FramebufferWidget {
  191. /** Intermediate positions will be interpolated between these positions */
  192. Vec minHandlePos, maxHandlePos;
  193. /** Not owned */
  194. SVGWidget *background;
  195. SVGWidget *handle;
  196. SVGFader();
  197. void step() override;
  198. void onChange(EventChange &e) override;
  199. };
  200. struct Switch : ParamWidget {
  201. };
  202. struct SVGSwitch : virtual Switch, FramebufferWidget {
  203. std::vector<std::shared_ptr<SVG>> frames;
  204. /** Not owned */
  205. SVGWidget *sw;
  206. SVGSwitch();
  207. /** Adds an SVG file to represent the next switch position */
  208. void addFrame(std::shared_ptr<SVG> svg);
  209. void onChange(EventChange &e) override;
  210. };
  211. /** A switch that cycles through each mechanical position */
  212. struct ToggleSwitch : virtual Switch {
  213. void onDragStart(EventDragStart &e) override {
  214. // Cycle through values
  215. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  216. if (value >= maxValue)
  217. setValue(minValue);
  218. else
  219. setValue(value + 1.0);
  220. }
  221. };
  222. /** A switch that is turned on when held */
  223. struct MomentarySwitch : virtual Switch {
  224. /** Don't randomize state */
  225. void randomize() override {}
  226. void onDragStart(EventDragStart &e) override {
  227. setValue(maxValue);
  228. EventAction eAction;
  229. onAction(eAction);
  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. virtual void drawLight(NVGcontext *vg);
  243. virtual void drawHalo(NVGcontext *vg);
  244. };
  245. /** Mixes a list of colors based on a list of brightness values */
  246. struct MultiLightWidget : LightWidget {
  247. std::vector<NVGcolor> baseColors;
  248. void addBaseColor(NVGcolor baseColor);
  249. /** Sets the color to a linear combination of the baseColors with the given weights */
  250. void setValues(const std::vector<float> &values);
  251. };
  252. /** A MultiLightWidget that points to a module's Light or a range of lights
  253. Will access firstLightId, firstLightId + 1, etc. for each added color
  254. */
  255. struct ModuleLightWidget : MultiLightWidget {
  256. Module *module = NULL;
  257. int firstLightId;
  258. void step() override;
  259. };
  260. ////////////////////
  261. // ports
  262. ////////////////////
  263. struct Port : OpaqueWidget {
  264. enum PortType {
  265. INPUT,
  266. OUTPUT
  267. };
  268. Module *module = NULL;
  269. PortType type = INPUT;
  270. int portId;
  271. MultiLightWidget *plugLight;
  272. Port();
  273. ~Port();
  274. void step() override;
  275. void draw(NVGcontext *vg) override;
  276. void onMouseDown(EventMouseDown &e) override;
  277. void onDragStart(EventDragStart &e) override;
  278. void onDragEnd(EventDragEnd &e) override;
  279. void onDragDrop(EventDragDrop &e) override;
  280. void onDragEnter(EventDragEnter &e) override;
  281. void onDragLeave(EventDragEnter &e) override;
  282. };
  283. struct SVGPort : Port, FramebufferWidget {
  284. SVGWidget *background;
  285. SVGPort();
  286. void draw(NVGcontext *vg) override;
  287. };
  288. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  289. struct SVGScrew : FramebufferWidget {
  290. SVGWidget *sw;
  291. SVGScrew();
  292. };
  293. ////////////////////
  294. // scene
  295. ////////////////////
  296. struct Toolbar : OpaqueWidget {
  297. Slider *wireOpacitySlider;
  298. Slider *wireTensionSlider;
  299. Slider *zoomSlider;
  300. RadioButton *cpuUsageButton;
  301. Toolbar();
  302. void draw(NVGcontext *vg) override;
  303. };
  304. struct PluginManagerWidget : Widget {
  305. Widget *loginWidget;
  306. Widget *manageWidget;
  307. Widget *downloadWidget;
  308. PluginManagerWidget();
  309. void step() override;
  310. };
  311. struct RackScrollWidget : ScrollWidget {
  312. void step() override;
  313. };
  314. struct RackScene : Scene {
  315. ScrollWidget *scrollWidget;
  316. ZoomWidget *zoomWidget;
  317. RackScene();
  318. void step() override;
  319. void draw(NVGcontext *vg) override;
  320. void onHoverKey(EventHoverKey &e) override;
  321. void onPathDrop(EventPathDrop &e) override;
  322. };
  323. ////////////////////
  324. // globals
  325. ////////////////////
  326. extern std::string gApplicationName;
  327. extern std::string gApplicationVersion;
  328. extern std::string gApiHost;
  329. // Easy access to "singleton" widgets
  330. extern RackScene *gRackScene;
  331. extern RackWidget *gRackWidget;
  332. extern Toolbar *gToolbar;
  333. void sceneInit();
  334. void sceneDestroy();
  335. } // namespace rack