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.

331 lines
8.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/ModuleLightWidget.hpp>
  10. #include <app/Scene.hpp>
  11. #include <app/SvgPanel.hpp>
  12. #include <engine/Module.hpp>
  13. #include <engine/ParamQuantity.hpp>
  14. #include <context.hpp>
  15. #include <functional>
  16. namespace rack {
  17. template <class TModule, class TModuleWidget>
  18. plugin::Model* createModel(const std::string& slug) {
  19. struct TModel : plugin::Model {
  20. engine::Module* createModule() override {
  21. engine::Module* m = new TModule;
  22. m->model = this;
  23. return m;
  24. }
  25. app::ModuleWidget* createModuleWidget(engine::Module* m) override {
  26. TModule* tm = NULL;
  27. if (m) {
  28. assert(m->model == this);
  29. tm = dynamic_cast<TModule*>(m);
  30. }
  31. app::ModuleWidget* mw = new TModuleWidget(tm);
  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. inline app::SvgPanel* createPanel(std::string svgPath) {
  53. app::SvgPanel* panel = new app::SvgPanel;
  54. std::shared_ptr<Svg> svg = Svg::load(svgPath);
  55. panel->setBackground(svg);
  56. return panel;
  57. }
  58. template <class TParamWidget>
  59. TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
  60. TParamWidget* o = new TParamWidget;
  61. o->box.pos = pos;
  62. o->app::ParamWidget::module = module;
  63. o->app::ParamWidget::paramId = paramId;
  64. o->initParamQuantity();
  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->app::PortWidget::module = module;
  78. o->app::PortWidget::type = engine::Port::INPUT;
  79. o->app::PortWidget::portId = inputId;
  80. return o;
  81. }
  82. template <class TPortWidget>
  83. TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
  84. TPortWidget* o = createInput<TPortWidget>(pos, module, inputId);
  85. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  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->app::PortWidget::module = module;
  93. o->app::PortWidget::type = engine::Port::OUTPUT;
  94. o->app::PortWidget::portId = outputId;
  95. return o;
  96. }
  97. template <class TPortWidget>
  98. TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
  99. TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId);
  100. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  101. return o;
  102. }
  103. template <class TModuleLightWidget>
  104. TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) {
  105. TModuleLightWidget* o = new TModuleLightWidget;
  106. o->box.pos = pos;
  107. o->app::ModuleLightWidget::module = module;
  108. o->app::ModuleLightWidget::firstLightId = firstLightId;
  109. return o;
  110. }
  111. template <class TModuleLightWidget>
  112. TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
  113. TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId);
  114. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  115. return o;
  116. }
  117. /** Creates a param with a light and calls setFirstLightId() on it.
  118. Requires ParamWidget to have a `light` member.
  119. */
  120. template <class TParamWidget>
  121. TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  122. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  123. o->light->module = module;
  124. o->light->firstLightId = firstLightId;
  125. return o;
  126. }
  127. template <class TParamWidget>
  128. TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  129. TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId);
  130. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  131. return o;
  132. }
  133. template <class TMenu = ui::Menu>
  134. TMenu* createMenu() {
  135. TMenu* o = new TMenu;
  136. o->box.pos = APP->scene->mousePos;
  137. ui::MenuOverlay* menuOverlay = new ui::MenuOverlay;
  138. menuOverlay->addChild(o);
  139. APP->scene->addChild(menuOverlay);
  140. return o;
  141. }
  142. template <class TMenuLabel = ui::MenuLabel>
  143. TMenuLabel* createMenuLabel(std::string text) {
  144. TMenuLabel* o = new TMenuLabel;
  145. o->text = text;
  146. return o;
  147. }
  148. template <class TMenuItem = ui::MenuItem>
  149. TMenuItem* createMenuItem(std::string text, std::string rightText = "") {
  150. TMenuItem* o = new TMenuItem;
  151. o->text = text;
  152. o->rightText = rightText;
  153. return o;
  154. }
  155. template <class TMenuItem = ui::MenuItem>
  156. TMenuItem* createMenuItem(std::string text, std::string rightText, std::function<void()> action) {
  157. struct Item : TMenuItem {
  158. std::function<void()> action;
  159. void onAction(const event::Action& e) override {
  160. action();
  161. }
  162. };
  163. Item* item = createMenuItem<Item>(text, rightText);
  164. item->action = action;
  165. return item;
  166. }
  167. /** Creates a MenuItem with a check mark set by a lambda function.
  168. */
  169. inline ui::MenuItem* createCheckMenuItem(std::string text, std::function<bool()> checked, std::function<void()> action) {
  170. struct Item : ui::MenuItem {
  171. std::function<void()> action;
  172. void onAction(const event::Action& e) override {
  173. action();
  174. }
  175. };
  176. Item* item = createMenuItem<Item>(text, CHECKMARK(checked()));
  177. item->action = action;
  178. return item;
  179. }
  180. /** Creates a MenuItem that controls a boolean value with a check mark.
  181. */
  182. inline ui::MenuItem* createBoolMenuItem(std::string text, std::function<bool()> getter, std::function<void(bool state)> setter) {
  183. struct Item : ui::MenuItem {
  184. std::function<void(size_t)> setter;
  185. bool val;
  186. void onAction(const event::Action& e) override {
  187. setter(val);
  188. }
  189. };
  190. bool currVal = getter();
  191. Item* item = createMenuItem<Item>(text, CHECKMARK(currVal));
  192. item->setter = setter;
  193. item->val = !currVal;
  194. return item;
  195. }
  196. /** Easy wrapper for createBoolMenuItem() to modify a bool pointer.
  197. */
  198. template <typename T>
  199. ui::MenuItem* createBoolPtrMenuItem(std::string text, T* ptr) {
  200. return createBoolMenuItem(text,
  201. [=]() {return *ptr;},
  202. [=](T val) {*ptr = val;}
  203. );
  204. }
  205. /** Creates a MenuItem that opens a submenu.
  206. */
  207. inline ui::MenuItem* createSubmenuItem(std::string text, std::function<void(ui::Menu* menu)> createMenu) {
  208. struct Item : ui::MenuItem {
  209. std::function<void(ui::Menu* menu)> createMenu;
  210. ui::Menu* createChildMenu() override {
  211. ui::Menu* menu = new ui::Menu;
  212. createMenu(menu);
  213. return menu;
  214. }
  215. };
  216. Item* item = createMenuItem<Item>(text, RIGHT_ARROW);
  217. item->createMenu = createMenu;
  218. return item;
  219. }
  220. /** Creates a MenuItem that when hovered, opens a submenu with several MenuItems indexed by an integer.
  221. */
  222. inline ui::MenuItem* createIndexSubmenuItem(std::string text, std::vector<std::string> labels, std::function<size_t()> getter, std::function<void(size_t val)> setter) {
  223. struct IndexItem : ui::MenuItem {
  224. std::function<void(size_t)> setter;
  225. size_t index;
  226. void onAction(const event::Action& e) override {
  227. setter(index);
  228. }
  229. };
  230. struct Item : ui::MenuItem {
  231. std::function<size_t()> getter;
  232. std::function<void(size_t)> setter;
  233. std::vector<std::string> labels;
  234. ui::Menu* createChildMenu() override {
  235. ui::Menu* menu = new ui::Menu;
  236. size_t currIndex = getter();
  237. for (size_t i = 0; i < labels.size(); i++) {
  238. IndexItem* item = createMenuItem<IndexItem>(labels[i], CHECKMARK(currIndex == i));
  239. item->setter = setter;
  240. item->index = i;
  241. menu->addChild(item);
  242. }
  243. return menu;
  244. }
  245. };
  246. size_t currIndex = getter();
  247. std::string label = (currIndex < labels.size()) ? labels[currIndex] : "";
  248. Item* item = createMenuItem<Item>(text, label + " " + RIGHT_ARROW);
  249. item->getter = getter;
  250. item->setter = setter;
  251. item->labels = labels;
  252. return item;
  253. }
  254. /** Easy wrapper for createIndexSubmenuItem() that controls an integer index at a pointer address.
  255. */
  256. template <typename T>
  257. ui::MenuItem* createIndexPtrSubmenuItem(std::string text, std::vector<std::string> labels, T* ptr) {
  258. return createIndexSubmenuItem(text, labels,
  259. [=]() {return *ptr;},
  260. [=](size_t index) {*ptr = T(index);}
  261. );
  262. }
  263. } // namespace rack