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.

304 lines
6.0KB

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