Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use bitmapAdapter for costume upload #2575

Merged
merged 8 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@
"scratch-paint": "0.2.0-prerelease.20180712144339",
"scratch-render": "0.1.0-prerelease.20180618173030",
"scratch-storage": "0.5.1",
"scratch-svg-renderer": "0.2.0-prerelease.20180618172917",
"scratch-vm": "0.1.0-prerelease.1531340421",
"scratch-svg-renderer": "0.2.0-prerelease.20180712223402",
"scratch-vm": "0.1.0-prerelease.1531436232",
"selenium-webdriver": "3.6.0",
"startaudiocontext": "1.2.1",
"style-loader": "^0.21.0",
Expand Down
8 changes: 6 additions & 2 deletions src/containers/costume-library.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class CostumeLibrary extends React.PureComponent {
]);
}
handleItemSelected (item) {
const split = item.md5.split('.');
const type = split.length > 1 ? split[1] : null;
const rotationCenterX = type === 'svg' ? item.info[0] : item.info[0] / 2;
const rotationCenterY = type === 'svg' ? item.info[1] : item.info[1] / 2;
const vmCostume = {
name: item.name,
rotationCenterX: item.info[0],
rotationCenterY: item.info[1],
rotationCenterX,
rotationCenterY,
bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
skinId: null
};
Expand Down
8 changes: 6 additions & 2 deletions src/containers/costume-tab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,15 @@ class CostumeTab extends React.Component {
}
handleSurpriseCostume () {
const item = costumeLibraryContent[Math.floor(Math.random() * costumeLibraryContent.length)];
const split = item.md5.split('.');
const type = split.length > 1 ? split[1] : null;
const rotationCenterX = type === 'svg' ? item.info[0] : item.info[0] / 2;
const rotationCenterY = type === 'svg' ? item.info[1] : item.info[1] / 2;
const vmCostume = {
name: item.name,
md5: item.md5,
rotationCenterX: item.info[0],
rotationCenterY: item.info[1],
rotationCenterX,
rotationCenterY,
bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
skinId: null
};
Expand Down
2 changes: 2 additions & 0 deletions src/containers/stage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {STAGE_DISPLAY_SIZES} from '../lib/layout-constants';
import {getEventXY} from '../lib/touch-utils';
import VideoProvider from '../lib/video/video-provider';
import {SVGRenderer as V2SVGAdapter} from 'scratch-svg-renderer';
import {BitmapAdapter as V2BitmapAdapter} from 'scratch-svg-renderer';

import StageComponent from '../components/stage/stage.jsx';

Expand Down Expand Up @@ -60,6 +61,7 @@ class Stage extends React.Component {
this.props.vm.attachRenderer(this.renderer);
}
this.props.vm.attachV2SVGAdapter(new V2SVGAdapter());
this.props.vm.attachV2BitmapAdapter(new V2BitmapAdapter());
this.props.vm.setVideoProvider(new VideoProvider());
}
componentDidMount () {
Expand Down
27 changes: 16 additions & 11 deletions src/lib/file-uploader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {importBitmap} from 'scratch-svg-renderer';
import {BitmapAdapter} from 'scratch-svg-renderer';
import log from './log.js';

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ const cacheAsset = function (storage, fileName, assetType, dataFormat, data) {

/**
* Handles loading a costume or a backdrop using the provided, context-relevant information.
* @param {ArrayBuffer | string} fileData The costume data to load (this can be an image url
* @param {ArrayBuffer | string} fileData The costume data to load (this can be a base64 string
* iff the image is a bitmap)
* @param {string} fileType The MIME type of this file
* @param {string} costumeName The user-readable name to use for the costume.
Expand Down Expand Up @@ -112,13 +112,15 @@ const costumeUpload = function (fileData, fileType, costumeName, storage, handle
return;
}

const addCostumeFromBuffer = function (error, costumeBuffer) {
if (error) {
log.warn(`An error occurred while trying to extract image data: ${error}`);
return;
}

const vmCostume = cacheAsset(storage, costumeName, assetType, costumeFormat, costumeBuffer);
const bitmapAdapter = new BitmapAdapter();
const addCostumeFromBuffer = function (dataBuffer) {
const vmCostume = cacheAsset(
storage,
costumeName,
assetType,
costumeFormat,
dataBuffer
);
handleCostume(vmCostume);
};

Expand All @@ -127,10 +129,13 @@ const costumeUpload = function (fileData, fileType, costumeName, storage, handle
// passing in an array buffer causes the sprite/costume
// thumbnails to not display because the data URI for the costume
// is invalid
addCostumeFromBuffer(null, new Uint8Array(fileData));
addCostumeFromBuffer(new Uint8Array(fileData));
} else {
// otherwise it's a bitmap
importBitmap(fileData, addCostumeFromBuffer);
bitmapAdapter.importBitmap(fileData, fileType).then(addCostumeFromBuffer)
.catch(e => {
log.error(e);
});
}
};

Expand Down