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.

165 lines
3.9KB

  1. #include "plugin/Model.hpp"
  2. #include "ui/MenuLabel.hpp"
  3. #include "ui/MenuItem.hpp"
  4. #include "ui/Menu.hpp"
  5. #include "app/PortWidget.hpp"
  6. #include "app/ParamQuantity.hpp"
  7. #include "app/ParamWidget.hpp"
  8. #include "app/Scene.hpp"
  9. #include "engine/Module.hpp"
  10. #include "app.hpp"
  11. #include "window.hpp"
  12. namespace rack {
  13. template <class TModule, class TModuleWidget, typename... Tags>
  14. Model *createModel(std::string slug) {
  15. struct TModel : Model {
  16. Module *createModule() override {
  17. TModule *o = new TModule;
  18. return o;
  19. }
  20. ModuleWidget *createModuleWidget() override {
  21. TModule *module = new TModule;
  22. TModuleWidget *o = new TModuleWidget(module);
  23. o->model = this;
  24. return o;
  25. }
  26. ModuleWidget *createModuleWidgetNull() override {
  27. TModuleWidget *o = new TModuleWidget(NULL);
  28. o->model = this;
  29. return o;
  30. }
  31. };
  32. Model *o = new TModel;
  33. o->slug = slug;
  34. return o;
  35. }
  36. template <class TWidget>
  37. TWidget *createWidget(math::Vec pos) {
  38. TWidget *o = new TWidget;
  39. o->box.pos = pos;
  40. return o;
  41. }
  42. template <class TWidget>
  43. TWidget *createWidgetCentered(math::Vec pos) {
  44. TWidget *o = new TWidget;
  45. o->box.pos = pos.minus(o->box.size.div(2));;
  46. return o;
  47. }
  48. template <class TParamWidget>
  49. TParamWidget *createParam(math::Vec pos, Module *module, int paramId) {
  50. TParamWidget *o = new TParamWidget;
  51. o->box.pos = pos;
  52. if (module) {
  53. ParamQuantityFactory *f = module->params[paramId].paramQuantityFactory;
  54. if (f)
  55. o->paramQuantity = f->create();
  56. else
  57. o->paramQuantity = new ParamQuantity;
  58. o->paramQuantity->module = module;
  59. o->paramQuantity->paramId = paramId;
  60. }
  61. return o;
  62. }
  63. template <class TParamWidget>
  64. TParamWidget *createParamCentered(math::Vec pos, Module *module, int paramId) {
  65. TParamWidget *o = createParam<TParamWidget>(pos, module, paramId);
  66. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  67. return o;
  68. }
  69. template <class TPortWidget>
  70. TPortWidget *createInput(math::Vec pos, Module *module, int inputId) {
  71. TPortWidget *o = new TPortWidget;
  72. o->box.pos = pos;
  73. o->module = module;
  74. o->type = PortWidget::INPUT;
  75. o->portId = inputId;
  76. return o;
  77. }
  78. template <class TPortWidget>
  79. TPortWidget *createInputCentered(math::Vec pos, Module *module, int inputId) {
  80. TPortWidget *o = new TPortWidget;
  81. o->box.pos = pos.minus(o->box.size.div(2));
  82. o->module = module;
  83. o->type = PortWidget::INPUT;
  84. o->portId = inputId;
  85. return o;
  86. }
  87. template <class TPortWidget>
  88. TPortWidget *createOutput(math::Vec pos, Module *module, int outputId) {
  89. TPortWidget *o = new TPortWidget;
  90. o->box.pos = pos;
  91. o->module = module;
  92. o->type = PortWidget::OUTPUT;
  93. o->portId = outputId;
  94. return o;
  95. }
  96. template <class TPortWidget>
  97. TPortWidget *createOutputCentered(math::Vec pos, Module *module, int outputId) {
  98. TPortWidget *o = new TPortWidget;
  99. o->box.pos = pos.minus(o->box.size.div(2));
  100. o->module = module;
  101. o->type = PortWidget::OUTPUT;
  102. o->portId = outputId;
  103. return o;
  104. }
  105. template <class TModuleLightWidget>
  106. TModuleLightWidget *createLight(math::Vec pos, Module *module, int firstLightId) {
  107. TModuleLightWidget *o = new TModuleLightWidget;
  108. o->box.pos = pos;
  109. o->module = module;
  110. o->firstLightId = firstLightId;
  111. return o;
  112. }
  113. template <class TModuleLightWidget>
  114. TModuleLightWidget *createLightCentered(math::Vec pos, Module *module, int firstLightId) {
  115. TModuleLightWidget *o = new TModuleLightWidget;
  116. o->box.pos = pos.minus(o->box.size.div(2));
  117. o->module = module;
  118. o->firstLightId = firstLightId;
  119. return o;
  120. }
  121. template <class TMenuLabel = MenuLabel>
  122. TMenuLabel *createMenuLabel(std::string text) {
  123. TMenuLabel *o = new TMenuLabel;
  124. o->text = text;
  125. return o;
  126. }
  127. template <class TMenuItem = MenuItem>
  128. TMenuItem *createMenuItem(std::string text, std::string rightText = "") {
  129. TMenuItem *o = new TMenuItem;
  130. o->text = text;
  131. o->rightText = rightText;
  132. return o;
  133. }
  134. inline Menu *createMenu() {
  135. Menu *o = new Menu;
  136. o->box.pos = app()->window->mousePos;
  137. MenuOverlay *menuOverlay = new MenuOverlay;
  138. menuOverlay->addChild(o);
  139. app()->scene->addChild(menuOverlay);
  140. return o;
  141. }
  142. } // namespace rack