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.

313 lines
6.2KB

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