2017-07-25 11:53:54 -04:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
|
|
|
import {connect} from 'react-redux';
|
|
|
|
import bindAll from 'lodash.bindall';
|
2017-08-15 18:11:02 -04:00
|
|
|
import Modes from '../modes/modes';
|
2017-08-17 13:53:54 -04:00
|
|
|
import Blobbiness from './blob/blob';
|
2017-08-16 18:16:37 -04:00
|
|
|
import {changeBrushSize} from '../reducers/eraser-mode';
|
2017-08-17 18:13:24 -04:00
|
|
|
import EraserModeComponent from '../components/eraser-mode.jsx';
|
|
|
|
import {changeMode} from '../reducers/modes';
|
2017-07-25 11:53:54 -04:00
|
|
|
|
2017-08-15 18:11:02 -04:00
|
|
|
class EraserMode extends React.Component {
|
2017-07-25 11:53:54 -04:00
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
|
|
|
'activateTool',
|
|
|
|
'deactivateTool',
|
|
|
|
'onScroll'
|
|
|
|
]);
|
2017-08-30 18:43:34 -04:00
|
|
|
this.blob = new Blobbiness(this.props.onUpdateSvg);
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
componentDidMount () {
|
2017-08-15 18:11:02 -04:00
|
|
|
if (this.props.isEraserModeActive) {
|
2017-07-25 11:53:54 -04:00
|
|
|
this.activateTool();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
componentWillReceiveProps (nextProps) {
|
2017-08-15 18:11:02 -04:00
|
|
|
if (nextProps.isEraserModeActive && !this.props.isEraserModeActive) {
|
2017-07-25 11:53:54 -04:00
|
|
|
this.activateTool();
|
2017-08-15 18:11:02 -04:00
|
|
|
} else if (!nextProps.isEraserModeActive && this.props.isEraserModeActive) {
|
2017-07-25 11:53:54 -04:00
|
|
|
this.deactivateTool();
|
2017-08-15 18:11:02 -04:00
|
|
|
} else if (nextProps.isEraserModeActive && this.props.isEraserModeActive) {
|
2017-08-16 18:16:37 -04:00
|
|
|
this.blob.setOptions({isEraser: true, ...nextProps.eraserModeState});
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldComponentUpdate () {
|
2017-08-23 10:56:37 -04:00
|
|
|
return false; // Static component, for now
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
activateTool () {
|
2017-07-27 11:45:41 -04:00
|
|
|
this.props.canvas.addEventListener('mousewheel', this.onScroll);
|
2017-07-25 11:53:54 -04:00
|
|
|
|
2017-08-16 18:16:37 -04:00
|
|
|
this.blob.activateTool({isEraser: true, ...this.props.eraserModeState});
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
deactivateTool () {
|
2017-07-27 11:45:41 -04:00
|
|
|
this.props.canvas.removeEventListener('mousewheel', this.onScroll);
|
2017-07-25 11:53:54 -04:00
|
|
|
this.blob.deactivateTool();
|
|
|
|
}
|
|
|
|
onScroll (event) {
|
2017-07-27 16:41:41 -04:00
|
|
|
event.preventDefault();
|
2017-07-25 11:53:54 -04:00
|
|
|
if (event.deltaY < 0) {
|
2017-08-15 18:11:02 -04:00
|
|
|
this.props.changeBrushSize(this.props.eraserModeState.brushSize + 1);
|
|
|
|
} else if (event.deltaY > 0 && this.props.eraserModeState.brushSize > 1) {
|
|
|
|
this.props.changeBrushSize(this.props.eraserModeState.brushSize - 1);
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
render () {
|
|
|
|
return (
|
2017-08-17 18:13:24 -04:00
|
|
|
<EraserModeComponent onMouseDown={this.props.handleMouseDown} />
|
2017-07-25 11:53:54 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 18:11:02 -04:00
|
|
|
EraserMode.propTypes = {
|
2017-07-27 16:41:41 -04:00
|
|
|
canvas: PropTypes.instanceOf(Element).isRequired,
|
2017-07-25 11:53:54 -04:00
|
|
|
changeBrushSize: PropTypes.func.isRequired,
|
2017-08-15 18:11:02 -04:00
|
|
|
eraserModeState: PropTypes.shape({
|
2017-07-25 11:53:54 -04:00
|
|
|
brushSize: PropTypes.number.isRequired
|
|
|
|
}),
|
2017-08-17 18:13:24 -04:00
|
|
|
handleMouseDown: PropTypes.func.isRequired,
|
2017-08-30 18:43:34 -04:00
|
|
|
isEraserModeActive: PropTypes.bool.isRequired,
|
|
|
|
onUpdateSvg: PropTypes.func.isRequired
|
2017-07-25 11:53:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2017-08-22 18:20:47 -04:00
|
|
|
eraserModeState: state.scratchPaint.eraserMode,
|
|
|
|
isEraserModeActive: state.scratchPaint.mode === Modes.ERASER
|
2017-07-25 11:53:54 -04:00
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
changeBrushSize: brushSize => {
|
2017-08-16 18:16:37 -04:00
|
|
|
dispatch(changeBrushSize(brushSize));
|
2017-08-17 18:13:24 -04:00
|
|
|
},
|
|
|
|
handleMouseDown: () => {
|
|
|
|
dispatch(changeMode(Modes.ERASER));
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-07-27 11:45:41 -04:00
|
|
|
export default connect(
|
2017-07-25 11:53:54 -04:00
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
2017-08-15 18:11:02 -04:00
|
|
|
)(EraserMode);
|