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.

180 lines
5.0KB

  1. #pragma once
  2. #include <plugin/Model.hpp>
  3. #include <ui/MenuOverlay.hpp>
  4. #include <ui/MenuItem.hpp>
  5. #include <ui/MenuLabel.hpp>
  6. #include <ui/Menu.hpp>
  7. #include <app/PortWidget.hpp>
  8. #include <app/ParamWidget.hpp>
  9. #include <app/ModuleLightWidget.hpp>
  10. #include <app/Scene.hpp>
  11. #include <app/SvgPanel.hpp>
  12. #include <engine/Module.hpp>
  13. #include <engine/ParamQuantity.hpp>
  14. #include <app.hpp>
  15. namespace rack {
  16. template <class TModule, class TModuleWidget>
  17. plugin::Model* createModel(const std::string& slug) {
  18. struct TModel : plugin::Model {
  19. engine::Module* createModule() override {
  20. engine::Module* m = new TModule;
  21. m->model = this;
  22. return m;
  23. }
  24. app::ModuleWidget* createModuleWidget(engine::Module* m) override {
  25. TModule *tm = NULL;
  26. if (m) {
  27. assert(m->model == this);
  28. tm = dynamic_cast<TModule*>(m);
  29. }
  30. app::ModuleWidget* mw = new TModuleWidget(tm);
  31. mw->model = this;
  32. return mw;
  33. }
  34. };
  35. plugin::Model* o = new TModel;
  36. o->slug = slug;
  37. return o;
  38. }
  39. template <class TWidget>
  40. TWidget* createWidget(math::Vec pos) {
  41. TWidget* o = new TWidget;
  42. o->box.pos = pos;
  43. return o;
  44. }
  45. template <class TWidget>
  46. TWidget* createWidgetCentered(math::Vec pos) {
  47. TWidget* o = createWidget<TWidget>(pos);
  48. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  49. return o;
  50. }
  51. inline app::SvgPanel* createPanel(std::string svgPath, std::string darkSvgPath = "") {
  52. app::SvgPanel* panel = new app::SvgPanel;
  53. std::shared_ptr<Svg> svg = APP->window->loadSvg(svgPath);
  54. std::shared_ptr<Svg> darkSvg;
  55. if (darkSvgPath != "")
  56. darkSvg = APP->window->loadSvg(darkSvgPath);
  57. panel->setBackground(svg, darkSvg);
  58. return panel;
  59. }
  60. template <class TParamWidget>
  61. TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
  62. TParamWidget* o = new TParamWidget;
  63. o->box.pos = pos;
  64. o->app::ParamWidget::module = module;
  65. o->app::ParamWidget::paramId = paramId;
  66. o->initParamQuantity();
  67. return o;
  68. }
  69. template <class TParamWidget>
  70. TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) {
  71. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  72. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  73. return o;
  74. }
  75. template <class TPortWidget>
  76. TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
  77. TPortWidget* o = new TPortWidget;
  78. o->box.pos = pos;
  79. o->app::PortWidget::module = module;
  80. o->app::PortWidget::type = engine::Port::INPUT;
  81. o->app::PortWidget::portId = inputId;
  82. return o;
  83. }
  84. template <class TPortWidget>
  85. TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
  86. TPortWidget* o = createInput<TPortWidget>(pos, module, inputId);
  87. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  88. return o;
  89. }
  90. template <class TPortWidget>
  91. TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
  92. TPortWidget* o = new TPortWidget;
  93. o->box.pos = pos;
  94. o->app::PortWidget::module = module;
  95. o->app::PortWidget::type = engine::Port::OUTPUT;
  96. o->app::PortWidget::portId = outputId;
  97. return o;
  98. }
  99. template <class TPortWidget>
  100. TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
  101. TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId);
  102. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  103. return o;
  104. }
  105. template <class TModuleLightWidget>
  106. TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) {
  107. TModuleLightWidget* o = new TModuleLightWidget;
  108. o->box.pos = pos;
  109. o->app::ModuleLightWidget::module = module;
  110. o->app::ModuleLightWidget::firstLightId = firstLightId;
  111. return o;
  112. }
  113. template <class TModuleLightWidget>
  114. TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
  115. TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId);
  116. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  117. return o;
  118. }
  119. /** Creates a param with a light and calls setFirstLightId() on it. */
  120. template <class TParamWidget>
  121. TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  122. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  123. o->setFirstLightId(firstLightId);
  124. return o;
  125. }
  126. template <class TParamWidget>
  127. TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  128. TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId);
  129. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  130. return o;
  131. }
  132. template <class TMenuLabel = ui::MenuLabel>
  133. TMenuLabel * createMenuLabel(std::string text) {
  134. TMenuLabel* o = new TMenuLabel;
  135. o->text = text;
  136. return o;
  137. }
  138. template <class TMenuItem = ui::MenuItem>
  139. TMenuItem * createMenuItem(std::string text, std::string rightText = "") {
  140. TMenuItem* o = new TMenuItem;
  141. o->text = text;
  142. o->rightText = rightText;
  143. return o;
  144. }
  145. template <class TMenu = ui::Menu>
  146. TMenu * createMenu() {
  147. TMenu* o = new TMenu;
  148. o->box.pos = APP->scene->mousePos;
  149. ui::MenuOverlay* menuOverlay = new ui::MenuOverlay;
  150. menuOverlay->addChild(o);
  151. APP->scene->addChild(menuOverlay);
  152. return o;
  153. }
  154. } // namespace rack