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.

438 lines
11KB

  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. /** Returns a Model that constructs a Module and ModuleWidget subclass. */
  18. template <class TModule, class TModuleWidget>
  19. plugin::Model* createModel(std::string slug) {
  20. struct TModel : plugin::Model {
  21. engine::Module* createModule() override {
  22. engine::Module* m = new TModule;
  23. m->model = this;
  24. return m;
  25. }
  26. app::ModuleWidget* createModuleWidget(engine::Module* m) override {
  27. TModule* tm = NULL;
  28. if (m) {
  29. assert(m->model == this);
  30. tm = dynamic_cast<TModule*>(m);
  31. }
  32. app::ModuleWidget* mw = new TModuleWidget(tm);
  33. mw->setModel(this);
  34. return mw;
  35. }
  36. };
  37. plugin::Model* o = new TModel;
  38. o->slug = slug;
  39. return o;
  40. }
  41. /** Creates a Widget subclass with its top-left at a position. */
  42. template <class TWidget>
  43. TWidget* createWidget(math::Vec pos) {
  44. TWidget* o = new TWidget;
  45. o->box.pos = pos;
  46. return o;
  47. }
  48. /** Creates a Widget subclass with its center at a position. */
  49. template <class TWidget>
  50. TWidget* createWidgetCentered(math::Vec pos) {
  51. TWidget* o = createWidget<TWidget>(pos);
  52. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  53. return o;
  54. }
  55. inline app::SvgPanel* createPanel(std::string svgPath) {
  56. app::SvgPanel* panel = new app::SvgPanel;
  57. std::shared_ptr<Svg> svg = Svg::load(svgPath);
  58. panel->setBackground(svg);
  59. return panel;
  60. }
  61. template <class TParamWidget>
  62. TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
  63. TParamWidget* o = new TParamWidget;
  64. o->box.pos = pos;
  65. o->app::ParamWidget::module = module;
  66. o->app::ParamWidget::paramId = paramId;
  67. o->initParamQuantity();
  68. return o;
  69. }
  70. template <class TParamWidget>
  71. TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) {
  72. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  73. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  74. return o;
  75. }
  76. template <class TPortWidget>
  77. TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
  78. TPortWidget* o = new TPortWidget;
  79. o->box.pos = pos;
  80. o->app::PortWidget::module = module;
  81. o->app::PortWidget::type = engine::Port::INPUT;
  82. o->app::PortWidget::portId = inputId;
  83. return o;
  84. }
  85. template <class TPortWidget>
  86. TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
  87. TPortWidget* o = createInput<TPortWidget>(pos, module, inputId);
  88. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  89. return o;
  90. }
  91. template <class TPortWidget>
  92. TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
  93. TPortWidget* o = new TPortWidget;
  94. o->box.pos = pos;
  95. o->app::PortWidget::module = module;
  96. o->app::PortWidget::type = engine::Port::OUTPUT;
  97. o->app::PortWidget::portId = outputId;
  98. return o;
  99. }
  100. template <class TPortWidget>
  101. TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
  102. TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId);
  103. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  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->app::ModuleLightWidget::module = module;
  111. o->app::ModuleLightWidget::firstLightId = firstLightId;
  112. return o;
  113. }
  114. template <class TModuleLightWidget>
  115. TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
  116. TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId);
  117. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  118. return o;
  119. }
  120. /** Creates a param with a light and calls setFirstLightId() on it.
  121. Requires ParamWidget to have a `light` member.
  122. */
  123. template <class TParamWidget>
  124. TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  125. TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
  126. o->light->module = module;
  127. o->light->firstLightId = firstLightId;
  128. return o;
  129. }
  130. template <class TParamWidget>
  131. TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
  132. TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId);
  133. o->box.pos = o->box.pos.minus(o->box.size.div(2));
  134. return o;
  135. }
  136. template <class TMenu = ui::Menu>
  137. TMenu* createMenu() {
  138. TMenu* o = new TMenu;
  139. o->box.pos = APP->scene->mousePos;
  140. ui::MenuOverlay* menuOverlay = new ui::MenuOverlay;
  141. menuOverlay->addChild(o);
  142. APP->scene->addChild(menuOverlay);
  143. return o;
  144. }
  145. template <class TMenuLabel = ui::MenuLabel>
  146. TMenuLabel* createMenuLabel(std::string text) {
  147. TMenuLabel* o = new TMenuLabel;
  148. o->text = text;
  149. return o;
  150. }
  151. template <class TMenuItem = ui::MenuItem>
  152. TMenuItem* createMenuItem(std::string text, std::string rightText = "") {
  153. TMenuItem* o = new TMenuItem;
  154. o->text = text;
  155. o->rightText = rightText;
  156. return o;
  157. }
  158. /** Creates a MenuItem with an action that calls a lambda function.
  159. Example:
  160. menu->addChild(createMenuItem("Load sample", "kick.wav",
  161. [=]() {
  162. module->loadSample();
  163. }
  164. ));
  165. */
  166. template <class TMenuItem = ui::MenuItem>
  167. TMenuItem* createMenuItem(std::string text, std::string rightText, std::function<void()> action, bool disabled = false, bool alwaysConsume = false) {
  168. struct Item : TMenuItem {
  169. std::function<void()> action;
  170. bool alwaysConsume;
  171. void onAction(const event::Action& e) override {
  172. action();
  173. if (alwaysConsume)
  174. e.consume(this);
  175. }
  176. };
  177. Item* item = createMenuItem<Item>(text, rightText);
  178. item->action = action;
  179. item->disabled = disabled;
  180. item->alwaysConsume = alwaysConsume;
  181. return item;
  182. }
  183. /** Creates a MenuItem with a check mark set by a lambda function.
  184. Example:
  185. menu->addChild(createCheckMenuItem("Loop",
  186. [=]() {
  187. return module->isLoop();
  188. },
  189. [=]() {
  190. module->toggleLoop();
  191. }
  192. ));
  193. */
  194. template <class TMenuItem = ui::MenuItem>
  195. ui::MenuItem* createCheckMenuItem(std::string text, std::function<bool()> checked, std::function<void()> action, bool disabled = false, bool alwaysConsume = false) {
  196. struct Item : TMenuItem {
  197. std::function<bool()> checked;
  198. std::function<void()> action;
  199. bool alwaysConsume;
  200. void step() override {
  201. this->rightText = CHECKMARK(checked());
  202. TMenuItem::step();
  203. }
  204. void onAction(const event::Action& e) override {
  205. action();
  206. if (alwaysConsume)
  207. e.consume(this);
  208. }
  209. };
  210. Item* item = createMenuItem<Item>(text);
  211. item->checked = checked;
  212. item->action = action;
  213. item->disabled = disabled;
  214. item->alwaysConsume = alwaysConsume;
  215. return item;
  216. }
  217. /** Creates a MenuItem that controls a boolean value with a check mark.
  218. Example:
  219. menu->addChild(createBoolMenuItem("Loop",
  220. [=]() {
  221. return module->isLoop();
  222. },
  223. [=](bool loop) {
  224. module->setLoop(loop);
  225. }
  226. ));
  227. */
  228. template <class TMenuItem = ui::MenuItem>
  229. ui::MenuItem* createBoolMenuItem(std::string text, std::function<bool()> getter, std::function<void(bool state)> setter, bool disabled = false, bool alwaysConsume = false) {
  230. struct Item : TMenuItem {
  231. std::function<bool()> getter;
  232. std::function<void(size_t)> setter;
  233. bool alwaysConsume;
  234. void step() override {
  235. this->rightText = CHECKMARK(getter());
  236. TMenuItem::step();
  237. }
  238. void onAction(const event::Action& e) override {
  239. setter(!getter());
  240. if (alwaysConsume)
  241. e.consume(this);
  242. }
  243. };
  244. Item* item = createMenuItem<Item>(text);
  245. item->getter = getter;
  246. item->setter = setter;
  247. item->disabled = disabled;
  248. item->alwaysConsume = alwaysConsume;
  249. return item;
  250. }
  251. /** Easy wrapper for createBoolMenuItem() to modify a bool pointer.
  252. Example:
  253. menu->addChild(createBoolPtrMenuItem("Loop", &module->loop));
  254. */
  255. template <typename T>
  256. ui::MenuItem* createBoolPtrMenuItem(std::string text, T* ptr) {
  257. return createBoolMenuItem(text,
  258. [=]() {return *ptr;},
  259. [=](T val) {*ptr = val;}
  260. );
  261. }
  262. /** Creates a MenuItem that opens a submenu.
  263. Example:
  264. menu->addChild(createSubmenuItem("Edit", "",
  265. [=](Menu* menu) {
  266. menu->addChild(createMenuItem("Copy", "", [=]() {copy();}));
  267. menu->addChild(createMenuItem("Paste", "", [=]() {paste();}));
  268. }
  269. ));
  270. */
  271. template <class TMenuItem = ui::MenuItem>
  272. ui::MenuItem* createSubmenuItem(std::string text, std::string rightText, std::function<void(ui::Menu* menu)> createMenu, bool disabled = false) {
  273. struct Item : TMenuItem {
  274. std::function<void(ui::Menu* menu)> createMenu;
  275. ui::Menu* createChildMenu() override {
  276. ui::Menu* menu = new ui::Menu;
  277. createMenu(menu);
  278. return menu;
  279. }
  280. };
  281. Item* item = createMenuItem<Item>(text, rightText + (rightText.empty() ? "" : " ") + RIGHT_ARROW);
  282. item->createMenu = createMenu;
  283. item->disabled = disabled;
  284. return item;
  285. }
  286. /** Creates a MenuItem that when hovered, opens a submenu with several MenuItems indexed by an integer.
  287. Example:
  288. menu->addChild(createIndexSubmenuItem("Mode",
  289. {"Hi-fi", "Mid-fi", "Lo-fi"},
  290. [=]() {
  291. return module->getMode();
  292. },
  293. [=](int mode) {
  294. module->setMode(mode);
  295. }
  296. ));
  297. */
  298. template <class TMenuItem = ui::MenuItem>
  299. ui::MenuItem* createIndexSubmenuItem(std::string text, std::vector<std::string> labels, std::function<size_t()> getter, std::function<void(size_t val)> setter, bool disabled = false, bool alwaysConsume = false) {
  300. struct IndexItem : ui::MenuItem {
  301. std::function<size_t()> getter;
  302. std::function<void(size_t)> setter;
  303. size_t index;
  304. bool alwaysConsume;
  305. void step() override {
  306. size_t currIndex = getter();
  307. this->rightText = CHECKMARK(currIndex == index);
  308. MenuItem::step();
  309. }
  310. void onAction(const event::Action& e) override {
  311. setter(index);
  312. if (alwaysConsume)
  313. e.consume(this);
  314. }
  315. };
  316. struct Item : TMenuItem {
  317. std::function<size_t()> getter;
  318. std::function<void(size_t)> setter;
  319. std::vector<std::string> labels;
  320. bool alwaysConsume;
  321. void step() override {
  322. size_t currIndex = getter();
  323. std::string label = (currIndex < labels.size()) ? labels[currIndex] : "";
  324. this->rightText = label + " " + RIGHT_ARROW;
  325. TMenuItem::step();
  326. }
  327. ui::Menu* createChildMenu() override {
  328. ui::Menu* menu = new ui::Menu;
  329. for (size_t i = 0; i < labels.size(); i++) {
  330. IndexItem* item = createMenuItem<IndexItem>(labels[i]);
  331. item->getter = getter;
  332. item->setter = setter;
  333. item->index = i;
  334. item->alwaysConsume = alwaysConsume;
  335. menu->addChild(item);
  336. }
  337. return menu;
  338. }
  339. };
  340. Item* item = createMenuItem<Item>(text);
  341. item->getter = getter;
  342. item->setter = setter;
  343. item->labels = labels;
  344. item->disabled = disabled;
  345. item->alwaysConsume = alwaysConsume;
  346. return item;
  347. }
  348. /** Easy wrapper for createIndexSubmenuItem() that controls an integer index at a pointer address.
  349. Example:
  350. menu->addChild(createIndexPtrSubmenuItem("Mode",
  351. {"Hi-fi", "Mid-fi", "Lo-fi"},
  352. &module->mode
  353. ));
  354. */
  355. template <typename T>
  356. ui::MenuItem* createIndexPtrSubmenuItem(std::string text, std::vector<std::string> labels, T* ptr) {
  357. return createIndexSubmenuItem(text, labels,
  358. [=]() {return *ptr;},
  359. [=](size_t index) {*ptr = T(index);}
  360. );
  361. }
  362. } // namespace rack