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.

388 lines
9.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. /** Creates a MenuItem with an action that calls a lambda function.
  156. Example:
  157. menu->addChild(createMenuItem("Load sample", "kick.wav",
  158. [=]() {
  159. module->loadSample();
  160. }
  161. ));
  162. */
  163. template <class TMenuItem = ui::MenuItem>
  164. TMenuItem* createMenuItem(std::string text, std::string rightText, std::function<void()> action) {
  165. struct Item : TMenuItem {
  166. std::function<void()> action;
  167. void onAction(const event::Action& e) override {
  168. action();
  169. }
  170. };
  171. Item* item = createMenuItem<Item>(text, rightText);
  172. item->action = action;
  173. return item;
  174. }
  175. /** Creates a MenuItem with a check mark set by a lambda function.
  176. Example:
  177. menu->addChild(createCheckMenuItem("Loop",
  178. [=]() {
  179. return module->isLoop();
  180. },
  181. [=]() {
  182. module->toggleLoop();
  183. }
  184. ));
  185. */
  186. inline ui::MenuItem* createCheckMenuItem(std::string text, std::function<bool()> checked, std::function<void()> action) {
  187. struct Item : ui::MenuItem {
  188. std::function<void()> action;
  189. void onAction(const event::Action& e) override {
  190. action();
  191. }
  192. };
  193. Item* item = createMenuItem<Item>(text, CHECKMARK(checked()));
  194. item->action = action;
  195. return item;
  196. }
  197. /** Creates a MenuItem that controls a boolean value with a check mark.
  198. Example:
  199. menu->addChild(createBoolMenuItem("Loop",
  200. [=]() {
  201. return module->isLoop();
  202. },
  203. [=](bool loop) {
  204. module->setLoop(loop);
  205. }
  206. ));
  207. */
  208. inline ui::MenuItem* createBoolMenuItem(std::string text, std::function<bool()> getter, std::function<void(bool state)> setter) {
  209. struct Item : ui::MenuItem {
  210. std::function<void(size_t)> setter;
  211. bool val;
  212. void onAction(const event::Action& e) override {
  213. setter(val);
  214. }
  215. };
  216. bool currVal = getter();
  217. Item* item = createMenuItem<Item>(text, CHECKMARK(currVal));
  218. item->setter = setter;
  219. item->val = !currVal;
  220. return item;
  221. }
  222. /** Easy wrapper for createBoolMenuItem() to modify a bool pointer.
  223. Example:
  224. menu->addChild(createBoolPtrMenuItem("Loop", &module->loop));
  225. */
  226. template <typename T>
  227. ui::MenuItem* createBoolPtrMenuItem(std::string text, T* ptr) {
  228. return createBoolMenuItem(text,
  229. [=]() {return *ptr;},
  230. [=](T val) {*ptr = val;}
  231. );
  232. }
  233. /** Creates a MenuItem that opens a submenu.
  234. Example:
  235. menu->addChild(createSubmenuItem("Edit",
  236. [=](Menu* menu) {
  237. menu->addChild(createMenuItem("Copy", "", [=]() {copy();}));
  238. menu->addChild(createMenuItem("Paste", "", [=]() {paste();}));
  239. }
  240. ));
  241. */
  242. inline ui::MenuItem* createSubmenuItem(std::string text, std::function<void(ui::Menu* menu)> createMenu) {
  243. struct Item : ui::MenuItem {
  244. std::function<void(ui::Menu* menu)> createMenu;
  245. ui::Menu* createChildMenu() override {
  246. ui::Menu* menu = new ui::Menu;
  247. createMenu(menu);
  248. return menu;
  249. }
  250. };
  251. Item* item = createMenuItem<Item>(text, RIGHT_ARROW);
  252. item->createMenu = createMenu;
  253. return item;
  254. }
  255. /** Creates a MenuItem that when hovered, opens a submenu with several MenuItems indexed by an integer.
  256. Example:
  257. menu->addChild(createIndexSubmenuItem("Mode",
  258. {"Hi-fi", "Mid-fi", "Lo-fi"},
  259. [=]() {
  260. return module->getMode();
  261. },
  262. [=](int mode) {
  263. module->setMode(mode);
  264. }
  265. ));
  266. */
  267. inline ui::MenuItem* createIndexSubmenuItem(std::string text, std::vector<std::string> labels, std::function<size_t()> getter, std::function<void(size_t val)> setter) {
  268. struct IndexItem : ui::MenuItem {
  269. std::function<void(size_t)> setter;
  270. size_t index;
  271. void onAction(const event::Action& e) override {
  272. setter(index);
  273. }
  274. };
  275. struct Item : ui::MenuItem {
  276. std::function<size_t()> getter;
  277. std::function<void(size_t)> setter;
  278. std::vector<std::string> labels;
  279. ui::Menu* createChildMenu() override {
  280. ui::Menu* menu = new ui::Menu;
  281. size_t currIndex = getter();
  282. for (size_t i = 0; i < labels.size(); i++) {
  283. IndexItem* item = createMenuItem<IndexItem>(labels[i], CHECKMARK(currIndex == i));
  284. item->setter = setter;
  285. item->index = i;
  286. menu->addChild(item);
  287. }
  288. return menu;
  289. }
  290. };
  291. size_t currIndex = getter();
  292. std::string label = (currIndex < labels.size()) ? labels[currIndex] : "";
  293. Item* item = createMenuItem<Item>(text, label + " " + RIGHT_ARROW);
  294. item->getter = getter;
  295. item->setter = setter;
  296. item->labels = labels;
  297. return item;
  298. }
  299. /** Easy wrapper for createIndexSubmenuItem() that controls an integer index at a pointer address.
  300. Example:
  301. menu->addChild(createIndexPtrSubmenuItem("Mode",
  302. {"Hi-fi", "Mid-fi", "Lo-fi"},
  303. &module->mode
  304. ));
  305. */
  306. template <typename T>
  307. ui::MenuItem* createIndexPtrSubmenuItem(std::string text, std::vector<std::string> labels, T* ptr) {
  308. return createIndexSubmenuItem(text, labels,
  309. [=]() {return *ptr;},
  310. [=](size_t index) {*ptr = T(index);}
  311. );
  312. }
  313. } // namespace rack