From 4c529a9541ffe986dff42d58871ab981685a262e Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 2 Jun 2021 00:25:02 -0400 Subject: [PATCH] Add createIndexMenuItem() helper function. --- include/helpers.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/include/helpers.hpp b/include/helpers.hpp index 79e44774..2e014361 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -42,6 +42,7 @@ plugin::Model* createModel(const std::string& slug) { return o; } + template TWidget* createWidget(math::Vec pos) { TWidget* o = new TWidget; @@ -49,6 +50,7 @@ TWidget* createWidget(math::Vec pos) { return o; } + template TWidget* createWidgetCentered(math::Vec pos) { TWidget* o = createWidget(pos); @@ -56,6 +58,7 @@ TWidget* createWidgetCentered(math::Vec pos) { return o; } + inline app::SvgPanel* createPanel(std::string svgPath) { app::SvgPanel* panel = new app::SvgPanel; std::shared_ptr svg = Svg::load(svgPath); @@ -63,6 +66,7 @@ inline app::SvgPanel* createPanel(std::string svgPath) { return panel; } + template TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) { TParamWidget* o = new TParamWidget; @@ -73,6 +77,7 @@ TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) { return o; } + template TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) { TParamWidget* o = createParam(pos, module, paramId); @@ -80,6 +85,7 @@ TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int par return o; } + template TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) { TPortWidget* o = new TPortWidget; @@ -90,6 +96,7 @@ TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) { return o; } + template TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) { TPortWidget* o = createInput(pos, module, inputId); @@ -97,6 +104,7 @@ TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inpu return o; } + template TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) { TPortWidget* o = new TPortWidget; @@ -107,6 +115,7 @@ TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) { return o; } + template TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) { TPortWidget* o = createOutput(pos, module, outputId); @@ -114,6 +123,7 @@ TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int out return o; } + template TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) { TModuleLightWidget* o = new TModuleLightWidget; @@ -123,6 +133,7 @@ TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int first return o; } + template TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) { TModuleLightWidget* o = createLight(pos, module, firstLightId); @@ -130,6 +141,7 @@ TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, i return o; } + /** Creates a param with a light and calls setFirstLightId() on it. */ template TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) { @@ -138,6 +150,7 @@ TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramI return o; } + template TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) { TParamWidget* o = createLightParam(pos, module, paramId, firstLightId); @@ -145,6 +158,7 @@ TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, in return o; } + template TMenuLabel* createMenuLabel(std::string text) { TMenuLabel* o = new TMenuLabel; @@ -152,6 +166,7 @@ TMenuLabel* createMenuLabel(std::string text) { return o; } + template TMenuItem* createMenuItem(std::string text, std::string rightText = "") { TMenuItem* o = new TMenuItem; @@ -160,6 +175,7 @@ TMenuItem* createMenuItem(std::string text, std::string rightText = "") { return o; } + template TMenu* createMenu() { TMenu* o = new TMenu; @@ -173,4 +189,37 @@ TMenu* createMenu() { } +template +ui::MenuItem* createIndexMenuItem(T* ptr, std::string name, std::vector labels) { + struct IndexItem : ui::MenuItem { + T* ptr; + T index; + void onAction(const event::Action& e) override { + *ptr = index; + } + }; + struct Item : ui::MenuItem { + T* ptr; + std::vector labels; + ui::Menu* createChildMenu() override { + ui::Menu* menu = new ui::Menu; + for (T i = 0; i < (T) labels.size(); i++) { + IndexItem* item = createMenuItem(labels[i], CHECKMARK(*ptr == i)); + item->ptr = ptr; + item->index = i; + menu->addChild(item); + } + return menu; + } + }; + + T index = *ptr; + std::string label = (0 <= index && index < (T) labels.size()) ? labels[index] : ""; + Item* item = createMenuItem(name, label + " " + RIGHT_ARROW); + item->ptr = ptr; + item->labels = labels; + return item; +} + + } // namespace rack