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';
|
|
|
|
import Blobbiness from '../modes/blob';
|
|
|
|
import EraserModeReducer from '../reducers/eraser-mode';
|
2017-07-25 11:53:54 -04:00
|
|
|
|
2017-08-15 18:11:02 -04:00
|
|
|
class EraserMode extends React.Component {
|
|
|
|
static get MODE () {
|
|
|
|
return Modes.ERASER;
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
constructor (props) {
|
|
|
|
super(props);
|
|
|
|
bindAll(this, [
|
|
|
|
'activateTool',
|
|
|
|
'deactivateTool',
|
|
|
|
'onScroll'
|
|
|
|
]);
|
2017-08-15 18:11:02 -04:00
|
|
|
this.blob = new Blobbiness();
|
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) {
|
|
|
|
this.blob.setOptions(nextProps.eraserModeState);
|
2017-07-25 11:53:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
shouldComponentUpdate () {
|
|
|
|
return false; // Logic only component
|
|
|
|
}
|
|
|
|
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 15:34:33 -04:00
|
|
|
this.blob.activateTool(true /* isEraser */, 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-15 18:11:02 -04:00
|
|
|
<div>Eraser Mode</div>
|
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-15 18:11:02 -04:00
|
|
|
isEraserModeActive: PropTypes.bool.isRequired
|
2017-07-25 11:53:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2017-08-15 18:11:02 -04:00
|
|
|
eraserModeState: state.eraserMode,
|
|
|
|
isEraserModeActive: state.mode === EraserMode.MODE
|
2017-07-25 11:53:54 -04:00
|
|
|
});
|
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
changeBrushSize: brushSize => {
|
2017-08-15 18:11:02 -04:00
|
|
|
dispatch(EraserModeReducer.changeBrushSize(brushSize));
|
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);
|