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.

390 lines
9.7KB

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