Skip to content
Snippets Groups Projects
Commit c2b5c10a authored by Ray Schamp's avatar Ray Schamp
Browse files

Move animate timer to Stage component

And use instanceOf for the vm prop type
parent 68c0adf9
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ const bindAll = require('lodash.bindall'); ...@@ -2,6 +2,7 @@ const bindAll = require('lodash.bindall');
const defaultsDeep = require('lodash.defaultsdeep'); const defaultsDeep = require('lodash.defaultsdeep');
const React = require('react'); const React = require('react');
const ScratchBlocks = require('scratch-blocks'); const ScratchBlocks = require('scratch-blocks');
const VM = require('scratch-vm');
const BlocksComponent = require('../components/blocks'); const BlocksComponent = require('../components/blocks');
...@@ -96,7 +97,7 @@ Blocks.propTypes = { ...@@ -96,7 +97,7 @@ Blocks.propTypes = {
dragShadowOpacity: React.PropTypes.number dragShadowOpacity: React.PropTypes.number
}) })
}), }),
vm: React.PropTypes.object vm: React.PropTypes.instanceOf(VM).isRequired
}; };
Blocks.defaultOptions = { Blocks.defaultOptions = {
......
...@@ -19,12 +19,10 @@ class GUI extends React.Component { ...@@ -19,12 +19,10 @@ class GUI extends React.Component {
this.vmManager.attachKeyboardEvents(); this.vmManager.attachKeyboardEvents();
this.props.vm.loadProject(this.props.projectData); this.props.vm.loadProject(this.props.projectData);
this.props.vm.start(); this.props.vm.start();
this.vmManager.startAnimation();
} }
componentWillUnmount () { componentWillUnmount () {
this.vmManager.detachKeyboardEvents(); this.vmManager.detachKeyboardEvents();
this.props.vm.stopAll(); this.props.vm.stopAll();
this.vmManager.stopAnimation();
} }
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
if (this.props.projectData !== nextProps.projectData) { if (this.props.projectData !== nextProps.projectData) {
......
const bindAll = require('lodash.bindall'); const bindAll = require('lodash.bindall');
const React = require('react'); const React = require('react');
const Renderer = require('scratch-render'); const Renderer = require('scratch-render');
const VM = require('scratch-vm');
const StageComponent = require('../components/stage'); const StageComponent = require('../components/stage');
...@@ -12,16 +13,21 @@ class Stage extends React.Component { ...@@ -12,16 +13,21 @@ class Stage extends React.Component {
'detachMouseEvents', 'detachMouseEvents',
'onMouseUp', 'onMouseUp',
'onMouseMove', 'onMouseMove',
'onMouseDown' 'onMouseDown',
'animate',
'startAnimation',
'stopAnimation'
]); ]);
} }
componentDidMount () { componentDidMount () {
this.renderer = new Renderer(this.canvas); this.renderer = new Renderer(this.canvas);
this.props.vm.attachRenderer(this.renderer); this.props.vm.attachRenderer(this.renderer);
this.attachMouseEvents(this.canvas); this.attachMouseEvents(this.canvas);
this.startAnimation();
} }
componentWillUnmount () { componentWillUnmount () {
this.detachMouseEvents(this.canvas); this.detachMouseEvents(this.canvas);
this.stopAnimation();
} }
attachMouseEvents (canvas) { attachMouseEvents (canvas) {
document.addEventListener('mousemove', this.onMouseMove); document.addEventListener('mousemove', this.onMouseMove);
...@@ -67,6 +73,16 @@ class Stage extends React.Component { ...@@ -67,6 +73,16 @@ class Stage extends React.Component {
this.props.vm.postIOData('mouse', data); this.props.vm.postIOData('mouse', data);
e.preventDefault(); e.preventDefault();
} }
startAnimation () {
this.animationFrame = requestAnimationFrame(this.animate);
}
stopAnimation () {
cancelAnimationFrame(this.animationFrame);
}
animate () {
this.props.vm.animationFrame();
this.animationFrame = requestAnimationFrame(this.animate);
}
render () { render () {
return ( return (
<StageComponent canvasRef={canvas => this.canvas = canvas} /> <StageComponent canvasRef={canvas => this.canvas = canvas} />
...@@ -75,10 +91,7 @@ class Stage extends React.Component { ...@@ -75,10 +91,7 @@ class Stage extends React.Component {
} }
Stage.propTypes = { Stage.propTypes = {
vm: React.PropTypes.shape({ vm: React.PropTypes.instanceOf(VM).isRequired
attachRenderer: React.PropTypes.func,
postIOData: React.PropTypes.func
}).isRequired
}; };
module.exports = Stage; module.exports = Stage;
...@@ -6,23 +6,10 @@ class VMManager { ...@@ -6,23 +6,10 @@ class VMManager {
'attachKeyboardEvents', 'attachKeyboardEvents',
'detachKeyboardEvents', 'detachKeyboardEvents',
'onKeyDown', 'onKeyDown',
'onKeyUp', 'onKeyUp'
'animate',
'startAnimation',
'stopAnimation'
]); ]);
this.vm = vm; this.vm = vm;
} }
startAnimation () {
this.animationFrame = requestAnimationFrame(this.animate);
}
stopAnimation () {
cancelAnimationFrame(this.animationFrame);
}
animate () {
this.vm.animationFrame();
this.animationFrame = requestAnimationFrame(this.animate);
}
attachKeyboardEvents () { attachKeyboardEvents () {
// Feed keyboard events as VM I/O events. // Feed keyboard events as VM I/O events.
document.addEventListener('keydown', this.onKeyDown); document.addEventListener('keydown', this.onKeyDown);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment