Skip to content
Snippets Groups Projects
costume-tab.jsx 3.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • const PropTypes = require('prop-types');
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    const React = require('react');
    const bindAll = require('lodash.bindall');
    
    const VM = require('scratch-vm');
    
    const AssetPanel = require('../components/asset-panel/asset-panel.jsx');
    
    const {connect} = require('react-redux');
    
    const {
    
        openCostumeLibrary,
        openBackdropLibrary
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    } = require('../reducers/modals');
    
    class CostumeTab extends React.Component {
        constructor (props) {
            super(props);
            bindAll(this, [
                'handleSelectCostume',
                'handleDeleteCostume'
            ]);
            this.state = {selectedCostumeIndex: 0};
        }
    
    
        componentWillReceiveProps (nextProps) {
            const {
                editingTarget,
                sprites,
                stage
            } = nextProps;
    
            const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
            if (target && target.costumes && this.state.selectedCostumeIndex > target.costumes.length - 1) {
                this.setState({selectedCostumeIndex: target.costumes.length - 1});
            }
        }
    
    
        handleSelectCostume (costumeIndex) {
            this.setState({selectedCostumeIndex: costumeIndex});
    
        handleDeleteCostume (costumeIndex) {
    
            this.props.vm.deleteCostume(costumeIndex);
    
    Paul Kaplan's avatar
    Paul Kaplan committed
        }
    
        render () {
            const {
    
                editingTarget,
                sprites,
    
                stage,
    
                onNewCostumeClick,
                onNewBackdropClick
    
    Paul Kaplan's avatar
    Paul Kaplan committed
            } = this.props;
    
    
            const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
    
            if (!target) {
                return null;
            }
    
            const addText = target.isStage ? 'Add Backdrop' : 'Add Costume';
            const addFunc = target.isStage ? onNewBackdropClick : onNewCostumeClick;
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    
            return (
                <AssetPanel
    
                    items={target.costumes || []}
    
    Paul Kaplan's avatar
    Paul Kaplan committed
                    newText={addText}
                    selectedItemIndex={this.state.selectedCostumeIndex}
                    onDeleteClick={this.handleDeleteCostume}
                    onItemClick={this.handleSelectCostume}
    
                    onNewClick={addFunc}
    
    Paul Kaplan's avatar
    Paul Kaplan committed
                />
            );
        }
    }
    
    CostumeTab.propTypes = {
    
        editingTarget: PropTypes.string,
        onNewBackdropClick: PropTypes.func.isRequired,
        onNewCostumeClick: PropTypes.func.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)
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    };
    
    const mapStateToProps = state => ({
        editingTarget: state.targets.editingTarget,
        sprites: state.targets.sprites,
    
        stage: state.targets.stage,
    
        costumeLibraryVisible: state.modals.costumeLibrary,
        backdropLibraryVisible: state.modals.backdropLibrary
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    });
    
    const mapDispatchToProps = dispatch => ({
    
        onNewBackdropClick: e => {
            e.preventDefault();
            dispatch(openBackdropLibrary());
        },
    
    Paul Kaplan's avatar
    Paul Kaplan committed
        onNewCostumeClick: e => {
            e.preventDefault();
            dispatch(openCostumeLibrary());
        }
    });
    
    module.exports = connect(
        mapStateToProps,
        mapDispatchToProps
    )(CostumeTab);