diff --git a/src/components/map/MapToken.js b/src/components/map/MapToken.js index 82b2f53..1a87a38 100644 --- a/src/components/map/MapToken.js +++ b/src/components/map/MapToken.js @@ -1,5 +1,5 @@ import React, { useContext, useState, useEffect, useRef } from "react"; -import { Image as KonvaImage } from "react-konva"; +import { Image as KonvaImage, Group } from "react-konva"; import useImage from "use-image"; import useDataSource from "../../helpers/useDataSource"; @@ -8,6 +8,9 @@ import useDebounce from "../../helpers/useDebounce"; import AuthContext from "../../contexts/AuthContext"; import MapInteractionContext from "../../contexts/MapInteractionContext"; +import TokenStatus from "../token/TokenStatus"; +import TokenLabel from "../token/TokenLabel"; + import { tokenSources, unknownSource } from "../../tokens"; function MapToken({ @@ -57,32 +60,49 @@ function MapToken({ const imageRef = useRef(); useEffect(() => { const image = imageRef.current; - if (image) { + if (image && tokenSourceStatus === "loaded") { image.cache({ pixelRatio: debouncedStageScale, }); image.drawHitFromCache(); // Force redraw - image.parent.draw(); + image.getLayer().draw(); } }, [debouncedStageScale, tokenWidth, tokenHeight, tokenSourceStatus]); return ( - setPreventMapInteraction(true)} onMouseUp={() => setPreventMapInteraction(false)} onTouchStart={() => setPreventMapInteraction(true)} onTouchEnd={() => setPreventMapInteraction(false)} - /> + onClick={handleClick} + onDragEnd={handleDragEnd} + > + + + + ); } diff --git a/src/components/token/ProxyToken.js b/src/components/token/ProxyToken.js index a0dd52c..07f85ea 100644 --- a/src/components/token/ProxyToken.js +++ b/src/components/token/ProxyToken.js @@ -5,9 +5,6 @@ import interact from "interactjs"; import usePortal from "../../helpers/usePortal"; -import TokenLabel from "./TokenLabel"; -import TokenStatus from "./TokenStatus"; - import MapStageContext from "../../contexts/MapStageContext"; /** @@ -21,15 +18,9 @@ import MapStageContext from "../../contexts/MapStageContext"; * @param {string} tokenClassName The class name to attach the interactjs handler to * @param {onProxyDragEnd} onProxyDragEnd Called when the proxy token is dropped * @param {Object} tokens An optional mapping of tokens to use as a base when calling OnProxyDragEnd - * @param {Object} disabledTokens An optional mapping of tokens that shouldn't allow movement */ -function ProxyToken({ - tokenClassName, - onProxyDragEnd, - tokens, - disabledTokens, -}) { +function ProxyToken({ tokenClassName, onProxyDragEnd, tokens }) { const proxyContainer = usePortal("root"); const [imageSource, setImageSource] = useState(""); @@ -39,11 +30,9 @@ function ProxyToken({ // Store the tokens in a ref and access in the interactjs loop // This is needed to stop interactjs from creating multiple listeners const tokensRef = useRef(tokens); - const disabledTokensRef = useRef(disabledTokens); useEffect(() => { tokensRef.current = tokens; - disabledTokensRef.current = disabledTokens; - }, [tokens, disabledTokens]); + }, [tokens]); const proxyOnMap = useRef(false); const mapStageRef = useContext(MapStageContext); @@ -54,9 +43,6 @@ function ProxyToken({ start: (event) => { let target = event.target; const id = target.dataset.id; - if (id in disabledTokensRef.current) { - return; - } // Hide the token and copy it's image to the proxy target.parentElement.style.opacity = "0.25"; @@ -108,9 +94,6 @@ function ProxyToken({ end: (event) => { let target = event.target; const id = target.dataset.id; - if (id in disabledTokensRef.current) { - return; - } let proxy = proxyRef.current; if (proxy) { const mapStage = mapStageRef.current; @@ -187,12 +170,6 @@ function ProxyToken({ width: "100%", }} /> - {tokens[tokenId] && tokens[tokenId].statuses && ( - - )} - {tokens[tokenId] && tokens[tokenId].label && ( - - )} , proxyContainer @@ -201,7 +178,6 @@ function ProxyToken({ ProxyToken.defaultProps = { tokens: {}, - disabledTokens: {}, }; export default ProxyToken; diff --git a/src/components/token/TokenLabel.js b/src/components/token/TokenLabel.js index ec62498..8d0e5f0 100644 --- a/src/components/token/TokenLabel.js +++ b/src/components/token/TokenLabel.js @@ -1,60 +1,49 @@ -import React from "react"; -import { Box, Text } from "theme-ui"; +import React, { useRef, useEffect, useState } from "react"; +import { Rect, Text, Group } from "react-konva"; + +function TokenLabel({ tokenState, width, height }) { + const fontSize = height / 6 / tokenState.size; + const paddingY = height / 16 / tokenState.size; + const paddingX = height / 8 / tokenState.size; + + const [rectWidth, setRectWidth] = useState(0); + useEffect(() => { + const text = textRef.current; + if (text && tokenState.label) { + setRectWidth(text.getTextWidth() + paddingX); + } else { + setRectWidth(0); + } + }, [tokenState.label, paddingX]); + + const textRef = useRef(); -function TokenLabel({ token }) { return ( - - {/* Use SVG so text is scaled with token size */} - - - - - {token.label} - - - - - + + + + ); } diff --git a/src/components/token/TokenStatus.js b/src/components/token/TokenStatus.js index 2374dc5..81c1acd 100644 --- a/src/components/token/TokenStatus.js +++ b/src/components/token/TokenStatus.js @@ -1,46 +1,23 @@ import React from "react"; -import { Box } from "theme-ui"; +import { Circle, Group } from "react-konva"; import colors from "../../helpers/colors"; -function TokenStatus({ token }) { +function TokenStatus({ tokenState, width, height }) { return ( - - {token.statuses.map((status, index) => ( - - - - - + + {tokenState.statuses.map((status, index) => ( + ))} - + ); }