From f22037e54016cd0a1d5636ece395c97dfa9f5e66 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Thu, 31 Aug 2017 22:03:58 +0200 Subject: Determine hover tile validity --- .../map/layers/HoverTileLayerComponent.js | 56 +++++++++++++++------- 1 file changed, 38 insertions(+), 18 deletions(-) (limited to 'src/components') diff --git a/src/components/map/layers/HoverTileLayerComponent.js b/src/components/map/layers/HoverTileLayerComponent.js index 691b9733..a4d9446a 100644 --- a/src/components/map/layers/HoverTileLayerComponent.js +++ b/src/components/map/layers/HoverTileLayerComponent.js @@ -3,25 +3,45 @@ import {Layer} from "react-konva"; import HoverTile from "../elements/HoverTile"; import {TILE_SIZE_IN_PIXELS} from "../MapConstants"; -const HoverTileLayerComponent = ({mainGroupX, mainGroupY, mouseX, mouseY, currentRoomInConstruction, isValid, onClick}) => { - if (currentRoomInConstruction === -1) { - return +class HoverTileLayerComponent extends React.Component { + state = { + positionX: -1, + positionY: -1, + validity: false, + }; + + componentDidUpdate() { + if (this.props.currentRoomInConstruction === -1) { + return; + } + + const positionX = Math.floor((this.props.mouseX - this.props.mainGroupX) / TILE_SIZE_IN_PIXELS); + const positionY = Math.floor((this.props.mouseY - this.props.mainGroupY) / TILE_SIZE_IN_PIXELS); + + if (positionX !== this.state.positionX || positionY !== this.state.positionY) { + this.setState({positionX, positionY, validity: this.props.isValid(positionX, positionY)}); + } } - const positionX = Math.floor((mouseX - mainGroupX) / TILE_SIZE_IN_PIXELS); - const positionY = Math.floor((mouseY - mainGroupY) / TILE_SIZE_IN_PIXELS); - const pixelX = positionX * TILE_SIZE_IN_PIXELS + mainGroupX; - const pixelY = positionY * TILE_SIZE_IN_PIXELS + mainGroupY; - const validity = isValid(positionX, positionY); - - return ( - - onClick(positionX, positionY)} - /> - - ); -}; + render() { + if (this.props.currentRoomInConstruction === -1) { + return ; + } + + const positionX = Math.floor((this.props.mouseX - this.props.mainGroupX) / TILE_SIZE_IN_PIXELS); + const positionY = Math.floor((this.props.mouseY - this.props.mainGroupY) / TILE_SIZE_IN_PIXELS); + const pixelX = positionX * TILE_SIZE_IN_PIXELS + this.props.mainGroupX; + const pixelY = positionY * TILE_SIZE_IN_PIXELS + this.props.mainGroupY; + + return ( + + this.props.onClick(positionX, positionY)} + /> + + ); + } +} export default HoverTileLayerComponent; -- cgit v1.2.3