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.5KB

  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>
  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. if (module) {
  55. o->paramQuantity = module->paramQuantities[paramId];
  56. }
  57. o->init();
  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->module = module;
  71. o->type = app::PortWidget::INPUT;
  72. o->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->module = module;
  86. o->type = app::PortWidget::OUTPUT;
  87. o->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->module = module;
  101. o->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