Skip to content
Snippets Groups Projects
Commit f36bc303 authored by DD's avatar DD
Browse files

make a new component that only updates when the paint editor should update

parent bfa09681
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ import VM from 'scratch-vm'; ...@@ -6,7 +6,7 @@ import VM from 'scratch-vm';
import AssetPanel from '../components/asset-panel/asset-panel.jsx'; import AssetPanel from '../components/asset-panel/asset-panel.jsx';
import addCostumeIcon from '../components/asset-panel/icon--add-costume-lib.svg'; import addCostumeIcon from '../components/asset-panel/icon--add-costume-lib.svg';
import PaintEditor from 'scratch-paint'; import PaintEditorWrapper from './paint-editor-wrapper.jsx';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
...@@ -20,8 +20,7 @@ class CostumeTab extends React.Component { ...@@ -20,8 +20,7 @@ class CostumeTab extends React.Component {
super(props); super(props);
bindAll(this, [ bindAll(this, [
'handleSelectCostume', 'handleSelectCostume',
'handleDeleteCostume', 'handleDeleteCostume'
'handleUpdateSvg'
]); ]);
this.state = {selectedCostumeIndex: 0}; this.state = {selectedCostumeIndex: 0};
} }
...@@ -39,25 +38,6 @@ class CostumeTab extends React.Component { ...@@ -39,25 +38,6 @@ class CostumeTab extends React.Component {
} }
} }
shouldComponentUpdate (nextProps) {
const {
editingTarget,
sprites,
stage
} = nextProps;
const nextTarget = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
const currentTarget =
this.props.editingTarget && this.props.sprites[this.props.editingTarget] ?
this.props.sprites[this.props.editingTarget] :
this.props.stage;
if (this.props.editingTarget !== editingTarget ||
currentTarget.currentCostume !== nextTarget.currentCostume) {
return true;
}
return false;
}
handleSelectCostume (costumeIndex) { handleSelectCostume (costumeIndex) {
this.props.vm.editingTarget.setCostume(costumeIndex); this.props.vm.editingTarget.setCostume(costumeIndex);
this.setState({selectedCostumeIndex: costumeIndex}); this.setState({selectedCostumeIndex: costumeIndex});
...@@ -67,19 +47,20 @@ class CostumeTab extends React.Component { ...@@ -67,19 +47,20 @@ class CostumeTab extends React.Component {
this.props.vm.deleteCostume(costumeIndex); this.props.vm.deleteCostume(costumeIndex);
} }
handleUpdateSvg (svg, rotationCenterX, rotationCenterY) {
this.props.vm.updateSvg(this.state.selectedCostumeIndex, svg, rotationCenterX, rotationCenterY);
}
render () { render () {
// For paint wrapper
const { const {
editingTarget, onNewBackdropClick,
sprites,
stage,
onNewCostumeClick, onNewCostumeClick,
onNewBackdropClick ...props
} = this.props; } = this.props;
const {
editingTarget,
sprites,
stage
} = props;
const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage; const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
if (!target) { if (!target) {
...@@ -117,11 +98,9 @@ class CostumeTab extends React.Component { ...@@ -117,11 +98,9 @@ class CostumeTab extends React.Component {
onItemClick={this.handleSelectCostume} onItemClick={this.handleSelectCostume}
> >
{target.costumes ? {target.costumes ?
<PaintEditor <PaintEditorWrapper
rotationCenterX={target.costumes[this.state.selectedCostumeIndex].rotationCenterX} {...props}
rotationCenterY={target.costumes[this.state.selectedCostumeIndex].rotationCenterY} selectedCostumeIndex={this.state.selectedCostumeIndex}
svg={this.props.vm.getCostumeSvg(this.state.selectedCostumeIndex)}
onUpdateSvg={this.handleUpdateSvg}
/> : /> :
null null
} }
......
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
import VM from 'scratch-vm';
import PaintEditor from 'scratch-paint';
import {connect} from 'react-redux';
class PaintEditorWrapper extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'handleUpdateSvg'
]);
}
shouldComponentUpdate (nextProps) {
// Only update on sprite change or costume change. No need to push the SVG to the paint
// editor that it just exported; it causes the paint editor to lose some state.
const {
editingTarget,
sprites,
stage
} = nextProps;
const nextTarget = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
const currentTarget =
this.props.editingTarget && this.props.sprites[this.props.editingTarget] ?
this.props.sprites[this.props.editingTarget] :
this.props.stage;
if (this.props.editingTarget !== editingTarget ||
currentTarget.currentCostume !== nextTarget.currentCostume) {
return true;
}
return false;
}
handleUpdateSvg (svg, rotationCenterX, rotationCenterY) {
this.props.vm.updateSvg(this.props.selectedCostumeIndex, svg, rotationCenterX, rotationCenterY);
}
render () {
const {
editingTarget,
sprites,
stage
} = this.props;
const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
if (!target || !target.costumes) {
return null;
}
return (
<PaintEditor
rotationCenterX={target.costumes[this.props.selectedCostumeIndex].rotationCenterX}
rotationCenterY={target.costumes[this.props.selectedCostumeIndex].rotationCenterY}
svg={this.props.vm.getCostumeSvg(this.props.selectedCostumeIndex)}
onUpdateSvg={this.handleUpdateSvg}
/>
);
}
}
PaintEditorWrapper.propTypes = {
editingTarget: PropTypes.string,
selectedCostumeIndex: PropTypes.number.isRequired,
sprites: PropTypes.shape({
id: PropTypes.shape({
costumes: PropTypes.arrayOf(PropTypes.shape({
url: PropTypes.string,
name: PropTypes.string.isRequired
}))
})
}),
stage: PropTypes.shape({
sounds: PropTypes.arrayOf(PropTypes.shape({
name: PropTypes.string.isRequired
}))
}),
vm: PropTypes.instanceOf(VM)
};
const mapStateToProps = state => ({
editingTarget: state.targets.editingTarget,
sprites: state.targets.sprites,
stage: state.targets.stage,
costumeLibraryVisible: state.modals.costumeLibrary,
backdropLibraryVisible: state.modals.backdropLibrary
});
export default connect(
mapStateToProps
)(PaintEditorWrapper);
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