Browse Source

Add SwitchQuantity. Add return value to config*() methods in Module.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
87935d7e6f
6 changed files with 51 additions and 9 deletions
  1. +1
    -0
      include/Quantity.hpp
  2. +9
    -6
      include/engine/Module.hpp
  3. +13
    -0
      include/engine/ParamQuantity.hpp
  4. +1
    -3
      include/string.hpp
  5. +21
    -0
      src/engine/ParamQuantity.cpp
  6. +6
    -0
      src/string.cpp

+ 1
- 0
include/Quantity.hpp View File

@@ -1,4 +1,5 @@
#pragma once
#include <common.hpp>
#include <math.hpp>
#include <random.hpp>



+ 9
- 6
include/engine/Module.hpp View File

@@ -98,12 +98,12 @@ struct Module {
void config(int numParams, int numInputs, int numOutputs, int numLights = 0);

template <class TParamQuantity = ParamQuantity>
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 = &params[paramId];
p->value = q->getDefaultValue();
return q;
}

template <class TPortInfo = PortInfo>
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 <class TPortInfo = PortInfo>
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. */


+ 13
- 0
include/engine/ParamQuantity.hpp View File

@@ -1,4 +1,6 @@
#pragma once
#include <vector>

#include <Quantity.hpp>
#include <engine/Param.hpp>

@@ -70,5 +72,16 @@ struct ParamQuantity : Quantity {
};


struct SwitchQuantity : ParamQuantity {
std::vector<std::string> labels;

void setLabels(const std::vector<std::string>& labels) {
this->labels = labels;
}
std::string getDisplayValueString() override;
void setDisplayValueString(std::string s) override;
};


} // namespace engine
} // namespace rack

+ 1
- 3
include/string.hpp View File

@@ -85,9 +85,7 @@ std::vector<uint8_t> uncompress(const std::vector<uint8_t>& 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;
};




+ 21
- 0
src/engine/ParamQuantity.cpp View File

@@ -1,6 +1,9 @@
#include <algorithm>

#include <engine/ParamQuantity.hpp>
#include <context.hpp>
#include <engine/Engine.hpp>
#include <string.hpp>


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

+ 6
- 0
src/string.cpp View File

@@ -293,5 +293,11 @@ std::vector<uint8_t> uncompress(const std::vector<uint8_t>& 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

Loading…
Cancel
Save