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';
|
2017-11-07 14:02:39 -05:00
|
|
|
import Modes from '../lib/modes';
|
2017-09-18 11:28:34 -04:00
|
|
|
|
|
|
|
import {changeMode} from '../reducers/modes';
|
2017-10-02 15:25:04 -04:00
|
|
|
import {clearHoveredItem, setHoveredItem} from '../reducers/hover';
|
|
|
|
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
|
2017-10-11 11:32:51 -04:00
|
|
|
import {getSelectedLeafItems} from '../helper/selection';
|
2017-09-18 11:28:34 -04:00
|
|
|
|
2017-09-21 10:36:26 -04:00
|
|
|
import ReshapeTool from '../helper/selection-tools/reshape-tool';
|
2017-10-19 15:08:15 -04:00
|
|
|
import ReshapeModeComponent from '../components/reshape-mode/reshape-mode.jsx';
|
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-22 13:56:58 -04:00
|
|
|
if (this.tool && nextProps.hoveredItemId !== this.props.hoveredItemId) {
|
|
|
|
this.tool.setPrevHoveredItemId(nextProps.hoveredItemId);
|
2017-09-21 10:36:26 -04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 15:08:15 -04:00
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
return nextProps.isReshapeModeActive !== this.props.isReshapeModeActive;
|
2017-09-18 11:28:34 -04:00
|
|
|
}
|
2018-09-11 10:35:08 -04:00
|
|
|
componentWillUnmount () {
|
|
|
|
if (this.tool) {
|
|
|
|
this.deactivateTool();
|
|
|
|
}
|
|
|
|
}
|
2017-09-18 11:28:34 -04:00
|
|
|
activateTool () {
|
2017-10-02 15:25:04 -04:00
|
|
|
this.tool = new ReshapeTool(
|
|
|
|
this.props.setHoveredItem,
|
|
|
|
this.props.clearHoveredItem,
|
|
|
|
this.props.setSelectedItems,
|
|
|
|
this.props.clearSelectedItems,
|
2018-08-09 10:55:44 -04:00
|
|
|
this.props.onUpdateImage,
|
|
|
|
this.props.switchToTextTool
|
2017-10-05 15:41:22 -04:00
|
|
|
);
|
2017-09-22 13:56:58 -04:00
|
|
|
this.tool.setPrevHoveredItemId(this.props.hoveredItemId);
|
2017-09-18 11:28:34 -04:00
|
|
|
this.tool.activate();
|
|
|
|
}
|
|
|
|
deactivateTool () {
|
2017-09-21 18:20:44 -04:00
|
|
|
this.tool.deactivateTool();
|
2017-09-18 11:28:34 -04:00
|
|
|
this.tool.remove();
|
|
|
|
this.tool = null;
|
|
|
|
this.hitResult = null;
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
2017-10-19 15:08:15 -04:00
|
|
|
<ReshapeModeComponent
|
|
|
|
isSelected={this.props.isReshapeModeActive}
|
|
|
|
onMouseDown={this.props.handleMouseDown}
|
|
|
|
/>
|
2017-09-18 11:28:34 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReshapeMode.propTypes = {
|
|
|
|
clearHoveredItem: PropTypes.func.isRequired,
|
2017-10-02 15:25:04 -04:00
|
|
|
clearSelectedItems: PropTypes.func.isRequired,
|
2017-09-18 11:28:34 -04:00
|
|
|
handleMouseDown: PropTypes.func.isRequired,
|
2017-09-22 13:56:58 -04:00
|
|
|
hoveredItemId: PropTypes.number,
|
2017-09-18 11:28:34 -04:00
|
|
|
isReshapeModeActive: PropTypes.bool.isRequired,
|
2018-04-26 18:45:50 -04:00
|
|
|
onUpdateImage: PropTypes.func.isRequired,
|
2017-10-02 15:25:04 -04:00
|
|
|
setHoveredItem: PropTypes.func.isRequired,
|
2018-08-09 10:55:44 -04:00
|
|
|
setSelectedItems: PropTypes.func.isRequired,
|
|
|
|
switchToTextTool: PropTypes.func.isRequired
|
2017-09-18 11:28:34 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
isReshapeModeActive: state.scratchPaint.mode === Modes.RESHAPE,
|
2017-09-22 13:56:58 -04:00
|
|
|
hoveredItemId: state.scratchPaint.hoveredItemId
|
2017-09-18 11:28:34 -04:00
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
2017-09-22 13:56:58 -04:00
|
|
|
setHoveredItem: hoveredItemId => {
|
|
|
|
dispatch(setHoveredItem(hoveredItemId));
|
2017-09-18 11:28:34 -04:00
|
|
|
},
|
|
|
|
clearHoveredItem: () => {
|
|
|
|
dispatch(clearHoveredItem());
|
|
|
|
},
|
2017-10-02 15:25:04 -04:00
|
|
|
clearSelectedItems: () => {
|
|
|
|
dispatch(clearSelectedItems());
|
|
|
|
},
|
|
|
|
setSelectedItems: () => {
|
2018-07-12 15:48:30 -04:00
|
|
|
dispatch(setSelectedItems(getSelectedLeafItems(), false /* bitmapMode */));
|
2017-10-02 15:25:04 -04:00
|
|
|
},
|
2017-09-18 11:28:34 -04:00
|
|
|
handleMouseDown: () => {
|
|
|
|
dispatch(changeMode(Modes.RESHAPE));
|
2018-08-09 10:55:44 -04:00
|
|
|
},
|
|
|
|
switchToTextTool: () => {
|
|
|
|
dispatch(changeMode(Modes.TEXT));
|
2017-09-18 11:28:34 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(ReshapeMode);
|