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

  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(std::string slug) {
  17. struct TModel : plugin::Model {
  18. engine::Module *createModule() override {
  19. TModule *m = new TModule;
  20. m->model = this;
  21. return m;
  22. }
  23. app::ModuleWidget *createModuleWidget() override {
  24. TModule *m = new TModule;
  25. m->model = this;
  26. TModuleWidget *mw = new TModuleWidget(m);
  27. mw->model = this;
  28. return mw;
  29. }
  30. app::ModuleWidget *createModuleWidgetNull() override {
  31. TModuleWidget *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 = new TWidget;
  49. o->box.pos = 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. engine::ParamQuantityFactory *f = module->params[paramId].paramQuantityFactory;
  58. if (f)
  59. o->paramQuantity = f->create();
  60. else
  61. o->paramQuantity = new engine::ParamQuantity;
  62. o->paramQuantity->module = module;
  63. o->paramQuantity->paramId = paramId;
  64. }
  65. return o;
  66. }
  67. template <class TParamWidget>
  68. TParamWidget *createParamCentered(math::Vec pos, engine::Module *module, int paramId) {
  69. TParamWidget *o = createParam<TParamWidget>(pos, module, paramId);
  70. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  71. return o;
  72. }
  73. template <class TPortWidget>
  74. TPortWidget *createInput(math::Vec pos, engine::Module *module, int inputId) {
  75. TPortWidget *o = new TPortWidget;
  76. o->box.pos = pos;
  77. o->module = module;
  78. o->type = app::PortWidget::INPUT;
  79. o->portId = inputId;
  80. return o;
  81. }
  82. template <class TPortWidget>
  83. TPortWidget *createInputCentered(math::Vec pos, engine::Module *module, int inputId) {
  84. TPortWidget *o = new TPortWidget;
  85. o->box.pos = pos.minus(o->box.size.div(2));
  86. o->module = module;
  87. o->type = app::PortWidget::INPUT;
  88. o->portId = inputId;
  89. return o;
  90. }
  91. template <class TPortWidget>
  92. TPortWidget *createOutput(math::Vec pos, engine::Module *module, int outputId) {
  93. TPortWidget *o = new TPortWidget;
  94. o->box.pos = pos;
  95. o->module = module;
  96. o->type = app::PortWidget::OUTPUT;
  97. o->portId = outputId;
  98. return o;
  99. }
  100. template <class TPortWidget>
  101. TPortWidget *createOutputCentered(math::Vec pos, engine::Module *module, int outputId) {
  102. TPortWidget *o = new TPortWidget;
  103. o->box.pos = pos.minus(o->box.size.div(2));
  104. o->module = module;
  105. o->type = app::PortWidget::OUTPUT;
  106. o->portId = outputId;
  107. return o;
  108. }
  109. template <class TModuleLightWidget>
  110. TModuleLightWidget *createLight(math::Vec pos, engine::Module *module, int firstLightId) {
  111. TModuleLightWidget *o = new TModuleLightWidget;
  112. o->box.pos = pos;
  113. o->module = module;
  114. o->firstLightId = firstLightId;
  115. return o;
  116. }
  117. template <class TModuleLightWidget>
  118. TModuleLightWidget *createLightCentered(math::Vec pos, engine::Module *module, int firstLightId) {
  119. TModuleLightWidget *o = new TModuleLightWidget;
  120. o->box.pos = pos.minus(o->box.size.div(2));
  121. o->module = module;
  122. o->firstLightId = firstLightId;
  123. return o;
  124. }
  125. template <class TMenuLabel = ui::MenuLabel>
  126. TMenuLabel *createMenuLabel(std::string text) {
  127. TMenuLabel *o = new TMenuLabel;
  128. o->text = text;
  129. return o;
  130. }
  131. template <class TMenuItem = ui::MenuItem>
  132. TMenuItem *createMenuItem(std::string text, std::string rightText = "") {
  133. TMenuItem *o = new TMenuItem;
  134. o->text = text;
  135. o->rightText = rightText;
  136. return o;
  137. }
  138. inline ui::Menu *createMenu() {
  139. ui::Menu *o = new ui::Menu;
  140. o->box.pos = APP->window->mousePos;
  141. ui::MenuOverlay *menuOverlay = new ui::MenuOverlay;
  142. menuOverlay->addChild(o);
  143. APP->scene->addChild(menuOverlay);
  144. return o;
  145. }
  146. } // namespace rack