Build menus using drum and instrument info arrays

This commit is contained in:
Eric Rosenbaum 2017-11-14 14:32:14 -05:00
parent 9f1bcd2594
commit 91878adfe1

View file

@ -25,27 +25,24 @@ class Scratch3MusicBlocks {
*/ */
this.tempo = 60; this.tempo = 60;
this.drumMenu = this._buildMenu(drumNames);
this.instrumentMenu = this._buildMenu(instrumentNames);
} }
/** /**
* Build a menu using an array of strings. * Create data for a menu in scratch-blocks format, consisting of an array of objects with text and
* Used for creating the drum and instrument menus. * value properties. The text is a translated string, and the value is one-indexed.
* @param {string[]} names - An array of names. * @param {object[]} info - An array of info objects each having a name property.
* @return {array} - An array of objects with text and value properties, for constructing a block menu. * @return {array} - An array of objects with text and value properties.
* @private * @private
*/ */
_buildMenu (names) { _buildMenu (info) {
const menu = []; return info.map((entry, index) => {
for (let i = 0; i < names.length; i++) { const obj = {};
const entry = {}; obj.text = entry.name;
const num = i + 1; // Menu numbers are one-indexed obj.value = index + 1;
entry.text = `(${num}) ${names[i]}`; return obj;
entry.value = String(num); });
menu.push(entry);
} }
return menu;
/** /**
* An array of translatable drum names and corresponding audio file names. * An array of translatable drum names and corresponding audio file names.
* @type {array} * @type {array}
@ -367,8 +364,8 @@ class Scratch3MusicBlocks {
} }
], ],
menus: { menus: {
drums: this.drumMenu, drums: this._buildMenu(this.DRUM_INFO),
instruments: this.instrumentMenu instruments: this._buildMenu(this.INSTRUMENT_INFO)
} }
}; };
} }