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.

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