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.

396 lines
9.6KB

  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. // ports
  237. ////////////////////
  238. struct Port : OpaqueWidget {
  239. enum PortType {
  240. INPUT,
  241. OUTPUT
  242. };
  243. Module *module = NULL;
  244. PortType type = INPUT;
  245. int portId;
  246. ~Port();
  247. void draw(NVGcontext *vg) override;
  248. void onMouseDown(EventMouseDown &e) override;
  249. void onDragStart(EventDragStart &e) override;
  250. void onDragEnd(EventDragEnd &e) override;
  251. void onDragDrop(EventDragDrop &e) override;
  252. void onDragEnter(EventDragEnter &e) override;
  253. void onDragLeave(EventDragEnter &e) override;
  254. };
  255. struct SVGPort : Port, FramebufferWidget {
  256. SVGWidget *background;
  257. SVGPort();
  258. void draw(NVGcontext *vg) override;
  259. };
  260. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  261. struct SVGScrew : FramebufferWidget {
  262. SVGWidget *sw;
  263. SVGScrew();
  264. };
  265. ////////////////////
  266. // lights
  267. ////////////////////
  268. struct LightWidget : TransparentWidget {
  269. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  270. NVGcolor color = nvgRGBf(1, 1, 1);
  271. void draw(NVGcontext *vg) override;
  272. };
  273. /** A LightWidget that points to a module's Light or a range of lights */
  274. struct ModuleLightWidget : LightWidget {
  275. Module *module = NULL;
  276. int lightId;
  277. };
  278. /** Mixes colors based on the brightness of the module light at lightId, lightId + 1, etc */
  279. struct ColorLightWidget : ModuleLightWidget {
  280. std::vector<NVGcolor> colors;
  281. void addColor(NVGcolor c);
  282. void step() override;
  283. };
  284. ////////////////////
  285. // scene
  286. ////////////////////
  287. struct Toolbar : OpaqueWidget {
  288. Slider *wireOpacitySlider;
  289. Slider *wireTensionSlider;
  290. Slider *zoomSlider;
  291. RadioButton *cpuUsageButton;
  292. RadioButton *plugLightButton;
  293. Toolbar();
  294. void draw(NVGcontext *vg) override;
  295. };
  296. struct PluginManagerWidget : Widget {
  297. Widget *loginWidget;
  298. Widget *manageWidget;
  299. Widget *downloadWidget;
  300. PluginManagerWidget();
  301. void step() override;
  302. };
  303. struct RackScrollWidget : ScrollWidget {
  304. void step() override;
  305. };
  306. struct RackScene : Scene {
  307. ScrollWidget *scrollWidget;
  308. ZoomWidget *zoomWidget;
  309. RackScene();
  310. void step() override;
  311. void draw(NVGcontext *vg) override;
  312. void onHoverKey(EventHoverKey &e) override;
  313. void onPathDrop(EventPathDrop &e) override;
  314. };
  315. ////////////////////
  316. // globals
  317. ////////////////////
  318. extern std::string gApplicationName;
  319. extern std::string gApplicationVersion;
  320. extern std::string gApiHost;
  321. // Easy access to "singleton" widgets
  322. extern RackScene *gRackScene;
  323. extern RackWidget *gRackWidget;
  324. extern Toolbar *gToolbar;
  325. void sceneInit();
  326. void sceneDestroy();
  327. } // namespace rack