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.

270 lines
5.3KB

  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. // Eventually this should be replaced with a `moduleId` which will be used for inter-process communication between the gui world and the audio world.
  21. Module *module = NULL;
  22. // int moduleId;
  23. std::vector<InputPort*> inputs;
  24. std::vector<OutputPort*> outputs;
  25. std::vector<ParamWidget*> params;
  26. ModuleWidget(Module *module);
  27. ~ModuleWidget();
  28. // Convenience functions for adding special widgets (calls addChild())
  29. void addInput(InputPort *input);
  30. void addOutput(OutputPort *output);
  31. void addParam(ParamWidget *param);
  32. json_t *toJson();
  33. void fromJson(json_t *root);
  34. void disconnectPorts();
  35. void resetParams();
  36. void cloneParams(ModuleWidget *source);
  37. void draw(NVGcontext *vg);
  38. bool requested = false;
  39. Vec requestedPos;
  40. Vec dragPos;
  41. void onDragStart();
  42. void onDragMove(Vec mouseRel);
  43. void onDragEnd();
  44. void onMouseDown(int button);
  45. };
  46. struct WireWidget : OpaqueWidget {
  47. OutputPort *outputPort = NULL;
  48. InputPort *inputPort = 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 ModulePanel : TransparentWidget {
  78. NVGcolor backgroundColor;
  79. std::shared_ptr<Image> backgroundImage;
  80. void draw(NVGcontext *vg);
  81. };
  82. ////////////////////
  83. // params
  84. ////////////////////
  85. struct Light : TransparentWidget, SpriteWidget {
  86. NVGcolor color;
  87. void draw(NVGcontext *vg);
  88. };
  89. // If you don't add these to your ModuleWidget, it will fall out of the RackWidget
  90. struct Screw : TransparentWidget, SpriteWidget {
  91. Screw();
  92. };
  93. struct ParamWidget : OpaqueWidget, QuantityWidget {
  94. Module *module = NULL;
  95. int paramId;
  96. json_t *toJson();
  97. void fromJson(json_t *root);
  98. void onMouseDown(int button);
  99. void onChange();
  100. };
  101. struct Knob : ParamWidget, SpriteWidget {
  102. int minIndex, maxIndex, spriteCount;
  103. void step();
  104. void onDragStart();
  105. void onDragMove(Vec mouseRel);
  106. void onDragEnd();
  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 {
  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. };
  153. struct OutputPort : Port {
  154. int outputId;
  155. void onDragStart();
  156. void onDragDrop(Widget *origin);
  157. };
  158. ////////////////////
  159. // scene
  160. ////////////////////
  161. struct Toolbar : OpaqueWidget {
  162. Slider *wireOpacitySlider;
  163. Slider *wireTensionSlider;
  164. RadioButton *cpuUsageButton;
  165. Toolbar();
  166. void draw(NVGcontext *vg);
  167. };
  168. struct RackScene : Scene {
  169. Toolbar *toolbar;
  170. ScrollWidget *scrollWidget;
  171. RackScene();
  172. void step();
  173. };
  174. ////////////////////
  175. // Component Library
  176. ////////////////////
  177. struct PJ301M : SpriteWidget {
  178. PJ301M() {
  179. box.size = Vec(24, 24);
  180. spriteOffset = Vec(-10, -10);
  181. spriteSize = Vec(48, 48);
  182. spriteImage = Image::load("res/ComponentLibrary/PJ301M.png");
  183. }
  184. };
  185. struct InputPortPJ301M : InputPort, PJ301M {};
  186. struct OutputPortPJ301M: OutputPort, PJ301M {};
  187. struct PJ3410 : SpriteWidget {
  188. PJ3410() {
  189. box.size = Vec(31, 31);
  190. spriteOffset = Vec(-9, -9);
  191. spriteSize = Vec(54, 54);
  192. spriteImage = Image::load("res/ComponentLibrary/PJ3410.png");
  193. }
  194. };
  195. struct InputPortPJ3410 : InputPort, PJ3410 {};
  196. struct OutputPortPJ3410: OutputPort, PJ3410 {};
  197. struct CL1362 : SpriteWidget {
  198. CL1362() {
  199. box.size = Vec(33, 29);
  200. spriteOffset = Vec(-10, -10);
  201. spriteSize = Vec(57, 54);
  202. spriteImage = Image::load("res/ComponentLibrary/CL1362.png");
  203. }
  204. };
  205. struct InputPortCL1362 : InputPort, CL1362 {};
  206. struct OutputPortCL1362 : OutputPort, CL1362 {};
  207. ////////////////////
  208. // globals
  209. ////////////////////
  210. extern std::string gApplicationName;
  211. extern std::string gApplicationVersion;
  212. extern Scene *gScene;
  213. extern RackWidget *gRackWidget;
  214. void sceneInit();
  215. void sceneDestroy();
  216. } // namespace rack