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.

170 lines
4.7KB

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