Skip to content
Snippets Groups Projects
target-pane.jsx 5.26 KiB
Newer Older
  • Learn to ignore specific revisions
  • Paul Kaplan's avatar
    Paul Kaplan committed
    import bindAll from 'lodash.bindall';
    import React from 'react';
    
    Ray Schamp's avatar
    Ray Schamp committed
    
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    import {connect} from 'react-redux';
    
    Ray Schamp's avatar
    Ray Schamp committed
    
    
    Ray Schamp's avatar
    Ray Schamp committed
        openSpriteLibrary,
        closeSpriteLibrary
    
    } from '../reducers/modals';
    
    Ray Schamp's avatar
    Ray Schamp committed
    
    
    import {activateTab, COSTUMES_TAB_INDEX} from '../reducers/editor-tab';
    
    import {setReceivedBlocks} from '../reducers/hovered-target';
    
    Paul Kaplan's avatar
    Paul Kaplan committed
    import TargetPaneComponent from '../components/target-pane/target-pane.jsx';
    
    import spriteLibraryContent from '../lib/libraries/sprites.json';
    
    Ray Schamp's avatar
    Ray Schamp committed
    
    class TargetPane extends React.Component {
        constructor (props) {
            super(props);
            bindAll(this, [
    
                'handleChangeSpriteDirection',
    
                'handleChangeSpriteName',
    
                'handleChangeSpriteSize',
    
                'handleChangeSpriteVisibility',
    
    Ray Schamp's avatar
    Ray Schamp committed
                'handleChangeSpriteX',
                'handleChangeSpriteY',
    
                'handleDeleteSprite',
    
                'handleDuplicateSprite',
    
                'handleSelectSprite',
                'handleSurpriseSpriteClick',
                'handlePaintSpriteClick'
    
    Ray Schamp's avatar
    Ray Schamp committed
            ]);
        }
    
        componentDidMount () {
            this.props.vm.addListener('BLOCK_DRAG_END', this.handleBlockDragEnd);
        }
        componentWillUnmount () {
            this.props.vm.removeListener('BLOCK_DRAG_END', this.handleBlockDragEnd);
        }
    
        handleChangeSpriteDirection (direction) {
            this.props.vm.postSpriteInfo({direction});
    
        handleChangeSpriteName (name) {
            this.props.vm.renameSprite(this.props.editingTarget, name);
    
        handleChangeSpriteSize (size) {
            this.props.vm.postSpriteInfo({size});
    
        handleChangeSpriteVisibility (visible) {
            this.props.vm.postSpriteInfo({visible});
    
        handleChangeSpriteX (x) {
    
    Ray Schamp's avatar
    Ray Schamp committed
            this.props.vm.postSpriteInfo({x});
        }
    
        handleChangeSpriteY (y) {
    
    Ray Schamp's avatar
    Ray Schamp committed
            this.props.vm.postSpriteInfo({y});
        }
    
        handleDeleteSprite (id) {
            this.props.vm.deleteSprite(id);
        }
    
        handleDuplicateSprite (id) {
            this.props.vm.duplicateSprite(id);
        }
    
    Ray Schamp's avatar
    Ray Schamp committed
        handleSelectSprite (id) {
            this.props.vm.setEditingTarget(id);
        }
    
        handleSurpriseSpriteClick () {
            const item = spriteLibraryContent[Math.floor(Math.random() * spriteLibraryContent.length)];
            this.props.vm.addSprite2(JSON.stringify(item.json));
        }
        handlePaintSpriteClick () {
            // @todo this is brittle, will need to be refactored for localized libraries
            const emptyItem = spriteLibraryContent.find(item => item.name === 'Empty');
            if (emptyItem) {
                this.props.vm.addSprite2(JSON.stringify(emptyItem.json)).then(() => {
                    this.props.onActivateTab(COSTUMES_TAB_INDEX);
                });
            }
        }
    
        handleBlockDragEnd (blocks) {
            if (this.props.hoveredTarget.sprite && this.props.hoveredTarget.sprite !== this.props.editingTarget) {
                this.props.vm.shareBlocksToTarget(blocks, this.props.hoveredTarget.sprite);
                this.props.onReceivedBlocks(true);
            }
        }
    
    Ray Schamp's avatar
    Ray Schamp committed
        render () {
    
    Paul Kaplan's avatar
    Paul Kaplan committed
            const {
                onActivateTab, // eslint-disable-line no-unused-vars
                ...componentProps
            } = this.props;
    
    Ray Schamp's avatar
    Ray Schamp committed
            return (
                <TargetPaneComponent
    
    Paul Kaplan's avatar
    Paul Kaplan committed
                    {...componentProps}
    
                    onChangeSpriteDirection={this.handleChangeSpriteDirection}
    
                    onChangeSpriteName={this.handleChangeSpriteName}
    
                    onChangeSpriteSize={this.handleChangeSpriteSize}
    
                    onChangeSpriteVisibility={this.handleChangeSpriteVisibility}
    
    Ray Schamp's avatar
    Ray Schamp committed
                    onChangeSpriteX={this.handleChangeSpriteX}
                    onChangeSpriteY={this.handleChangeSpriteY}
    
                    onDeleteSprite={this.handleDeleteSprite}
    
                    onDuplicateSprite={this.handleDuplicateSprite}
    
                    onPaintSpriteClick={this.handlePaintSpriteClick}
    
    Ray Schamp's avatar
    Ray Schamp committed
                    onSelectSprite={this.handleSelectSprite}
    
                    onSurpriseSpriteClick={this.handleSurpriseSpriteClick}
    
    Ray Schamp's avatar
    Ray Schamp committed
                />
            );
        }
    }
    
    const {
        onSelectSprite, // eslint-disable-line no-unused-vars
    
        ...targetPaneProps
    
    Ray Schamp's avatar
    Ray Schamp committed
    } = TargetPaneComponent.propTypes;
    
    TargetPane.propTypes = {
    
        ...targetPaneProps
    
    Ray Schamp's avatar
    Ray Schamp committed
    };
    
    const mapStateToProps = state => ({
        editingTarget: state.targets.editingTarget,
    
    DD's avatar
    DD committed
        hoveredTarget: state.hoveredTarget,
    
        sprites: Object.keys(state.targets.sprites).reduce((sprites, k) => {
    
    Paul Kaplan's avatar
    Paul Kaplan committed
            let {direction, size, x, y, ...sprite} = state.targets.sprites[k];
    
            if (typeof direction !== 'undefined') direction = Math.round(direction);
    
    Ray Schamp's avatar
    Ray Schamp committed
            if (typeof x !== 'undefined') x = Math.round(x);
            if (typeof y !== 'undefined') y = Math.round(y);
    
    Paul Kaplan's avatar
    Paul Kaplan committed
            if (typeof size !== 'undefined') size = Math.round(size);
            sprites[k] = {...sprite, direction, size, x, y};
    
            return sprites;
        }, {}),
    
    Ray Schamp's avatar
    Ray Schamp committed
        stage: state.targets.stage,
    
        spriteLibraryVisible: state.modals.spriteLibrary
    
    Ray Schamp's avatar
    Ray Schamp committed
    });
    const mapDispatchToProps = dispatch => ({
        onNewSpriteClick: e => {
            e.preventDefault();
            dispatch(openSpriteLibrary());
        },
        onRequestCloseSpriteLibrary: () => {
            dispatch(closeSpriteLibrary());
    
    Paul Kaplan's avatar
    Paul Kaplan committed
        },
    
        onActivateTab: tabIndex => {
            dispatch(activateTab(tabIndex));
    
        },
        onReceivedBlocks: receivedBlocks => {
            dispatch(setReceivedBlocks(receivedBlocks));
    
    Ray Schamp's avatar
    Ray Schamp committed
        }
    });
    
    
    export default connect(
    
    Ray Schamp's avatar
    Ray Schamp committed
        mapStateToProps,
        mapDispatchToProps
    )(TargetPane);