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.

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