Merge pull request #200 from rschamp/render-scope

Scope renderer to instance
This commit is contained in:
Ray Schamp 2016-09-20 15:29:28 -04:00 committed by GitHub
commit 9884c583a9
9 changed files with 814 additions and 420 deletions

View file

@ -20,6 +20,7 @@
},
"devDependencies": {
"eslint": "2.7.0",
"expose-loader": "0.7.1",
"highlightjs": "8.7.0",
"json-loader": "0.5.4",
"scratch-blocks": "git+https://git@github.com/LLK/scratch-blocks.git#develop",

View file

@ -21,7 +21,7 @@ var loadProject = function () {
window.onload = function() {
// Lots of global variables to make debugging easier
// Instantiate the VM worker.
// Instantiate the VM.
var vm = new window.VirtualMachine();
window.vm = vm;
@ -39,7 +39,9 @@ window.onload = function() {
// Instantiate the renderer and connect it to the VM.
var canvas = document.getElementById('scratch-stage');
window.renderer = new window.RenderWebGL(canvas);
var renderer = new window.RenderWebGL(canvas);
window.renderer = renderer;
vm.attachRenderer(renderer);
// Instantiate scratch-blocks and attach it to the DOM.
var toolbox = document.getElementById('toolbox');

View file

@ -199,6 +199,14 @@ Runtime.prototype.clearEdgeActivatedValues = function () {
this._edgeActivatedHatValues = {};
};
/**
* Attach the renderer
* @param {!RenderWebGL} renderer The renderer to attach
*/
Runtime.prototype.attachRenderer = function (renderer) {
this.renderer = renderer;
};
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
@ -573,8 +581,8 @@ Runtime.prototype.getTargetForStage = function () {
* Handle an animation frame from the main thread.
*/
Runtime.prototype.animationFrame = function () {
if (self.renderer) {
self.renderer.draw();
if (this.renderer) {
this.renderer.draw();
}
};

View file

@ -162,6 +162,14 @@ VirtualMachine.prototype.createEmptyProject = function () {
this.emitWorkspaceUpdate();
};
/**
* Set the renderer for the VM/runtime
* @param {!RenderWebGL} renderer The renderer to attach
*/
VirtualMachine.prototype.attachRenderer = function (renderer) {
this.runtime.attachRenderer(renderer);
};
/**
* Handle a Blockly event for the current editing target.
* @param {!Blockly.Event} e Any Blockly event.
@ -227,8 +235,5 @@ VirtualMachine.prototype.emitWorkspaceUpdate = function () {
'xml': this.editingTarget.blocks.toXML()
});
};
/**
* Export and bind to `window`
*/
module.exports = VirtualMachine;
if (typeof window !== 'undefined') window.VirtualMachine = module.exports;

View file

@ -28,8 +28,8 @@ Mouse.prototype.postData = function(data) {
};
Mouse.prototype._activateClickHats = function (x, y) {
if (self.renderer) {
var drawableID = self.renderer.pick(x, y);
if (this.runtime.renderer) {
var drawableID = this.runtime.renderer.pick(x, y);
for (var i = 0; i < this.runtime.targets.length; i++) {
var target = this.runtime.targets[i];
if (target.hasOwnProperty('drawableID') &&

View file

@ -21,10 +21,8 @@ function Clone(sprite, runtime) {
* @type {?RenderWebGLWorker}
*/
this.renderer = null;
// If this is not true, there is no renderer (e.g., running in a test env).
if (typeof self !== 'undefined' && self.renderer) {
// Pull from `self.renderer`.
this.renderer = self.renderer;
if (this.runtime) {
this.renderer = this.runtime.renderer;
}
/**
* ID of the drawable for this clone returned by the renderer, if rendered.

1177
vm.js

File diff suppressed because it is too large Load diff

12
vm.min.js vendored

File diff suppressed because one or more lines are too long

View file

@ -14,6 +14,9 @@ module.exports = {
{
test: /\.json$/,
loader: 'json-loader'
}, {
test: require.resolve('./src/index.js'),
loader: 'expose?VirtualMachine'
}
]
},