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

142 lines
5 KiB
React
Raw Normal View History

2017-10-23 15:38:52 -04:00
import paper from '@scratch/paper';
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';
import {MIXED} from '../helper/style-path';
import {changeFillColor, DEFAULT_COLOR} from '../reducers/fill-color';
import {changeStrokeColor} from '../reducers/stroke-color';
import {changeMode} from '../reducers/modes';
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
2018-07-17 16:37:03 -04:00
import {clearGradient} from '../reducers/selection-gradient-type';
2017-10-19 20:43:28 -04:00
import {clearSelection, getSelectedLeafItems} from '../helper/selection';
import RectTool from '../helper/tools/rect-tool';
import RectModeComponent from '../components/rect-mode/rect-mode.jsx';
class RectMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool'
]);
}
componentDidMount () {
if (this.props.isRectModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
2017-10-19 16:52:24 -04:00
if (this.tool && nextProps.colorState !== this.props.colorState) {
this.tool.setColorState(nextProps.colorState);
}
2017-10-23 15:38:52 -04:00
if (this.tool && nextProps.selectedItems !== this.props.selectedItems) {
this.tool.onSelectionChanged(nextProps.selectedItems);
}
2017-10-19 16:52:24 -04:00
if (nextProps.isRectModeActive && !this.props.isRectModeActive) {
this.activateTool();
} else if (!nextProps.isRectModeActive && this.props.isRectModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate (nextProps) {
return nextProps.isRectModeActive !== this.props.isRectModeActive;
}
componentWillUnmount () {
if (this.tool) {
this.deactivateTool();
}
}
activateTool () {
2017-10-19 20:43:28 -04:00
clearSelection(this.props.clearSelectedItems);
2018-07-17 16:37:03 -04:00
this.props.clearGradient();
// If fill and stroke color are both mixed/transparent/absent, set fill to default and stroke to transparent.
// If exactly one of fill or stroke color is set, set the other one to transparent.
// This way the tool won't draw an invisible state, or be unclear about what will be drawn.
const {fillColor, strokeColor, strokeWidth} = this.props.colorState;
const fillColorPresent = fillColor !== MIXED && fillColor !== null;
const strokeColorPresent =
strokeColor !== MIXED && strokeColor !== null && strokeWidth !== null && strokeWidth !== 0;
if (!fillColorPresent && !strokeColorPresent) {
this.props.onChangeFillColor(DEFAULT_COLOR);
this.props.onChangeStrokeColor(null);
} else if (!fillColorPresent && strokeColorPresent) {
this.props.onChangeFillColor(null);
} else if (fillColorPresent && !strokeColorPresent) {
this.props.onChangeStrokeColor(null);
}
this.tool = new RectTool(
this.props.setSelectedItems,
this.props.clearSelectedItems,
this.props.onUpdateImage
);
2017-10-19 16:52:24 -04:00
this.tool.setColorState(this.props.colorState);
this.tool.activate();
}
deactivateTool () {
this.tool.deactivateTool();
this.tool.remove();
this.tool = null;
}
render () {
return (
<RectModeComponent
isSelected={this.props.isRectModeActive}
onMouseDown={this.props.handleMouseDown}
/>
);
}
}
RectMode.propTypes = {
2018-07-17 16:37:03 -04:00
clearGradient: PropTypes.func.isRequired,
clearSelectedItems: PropTypes.func.isRequired,
2017-10-19 16:52:24 -04:00
colorState: PropTypes.shape({
fillColor: PropTypes.string,
strokeColor: PropTypes.string,
strokeWidth: PropTypes.number
}).isRequired,
handleMouseDown: PropTypes.func.isRequired,
isRectModeActive: PropTypes.bool.isRequired,
onChangeFillColor: PropTypes.func.isRequired,
onChangeStrokeColor: PropTypes.func.isRequired,
onUpdateImage: PropTypes.func.isRequired,
2017-10-23 15:38:52 -04:00
selectedItems: PropTypes.arrayOf(PropTypes.instanceOf(paper.Item)),
setSelectedItems: PropTypes.func.isRequired
};
const mapStateToProps = state => ({
2017-10-19 16:52:24 -04:00
colorState: state.scratchPaint.color,
2017-10-23 15:38:52 -04:00
isRectModeActive: state.scratchPaint.mode === Modes.RECT,
selectedItems: state.scratchPaint.selectedItems
});
const mapDispatchToProps = dispatch => ({
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
2018-07-17 16:37:03 -04:00
clearGradient: () => {
dispatch(clearGradient());
},
setSelectedItems: () => {
dispatch(setSelectedItems(getSelectedLeafItems(), false /* bitmapMode */));
},
handleMouseDown: () => {
dispatch(changeMode(Modes.RECT));
},
onChangeFillColor: fillColor => {
dispatch(changeFillColor(fillColor));
},
onChangeStrokeColor: strokeColor => {
dispatch(changeStrokeColor(strokeColor));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(RectMode);