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.

298 lines
5.8KB

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