Skip to content
Snippets Groups Projects
Commit 6a87934f authored by Paul Kaplan's avatar Paul Kaplan
Browse files

Use hover to cycle costumes

parent ef9b2d61
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,9 @@ class LibraryItem extends React.Component { ...@@ -14,6 +14,9 @@ class LibraryItem extends React.Component {
'handleMouseLeave' 'handleMouseLeave'
]); ]);
} }
shouldComponentUpdate (nextProps) {
return this.props.iconURL !== nextProps.iconURL;
}
handleClick (e) { handleClick (e) {
this.props.onSelect(this.props.id); this.props.onSelect(this.props.id);
e.preventDefault(); e.preventDefault();
......
...@@ -11,18 +11,55 @@ class SpriteLibrary extends React.Component { ...@@ -11,18 +11,55 @@ class SpriteLibrary extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'handleItemSelect' 'handleItemSelect',
'handleMouseEnter',
'handleMouseLeave',
'rotateCostume'
]); ]);
this.state = {
activeSprite: null,
costumeIndex: 0,
sprites: spriteLibraryContent
};
}
componentWillReceiveProps (newProps) {
if (!newProps.visible) clearInterval(this.intervalId);
} }
handleItemSelect (item) { handleItemSelect (item) {
this.props.vm.addSprite2(JSON.stringify(item.json)); this.props.vm.addSprite2(JSON.stringify(item.json));
} }
handleMouseEnter (item) {
this.setState({activeSprite: item});
if (this.intervalId) clearInterval(this.intervalId);
this.intervalId = setInterval(this.rotateCostume, 300);
}
handleMouseLeave () {
this.intervalId = clearInterval(this.intervalId);
}
rotateCostume () {
const costumes = this.state.activeSprite.json.costumes;
const nextCostumeIndex = (this.state.costumeIndex + 1) % costumes.length;
this.setState({
costumeIndex: nextCostumeIndex,
sprites: this.state.sprites.map(sprite => {
if (sprite.name === this.state.activeSprite.name) {
return {
...sprite,
md5: sprite.json.costumes[nextCostumeIndex].baseLayerMD5
};
}
return sprite;
})
});
}
render () { render () {
return ( return (
<LibraryComponent <LibraryComponent
data={spriteLibraryContent} data={this.state.sprites}
title="Sprite Library" title="Sprite Library"
visible={this.props.visible} visible={this.props.visible}
onItemMouseEnter={this.handleMouseEnter}
onItemMouseLeave={this.handleMouseLeave}
onItemSelected={this.handleItemSelect} onItemSelected={this.handleItemSelect}
onRequestClose={this.props.onRequestClose} onRequestClose={this.props.onRequestClose}
/> />
......
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