CtrlrCombo Custom Value Workaround - unityconstruct/CtrlrDocs GitHub Wiki
CtrlrCombo Integration Workaround
Provides custom value assignments in a CtrlrCombo object
Summary
- uiComboList displays a list of options to choose from but will only return a 0-based index of the option selected.
- The FIRST item in the list will return 0, the SECOND = 1, etc
- This implementation leverages a
- 'token parser' to extract value from selected item's text
- Enum/HashTable to translate value to its index in the ComboList
- Example: Clicking the LAST option will return [3]
#0 = | [0] - Off
#1 = | [1] - FilterSweep
#2 = | 3 = Formant
#3 = | [9] - Notched
- no user-assignable mapping is possible.
Getting value from a selection
-- Fetch selected text
local text = L(panel:getModulator("layer_filt_type_L1_15371"):getComponent():getComponentText())
-- Fetch data from text ( [###] ) using helper function
local value = GetStringBetween(text,"[","]")
-------------------------------------------------------------------------
---Get string between two specified chars/strings
---@param text string
---@param startChar string
---@param endChar string
---@return number|nil
function GetStringBetween(text,startChar,endChar)
startChar = startChar or "["
endChar = endChar or "["
if ( text == nil) then return nil end
local first = string.find(text,startChar,1,true)
local last = string.find(text,endChar,1,true)
if ( first ~= nil) and ( last ~= nil) then
local value = string.sub(text,first+1,last-1)
if ( value ~= nil) then
return tonumber(value)
end
end
return nil
end
Set modulator by data
- GIVEN: device's parameter id = 137
---lookup table/enum
---@enum LayerFilterType
LayerFilterType = {
[127] = 0,
[0] = 1,
[1] = 2,
[2] = 3,
[132] = 4,
[133] = 5,
[134] = 6,
[136] = 7,
[137] = 8,
[8] = 9,
[9] = 10,
[16] = 11,
[17] = 12,
[18] = 13,
[32] = 14,
[33] = 15,
[34] = 16,
[146] = 17,
}
--- translate a value to a ComboList index
-- given value of 137
local value = 137
-- perform lookup and reassign `value`
value = LayerFilterType[value]
-- assign value to modulator
mod:setModulatorValue(tonumber(value),false,false,false)
OnChangeHandler function
--@param mod CtrlrModulator
---@param value number: modulator value
---@param source number: where did change get called from
function OnChangeHandler(mod, value, source)
local modName = L(mod:getName())
-- parse modname to detect call from specific modulator so ONE function can handle many mods
if (found ~= string.find(modName, "layer_filt_type_L1_15371", 1, true)) then -- true = plain text search
-- Fetch selected text
local text = L(panel:getModulator("layer_filt_type_L1_15371"):getComponent():getComponentText())
-- Fetch data from text ( [###] ) using helper function
local value = GetStringBetween(text,"[","]")
end
end
Permutations of Calls
L(panel:getModulator("layer_filt_type_L1_15371"):getComponent():getComponentText())
L(layer_filt_type_L1_15371:getComponent():getComponentText())
mod = panel:getModulator("layer_filt_type_L1_15371")
L(mod:getComponent():getComponentText())
comp = panel:getModulator("layer_filt_type_L1_15371"):getComponent()
comp = panel:getComponent("layer_filt_type_L1_15371")
L(comp:getComponentText())