2018-04-25 15:26:48 -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';
|
|
|
|
import {MIXED} from '../helper/style-path';
|
|
|
|
|
|
|
|
import {changeFillColor, DEFAULT_COLOR} from '../reducers/fill-color';
|
|
|
|
import {changeMode} from '../reducers/modes';
|
|
|
|
import {clearSelectedItems} from '../reducers/selected-items';
|
|
|
|
import {clearSelection} from '../helper/selection';
|
2018-07-17 16:37:03 -04:00
|
|
|
import {clearGradient} from '../reducers/selection-gradient-type';
|
2018-04-25 15:26:48 -04:00
|
|
|
|
|
|
|
import BitLineModeComponent from '../components/bit-line-mode/bit-line-mode.jsx';
|
|
|
|
import BitLineTool from '../helper/bit-tools/line-tool';
|
|
|
|
|
|
|
|
class BitLineMode extends React.Component {
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
|
|
|
'activateTool',
|
|
|
|
'deactivateTool'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
componentDidMount () {
|
|
|
|
if (this.props.isBitLineModeActive) {
|
|
|
|
this.activateTool(this.props);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (this.tool && nextProps.color !== this.props.color) {
|
|
|
|
this.tool.setColor(nextProps.color);
|
|
|
|
}
|
|
|
|
if (this.tool && nextProps.bitBrushSize !== this.props.bitBrushSize) {
|
|
|
|
this.tool.setLineSize(nextProps.bitBrushSize);
|
|
|
|
}
|
2018-07-17 16:37:03 -04:00
|
|
|
|
2018-04-25 15:26:48 -04:00
|
|
|
if (nextProps.isBitLineModeActive && !this.props.isBitLineModeActive) {
|
|
|
|
this.activateTool();
|
|
|
|
} else if (!nextProps.isBitLineModeActive && this.props.isBitLineModeActive) {
|
|
|
|
this.deactivateTool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldComponentUpdate (nextProps) {
|
|
|
|
return nextProps.isBitLineModeActive !== this.props.isBitLineModeActive;
|
|
|
|
}
|
2018-09-11 10:35:08 -04:00
|
|
|
componentWillUnmount () {
|
|
|
|
if (this.tool) {
|
|
|
|
this.deactivateTool();
|
|
|
|
}
|
|
|
|
}
|
2018-04-25 15:26:48 -04:00
|
|
|
activateTool () {
|
|
|
|
clearSelection(this.props.clearSelectedItems);
|
2018-07-17 16:37:03 -04:00
|
|
|
this.props.clearGradient();
|
2018-04-25 15:26:48 -04:00
|
|
|
// Force the default line color if fill is MIXED or transparent
|
|
|
|
let color = this.props.color;
|
|
|
|
if (!color || color === MIXED) {
|
|
|
|
this.props.onChangeFillColor(DEFAULT_COLOR);
|
|
|
|
color = DEFAULT_COLOR;
|
|
|
|
}
|
|
|
|
this.tool = new BitLineTool(
|
2018-04-26 18:45:50 -04:00
|
|
|
this.props.onUpdateImage
|
2018-04-25 15:26:48 -04:00
|
|
|
);
|
|
|
|
this.tool.setColor(color);
|
|
|
|
this.tool.setLineSize(this.props.bitBrushSize);
|
|
|
|
|
|
|
|
this.tool.activate();
|
|
|
|
}
|
|
|
|
deactivateTool () {
|
|
|
|
this.tool.deactivateTool();
|
|
|
|
this.tool.remove();
|
|
|
|
this.tool = null;
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<BitLineModeComponent
|
|
|
|
isSelected={this.props.isBitLineModeActive}
|
|
|
|
onMouseDown={this.props.handleMouseDown}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BitLineMode.propTypes = {
|
|
|
|
bitBrushSize: PropTypes.number.isRequired,
|
2018-07-17 16:37:03 -04:00
|
|
|
clearGradient: PropTypes.func.isRequired,
|
2018-04-25 15:26:48 -04:00
|
|
|
clearSelectedItems: PropTypes.func.isRequired,
|
|
|
|
color: PropTypes.string,
|
|
|
|
handleMouseDown: PropTypes.func.isRequired,
|
|
|
|
isBitLineModeActive: PropTypes.bool.isRequired,
|
|
|
|
onChangeFillColor: PropTypes.func.isRequired,
|
2018-04-26 18:45:50 -04:00
|
|
|
onUpdateImage: PropTypes.func.isRequired
|
2018-04-25 15:26:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
bitBrushSize: state.scratchPaint.bitBrushSize,
|
|
|
|
color: state.scratchPaint.color.fillColor,
|
|
|
|
isBitLineModeActive: state.scratchPaint.mode === Modes.BIT_LINE
|
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
clearSelectedItems: () => {
|
|
|
|
dispatch(clearSelectedItems());
|
|
|
|
},
|
2018-07-17 16:37:03 -04:00
|
|
|
clearGradient: () => {
|
|
|
|
dispatch(clearGradient());
|
|
|
|
},
|
2018-04-25 15:26:48 -04:00
|
|
|
handleMouseDown: () => {
|
|
|
|
dispatch(changeMode(Modes.BIT_LINE));
|
|
|
|
},
|
|
|
|
onChangeFillColor: fillColor => {
|
|
|
|
dispatch(changeFillColor(fillColor));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
)(BitLineMode);
|