Browse Source

Add createIndexMenuItem() helper function.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
4c529a9541
1 changed files with 49 additions and 0 deletions
  1. +49
    -0
      include/helpers.hpp

+ 49
- 0
include/helpers.hpp View File

@@ -42,6 +42,7 @@ plugin::Model* createModel(const std::string& slug) {
return o; return o;
} }



template <class TWidget> template <class TWidget>
TWidget* createWidget(math::Vec pos) { TWidget* createWidget(math::Vec pos) {
TWidget* o = new TWidget; TWidget* o = new TWidget;
@@ -49,6 +50,7 @@ TWidget* createWidget(math::Vec pos) {
return o; return o;
} }



template <class TWidget> template <class TWidget>
TWidget* createWidgetCentered(math::Vec pos) { TWidget* createWidgetCentered(math::Vec pos) {
TWidget* o = createWidget<TWidget>(pos); TWidget* o = createWidget<TWidget>(pos);
@@ -56,6 +58,7 @@ TWidget* createWidgetCentered(math::Vec pos) {
return o; return o;
} }



inline app::SvgPanel* createPanel(std::string svgPath) { inline app::SvgPanel* createPanel(std::string svgPath) {
app::SvgPanel* panel = new app::SvgPanel; app::SvgPanel* panel = new app::SvgPanel;
std::shared_ptr<Svg> svg = Svg::load(svgPath); std::shared_ptr<Svg> svg = Svg::load(svgPath);
@@ -63,6 +66,7 @@ inline app::SvgPanel* createPanel(std::string svgPath) {
return panel; return panel;
} }



template <class TParamWidget> template <class TParamWidget>
TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) { TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
TParamWidget* o = new TParamWidget; TParamWidget* o = new TParamWidget;
@@ -73,6 +77,7 @@ TParamWidget* createParam(math::Vec pos, engine::Module* module, int paramId) {
return o; return o;
} }



template <class TParamWidget> template <class TParamWidget>
TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) { TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int paramId) {
TParamWidget* o = createParam<TParamWidget>(pos, module, paramId); TParamWidget* o = createParam<TParamWidget>(pos, module, paramId);
@@ -80,6 +85,7 @@ TParamWidget* createParamCentered(math::Vec pos, engine::Module* module, int par
return o; return o;
} }



template <class TPortWidget> template <class TPortWidget>
TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) { TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
TPortWidget* o = new TPortWidget; TPortWidget* o = new TPortWidget;
@@ -90,6 +96,7 @@ TPortWidget* createInput(math::Vec pos, engine::Module* module, int inputId) {
return o; return o;
} }



template <class TPortWidget> template <class TPortWidget>
TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) { TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inputId) {
TPortWidget* o = createInput<TPortWidget>(pos, module, inputId); TPortWidget* o = createInput<TPortWidget>(pos, module, inputId);
@@ -97,6 +104,7 @@ TPortWidget* createInputCentered(math::Vec pos, engine::Module* module, int inpu
return o; return o;
} }



template <class TPortWidget> template <class TPortWidget>
TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) { TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
TPortWidget* o = new TPortWidget; TPortWidget* o = new TPortWidget;
@@ -107,6 +115,7 @@ TPortWidget* createOutput(math::Vec pos, engine::Module* module, int outputId) {
return o; return o;
} }



template <class TPortWidget> template <class TPortWidget>
TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) { TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int outputId) {
TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId); TPortWidget* o = createOutput<TPortWidget>(pos, module, outputId);
@@ -114,6 +123,7 @@ TPortWidget* createOutputCentered(math::Vec pos, engine::Module* module, int out
return o; return o;
} }



template <class TModuleLightWidget> template <class TModuleLightWidget>
TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) { TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int firstLightId) {
TModuleLightWidget* o = new TModuleLightWidget; TModuleLightWidget* o = new TModuleLightWidget;
@@ -123,6 +133,7 @@ TModuleLightWidget* createLight(math::Vec pos, engine::Module* module, int first
return o; return o;
} }



template <class TModuleLightWidget> template <class TModuleLightWidget>
TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) { TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, int firstLightId) {
TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId); TModuleLightWidget* o = createLight<TModuleLightWidget>(pos, module, firstLightId);
@@ -130,6 +141,7 @@ TModuleLightWidget* createLightCentered(math::Vec pos, engine::Module* module, i
return o; return o;
} }



/** Creates a param with a light and calls setFirstLightId() on it. */ /** Creates a param with a light and calls setFirstLightId() on it. */
template <class TParamWidget> template <class TParamWidget>
TParamWidget* createLightParam(math::Vec pos, engine::Module* module, int paramId, int firstLightId) { 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; return o;
} }



template <class TParamWidget> template <class TParamWidget>
TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) { TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, int paramId, int firstLightId) {
TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId); TParamWidget* o = createLightParam<TParamWidget>(pos, module, paramId, firstLightId);
@@ -145,6 +158,7 @@ TParamWidget* createLightParamCentered(math::Vec pos, engine::Module* module, in
return o; return o;
} }



template <class TMenuLabel = ui::MenuLabel> template <class TMenuLabel = ui::MenuLabel>
TMenuLabel* createMenuLabel(std::string text) { TMenuLabel* createMenuLabel(std::string text) {
TMenuLabel* o = new TMenuLabel; TMenuLabel* o = new TMenuLabel;
@@ -152,6 +166,7 @@ TMenuLabel* createMenuLabel(std::string text) {
return o; return o;
} }



template <class TMenuItem = ui::MenuItem> template <class TMenuItem = ui::MenuItem>
TMenuItem* createMenuItem(std::string text, std::string rightText = "") { TMenuItem* createMenuItem(std::string text, std::string rightText = "") {
TMenuItem* o = new TMenuItem; TMenuItem* o = new TMenuItem;
@@ -160,6 +175,7 @@ TMenuItem* createMenuItem(std::string text, std::string rightText = "") {
return o; return o;
} }



template <class TMenu = ui::Menu> template <class TMenu = ui::Menu>
TMenu* createMenu() { TMenu* createMenu() {
TMenu* o = new TMenu; TMenu* o = new TMenu;
@@ -173,4 +189,37 @@ TMenu* createMenu() {
} }




template <typename T = int>
ui::MenuItem* createIndexMenuItem(T* ptr, std::string name, std::vector<std::string> 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<std::string> labels;
ui::Menu* createChildMenu() override {
ui::Menu* menu = new ui::Menu;
for (T i = 0; i < (T) labels.size(); i++) {
IndexItem* item = createMenuItem<IndexItem>(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<Item>(name, label + " " + RIGHT_ARROW);
item->ptr = ptr;
item->labels = labels;
return item;
}


} // namespace rack } // namespace rack

Loading…
Cancel
Save