max-mod-enum/enums.js
2025-07-01 09:18:02 +02:00

77 lines
3.1 KiB
JavaScript

/////////////////////////
// ENUMERATORS
/////////////////////////
// NOT A REACT FUNCTIONAL COMPONENT. MERELY RETURNS AN ARRAY WHICH IS UNPACKED
function EnumeratorItems(index, enumBreakPoints, setEnumBreakPoints, enumNames, setEnumNames, djParam, locked){
let items = [];
for (let i = 0; i < MAXENUMPOINTS; i++){
items.push(ListItem(e(TextBox, {locked, onChange: CreateMatrixParamChanger(enumNames, setEnumNames, index, i), value: enumNames[index][i], id:`text-${djParam}-${enumNames[index][i]}`}, null)));
// Add 1 to make up for the lower enum bound
items.push(ListItem(e(NumberBox, {locked, onChange: CreateMatrixParamChanger(enumBreakPoints, setEnumBreakPoints, index, i + 1), value:enumBreakPoints[index][i + 1]}, null)));
}
return items;
}
function DataCell(element) {
return e('td', null, element);
}
function EnumeratorRow(props){
let linkedText = props.linked ? "<- mods" : "";
let content = e('tr', {className: 'lfo-item', id: `${props.djParam}-enum-row`},
DataCell(DropDown({locked:props.locked, onChange: props.setInstanceNum, value:props.instanceNum, options: INSTANCEOPTIONS})),
DataCell(DropDown({locked:props.locked, onChange: props.setDjParam, value: props.djParam, options: MODPARAMOPTIONS})),
DataCell(e(NumberBox, {locked:props.locked, onChange: props.setEnumItemCounts, step:1, value:props.enumItems, className: 'enum-count'}, null)),
DataCell(e(NumberBox, {locked:props.locked, onChange: CreateMatrixParamChanger(props.enumBreakPoints, props.setEnumBreakPoints, props.index, 0), value:props.enumBreakPoints[props.index][0], step:0.1}, null)),
...(EnumeratorItems(props.index, props.enumBreakPoints, props.setEnumBreakPoints, props.enumNames, props.setEnumNames, props.djParam, props.locked).slice(0, props.enumItems * 2)),
DataCell(e(Button, {locked:props.locked, text:'+', onClick: props.addEnum}, null)),
DataCell(e(Button, {locked:props.locked, text:'-', onClick: props.removeEnum}, null)),
DataCell(e("div", {className:"linked"}, linkedText))
);
if (props.visible){
return content;
};
}
function denumerate(inval, count, keys, vals){
let output = inval;
for (let i=0; i < count; i++){
log(vals[i]);
if (inval == vals[i]){
output = (parseFloat(keys[i]) + parseFloat(keys[i+1])) / 2; // linear interpolate
}
}
return output;
}
function enumerate(name, inst, inval, count, keys, vals){
let output = "OUT OF RANGE";
for (let i=0; i < count + 1; i++){
if (inval <= keys[i]){
if (i > 0)
output = vals[i - 1];
break
}
}
let highlightedItem = document.getElementById(`text-${name}-${output}`);
if (highlightedItem){
highlightedItem.style.animation = "pulse-animation 0.5s normal";
highlightedItem.addEventListener('animationend', () => {
highlightedItem.style.animation = "";
});
}
if (name !== "NONE")
window.max.outlet(inst + " " + name + " " + output);
}