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.

334 lines
6.9KB

  1. #pragma once
  2. #include <vector>
  3. #include <jansson.h>
  4. #include "widgets.hpp"
  5. namespace rack {
  6. struct Module;
  7. struct Wire;
  8. struct RackWidget;
  9. struct ParamWidget;
  10. struct Port;
  11. struct Scene;
  12. ////////////////////
  13. // module
  14. ////////////////////
  15. // A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
  16. #define RACK_GRID_WIDTH 15
  17. #define RACK_GRID_HEIGHT 380
  18. struct Model;
  19. struct ModuleWidget : OpaqueWidget {
  20. Model *model = NULL;
  21. /** Owns the module pointer */
  22. Module *module = NULL;
  23. std::vector<Port*> inputs;
  24. std::vector<Port*> outputs;
  25. std::vector<ParamWidget*> params;
  26. ~ModuleWidget();
  27. void setModule(Module *module);
  28. // Convenience functions for adding special widgets (calls addChild())
  29. void addInput(Port *input);
  30. void addOutput(Port *output);
  31. void addParam(ParamWidget *param);
  32. json_t *toJson();
  33. void fromJson(json_t *root);
  34. /** Disconnects cables from all ports */
  35. void disconnect();
  36. /** Resets the state of the module */
  37. void initialize();
  38. /** Randomizes the state of the module
  39. This method just randomizes parameters. Override and call this function if your module contains other state information that you wish to randomize.
  40. */
  41. void randomize();
  42. virtual Menu *createContextMenu();
  43. void draw(NVGcontext *vg);
  44. bool requested = false;
  45. Vec requestedPos;
  46. Vec dragPos;
  47. Widget *onHoverKey(Vec pos, int key);
  48. void onDragStart();
  49. void onDragMove(Vec mouseRel);
  50. void onDragEnd();
  51. void onMouseDownOpaque(int button);
  52. };
  53. struct WireWidget : OpaqueWidget {
  54. Port *inputPort = NULL;
  55. Port *outputPort = NULL;
  56. Port *hoveredInputPort = NULL;
  57. Port *hoveredOutputPort = NULL;
  58. Wire *wire = NULL;
  59. NVGcolor color;
  60. WireWidget();
  61. ~WireWidget();
  62. /** Synchronizes the plugged state of the widget to the owned wire */
  63. void updateWire();
  64. Vec getOutputPos();
  65. Vec getInputPos();
  66. void draw(NVGcontext *vg);
  67. void drawPlugs(NVGcontext *vg);
  68. };
  69. struct RackWidget : OpaqueWidget {
  70. FramebufferWidget *rails;
  71. // Only put ModuleWidgets in here
  72. Widget *moduleContainer;
  73. // Only put WireWidgets in here
  74. Widget *wireContainer;
  75. WireWidget *activeWire = NULL;
  76. std::string lastPath;
  77. RackWidget();
  78. ~RackWidget();
  79. void clear();
  80. void openDialog();
  81. void saveDialog();
  82. void saveAsDialog();
  83. void savePatch(std::string filename);
  84. void loadPatch(std::string filename);
  85. json_t *toJson();
  86. void fromJson(json_t *root);
  87. void addModule(ModuleWidget *m);
  88. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  89. void deleteModule(ModuleWidget *m);
  90. void cloneModule(ModuleWidget *m);
  91. /** Moves a module to the closest non-colliding position */
  92. void repositionModule(ModuleWidget *m);
  93. void step();
  94. void draw(NVGcontext *vg);
  95. void onMouseDownOpaque(int button);
  96. };
  97. struct RackRail : TransparentWidget {
  98. void draw(NVGcontext *vg);
  99. };
  100. struct Panel : TransparentWidget {
  101. NVGcolor backgroundColor;
  102. NVGcolor borderColor;
  103. std::shared_ptr<Image> backgroundImage;
  104. void draw(NVGcontext *vg);
  105. };
  106. struct SVGPanel : FramebufferWidget {
  107. void setBackground(std::shared_ptr<SVG> svg);
  108. };
  109. ////////////////////
  110. // params
  111. ////////////////////
  112. struct CircularShadow : TransparentWidget {
  113. float blur = 0.0;
  114. void draw(NVGcontext *vg);
  115. };
  116. struct Light : TransparentWidget {
  117. NVGcolor color;
  118. void draw(NVGcontext *vg);
  119. };
  120. struct ParamWidget : OpaqueWidget, QuantityWidget {
  121. Module *module = NULL;
  122. int paramId;
  123. json_t *toJson();
  124. void fromJson(json_t *root);
  125. virtual void randomize();
  126. void onMouseDownOpaque(int button);
  127. void onChange();
  128. };
  129. /** Implements vertical dragging behavior for ParamWidgets */
  130. struct Knob : ParamWidget {
  131. void onDragStart();
  132. void onDragMove(Vec mouseRel);
  133. void onDragEnd();
  134. /** Tell engine to smoothly vary this parameter */
  135. void onChange();
  136. };
  137. struct SpriteKnob : virtual Knob, SpriteWidget {
  138. int minIndex, maxIndex, spriteCount;
  139. void step();
  140. };
  141. /** A knob which rotates an SVG and caches it in a framebuffer */
  142. struct SVGKnob : virtual Knob, FramebufferWidget {
  143. /** Angles in radians */
  144. float minAngle, maxAngle;
  145. /** Not owned */
  146. TransformWidget *tw;
  147. SVGWidget *sw;
  148. SVGKnob();
  149. void setSVG(std::shared_ptr<SVG> svg);
  150. void step();
  151. void onChange();
  152. };
  153. /** Snaps to the nearest integer value on mouse release */
  154. struct SnapKnob : virtual Knob {
  155. void onDragEnd() {
  156. setValue(roundf(value));
  157. Knob::onDragEnd();
  158. }
  159. };
  160. struct SVGSlider : Knob, FramebufferWidget {
  161. /** Intermediate positions will be interpolated between these positions */
  162. Vec minHandlePos, maxHandlePos;
  163. /** Not owned */
  164. SVGWidget *background;
  165. SVGWidget *handle;
  166. SVGSlider();
  167. void step();
  168. void onChange();
  169. };
  170. struct Switch : ParamWidget {
  171. };
  172. struct SVGSwitch : virtual Switch, FramebufferWidget {
  173. std::vector<std::shared_ptr<SVG>> frames;
  174. /** Not owned */
  175. SVGWidget *sw;
  176. SVGSwitch();
  177. /** Adds an SVG file to represent the next switch position */
  178. void addFrame(std::shared_ptr<SVG> svg);
  179. void step();
  180. void onChange();
  181. };
  182. /** A switch that cycles through each mechanical position */
  183. struct ToggleSwitch : virtual Switch {
  184. void onDragStart() {
  185. // Cycle through values
  186. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  187. if (value >= maxValue)
  188. setValue(minValue);
  189. else
  190. setValue(value + 1.0);
  191. }
  192. };
  193. /** A switch that is turned on when held */
  194. struct MomentarySwitch : virtual Switch {
  195. /** Don't randomize state */
  196. void randomize() {}
  197. void onDragStart() {
  198. setValue(maxValue);
  199. }
  200. void onDragEnd() {
  201. setValue(minValue);
  202. }
  203. };
  204. ////////////////////
  205. // ports
  206. ////////////////////
  207. struct Port : OpaqueWidget {
  208. enum PortType {
  209. DEFAULT,
  210. INPUT,
  211. OUTPUT
  212. };
  213. Module *module = NULL;
  214. WireWidget *connectedWire = NULL;
  215. PortType type = DEFAULT;
  216. int portId;
  217. Port();
  218. ~Port();
  219. void disconnect();
  220. void draw(NVGcontext *vg);
  221. void onMouseDownOpaque(int button);
  222. void onDragEnd();
  223. void onDragStart();
  224. void onDragDrop(Widget *origin);
  225. void onDragEnter(Widget *origin);
  226. void onDragLeave(Widget *origin);
  227. };
  228. struct SVGPort : Port, FramebufferWidget {
  229. SVGWidget *background;
  230. SVGPort();
  231. void draw(NVGcontext *vg);
  232. };
  233. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  234. struct SVGScrew : FramebufferWidget {
  235. SVGWidget *sw;
  236. SVGScrew();
  237. };
  238. ////////////////////
  239. // scene
  240. ////////////////////
  241. struct Toolbar : OpaqueWidget {
  242. Slider *wireOpacitySlider;
  243. Slider *wireTensionSlider;
  244. RadioButton *cpuUsageButton;
  245. Toolbar();
  246. void draw(NVGcontext *vg);
  247. };
  248. struct PluginManagerWidget : Widget {
  249. Widget *loginWidget;
  250. Widget *manageWidget;
  251. Widget *downloadWidget;
  252. PluginManagerWidget();
  253. void step();
  254. };
  255. struct RackScene : Scene {
  256. Toolbar *toolbar;
  257. ScrollWidget *scrollWidget;
  258. RackScene();
  259. void step();
  260. void draw(NVGcontext *vg);
  261. Widget *onHoverKey(Vec pos, int key);
  262. };
  263. ////////////////////
  264. // globals
  265. ////////////////////
  266. extern std::string gApplicationName;
  267. extern std::string gApplicationVersion;
  268. extern std::string gApiHost;
  269. extern RackWidget *gRackWidget;
  270. void sceneInit();
  271. void sceneDestroy();
  272. } // namespace rack