入口
src/playground/index.jsx
export default appTarget => {
GUI.setAppElement(appTarget);
// note that redux's 'compose' function is just being used as a general utility to make
// the hierarchy of HOC constructor calls clearer here; it has nothing to do with redux's
// ability to compose reducers.
const WrappedGui = compose(
AppStateHOC,
HashParserHOC
)(GUI);
..........
AppStateHOC里面会初始化一个VM,guiInitialState
里
const guiRedux = require('../reducers/gui');
const guiReducer = guiRedux.default;
const {
guiInitialState,
guiMiddleware,
initFullScreen,
initPlayer,
initTelemetryModal
} = guiRedux;
同时,GUI里面也会将vm设置为Redux的vm
const mapStateToProps = state => {
const loadingState = state.scratchGui.projectState.loadingState;
return {
......
vm: state.scratchGui.vm
};
};
Scratch-VM主要作用:
- 侦听scratch-blocks发出的事件来构造和维护抽象语法树的状态。
- 加载解析项目:解析sb2和sb3项目文件,生成积木和角色等
- 导出项目:将当前项目资源打包压缩成 .sb3 文件或者生成json格式
- 运行项目并将结果渲染至舞台
- 扩展管理
能够导出Json格式的当前项目状态
vm.toJson()
返回对应的json配置,可以检查用户拖拽了哪些块到工作区域
{
"targets": [
{
"isStage": true,
"name": "Stage",
"variables": { "`jEk@4|i[#Fk?(8x)AV.-my variable": ["我的变量", 0] },
"lists": {},
"broadcasts": {},
"blocks": {},
"comments": {},
"currentCostume": 0,
"costumes": [
{
"assetId": "cd21514d0531fdffb22204e0ec5ed84a",
"name": "背景1",
"md5ext": "cd21514d0531fdffb22204e0ec5ed84a.svg",
"dataFormat": "svg",
"rotationCenterX": 240,
"rotationCenterY": 180
}
],
"sounds": [
{
"assetId": "83a9787d4cb6f3b7632b4ddfebf74367",
"name": "啵",
"dataFormat": "wav",
"format": "",
"rate": 44100,
"sampleCount": 1032,
"md5ext": "83a9787d4cb6f3b7632b4ddfebf74367.wav"
}
],
"volume": 100,
"layerOrder": 0,
"tempo": 60,
"videoTransparency": 50,
"videoState": "on",
"textToSpeechLanguage": null
},
{
"isStage": false,
"name": "角色1",
"variables": {},
"lists": {},
"broadcasts": {},
"blocks": {
"+fkz-|QFBT|$2[/WV9_c": {
"opcode": "event_whenflagclicked",
"next": "hE!6vvV:(Ez6#_d}QRZ:",
"parent": null,
"inputs": {},
"fields": {},
"shadow": false,
"topLevel": true,
"x": 65,
"y": 188
},
"hE!6vvV:(Ez6#_d}QRZ:": {
"opcode": "motion_turnleft",
"next": null,
"parent": "+fkz-|QFBT|$2[/WV9_c",
"inputs": { "DEGREES": [1, [4, "15"]] },
"fields": {},
"shadow": false,
"topLevel": false
}
},
"comments": {},
"currentCostume": 0,
"costumes": [
{
"assetId": "b7853f557e4426412e64bb3da6531a99",
"name": "造型1",
"bitmapResolution": 1,
"md5ext": "b7853f557e4426412e64bb3da6531a99.svg",
"dataFormat": "svg",
"rotationCenterX": 48,
"rotationCenterY": 50
},
{
"assetId": "e6ddc55a6ddd9cc9d84fe0b4c21e016f",
"name": "造型2",
"bitmapResolution": 1,
"md5ext": "e6ddc55a6ddd9cc9d84fe0b4c21e016f.svg",
"dataFormat": "svg",
"rotationCenterX": 46,
"rotationCenterY": 53
}
],
"sounds": [
{
"assetId": "83c36d806dc92327b9e7049a565c6bff",
"name": "喵",
"dataFormat": "wav",
"format": "",
"rate": 44100,
"sampleCount": 37376,
"md5ext": "83c36d806dc92327b9e7049a565c6bff.wav"
}
],
"volume": 100,
"layerOrder": 1,
"visible": true,
"x": 0,
"y": 0,
"size": 100,
"direction": 47.606532528840425,
"draggable": false,
"rotationStyle": "all around"
}
],
"monitors": [],
"extensions": [],
"meta": {
"semver": "3.0.0",
"vm": "0.2.0-prerelease.20200227204654",
"agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
}
}
如何得到VM对象
默认的vm实力通过export的方式(单例)导出,在源码src/reducers/vm.js
里,
其实在src/containers/gui.jsx
的GUI
的componentDidMount
就可以得到vm,
this.props.vm
,因为AppStateHOC会加载传给GUI
网友评论