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.

389 lines
9.5KB

  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 Panel : TransparentWidget {
  131. NVGcolor backgroundColor;
  132. std::shared_ptr<Image> backgroundImage;
  133. void draw(NVGcontext *vg) override;
  134. };
  135. struct SVGPanel : FramebufferWidget {
  136. void step() override;
  137. void setBackground(std::shared_ptr<SVG> svg);
  138. };
  139. ////////////////////
  140. // params
  141. ////////////////////
  142. struct CircularShadow : TransparentWidget {
  143. float blur = 0.0;
  144. void draw(NVGcontext *vg) override;
  145. };
  146. struct ParamWidget : OpaqueWidget, QuantityWidget {
  147. Module *module = NULL;
  148. int paramId;
  149. /** Used to momentarily disable value randomization
  150. To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
  151. */
  152. bool randomizable = true;
  153. json_t *toJson();
  154. void fromJson(json_t *rootJ);
  155. virtual void randomize();
  156. void onMouseDown(EventMouseDown &e) override;
  157. void onChange(EventChange &e) override;
  158. };
  159. /** Implements vertical dragging behavior for ParamWidgets */
  160. struct Knob : ParamWidget {
  161. /** Snap to nearest integer while dragging */
  162. bool snap = false;
  163. float dragValue;
  164. void onDragStart(EventDragStart &e) override;
  165. void onDragMove(EventDragMove &e) override;
  166. void onDragEnd(EventDragEnd &e) override;
  167. /** Tell engine to smoothly vary this parameter */
  168. void onChange(EventChange &e) override;
  169. };
  170. struct SpriteKnob : virtual Knob, SpriteWidget {
  171. int minIndex, maxIndex, spriteCount;
  172. void step() override;
  173. };
  174. /** A knob which rotates an SVG and caches it in a framebuffer */
  175. struct SVGKnob : virtual Knob, FramebufferWidget {
  176. /** Angles in radians */
  177. float minAngle, maxAngle;
  178. /** Not owned */
  179. TransformWidget *tw;
  180. SVGWidget *sw;
  181. SVGKnob();
  182. void setSVG(std::shared_ptr<SVG> svg);
  183. void step() override;
  184. void onChange(EventChange &e) override;
  185. };
  186. struct SVGSlider : Knob, FramebufferWidget {
  187. /** Intermediate positions will be interpolated between these positions */
  188. Vec minHandlePos, maxHandlePos;
  189. /** Not owned */
  190. SVGWidget *background;
  191. SVGWidget *handle;
  192. SVGSlider();
  193. void step() override;
  194. void onChange(EventChange &e) override;
  195. };
  196. struct Switch : ParamWidget {
  197. };
  198. struct SVGSwitch : virtual Switch, FramebufferWidget {
  199. std::vector<std::shared_ptr<SVG>> frames;
  200. /** Not owned */
  201. SVGWidget *sw;
  202. SVGSwitch();
  203. /** Adds an SVG file to represent the next switch position */
  204. void addFrame(std::shared_ptr<SVG> svg);
  205. void step() override;
  206. void onChange(EventChange &e) override;
  207. };
  208. /** A switch that cycles through each mechanical position */
  209. struct ToggleSwitch : virtual Switch {
  210. void onDragStart(EventDragStart &e) override {
  211. // Cycle through values
  212. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  213. if (value >= maxValue)
  214. setValue(minValue);
  215. else
  216. setValue(value + 1.0);
  217. }
  218. };
  219. /** A switch that is turned on when held */
  220. struct MomentarySwitch : virtual Switch {
  221. /** Don't randomize state */
  222. void randomize() override {}
  223. void onDragStart(EventDragStart &e) override {
  224. setValue(maxValue);
  225. }
  226. void onDragEnd(EventDragEnd &e) override {
  227. setValue(minValue);
  228. }
  229. };
  230. ////////////////////
  231. // ports
  232. ////////////////////
  233. struct Port : OpaqueWidget {
  234. enum PortType {
  235. INPUT,
  236. OUTPUT
  237. };
  238. Module *module = NULL;
  239. PortType type = INPUT;
  240. int portId;
  241. ~Port();
  242. void draw(NVGcontext *vg) override;
  243. void onMouseDown(EventMouseDown &e) override;
  244. void onDragStart(EventDragStart &e) override;
  245. void onDragEnd(EventDragEnd &e) override;
  246. void onDragDrop(EventDragDrop &e) override;
  247. void onDragEnter(EventDragEnter &e) override;
  248. void onDragLeave(EventDragEnter &e) override;
  249. };
  250. struct SVGPort : Port, FramebufferWidget {
  251. SVGWidget *background;
  252. SVGPort();
  253. void draw(NVGcontext *vg) override;
  254. };
  255. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  256. struct SVGScrew : FramebufferWidget {
  257. SVGWidget *sw;
  258. SVGScrew();
  259. };
  260. ////////////////////
  261. // lights
  262. ////////////////////
  263. struct LightWidget : TransparentWidget {
  264. NVGcolor bgColor = nvgRGBf(0, 0, 0);
  265. NVGcolor color = nvgRGBf(1, 1, 1);
  266. void draw(NVGcontext *vg) override;
  267. };
  268. /** A LightWidget that points to a module's Light or a range of lights */
  269. struct ModuleLightWidget : LightWidget {
  270. Module *module = NULL;
  271. int lightId;
  272. };
  273. /** Mixes colors based on the brightness of the module light at lightId, lightId + 1, etc */
  274. struct ColorLightWidget : ModuleLightWidget {
  275. std::vector<NVGcolor> colors;
  276. void addColor(NVGcolor c);
  277. void step() override;
  278. };
  279. ////////////////////
  280. // scene
  281. ////////////////////
  282. struct Toolbar : OpaqueWidget {
  283. Slider *wireOpacitySlider;
  284. Slider *wireTensionSlider;
  285. Slider *zoomSlider;
  286. RadioButton *cpuUsageButton;
  287. RadioButton *plugLightButton;
  288. Toolbar();
  289. void draw(NVGcontext *vg) override;
  290. };
  291. struct PluginManagerWidget : Widget {
  292. Widget *loginWidget;
  293. Widget *manageWidget;
  294. Widget *downloadWidget;
  295. PluginManagerWidget();
  296. void step() override;
  297. };
  298. struct RackScrollWidget : ScrollWidget {
  299. void step() override;
  300. };
  301. struct RackScene : Scene {
  302. ScrollWidget *scrollWidget;
  303. ZoomWidget *zoomWidget;
  304. RackScene();
  305. void step() override;
  306. void draw(NVGcontext *vg) override;
  307. void onHoverKey(EventHoverKey &e) override;
  308. void onPathDrop(EventPathDrop &e) override;
  309. };
  310. ////////////////////
  311. // globals
  312. ////////////////////
  313. extern std::string gApplicationName;
  314. extern std::string gApplicationVersion;
  315. extern std::string gApiHost;
  316. // Easy access to "singleton" widgets
  317. extern RackScene *gRackScene;
  318. extern RackWidget *gRackWidget;
  319. extern Toolbar *gToolbar;
  320. void sceneInit();
  321. void sceneDestroy();
  322. } // namespace rack