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.
|
|
|
|
workspace.addChangeListener(vm.blockListener);
|
2016-06-09 11:49:03 -04:00
|
|
|
// @todo: Re-enable flyout listening after fixing GH-69.
|
|
|
|
//var flyoutWorkspace = workspace.toolbox_.flyout_.workspace_;
|
|
|
|
//flyoutWorkspace.addChangeListener(vm.flyoutBlockListener);
|
2016-06-01 10:04:56 -04:00
|
|
|
|
2016-06-07 20:44:08 -04:00
|
|
|
var blockexplorer = document.getElementById('blockexplorer');
|
2016-06-01 10:18:08 -04:00
|
|
|
workspace.addChangeListener(function() {
|
|
|
|
// On a change, update the block explorer.
|
2016-06-07 20:44:08 -04:00
|
|
|
blockexplorer.innerHTML = JSON.stringify(vm.runtime.blocks, null, 2);
|
|
|
|
window.hljs.highlightBlock(blockexplorer);
|
2016-06-01 10:18:08 -04:00
|
|
|
});
|
|
|
|
|
2016-06-07 20:44:08 -04:00
|
|
|
var threadexplorer = document.getElementById('threadexplorer');
|
|
|
|
var cachedThreadJSON = '';
|
|
|
|
var updateThreadExplorer = function () {
|
|
|
|
var newJSON = JSON.stringify(vm.runtime.threads, null, 2);
|
|
|
|
if (newJSON != cachedThreadJSON) {
|
|
|
|
cachedThreadJSON = newJSON;
|
|
|
|
threadexplorer.innerHTML = cachedThreadJSON;
|
|
|
|
window.hljs.highlightBlock(threadexplorer);
|
|
|
|
}
|
|
|
|
window.requestAnimationFrame(updateThreadExplorer);
|
|
|
|
};
|
|
|
|
updateThreadExplorer();
|
|
|
|
|
2016-06-07 20:59:34 -04:00
|
|
|
// Feedback for stacks and blocks running.
|
2016-06-01 13:54:33 -04:00
|
|
|
vm.runtime.on('STACK_GLOW_ON', function(blockId) {
|
|
|
|
workspace.glowStack(blockId, true);
|
|
|
|
});
|
|
|
|
vm.runtime.on('STACK_GLOW_OFF', function(blockId) {
|
|
|
|
workspace.glowStack(blockId, false);
|
|
|
|
});
|
2016-06-07 20:59:34 -04:00
|
|
|
vm.runtime.on('BLOCK_GLOW_ON', function(blockId) {
|
|
|
|
workspace.glowBlock(blockId, true);
|
|
|
|
});
|
|
|
|
vm.runtime.on('BLOCK_GLOW_OFF', function(blockId) {
|
|
|
|
workspace.glowBlock(blockId, false);
|
|
|
|
});
|
|
|
|
|
2016-06-01 13:54:33 -04:00
|
|
|
|
2016-06-01 10:04:56 -04:00
|
|
|
// Run threads
|
|
|
|
vm.runtime.start();
|
2016-06-01 14:17:18 -04:00
|
|
|
|
|
|
|
// Handlers for green flag and stop all.
|
|
|
|
document.getElementById('greenflag').addEventListener('click', function() {
|
|
|
|
vm.runtime.greenFlag();
|
|
|
|
});
|
|
|
|
document.getElementById('stopall').addEventListener('click', function() {
|
|
|
|
vm.runtime.stopAll();
|
|
|
|
});
|
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
|
|
|
};
|