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.

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