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.

405 lines
10KB

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