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.

helpers.hpp 3.8KB

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