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.

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