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.

434 lines
11KB

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