2017-10-23 15:38:52 -04:00
|
|
|
import paper from '@scratch/paper';
|
2017-10-16 14:33:11 -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-10-30 18:23:41 -04:00
|
|
|
import {MIXED} from '../helper/style-path';
|
2017-10-16 14:33:11 -04:00
|
|
|
|
2017-10-30 18:23:41 -04:00
|
|
|
import {changeFillColor, DEFAULT_COLOR} from '../reducers/fill-color';
|
|
|
|
import {changeStrokeColor} from '../reducers/stroke-color';
|
2017-10-16 14:33:11 -04:00
|
|
|
import {changeMode} from '../reducers/modes';
|
|
|
|
import {clearSelectedItems, setSelectedItems} from '../reducers/selected-items';
|
|
|
|
|
2017-10-19 20:43:28 -04:00
|
|
|
import {clearSelection, getSelectedLeafItems} from '../helper/selection';
|
2017-10-16 14:33:11 -04:00
|
|
|
import RectTool from '../helper/tools/rect-tool';
|
2017-10-19 15:08:15 -04:00
|
|
|
import RectModeComponent from '../components/rect-mode/rect-mode.jsx';
|
2017-10-16 14:33:11 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-10-16 14:33:11 -04:00
|
|
|
if (nextProps.isRectModeActive && !this.props.isRectModeActive) {
|
|
|
|
this.activateTool();
|
|
|
|
} else if (!nextProps.isRectModeActive && this.props.isRectModeActive) {
|
|
|
|
this.deactivateTool();
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 15:08:15 -04:00
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
return nextProps.isRectModeActive !== this.props.isRectModeActive;
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
|
|
|
activateTool () {
|
2017-10-19 20:43:28 -04:00
|
|
|
clearSelection(this.props.clearSelectedItems);
|
2017-10-30 18:23:41 -04:00
|
|
|
// 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);
|
|
|
|
}
|
2017-10-16 14:33:11 -04:00
|
|
|
this.tool = new RectTool(
|
|
|
|
this.props.setSelectedItems,
|
|
|
|
this.props.clearSelectedItems,
|
2018-04-26 18:45:50 -04:00
|
|
|
this.props.onUpdateImage
|
2017-10-16 14:33:11 -04:00
|
|
|
);
|
2017-10-19 16:52:24 -04:00
|
|
|
this.tool.setColorState(this.props.colorState);
|
2017-10-16 14:33:11 -04:00
|
|
|
this.tool.activate();
|
|
|
|
}
|
|
|
|
deactivateTool () {
|
|
|
|
this.tool.deactivateTool();
|
|
|
|
this.tool.remove();
|
|
|
|
this.tool = null;
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
2017-10-19 15:08:15 -04:00
|
|
|
<RectModeComponent
|
|
|
|
isSelected={this.props.isRectModeActive}
|
|
|
|
onMouseDown={this.props.handleMouseDown}
|
|
|
|
/>
|
2017-10-16 14:33:11 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RectMode.propTypes = {
|
|
|
|
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,
|
2017-10-16 14:33:11 -04:00
|
|
|
handleMouseDown: PropTypes.func.isRequired,
|
|
|
|
isRectModeActive: PropTypes.bool.isRequired,
|
2017-10-30 18:23:41 -04:00
|
|
|
onChangeFillColor: PropTypes.func.isRequired,
|
|
|
|
onChangeStrokeColor: PropTypes.func.isRequired,
|
2018-04-26 18:45:50 -04:00
|
|
|
onUpdateImage: PropTypes.func.isRequired,
|
2017-10-23 15:38:52 -04:00
|
|
|
selectedItems: PropTypes.arrayOf(PropTypes.instanceOf(paper.Item)),
|
2017-10-16 14:33:11 -04:00
|
|
|
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
|
2017-10-16 14:33:11 -04:00
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
clearSelectedItems: () => {
|
|
|
|
dispatch(clearSelectedItems());
|
|
|
|
},
|
|
|
|
setSelectedItems: () => {
|
2018-07-12 15:48:30 -04:00
|
|
|
dispatch(setSelectedItems(getSelectedLeafItems(), false /* bitmapMode */));
|
2017-10-16 14:33:11 -04:00
|
|
|
},
|
|
|
|
handleMouseDown: () => {
|
|
|
|
dispatch(changeMode(Modes.RECT));
|
|
|
|
},
|
2017-10-30 18:23:41 -04:00
|
|
|
onChangeFillColor: fillColor => {
|
|
|
|
dispatch(changeFillColor(fillColor));
|
|
|
|
},
|
|
|
|
onChangeStrokeColor: strokeColor => {
|
|
|
|
dispatch(changeStrokeColor(strokeColor));
|
2017-10-16 14:33:11 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(RectMode);
|