scratch-paint/src/containers/bit-fill-mode.jsx

145 lines
5 KiB
React
Raw Normal View History

2018-06-14 11:44:11 -04:00
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import Modes from '../lib/modes';
2018-07-17 17:21:02 -04:00
import GradientTypes from '../lib/gradient-types';
2018-06-14 11:44:11 -04:00
import FillModeComponent from '../components/bit-fill-mode/bit-fill-mode.jsx';
import {changeFillColor, changeFillColor2, DEFAULT_COLOR} from '../reducers/fill-style';
2018-06-14 11:44:11 -04:00
import {changeMode} from '../reducers/modes';
import {clearSelectedItems} from '../reducers/selected-items';
2018-07-17 17:21:02 -04:00
import {changeGradientType} from '../reducers/fill-mode-gradient-type';
2018-06-14 11:44:11 -04:00
import {clearSelection} from '../helper/selection';
import FillTool from '../helper/bit-tools/fill-tool';
import {generateSecondaryColor, MIXED} from '../helper/style-path';
2018-06-14 11:44:11 -04:00
class BitFillMode extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'activateTool',
'deactivateTool'
]);
}
componentDidMount () {
if (this.props.isFillModeActive) {
this.activateTool(this.props);
}
}
componentWillReceiveProps (nextProps) {
2018-07-17 17:21:02 -04:00
if (this.tool) {
if (nextProps.color !== this.props.color) {
this.tool.setColor(nextProps.color);
}
if (nextProps.color2 !== this.props.color2) {
this.tool.setColor2(nextProps.color2);
}
if (nextProps.fillModeGradientType !== this.props.fillModeGradientType) {
this.tool.setGradientType(nextProps.fillModeGradientType);
}
2018-06-14 11:44:11 -04:00
}
if (nextProps.isFillModeActive && !this.props.isFillModeActive) {
this.activateTool();
} else if (!nextProps.isFillModeActive && this.props.isFillModeActive) {
this.deactivateTool();
}
}
shouldComponentUpdate (nextProps) {
return nextProps.isFillModeActive !== this.props.isFillModeActive;
}
componentWillUnmount () {
if (this.tool) {
this.deactivateTool();
}
}
2018-06-14 11:44:11 -04:00
activateTool () {
clearSelection(this.props.clearSelectedItems);
2018-07-17 17:21:02 -04:00
2018-06-14 11:44:11 -04:00
// Force the default brush color if fill is MIXED or transparent
2018-07-17 17:21:02 -04:00
let color = this.props.color;
if (this.props.color === MIXED) {
color = DEFAULT_COLOR;
this.props.onChangeFillColor(DEFAULT_COLOR, 0);
}
const gradientType = this.props.fillModeGradientType ?
this.props.fillModeGradientType : this.props.styleGradientType;
2018-07-17 17:21:02 -04:00
let color2 = this.props.color2;
if (gradientType !== this.props.styleGradientType) {
if (this.props.styleGradientType === GradientTypes.SOLID) {
color2 = generateSecondaryColor(color);
2018-07-17 17:21:02 -04:00
this.props.onChangeFillColor(color2, 1);
}
this.props.changeGradientType(gradientType);
}
if (this.props.color2 === MIXED) {
color2 = generateSecondaryColor();
2018-07-17 17:21:02 -04:00
this.props.onChangeFillColor(color2, 1);
2018-06-14 11:44:11 -04:00
}
this.tool = new FillTool(this.props.onUpdateImage);
2018-07-17 17:21:02 -04:00
this.tool.setColor(color);
this.tool.setColor2(color2);
this.tool.setGradientType(gradientType);
2018-06-14 11:44:11 -04:00
this.tool.activate();
}
deactivateTool () {
this.tool.deactivateTool();
this.tool.remove();
this.tool = null;
}
render () {
return (
<FillModeComponent
isSelected={this.props.isFillModeActive}
onMouseDown={this.props.handleMouseDown}
/>
);
}
}
BitFillMode.propTypes = {
2018-07-17 17:21:02 -04:00
changeGradientType: PropTypes.func.isRequired,
2018-06-14 11:44:11 -04:00
clearSelectedItems: PropTypes.func.isRequired,
color: PropTypes.string,
2018-07-17 17:21:02 -04:00
color2: PropTypes.string,
styleGradientType: PropTypes.oneOf(Object.keys(GradientTypes)).isRequired,
2018-07-17 17:21:02 -04:00
fillModeGradientType: PropTypes.oneOf(Object.keys(GradientTypes)),
2018-06-14 11:44:11 -04:00
handleMouseDown: PropTypes.func.isRequired,
isFillModeActive: PropTypes.bool.isRequired,
onChangeFillColor: PropTypes.func.isRequired,
onUpdateImage: PropTypes.func.isRequired
2018-06-14 11:44:11 -04:00
};
const mapStateToProps = state => ({
2018-07-17 17:21:02 -04:00
fillModeGradientType: state.scratchPaint.fillMode.gradientType, // Last user-selected gradient type
color: state.scratchPaint.color.fillColor.primary,
color2: state.scratchPaint.color.fillColor.secondary,
styleGradientType: state.scratchPaint.color.fillColor.gradientType,
isFillModeActive: state.scratchPaint.mode === Modes.BIT_FILL
2018-06-14 11:44:11 -04:00
});
const mapDispatchToProps = dispatch => ({
clearSelectedItems: () => {
dispatch(clearSelectedItems());
},
2018-07-17 17:21:02 -04:00
changeGradientType: gradientType => {
dispatch(changeGradientType(gradientType));
},
2018-06-14 11:44:11 -04:00
handleMouseDown: () => {
dispatch(changeMode(Modes.BIT_FILL));
},
2018-07-17 17:21:02 -04:00
onChangeFillColor: (fillColor, index) => {
if (index === 0) {
dispatch(changeFillColor(fillColor));
} else if (index === 1) {
dispatch(changeFillColor2(fillColor));
}
2018-06-14 11:44:11 -04:00
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(BitFillMode);