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.

243 lines
4.5KB

  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 onChange();
  118. };
  119. struct Switch : ParamWidget, SpriteWidget {
  120. };
  121. struct ToggleSwitch : virtual Switch {
  122. void onDragStart() {
  123. index = 1;
  124. }
  125. void onDragEnd() {
  126. index = 0;
  127. }
  128. void onDragDrop(Widget *origin) {
  129. if (origin != this)
  130. return;
  131. // Cycle through modes
  132. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  133. float v = value + 1.0;
  134. setValue(v > maxValue ? minValue : v);
  135. }
  136. };
  137. struct MomentarySwitch : virtual Switch {
  138. void onDragStart() {
  139. setValue(maxValue);
  140. index = 1;
  141. }
  142. void onDragEnd() {
  143. setValue(minValue);
  144. index = 0;
  145. }
  146. };
  147. ////////////////////
  148. // ports
  149. ////////////////////
  150. struct Port : OpaqueWidget, SpriteWidget {
  151. enum PortType {
  152. INPUT,
  153. OUTPUT
  154. };
  155. Module *module = NULL;
  156. WireWidget *connectedWire = NULL;
  157. PortType type;
  158. int portId;
  159. Port();
  160. ~Port();
  161. void disconnect();
  162. void draw(NVGcontext *vg);
  163. void onMouseDown(int button);
  164. void onDragEnd();
  165. void onDragStart();
  166. void onDragDrop(Widget *origin);
  167. void onDragEnter(Widget *origin);
  168. void onDragLeave(Widget *origin);
  169. };
  170. ////////////////////
  171. // scene
  172. ////////////////////
  173. struct Toolbar : OpaqueWidget {
  174. Slider *wireOpacitySlider;
  175. Slider *wireTensionSlider;
  176. RadioButton *cpuUsageButton;
  177. Toolbar();
  178. void draw(NVGcontext *vg);
  179. };
  180. struct RackScene : Scene {
  181. Toolbar *toolbar;
  182. ScrollWidget *scrollWidget;
  183. RackScene();
  184. void step();
  185. void draw(NVGcontext *vg);
  186. };
  187. ////////////////////
  188. // globals
  189. ////////////////////
  190. extern std::string gApplicationName;
  191. extern std::string gApplicationVersion;
  192. extern Scene *gScene;
  193. extern RackWidget *gRackWidget;
  194. void sceneInit();
  195. void sceneDestroy();
  196. } // namespace rack