From 87935d7e6f0308ef1a2f516166a0c9f502e48d32 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 4 Feb 2020 22:12:35 -0500 Subject: [PATCH] Add SwitchQuantity. Add return value to config*() methods in Module. --- include/Quantity.hpp | 1 + include/engine/Module.hpp | 15 +++++++++------ include/engine/ParamQuantity.hpp | 13 +++++++++++++ include/string.hpp | 4 +--- src/engine/ParamQuantity.cpp | 21 +++++++++++++++++++++ src/string.cpp | 6 ++++++ 6 files changed, 51 insertions(+), 9 deletions(-) diff --git a/include/Quantity.hpp b/include/Quantity.hpp index 62fc7aa7..3f3f07ad 100644 --- a/include/Quantity.hpp +++ b/include/Quantity.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index 8c412e6c..c17fd5db 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -98,12 +98,12 @@ struct Module { void config(int numParams, int numInputs, int numOutputs, int numLights = 0); template - void configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) { + TParamQuantity* configParam(int paramId, float minValue, float maxValue, float defaultValue, std::string name = "", std::string unit = "", float displayBase = 0.f, float displayMultiplier = 1.f, float displayOffset = 0.f) { assert(paramId < (int) params.size() && paramId < (int) paramQuantities.size()); if (paramQuantities[paramId]) delete paramQuantities[paramId]; - ParamQuantity* q = new TParamQuantity; + TParamQuantity* q = new TParamQuantity; q->module = this; q->paramId = paramId; q->minValue = minValue; @@ -118,34 +118,37 @@ struct Module { Param* p = ¶ms[paramId]; p->value = q->getDefaultValue(); + return q; } template - void configInput(int portId, std::string name = "") { + TPortInfo* configInput(int portId, std::string name = "") { assert(portId < (int) inputs.size() && portId < (int) inputInfos.size()); if (inputInfos[portId]) delete inputInfos[portId]; - PortInfo* p = new TPortInfo; + TPortInfo* p = new TPortInfo; p->module = this; p->type = Port::INPUT; p->portId = portId; p->name = name; inputInfos[portId] = p; + return p; } template - void configOutput(int portId, std::string name = "") { + TPortInfo* configOutput(int portId, std::string name = "") { assert(portId < (int) outputs.size() && portId < (int) outputInfos.size()); if (outputInfos[portId]) delete outputInfos[portId]; - PortInfo* p = new TPortInfo; + TPortInfo* p = new TPortInfo; p->module = this; p->type = Port::OUTPUT; p->portId = portId; p->name = name; outputInfos[portId] = p; + return p; } /** Adds a direct route from an input to an output when the module is bypassed. */ diff --git a/include/engine/ParamQuantity.hpp b/include/engine/ParamQuantity.hpp index 041256cc..440ba13f 100644 --- a/include/engine/ParamQuantity.hpp +++ b/include/engine/ParamQuantity.hpp @@ -1,4 +1,6 @@ #pragma once +#include + #include #include @@ -70,5 +72,16 @@ struct ParamQuantity : Quantity { }; +struct SwitchQuantity : ParamQuantity { + std::vector labels; + + void setLabels(const std::vector& labels) { + this->labels = labels; + } + std::string getDisplayValueString() override; + void setDisplayValueString(std::string s) override; +}; + + } // namespace engine } // namespace rack diff --git a/include/string.hpp b/include/string.hpp index 32cb5059..612228db 100644 --- a/include/string.hpp +++ b/include/string.hpp @@ -85,9 +85,7 @@ std::vector uncompress(const std::vector& compressed); struct CaseInsensitiveCompare { - bool operator()(const std::string& a, const std::string& b) const { - return lowercase(a) < lowercase(b); - } + bool operator()(const std::string& a, const std::string& b) const; }; diff --git a/src/engine/ParamQuantity.cpp b/src/engine/ParamQuantity.cpp index 19d4ad46..162355cf 100644 --- a/src/engine/ParamQuantity.cpp +++ b/src/engine/ParamQuantity.cpp @@ -1,6 +1,9 @@ +#include + #include #include #include +#include namespace rack { @@ -124,5 +127,23 @@ std::string ParamQuantity::getDescription() { } +std::string SwitchQuantity::getDisplayValueString() { + int index = std::floor(getDisplayValue()); + if (!(0 <= index && index < (int) labels.size())) + return ""; + return labels[index]; +} + +void SwitchQuantity::setDisplayValueString(std::string s) { + auto it = std::find_if(labels.begin(), labels.end(), [&](const std::string& a) { + return string::lowercase(a) == string::lowercase(s); + }); + if (it == labels.end()) + return; + int index = std::distance(labels.begin(), it); + setDisplayValue(index); +} + + } // namespace engine } // namespace rack diff --git a/src/string.cpp b/src/string.cpp index d5b3e124..7515c3e7 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -293,5 +293,11 @@ std::vector uncompress(const std::vector& compressed) { } +bool CaseInsensitiveCompare::operator()(const std::string& a, const std::string& b) const { + // TODO Make more efficient by iterating characters + return lowercase(a) < lowercase(b); +} + + } // namespace string } // namespace rack