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.

327 lines
6.7KB

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