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.

169 lines
4.6KB

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