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.

177 lines
4.9KB

  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 <context.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) {
  52. app::SvgPanel* panel = new app::SvgPanel;
  53. std::shared_ptr<Svg> svg = Svg::load(svgPath);
  54. panel->setBackground(svg);
  55. return panel;
  56. }
  57. template <class TParamWidget>
  58. TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
  59. TParamWidget* o = new TParamWidget;
  60. o->box.pos = pos;
  61. o->app::ParamWidget::module = module;
  62. o->app::ParamWidget::paramId = paramId;
  63. o->initParamQuantity();
  64. return o;
  65. }
  66. template <class TParamWidget>
  67. TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) {
  68. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  69. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  70. return o;
  71. }
  72. template <class TPortWidget>
  73. TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
  74. TPortWidget* o = new TPortWidget;
  75. o->box.pos = pos;
  76. o->app::PortWidget::module = module;
  77. o->app::PortWidget::type = engine::Port::INPUT;
  78. o->app::PortWidget::portId = inputId;
  79. return o;
  80. }
  81. template <class TPortWidget>
  82. TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
  83. TPortWidget* o = createInput<TPortWidget>(pos, module, inputId);
  84. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  85. return o;
  86. }
  87. template <class TPortWidget>
  88. TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
  89. TPortWidget* o = new TPortWidget;
  90. o->box.pos = pos;
  91. o->app::PortWidget::module = module;
  92. o->app::PortWidget::type = engine::Port::OUTPUT;
  93. o->app::PortWidget::portId = outputId;
  94. return o;
  95. }
  96. template <class TPortWidget>
  97. TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
  98. TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId);
  99. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  100. return o;
  101. }
  102. template <class TModuleLightWidget>
  103. TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) {
  104. TModuleLightWidget* o = new TModuleLightWidget;
  105. o->box.pos = pos;
  106. o->app::ModuleLightWidget::module = module;
  107. o->app::ModuleLightWidget::firstLightId = firstLightId;
  108. return o;
  109. }
  110. template <class TModuleLightWidget>
  111. TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
  112. TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId);
  113. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  114. return o;
  115. }
  116. /** Creates a param with a light and calls setFirstLightId() on it. */
  117. template <class TParamWidget>
  118. TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  119. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  120. o->setFirstLightId(firstLightId);
  121. return o;
  122. }
  123. template <class TParamWidget>
  124. TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  125. TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId);
  126. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  127. return o;
  128. }
  129. template <class TMenuLabel = ui::MenuLabel>
  130. TMenuLabel* createMenuLabel(std::string text) {
  131. TMenuLabel* o = new TMenuLabel;
  132. o->text = text;
  133. return o;
  134. }
  135. template <class TMenuItem = ui::MenuItem>
  136. TMenuItem* createMenuItem(std::string text, std::string rightText = "") {
  137. TMenuItem* o = new TMenuItem;
  138. o->text = text;
  139. o->rightText = rightText;
  140. return o;
  141. }
  142. template <class TMenu = ui::Menu>
  143. TMenu* createMenu() {
  144. TMenu* o = new TMenu;
  145. o->box.pos = APP->scene->mousePos;
  146. ui::MenuOverlay* menuOverlay = new ui::MenuOverlay;
  147. menuOverlay->addChild(o);
  148. APP->scene->addChild(menuOverlay);
  149. return o;
  150. }
  151. } // namespace rack