scratch-paint/src/containers/reshape-mode.jsx

93 lines
2.9 KiB
React
Raw Normal View History

2017-09-18 11:28:34 -04:00
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import Modes from '../modes/modes';
import {changeMode} from '../reducers/modes';
import {setHoveredItem, clearHoveredItem} from '../reducers/hover';
2017-09-21 10:36:26 -04:00
import {selectSubItems} from '../helper/selection';
import ReshapeTool from '../helper/selection-tools/reshape-tool';
2017-09-18 11:28:34 -04:00
import ReshapeModeComponent from '../components/reshape-mode.jsx';
import paper from 'paper';
2017-09-21 10:36:26 -04:00
2017-09-18 11:28:34 -04:00
class ReshapeMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
2017-09-21 10:36:26 -04:00
'deactivateTool'
2017-09-18 11:28:34 -04:00
]);
}
componentDidMount () {
if (this.props.isReshapeModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
2017-09-21 10:36:26 -04:00
if (this.tool && nextProps.hoveredItem !== this.props.hoveredItem) {
this.tool.setPrevHoveredItem(nextProps.hoveredItem);
}
2017-09-18 11:28:34 -04:00
if (nextProps.isReshapeModeActive && !this.props.isReshapeModeActive) {
this.activateTool();
} else if (!nextProps.isReshapeModeActive && this.props.isReshapeModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate () {
return false; // Static component, for now
}
activateTool () {
2017-09-21 10:36:26 -04:00
selectSubItems();
this.tool = new ReshapeTool(this.props.setHoveredItem, this.props.clearHoveredItem);
this.tool.setPrevHoveredItem(this.props.hoveredItem);
2017-09-18 11:28:34 -04:00
this.tool.activate();
2017-09-21 10:36:26 -04:00
paper.settings.handleSize = 8;
2017-09-18 11:28:34 -04:00
}
deactivateTool () {
paper.settings.handleSize = 0;
this.props.clearHoveredItem();
this.tool.remove();
this.tool = null;
this.hitResult = null;
}
render () {
return (
<ReshapeModeComponent onMouseDown={this.props.handleMouseDown} />
);
}
}
ReshapeMode.propTypes = {
clearHoveredItem: PropTypes.func.isRequired,
handleMouseDown: PropTypes.func.isRequired,
hoveredItem: PropTypes.instanceOf(paper.Item), // eslint-disable-line react/no-unused-prop-types
isReshapeModeActive: PropTypes.bool.isRequired,
onUpdateSvg: PropTypes.func.isRequired, // eslint-disable-line react/no-unused-prop-types
setHoveredItem: PropTypes.func.isRequired // eslint-disable-line react/no-unused-prop-types
};
const mapStateToProps = state => ({
isReshapeModeActive: state.scratchPaint.mode === Modes.RESHAPE,
hoveredItem: state.scratchPaint.hoveredItem
});
const mapDispatchToProps = dispatch => ({
setHoveredItem: hoveredItem => {
dispatch(setHoveredItem(hoveredItem));
},
clearHoveredItem: () => {
dispatch(clearHoveredItem());
},
handleMouseDown: () => {
dispatch(changeMode(Modes.RESHAPE));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(ReshapeMode);