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.

314 lines
6.3KB

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