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.

325 lines
6.7KB

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