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.

319 lines
6.5KB

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