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.

305 lines
6.1KB

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