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.

329 lines
6.8KB

  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. RackWidget();
  77. ~RackWidget();
  78. void clear();
  79. void savePatch(std::string filename);
  80. void loadPatch(std::string filename);
  81. json_t *toJson();
  82. void fromJson(json_t *root);
  83. void addModule(ModuleWidget *m);
  84. /** Transfers ownership to the caller so they must `delete` it if that is the intension */
  85. void deleteModule(ModuleWidget *m);
  86. void cloneModule(ModuleWidget *m);
  87. /** Moves a module to the closest non-colliding position */
  88. void repositionModule(ModuleWidget *m);
  89. void step();
  90. void draw(NVGcontext *vg);
  91. void onMouseDownOpaque(int button);
  92. };
  93. struct RackRail : TransparentWidget {
  94. void draw(NVGcontext *vg);
  95. };
  96. struct Panel : TransparentWidget {
  97. NVGcolor backgroundColor;
  98. NVGcolor borderColor;
  99. std::shared_ptr<Image> backgroundImage;
  100. void draw(NVGcontext *vg);
  101. };
  102. struct SVGPanel : FramebufferWidget {
  103. void setBackground(std::shared_ptr<SVG> svg);
  104. };
  105. ////////////////////
  106. // params
  107. ////////////////////
  108. struct CircularShadow : TransparentWidget {
  109. float blur = 0.0;
  110. void draw(NVGcontext *vg);
  111. };
  112. struct Light : TransparentWidget {
  113. NVGcolor color;
  114. void draw(NVGcontext *vg);
  115. };
  116. struct ParamWidget : OpaqueWidget, QuantityWidget {
  117. Module *module = NULL;
  118. int paramId;
  119. json_t *toJson();
  120. void fromJson(json_t *root);
  121. virtual void randomize();
  122. void onMouseDownOpaque(int button);
  123. void onChange();
  124. };
  125. /** Implements vertical dragging behavior for ParamWidgets */
  126. struct Knob : ParamWidget {
  127. void onDragStart();
  128. void onDragMove(Vec mouseRel);
  129. void onDragEnd();
  130. /** Tell engine to smoothly vary this parameter */
  131. void onChange();
  132. };
  133. struct SpriteKnob : virtual Knob, SpriteWidget {
  134. int minIndex, maxIndex, spriteCount;
  135. void step();
  136. };
  137. /** A knob which rotates an SVG and caches it in a framebuffer */
  138. struct SVGKnob : virtual Knob, FramebufferWidget {
  139. /** Angles in radians */
  140. float minAngle, maxAngle;
  141. /** Not owned */
  142. TransformWidget *tw;
  143. SVGWidget *sw;
  144. SVGKnob();
  145. void setSVG(std::shared_ptr<SVG> svg);
  146. void step();
  147. void onChange();
  148. };
  149. /** Snaps to the nearest integer value on mouse release */
  150. struct SnapKnob : virtual Knob {
  151. void onDragEnd() {
  152. setValue(roundf(value));
  153. Knob::onDragEnd();
  154. }
  155. };
  156. struct SVGSlider : Knob, FramebufferWidget {
  157. /** Intermediate positions will be interpolated between these positions */
  158. Vec minHandlePos, maxHandlePos;
  159. /** Not owned */
  160. SVGWidget *background;
  161. SVGWidget *handle;
  162. SVGSlider();
  163. void step();
  164. void onChange();
  165. };
  166. struct Switch : ParamWidget {
  167. };
  168. struct SVGSwitch : virtual Switch, FramebufferWidget {
  169. std::vector<std::shared_ptr<SVG>> frames;
  170. /** Not owned */
  171. SVGWidget *sw;
  172. SVGSwitch();
  173. /** Adds an SVG file to represent the next switch position */
  174. void addFrame(std::shared_ptr<SVG> svg);
  175. void step();
  176. void onChange();
  177. };
  178. /** A switch that cycles through each mechanical position */
  179. struct ToggleSwitch : virtual Switch {
  180. void onDragStart() {
  181. // Cycle through values
  182. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  183. if (value >= maxValue)
  184. setValue(minValue);
  185. else
  186. setValue(value + 1.0);
  187. }
  188. };
  189. /** A switch that is turned on when held */
  190. struct MomentarySwitch : virtual Switch {
  191. /** Don't randomize state */
  192. void randomize() {}
  193. void onDragStart() {
  194. setValue(maxValue);
  195. }
  196. void onDragEnd() {
  197. setValue(minValue);
  198. }
  199. };
  200. ////////////////////
  201. // ports
  202. ////////////////////
  203. struct Port : OpaqueWidget {
  204. enum PortType {
  205. DEFAULT,
  206. INPUT,
  207. OUTPUT
  208. };
  209. Module *module = NULL;
  210. WireWidget *connectedWire = NULL;
  211. PortType type = DEFAULT;
  212. int portId;
  213. Port();
  214. ~Port();
  215. void disconnect();
  216. void draw(NVGcontext *vg);
  217. void onMouseDownOpaque(int button);
  218. void onDragEnd();
  219. void onDragStart();
  220. void onDragDrop(Widget *origin);
  221. void onDragEnter(Widget *origin);
  222. void onDragLeave(Widget *origin);
  223. };
  224. struct SVGPort : Port, FramebufferWidget {
  225. SVGWidget *background;
  226. SVGPort();
  227. void draw(NVGcontext *vg);
  228. };
  229. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  230. struct SVGScrew : FramebufferWidget {
  231. SVGWidget *sw;
  232. SVGScrew();
  233. };
  234. ////////////////////
  235. // scene
  236. ////////////////////
  237. struct Toolbar : OpaqueWidget {
  238. Slider *wireOpacitySlider;
  239. Slider *wireTensionSlider;
  240. RadioButton *cpuUsageButton;
  241. Toolbar();
  242. void draw(NVGcontext *vg);
  243. };
  244. struct PluginManagerWidget : Widget {
  245. Widget *loginWidget;
  246. Widget *manageWidget;
  247. Widget *downloadWidget;
  248. PluginManagerWidget();
  249. void step();
  250. };
  251. struct RackScene : Scene {
  252. Toolbar *toolbar;
  253. ScrollWidget *scrollWidget;
  254. RackScene();
  255. void step();
  256. void draw(NVGcontext *vg);
  257. Widget *onHoverKey(Vec pos, int key);
  258. };
  259. ////////////////////
  260. // globals
  261. ////////////////////
  262. extern std::string gApplicationName;
  263. extern std::string gApplicationVersion;
  264. extern std::string gApiHost;
  265. extern RackWidget *gRackWidget;
  266. void sceneInit();
  267. void sceneDestroy();
  268. } // namespace rack