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.

326 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. 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. void onMouseDownOpaque(int button);
  122. void onChange();
  123. };
  124. /** Implements vertical dragging behavior for ParamWidgets */
  125. struct Knob : ParamWidget {
  126. void onDragStart();
  127. void onDragMove(Vec mouseRel);
  128. void onDragEnd();
  129. /** Tell engine to smoothly vary this parameter */
  130. void onChange();
  131. };
  132. struct SpriteKnob : virtual Knob, SpriteWidget {
  133. int minIndex, maxIndex, spriteCount;
  134. void step();
  135. };
  136. /** A knob which rotates an SVG and caches it in a framebuffer */
  137. struct SVGKnob : virtual Knob, FramebufferWidget {
  138. /** Angles in radians */
  139. float minAngle, maxAngle;
  140. /** Not owned */
  141. TransformWidget *tw;
  142. SVGWidget *sw;
  143. SVGKnob();
  144. void setSVG(std::shared_ptr<SVG> svg);
  145. void step();
  146. void onChange();
  147. };
  148. /** Snaps to the nearest integer value on mouse release */
  149. struct SnapKnob : virtual Knob {
  150. void onDragEnd() {
  151. setValue(roundf(value));
  152. Knob::onDragEnd();
  153. }
  154. };
  155. struct SVGSlider : Knob, FramebufferWidget {
  156. /** Intermediate positions will be interpolated between these positions */
  157. Vec minHandlePos, maxHandlePos;
  158. /** Not owned */
  159. SVGWidget *background;
  160. SVGWidget *handle;
  161. SVGSlider();
  162. void step();
  163. void onChange();
  164. };
  165. struct Switch : ParamWidget {
  166. };
  167. struct SVGSwitch : virtual Switch, FramebufferWidget {
  168. std::vector<std::shared_ptr<SVG>> frames;
  169. /** Not owned */
  170. SVGWidget *sw;
  171. SVGSwitch();
  172. /** Adds an SVG file to represent the next switch position */
  173. void addFrame(std::shared_ptr<SVG> svg);
  174. void step();
  175. void onChange();
  176. };
  177. /** A switch that cycles through each mechanical position */
  178. struct ToggleSwitch : virtual Switch {
  179. void onDragStart() {
  180. // Cycle through values
  181. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  182. if (value >= maxValue)
  183. setValue(minValue);
  184. else
  185. setValue(value + 1.0);
  186. }
  187. };
  188. /** A switch that is turned on when held */
  189. struct MomentarySwitch : virtual Switch {
  190. void onDragStart() {
  191. setValue(maxValue);
  192. }
  193. void onDragEnd() {
  194. setValue(minValue);
  195. }
  196. };
  197. ////////////////////
  198. // ports
  199. ////////////////////
  200. struct Port : OpaqueWidget {
  201. enum PortType {
  202. DEFAULT,
  203. INPUT,
  204. OUTPUT
  205. };
  206. Module *module = NULL;
  207. WireWidget *connectedWire = NULL;
  208. PortType type = DEFAULT;
  209. int portId;
  210. Port();
  211. ~Port();
  212. void disconnect();
  213. void draw(NVGcontext *vg);
  214. void onMouseDownOpaque(int button);
  215. void onDragEnd();
  216. void onDragStart();
  217. void onDragDrop(Widget *origin);
  218. void onDragEnter(Widget *origin);
  219. void onDragLeave(Widget *origin);
  220. };
  221. struct SVGPort : Port, FramebufferWidget {
  222. SVGWidget *background;
  223. SVGPort();
  224. void draw(NVGcontext *vg);
  225. };
  226. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  227. struct SVGScrew : FramebufferWidget {
  228. SVGWidget *sw;
  229. SVGScrew();
  230. };
  231. ////////////////////
  232. // scene
  233. ////////////////////
  234. struct Toolbar : OpaqueWidget {
  235. Slider *wireOpacitySlider;
  236. Slider *wireTensionSlider;
  237. RadioButton *cpuUsageButton;
  238. Toolbar();
  239. void draw(NVGcontext *vg);
  240. };
  241. struct PluginManagerWidget : Widget {
  242. Widget *loginWidget;
  243. Widget *manageWidget;
  244. Widget *downloadWidget;
  245. PluginManagerWidget();
  246. void step();
  247. };
  248. struct RackScene : Scene {
  249. Toolbar *toolbar;
  250. ScrollWidget *scrollWidget;
  251. RackScene();
  252. void step();
  253. void draw(NVGcontext *vg);
  254. Widget *onHoverKey(Vec pos, int key);
  255. };
  256. ////////////////////
  257. // globals
  258. ////////////////////
  259. extern std::string gApplicationName;
  260. extern std::string gApplicationVersion;
  261. extern std::string gApiHost;
  262. extern RackWidget *gRackWidget;
  263. void sceneInit();
  264. void sceneDestroy();
  265. } // namespace rack