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