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.

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