mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-10 15:02:06 -05:00
Refactor method exports to fix linting and CWF comment
This commit is contained in:
parent
b68b874067
commit
81370f7625
6 changed files with 88 additions and 65 deletions
|
@ -1,6 +1,59 @@
|
|||
const StringUtil = require('../util/string-util');
|
||||
const log = require('../util/log');
|
||||
|
||||
/**
|
||||
* Initialize a costume from an asset asynchronously.
|
||||
* Do not call this unless there is a renderer attached.
|
||||
* @param {!object} costume - the Scratch costume object.
|
||||
* @property {int} skinId - the ID of the costume's render skin, once installed.
|
||||
* @property {number} rotationCenterX - the X component of the costume's origin.
|
||||
* @property {number} rotationCenterY - the Y component of the costume's origin.
|
||||
* @property {number} [bitmapResolution] - the resolution scale for a bitmap costume.
|
||||
* @param {!Asset} costumeAsset - the asset of the costume loaded from storage.
|
||||
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
|
||||
* @returns {?Promise} - a promise which will resolve after skinId is set, or null on error.
|
||||
*/
|
||||
const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
|
||||
costume.assetId = costumeAsset.assetId;
|
||||
if (!runtime.renderer) {
|
||||
log.error('No rendering module present; cannot load costume: ', costume.name);
|
||||
return costume;
|
||||
}
|
||||
const AssetType = runtime.storage.AssetType;
|
||||
const rotationCenter = [
|
||||
costume.rotationCenterX / costume.bitmapResolution,
|
||||
costume.rotationCenterY / costume.bitmapResolution
|
||||
];
|
||||
if (costumeAsset.assetType === AssetType.ImageVector) {
|
||||
costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
|
||||
return costume;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const imageElement = new Image();
|
||||
const onError = function () {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
removeEventListeners();
|
||||
reject();
|
||||
};
|
||||
const onLoad = function () {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
removeEventListeners();
|
||||
resolve(imageElement);
|
||||
};
|
||||
const removeEventListeners = function () {
|
||||
imageElement.removeEventListener('error', onError);
|
||||
imageElement.removeEventListener('load', onLoad);
|
||||
};
|
||||
imageElement.addEventListener('error', onError);
|
||||
imageElement.addEventListener('load', onLoad);
|
||||
imageElement.src = costumeAsset.encodeDataURI();
|
||||
}).then(imageElement => {
|
||||
costume.skinId = runtime.renderer.createBitmapSkin(imageElement, costume.bitmapResolution, rotationCenter);
|
||||
return costume;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Load a costume's asset into memory asynchronously.
|
||||
* Do not call this unless there is a renderer attached.
|
||||
|
@ -31,48 +84,7 @@ const loadCostume = function (md5ext, costume, runtime) {
|
|||
});
|
||||
};
|
||||
|
||||
const loadCostumeFromAsset = function (costume, costumeAsset, runtime) {
|
||||
const rotationCenter = [
|
||||
costume.rotationCenterX / costume.bitmapResolution,
|
||||
costume.rotationCenterY / costume.bitmapResolution
|
||||
];
|
||||
|
||||
if (!runtime.renderer) {
|
||||
log.error('No rendering module present; cannot load costume asset: ', md5ext);
|
||||
return costume;
|
||||
}
|
||||
const AssetType = runtime.storage.AssetType;
|
||||
costume.assetId = costumeAsset.assetId;
|
||||
if (costumeAsset.assetType === AssetType.ImageVector) {
|
||||
costume.skinId = runtime.renderer.createSVGSkin(costumeAsset.decodeText(), rotationCenter);
|
||||
return costume;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const imageElement = new Image();
|
||||
const onError = function () {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
removeEventListeners();
|
||||
reject();
|
||||
module.exports = {
|
||||
loadCostume,
|
||||
loadCostumeFromAsset
|
||||
};
|
||||
const onLoad = function () {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
removeEventListeners();
|
||||
resolve(imageElement);
|
||||
};
|
||||
const removeEventListeners = function () {
|
||||
imageElement.removeEventListener('error', onError);
|
||||
imageElement.removeEventListener('load', onLoad);
|
||||
};
|
||||
imageElement.addEventListener('error', onError);
|
||||
imageElement.addEventListener('load', onLoad);
|
||||
imageElement.src = costumeAsset.encodeDataURI();
|
||||
}).then(imageElement => {
|
||||
costume.skinId = runtime.renderer.createBitmapSkin(imageElement, costume.bitmapResolution, rotationCenter);
|
||||
return costume;
|
||||
});
|
||||
};
|
||||
|
||||
loadCostume.loadCostumeFromAsset = loadCostumeFromAsset;
|
||||
|
||||
module.exports = loadCostume;
|
||||
|
|
|
@ -1,6 +1,27 @@
|
|||
const StringUtil = require('../util/string-util');
|
||||
const log = require('../util/log');
|
||||
|
||||
/**
|
||||
* Initialize a sound from an asset asynchronously.
|
||||
* @param {!object} sound - the Scratch sound object.
|
||||
* @property {string} md5 - the MD5 and extension of the sound to be loaded.
|
||||
* @property {Buffer} data - sound data will be written here once loaded.
|
||||
* @param {!Asset} soundAsset - the asset loaded from storage.
|
||||
* @param {!Runtime} runtime - Scratch runtime, used to access the storage module.
|
||||
* @returns {!Promise} - a promise which will resolve to the sound when ready.
|
||||
*/
|
||||
const loadSoundFromAsset = function (sound, soundAsset, runtime) {
|
||||
sound.assetId = soundAsset.assetId;
|
||||
return runtime.audioEngine.decodeSound(Object.assign(
|
||||
{},
|
||||
sound,
|
||||
{data: soundAsset.data}
|
||||
)).then(soundId => {
|
||||
sound.soundId = soundId;
|
||||
return sound;
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Load a sound's asset into memory asynchronously.
|
||||
* @param {!object} sound - the Scratch sound object.
|
||||
|
@ -28,17 +49,7 @@ const loadSound = function (sound, runtime) {
|
|||
});
|
||||
};
|
||||
|
||||
const loadSoundFromAsset = function (sound, soundAsset, runtime) {
|
||||
sound.assetId = soundAsset.assetId;
|
||||
return runtime.audioEngine.decodeSound(Object.assign(
|
||||
{},
|
||||
sound,
|
||||
{data: soundAsset.data}
|
||||
)).then(soundId => {
|
||||
sound.soundId = soundId;
|
||||
return sound;
|
||||
});
|
||||
module.exports = {
|
||||
loadSound,
|
||||
loadSoundFromAsset
|
||||
};
|
||||
|
||||
loadSound.loadSoundFromAsset = loadSoundFromAsset;
|
||||
module.exports = loadSound;
|
||||
|
|
|
@ -15,8 +15,8 @@ const specMap = require('./sb2_specmap');
|
|||
const Variable = require('../engine/variable');
|
||||
const List = require('../engine/list');
|
||||
|
||||
const loadCostume = require('../import/load-costume.js');
|
||||
const loadSound = require('../import/load-sound.js');
|
||||
const {loadCostume} = require('../import/load-costume.js');
|
||||
const {loadSound} = require('../import/load-sound.js');
|
||||
|
||||
/**
|
||||
* Convert a Scratch 2.0 procedure string (e.g., "my_procedure %s %b %n")
|
||||
|
|
|
@ -10,8 +10,8 @@ const Sprite = require('../sprites/sprite');
|
|||
const Variable = require('../engine/variable');
|
||||
const List = require('../engine/list');
|
||||
|
||||
const loadCostume = require('../import/load-costume.js');
|
||||
const loadSound = require('../import/load-sound.js');
|
||||
const {loadCostume} = require('../import/load-costume.js');
|
||||
const {loadSound} = require('../import/load-sound.js');
|
||||
|
||||
/**
|
||||
* Serializes the specified VM runtime.
|
||||
|
|
|
@ -6,8 +6,8 @@ const sb2 = require('./serialization/sb2');
|
|||
const sb3 = require('./serialization/sb3');
|
||||
const StringUtil = require('./util/string-util');
|
||||
|
||||
const loadCostume = require('./import/load-costume.js');
|
||||
const loadSound = require('./import/load-sound.js');
|
||||
const {loadCostume} = require('./import/load-costume.js');
|
||||
const {loadSound} = require('./import/load-sound.js');
|
||||
|
||||
const RESERVED_NAMES = ['_mouse_', '_stage_', '_edge_', '_myself_', '_random_'];
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@ const test = require('tap').test;
|
|||
|
||||
const Blocks = require('../../src/engine/blocks');
|
||||
const Clone = require('../../src/util/clone');
|
||||
const loadCostume = require('../../src/import/load-costume');
|
||||
const loadSound = require('../../src/import/load-sound');
|
||||
const {loadCostume} = require('../../src/import/load-costume');
|
||||
const {loadSound} = require('../../src/import/load-sound');
|
||||
const makeTestStorage = require('../fixtures/make-test-storage');
|
||||
const Runtime = require('../../src/engine/runtime');
|
||||
const sb3 = require('../../src/serialization/sb3');
|
||||
|
|
Loading…
Reference in a new issue