2016-06-01 10:04:56 -04:00
|
|
|
window.onload = function() {
|
|
|
|
// Lots of global variables to make debugging easier
|
|
|
|
var vm = new window.VirtualMachine();
|
|
|
|
window.vm = vm;
|
|
|
|
|
|
|
|
var toolbox = document.getElementById('toolbox');
|
|
|
|
var workspace = window.Blockly.inject('blocks', {
|
|
|
|
toolbox: toolbox,
|
2016-06-01 14:00:46 -04:00
|
|
|
media: '../node_modules/scratch-blocks/media/',
|
2016-06-07 20:26:58 -04:00
|
|
|
zoom: {
|
|
|
|
controls: true,
|
|
|
|
wheel: true,
|
|
|
|
startScale: 0.75
|
|
|
|
},
|
2016-06-01 14:00:46 -04:00
|
|
|
colours: {
|
|
|
|
workspace: '#334771',
|
|
|
|
flyout: '#283856',
|
|
|
|
scrollbar: '#24324D',
|
|
|
|
scrollbarHover: '#0C111A',
|
|
|
|
insertionMarker: '#FFFFFF',
|
|
|
|
insertionMarkerOpacity: 0.3,
|
|
|
|
fieldShadow: 'rgba(255, 255, 255, 0.3)',
|
|
|
|
dragShadowOpacity: 0.6
|
|
|
|
}
|
2016-06-01 10:04:56 -04:00
|
|
|
});
|
|
|
|
window.workspace = workspace;
|
|
|
|
|
|
|
|
// Block events.
|
2016-06-09 11:49:03 -04:00
|
|
|
// @todo: Re-enable flyout listening after fixing GH-69.
|
2016-06-21 15:30:36 -04:00
|
|
|
workspace.addChangeListener(vm.blockListener);
|
2016-06-01 10:04:56 -04:00
|
|
|
|
2016-06-21 15:30:36 -04:00
|
|
|
// Playground data
|
2016-06-07 20:44:08 -04:00
|
|
|
var blockexplorer = document.getElementById('blockexplorer');
|
2016-06-21 15:30:36 -04:00
|
|
|
var updateBlockExplorer = function(blocks) {
|
|
|
|
blockexplorer.innerHTML = JSON.stringify(blocks, null, 2);
|
2016-06-07 20:44:08 -04:00
|
|
|
window.hljs.highlightBlock(blockexplorer);
|
2016-06-21 15:30:36 -04:00
|
|
|
};
|
2016-06-01 10:18:08 -04:00
|
|
|
|
2016-06-07 20:44:08 -04:00
|
|
|
var threadexplorer = document.getElementById('threadexplorer');
|
|
|
|
var cachedThreadJSON = '';
|
2016-06-21 15:30:36 -04:00
|
|
|
var updateThreadExplorer = function (threads) {
|
|
|
|
var newJSON = JSON.stringify(threads, null, 2);
|
2016-06-07 20:44:08 -04:00
|
|
|
if (newJSON != cachedThreadJSON) {
|
|
|
|
cachedThreadJSON = newJSON;
|
|
|
|
threadexplorer.innerHTML = cachedThreadJSON;
|
|
|
|
window.hljs.highlightBlock(threadexplorer);
|
|
|
|
}
|
|
|
|
};
|
2016-06-21 15:30:36 -04:00
|
|
|
|
|
|
|
var getPlaygroundData = function () {
|
|
|
|
vm.getPlaygroundData();
|
|
|
|
window.requestAnimationFrame(getPlaygroundData);
|
|
|
|
};
|
|
|
|
getPlaygroundData();
|
|
|
|
|
|
|
|
vm.on('playgroundData', function(data) {
|
|
|
|
updateThreadExplorer(data.threads);
|
|
|
|
updateBlockExplorer(data.blocks);
|
|
|
|
});
|
2016-06-07 20:44:08 -04:00
|
|
|
|
2016-06-07 20:59:34 -04:00
|
|
|
// Feedback for stacks and blocks running.
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.on('STACK_GLOW_ON', function(data) {
|
|
|
|
workspace.glowStack(data.id, true);
|
2016-06-01 13:54:33 -04:00
|
|
|
});
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.on('STACK_GLOW_OFF', function(data) {
|
|
|
|
workspace.glowStack(data.id, false);
|
2016-06-01 13:54:33 -04:00
|
|
|
});
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.on('BLOCK_GLOW_ON', function(data) {
|
|
|
|
workspace.glowBlock(data.id, true);
|
2016-06-07 20:59:34 -04:00
|
|
|
});
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.on('BLOCK_GLOW_OFF', function(data) {
|
|
|
|
workspace.glowBlock(data.id, false);
|
2016-06-07 20:59:34 -04:00
|
|
|
});
|
|
|
|
|
2016-06-01 10:04:56 -04:00
|
|
|
// Run threads
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.start();
|
2016-06-01 14:17:18 -04:00
|
|
|
|
|
|
|
// Handlers for green flag and stop all.
|
|
|
|
document.getElementById('greenflag').addEventListener('click', function() {
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.greenFlag();
|
2016-06-01 14:17:18 -04:00
|
|
|
});
|
|
|
|
document.getElementById('stopall').addEventListener('click', function() {
|
2016-06-21 15:30:36 -04:00
|
|
|
vm.stopAll();
|
2016-06-01 14:17:18 -04:00
|
|
|
});
|
2016-06-07 20:44:08 -04:00
|
|
|
|
|
|
|
var tabBlockExplorer = document.getElementById('tab-blockexplorer');
|
|
|
|
var tabThreadExplorer = document.getElementById('tab-threadexplorer');
|
|
|
|
|
|
|
|
// Handlers to show different explorers.
|
|
|
|
document.getElementById('threadexplorer-link').addEventListener('click',
|
|
|
|
function () {
|
|
|
|
tabBlockExplorer.style.display = 'none';
|
|
|
|
tabThreadExplorer.style.display = 'block';
|
|
|
|
});
|
|
|
|
document.getElementById('blockexplorer-link').addEventListener('click',
|
|
|
|
function () {
|
|
|
|
tabBlockExplorer.style.display = 'block';
|
|
|
|
tabThreadExplorer.style.display = 'none';
|
|
|
|
});
|
2016-06-01 10:04:56 -04:00
|
|
|
};
|