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.

166 lines
4.1KB

  1. #pragma once
  2. #include "plugin/Model.hpp"
  3. #include "ui/MenuLabel.hpp"
  4. #include "ui/MenuItem.hpp"
  5. #include "ui/Menu.hpp"
  6. #include "app/PortWidget.hpp"
  7. #include "app/ParamQuantity.hpp"
  8. #include "app/ParamWidget.hpp"
  9. #include "app/Scene.hpp"
  10. #include "engine/Module.hpp"
  11. #include "app.hpp"
  12. #include "window.hpp"
  13. namespace rack {
  14. template <class TModule, class TModuleWidget, typename... Tags>
  15. plugin::Model *createModel(std::string slug) {
  16. struct TModel : plugin::Model {
  17. engine::Module *createModule() override {
  18. TModule *o = new TModule;
  19. return o;
  20. }
  21. app::ModuleWidget *createModuleWidget() override {
  22. TModule *module = new TModule;
  23. TModuleWidget *o = new TModuleWidget(module);
  24. o->model = this;
  25. return o;
  26. }
  27. app::ModuleWidget *createModuleWidgetNull() override {
  28. TModuleWidget *o = new TModuleWidget(NULL);
  29. o->model = this;
  30. return o;
  31. }
  32. };
  33. plugin::Model *o = new TModel;
  34. o->slug = slug;
  35. return o;
  36. }
  37. template <class TWidget>
  38. TWidget *createWidget(math::Vec pos) {
  39. TWidget *o = new TWidget;
  40. o->box.pos = pos;
  41. return o;
  42. }
  43. template <class TWidget>
  44. TWidget *createWidgetCentered(math::Vec pos) {
  45. TWidget *o = new TWidget;
  46. o->box.pos = pos.minus(o->box.size.div(2));;
  47. return o;
  48. }
  49. template <class TParamWidget>
  50. TParamWidget *createParam(math::Vec pos, engine::Module *module, int paramId) {
  51. TParamWidget *o = new TParamWidget;
  52. o->box.pos = pos;
  53. if (module) {
  54. engine::ParamQuantityFactory *f = module->params[paramId].paramQuantityFactory;
  55. if (f)
  56. o->paramQuantity = f->create();
  57. else
  58. o->paramQuantity = new app::ParamQuantity;
  59. o->paramQuantity->module = module;
  60. o->paramQuantity->paramId = paramId;
  61. }
  62. return o;
  63. }
  64. template <class TParamWidget>
  65. TParamWidget *createParamCentered(math::Vec pos, engine::Module *module, int paramId) {
  66. TParamWidget *o = createParam<TParamWidget>(pos, module, paramId);
  67. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  68. return o;
  69. }
  70. template <class TPortWidget>
  71. TPortWidget *createInput(math::Vec pos, engine::Module *module, int inputId) {
  72. TPortWidget *o = new TPortWidget;
  73. o->box.pos = pos;
  74. o->module = module;
  75. o->type = app::PortWidget::INPUT;
  76. o->portId = inputId;
  77. return o;
  78. }
  79. template <class TPortWidget>
  80. TPortWidget *createInputCentered(math::Vec pos, engine::Module *module, int inputId) {
  81. TPortWidget *o = new TPortWidget;
  82. o->box.pos = pos.minus(o->box.size.div(2));
  83. o->module = module;
  84. o->type = app::PortWidget::INPUT;
  85. o->portId = inputId;
  86. return o;
  87. }
  88. template <class TPortWidget>
  89. TPortWidget *createOutput(math::Vec pos, engine::Module *module, int outputId) {
  90. TPortWidget *o = new TPortWidget;
  91. o->box.pos = pos;
  92. o->module = module;
  93. o->type = app::PortWidget::OUTPUT;
  94. o->portId = outputId;
  95. return o;
  96. }
  97. template <class TPortWidget>
  98. TPortWidget *createOutputCentered(math::Vec pos, engine::Module *module, int outputId) {
  99. TPortWidget *o = new TPortWidget;
  100. o->box.pos = pos.minus(o->box.size.div(2));
  101. o->module = module;
  102. o->type = app::PortWidget::OUTPUT;
  103. o->portId = outputId;
  104. return o;
  105. }
  106. template <class TModuleLightWidget>
  107. TModuleLightWidget *createLight(math::Vec pos, engine::Module *module, int firstLightId) {
  108. TModuleLightWidget *o = new TModuleLightWidget;
  109. o->box.pos = pos;
  110. o->module = module;
  111. o->firstLightId = firstLightId;
  112. return o;
  113. }
  114. template <class TModuleLightWidget>
  115. TModuleLightWidget *createLightCentered(math::Vec pos, engine::Module *module, int firstLightId) {
  116. TModuleLightWidget *o = new TModuleLightWidget;
  117. o->box.pos = pos.minus(o->box.size.div(2));
  118. o->module = module;
  119. o->firstLightId = firstLightId;
  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. inline ui::Menu *createMenu() {
  136. ui::Menu *o = new ui::Menu;
  137. o->box.pos = APP->window->mousePos;
  138. ui::MenuOverlay *menuOverlay = new ui::MenuOverlay;
  139. menuOverlay->addChild(o);
  140. APP->scene->addChild(menuOverlay);
  141. return o;
  142. }
  143. } // namespace rack