diff --git a/src/components/image/GlobalImageDrop.js b/src/components/image/GlobalImageDrop.js index 5287b72..24588fe 100644 --- a/src/components/image/GlobalImageDrop.js +++ b/src/components/image/GlobalImageDrop.js @@ -8,22 +8,30 @@ import ConfirmModal from "../../modals/ConfirmModal"; import { createMapFromFile } from "../../helpers/map"; import { createTokenFromFile } from "../../helpers/token"; +import { + createTokenState, + clientPositionToMapPosition, +} from "../../helpers/token"; +import Vector2 from "../../helpers/Vector2"; import { useAuth } from "../../contexts/AuthContext"; import { useMapData } from "../../contexts/MapDataContext"; import { useTokenData } from "../../contexts/TokenDataContext"; import { useAssets } from "../../contexts/AssetsContext"; +import { useMapStage } from "../../contexts/MapStageContext"; import useImageDrop from "../../hooks/useImageDrop"; -function GlobalImageDrop({ children, onMapTokensStateCreate }) { +function GlobalImageDrop({ children, onMapChange, onMapTokensStateCreate }) { const { addToast } = useToasts(); const { userId } = useAuth(); - const { addMap } = useMapData(); + const { addMap, getMapState } = useMapData(); const { addToken } = useTokenData(); const { addAssets } = useAssets(); + const mapStageRef = useMapStage(); + const [isLargeImageWarningModalOpen, setShowLargeImageWarning] = useState( false ); @@ -80,24 +88,57 @@ function GlobalImageDrop({ children, onMapTokensStateCreate }) { async function handleMaps() { setIsLoading(true); + let maps = []; for (let file of droppedImagesRef.current) { const { map, assets } = await createMapFromFile(file, userId); await addMap(map); await addAssets(assets); + maps.push(map); } + + // Change map if only 1 dropped + if (maps.length === 1) { + const mapState = await getMapState(maps[0].id); + onMapChange(maps[0], mapState); + } + setIsLoading(false); droppedImagesRef.current = undefined; } async function handleTokens() { setIsLoading(true); + // Keep track of tokens so we can add them to the map + let tokens = []; for (let file of droppedImagesRef.current) { const { token, assets } = await createTokenFromFile(file, userId); await addToken(token); await addAssets(assets); + tokens.push(token); } setIsLoading(false); droppedImagesRef.current = undefined; + + const dropPosition = dropPositionRef.current; + const mapStage = mapStageRef.current; + if (mapStage && dropPosition) { + const mapPosition = clientPositionToMapPosition(mapStage, dropPosition); + if (mapPosition) { + let tokenStates = []; + let offset = new Vector2(0, 0); + for (let token of tokens) { + if (token) { + tokenStates.push( + createTokenState(token, Vector2.add(mapPosition, offset), userId) + ); + offset = Vector2.add(offset, 0.01); + } + } + if (tokenStates.length > 0) { + onMapTokensStateCreate(tokenStates); + } + } + } } function handleMapsOver() { @@ -131,31 +172,39 @@ function GlobalImageDrop({ children, onMapTokensStateCreate }) { - - {"Drop image to import as a map"} + + Drop as map - - {"Drop image to import as a token"} + + Drop as token diff --git a/src/components/image/ImageDrop.js b/src/components/image/ImageDrop.js index f93aa97..d46e14f 100644 --- a/src/components/image/ImageDrop.js +++ b/src/components/image/ImageDrop.js @@ -25,7 +25,7 @@ function ImageDrop({ onDrop, dropText, children }) { }} {...overlayListeners} > - + {dropText || "Drop image to import"} diff --git a/src/network/NetworkedMapAndTokens.js b/src/network/NetworkedMapAndTokens.js index 9f587aa..2c65a80 100644 --- a/src/network/NetworkedMapAndTokens.js +++ b/src/network/NetworkedMapAndTokens.js @@ -459,7 +459,10 @@ function NetworkedMapAndTokens({ session }) { } return ( - +