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.

290 lines
5.6KB

  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 : 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 : 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. struct SVGSlider : Knob, FramebufferWidget {
  126. /** Intermediate positions will be interpolated between these positions */
  127. Vec minHandlePos, maxHandlePos;
  128. /** Not owned */
  129. SVGWidget *background;
  130. SVGWidget *handle;
  131. SVGSlider();
  132. void step();
  133. void onChange();
  134. };
  135. struct Switch : ParamWidget {
  136. };
  137. struct SVGSwitch : virtual Switch, FramebufferWidget {
  138. std::vector<std::shared_ptr<SVG>> frames;
  139. /** Not owned */
  140. SVGWidget *sw;
  141. SVGSwitch();
  142. /** Adds an SVG file to represent the next switch position */
  143. void addFrame(std::shared_ptr<SVG> svg);
  144. void step();
  145. void onChange();
  146. };
  147. /** A switch that cycles through each mechanical position */
  148. struct ToggleSwitch : virtual Switch {
  149. void onDragStart() {
  150. // Cycle through values
  151. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  152. if (value >= maxValue)
  153. setValue(minValue);
  154. else
  155. setValue(value + 1.0);
  156. }
  157. };
  158. /** A switch that is turned on when held */
  159. struct MomentarySwitch : virtual Switch {
  160. void onDragStart() {
  161. setValue(maxValue);
  162. }
  163. void onDragEnd() {
  164. setValue(minValue);
  165. }
  166. };
  167. ////////////////////
  168. // ports
  169. ////////////////////
  170. struct Port : OpaqueWidget {
  171. enum PortType {
  172. DEFAULT,
  173. INPUT,
  174. OUTPUT
  175. };
  176. Module *module = NULL;
  177. WireWidget *connectedWire = NULL;
  178. PortType type = DEFAULT;
  179. int portId;
  180. Port();
  181. ~Port();
  182. void disconnect();
  183. void draw(NVGcontext *vg);
  184. void onMouseDown(int button);
  185. void onDragEnd();
  186. void onDragStart();
  187. void onDragDrop(Widget *origin);
  188. void onDragEnter(Widget *origin);
  189. void onDragLeave(Widget *origin);
  190. };
  191. struct SVGPort : Port, FramebufferWidget {
  192. SVGWidget *background;
  193. SVGPort();
  194. void draw(NVGcontext *vg);
  195. };
  196. /** If you don't add these to your ModuleWidget, they will fall out of the rack... */
  197. struct SVGScrew : FramebufferWidget {
  198. SVGWidget *sw;
  199. SVGScrew();
  200. };
  201. ////////////////////
  202. // scene
  203. ////////////////////
  204. struct Toolbar : OpaqueWidget {
  205. Slider *wireOpacitySlider;
  206. Slider *wireTensionSlider;
  207. RadioButton *cpuUsageButton;
  208. Toolbar();
  209. void draw(NVGcontext *vg);
  210. };
  211. struct PluginManagerWidget : Widget {
  212. Widget *loginWidget;
  213. Widget *manageWidget;
  214. Widget *downloadWidget;
  215. PluginManagerWidget();
  216. void step();
  217. };
  218. struct RackScene : Scene {
  219. Toolbar *toolbar;
  220. ScrollWidget *scrollWidget;
  221. RackScene();
  222. void step();
  223. void draw(NVGcontext *vg);
  224. };
  225. ////////////////////
  226. // globals
  227. ////////////////////
  228. extern std::string gApplicationName;
  229. extern std::string gApplicationVersion;
  230. extern Scene *gScene;
  231. extern RackWidget *gRackWidget;
  232. void sceneInit();
  233. void sceneDestroy();
  234. } // namespace rack