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.

238 lines
4.4KB

  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 InputPort;
  11. struct OutputPort;
  12. struct Scene;
  13. ////////////////////
  14. // module
  15. ////////////////////
  16. // A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
  17. struct Model;
  18. struct ModuleWidget : OpaqueWidget {
  19. Model *model = NULL;
  20. /** Owns the module pointer */
  21. Module *module = NULL;
  22. std::vector<InputPort*> inputs;
  23. std::vector<OutputPort*> outputs;
  24. std::vector<ParamWidget*> params;
  25. ~ModuleWidget();
  26. void setModule(Module *module);
  27. // Convenience functions for adding special widgets (calls addChild())
  28. void addInput(InputPort *input);
  29. void addOutput(OutputPort *output);
  30. void addParam(ParamWidget *param);
  31. json_t *toJson();
  32. void fromJson(json_t *root);
  33. void disconnectPorts();
  34. void resetParams();
  35. void cloneParams(ModuleWidget *source);
  36. void draw(NVGcontext *vg);
  37. bool requested = false;
  38. Vec requestedPos;
  39. Vec dragPos;
  40. void onDragStart();
  41. void onDragMove(Vec mouseRel);
  42. void onDragEnd();
  43. void onMouseDown(int button);
  44. };
  45. struct WireWidget : OpaqueWidget {
  46. OutputPort *outputPort = NULL;
  47. InputPort *inputPort = NULL;
  48. OutputPort *hoveredOutputPort = NULL;
  49. InputPort *hoveredInputPort = NULL;
  50. Wire *wire = NULL;
  51. NVGcolor color;
  52. WireWidget();
  53. ~WireWidget();
  54. void updateWire();
  55. void draw(NVGcontext *vg);
  56. void drawOutputPlug(NVGcontext *vg);
  57. void drawInputPlug(NVGcontext *vg);
  58. };
  59. struct RackWidget : OpaqueWidget {
  60. // Only put ModuleWidgets in here
  61. Widget *moduleContainer;
  62. // Only put WireWidgets in here
  63. Widget *wireContainer;
  64. WireWidget *activeWire = NULL;
  65. std::shared_ptr<Image> railsImage;
  66. RackWidget();
  67. ~RackWidget();
  68. void clear();
  69. void savePatch(std::string filename);
  70. void loadPatch(std::string filename);
  71. json_t *toJson();
  72. void fromJson(json_t *root);
  73. void repositionModule(ModuleWidget *module);
  74. void step();
  75. void draw(NVGcontext *vg);
  76. void onMouseDown(int button);
  77. };
  78. struct Panel : TransparentWidget {
  79. NVGcolor backgroundColor;
  80. NVGcolor borderColor;
  81. std::shared_ptr<Image> backgroundImage;
  82. void draw(NVGcontext *vg);
  83. };
  84. ////////////////////
  85. // params
  86. ////////////////////
  87. struct Light : TransparentWidget {
  88. NVGcolor color;
  89. void draw(NVGcontext *vg);
  90. };
  91. struct ParamWidget : OpaqueWidget, QuantityWidget {
  92. Module *module = NULL;
  93. int paramId;
  94. json_t *toJson();
  95. void fromJson(json_t *root);
  96. void onMouseDown(int button);
  97. void onChange();
  98. };
  99. struct Knob : ParamWidget {
  100. void onDragStart();
  101. void onDragMove(Vec mouseRel);
  102. void onDragEnd();
  103. };
  104. struct SpriteKnob : Knob, SpriteWidget {
  105. int minIndex, maxIndex, spriteCount;
  106. void step();
  107. };
  108. struct Switch : ParamWidget, SpriteWidget {
  109. };
  110. struct ToggleSwitch : virtual Switch {
  111. void onDragStart() {
  112. index = 1;
  113. }
  114. void onDragEnd() {
  115. index = 0;
  116. }
  117. void onDragDrop(Widget *origin) {
  118. if (origin != this)
  119. return;
  120. // Cycle through modes
  121. // e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
  122. float v = value + 1.0;
  123. setValue(v > maxValue ? minValue : v);
  124. }
  125. };
  126. struct MomentarySwitch : virtual Switch {
  127. void onDragStart() {
  128. setValue(maxValue);
  129. index = 1;
  130. }
  131. void onDragEnd() {
  132. setValue(minValue);
  133. index = 0;
  134. }
  135. };
  136. ////////////////////
  137. // ports
  138. ////////////////////
  139. struct Port : OpaqueWidget, SpriteWidget {
  140. Module *module = NULL;
  141. WireWidget *connectedWire = NULL;
  142. Port();
  143. ~Port();
  144. void disconnect();
  145. void onMouseDown(int button);
  146. void onDragEnd();
  147. };
  148. struct InputPort : Port {
  149. int inputId;
  150. void onDragStart();
  151. void onDragDrop(Widget *origin);
  152. void onDragEnter(Widget *origin);
  153. void onDragLeave(Widget *origin);
  154. void draw(NVGcontext *vg);
  155. };
  156. struct OutputPort : Port {
  157. int outputId;
  158. void onDragStart();
  159. void onDragDrop(Widget *origin);
  160. void onDragEnter(Widget *origin);
  161. void onDragLeave(Widget *origin);
  162. void draw(NVGcontext *vg);
  163. };
  164. ////////////////////
  165. // scene
  166. ////////////////////
  167. struct Toolbar : OpaqueWidget {
  168. Slider *wireOpacitySlider;
  169. Slider *wireTensionSlider;
  170. RadioButton *cpuUsageButton;
  171. Toolbar();
  172. void draw(NVGcontext *vg);
  173. };
  174. struct RackScene : Scene {
  175. Toolbar *toolbar;
  176. ScrollWidget *scrollWidget;
  177. RackScene();
  178. void step();
  179. void draw(NVGcontext *vg);
  180. };
  181. ////////////////////
  182. // globals
  183. ////////////////////
  184. extern std::string gApplicationName;
  185. extern std::string gApplicationVersion;
  186. extern Scene *gScene;
  187. extern RackWidget *gRackWidget;
  188. void sceneInit();
  189. void sceneDestroy();
  190. } // namespace rack