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.

280 lines
5.2KB

  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 cloneParams(ModuleWidget *source);
  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. struct Knob : ParamWidget {
  103. void onDragStart();
  104. void onDragMove(Vec mouseRel);
  105. void onDragEnd();
  106. };
  107. struct SpriteKnob : Knob, SpriteWidget {
  108. int minIndex, maxIndex, spriteCount;
  109. void step();
  110. };
  111. /** A knob which rotates an SVG and caches it in a framebuffer */
  112. struct SVGKnob : Knob, FramebufferWidget {
  113. /** Angles in radians */
  114. float minAngle, maxAngle;
  115. /** Not owned */
  116. TransformWidget *tw;
  117. SVGWidget *sw;
  118. CircularShadow *shadow;
  119. SVGKnob();
  120. void setSVG(std::shared_ptr<SVG> svg);
  121. void step();
  122. void onChange();
  123. };
  124. struct Switch : ParamWidget, SpriteWidget {
  125. };
  126. struct SVGSwitch : ParamWidget, FramebufferWidget {
  127. /** Not owned */
  128. TransformWidget *tw;
  129. SVGWidget *swPressed;
  130. SVGWidget *swReleased;
  131. };
  132. struct ToggleSwitch : virtual Switch {
  133. void onDragStart() {
  134. index = 1;
  135. }
  136. void onDragEnd() {
  137. index = 0;
  138. }
  139. void onDragDrop(Widget *origin) {
  140. if (origin != this)
  141. return;
  142. // Cycle through modes
  143. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  144. float v = value + 1.0;
  145. setValue(v > maxValue ? minValue : v);
  146. }
  147. };
  148. struct MomentarySwitch : virtual Switch {
  149. void onDragStart() {
  150. setValue(maxValue);
  151. index = 1;
  152. }
  153. void onDragEnd() {
  154. setValue(minValue);
  155. index = 0;
  156. }
  157. };
  158. ////////////////////
  159. // ports
  160. ////////////////////
  161. struct Port : OpaqueWidget {
  162. enum PortType {
  163. DEFAULT,
  164. INPUT,
  165. OUTPUT
  166. };
  167. Module *module = NULL;
  168. WireWidget *connectedWire = NULL;
  169. PortType type = DEFAULT;
  170. int portId;
  171. Port();
  172. ~Port();
  173. void disconnect();
  174. void draw(NVGcontext *vg);
  175. void onMouseDown(int button);
  176. void onDragEnd();
  177. void onDragStart();
  178. void onDragDrop(Widget *origin);
  179. void onDragEnter(Widget *origin);
  180. void onDragLeave(Widget *origin);
  181. };
  182. struct SpritePort : Port, SpriteWidget {
  183. void draw(NVGcontext *vg) {
  184. Port::draw(vg);
  185. SpriteWidget::draw(vg);
  186. }
  187. };
  188. struct SVGPort : Port, FramebufferWidget {
  189. SVGWidget *sw;
  190. SVGPort();
  191. void setSVG(std::shared_ptr<SVG> svg);
  192. void draw(NVGcontext *vg);
  193. };
  194. ////////////////////
  195. // scene
  196. ////////////////////
  197. struct Toolbar : OpaqueWidget {
  198. Slider *wireOpacitySlider;
  199. Slider *wireTensionSlider;
  200. RadioButton *cpuUsageButton;
  201. Toolbar();
  202. void draw(NVGcontext *vg);
  203. };
  204. struct PluginManagerWidget : Widget {
  205. Widget *loginWidget;
  206. Widget *manageWidget;
  207. Widget *downloadWidget;
  208. PluginManagerWidget();
  209. void step();
  210. };
  211. struct RackScene : Scene {
  212. Toolbar *toolbar;
  213. ScrollWidget *scrollWidget;
  214. RackScene();
  215. void step();
  216. void draw(NVGcontext *vg);
  217. };
  218. ////////////////////
  219. // globals
  220. ////////////////////
  221. extern std::string gApplicationName;
  222. extern std::string gApplicationVersion;
  223. extern Scene *gScene;
  224. extern RackWidget *gRackWidget;
  225. void sceneInit();
  226. void sceneDestroy();
  227. } // namespace rack