Added sending of default maps

This commit is contained in:
Mitchell McCaffrey 2020-04-23 13:31:54 +10:00
parent 22c5b5cf75
commit 8681864ddc
3 changed files with 61 additions and 16 deletions

View File

@ -3,24 +3,34 @@ import { Flex, Image as UIImage } from "theme-ui";
import AddIcon from "../../icons/AddIcon"; import AddIcon from "../../icons/AddIcon";
function MapSelect({ maps, onMapAdd }) { function MapSelect({ maps, selectedMap, onMapSelected, onMapAdd }) {
const tileProps = { const tileProps = {
m: 2, m: 2,
sx: {
width: "150px",
height: "150px",
borderRadius: "4px",
justifyContent: "center",
alignItems: "center",
},
bg: "muted", bg: "muted",
}; };
function tile(map) { const tileStyle = {
width: "150px",
height: "150px",
borderRadius: "4px",
justifyContent: "center",
alignItems: "center",
cursor: "pointer",
};
// TODO move from passing index in to using DB ID
function tile(map, index) {
return ( return (
<Flex // TODO: use DB key <Flex // TODO: use DB key
key={map.source} key={map.source}
sx={{
borderColor: "primary",
borderStyle: index === selectedMap ? "solid" : "none",
borderWidth: "4px",
...tileStyle,
}}
{...tileProps} {...tileProps}
onClick={() => onMapSelected(index)}
> >
<UIImage <UIImage
sx={{ width: "100%", objectFit: "contain" }} sx={{ width: "100%", objectFit: "contain" }}
@ -44,8 +54,23 @@ function MapSelect({ maps, onMapAdd }) {
flexGrow: 1, flexGrow: 1,
}} }}
> >
{maps.map(tile)} {maps.map((map, index) => tile(map, index))}
<Flex onClick={onMapAdd} {...tileProps}> <Flex
onClick={onMapAdd}
sx={{
":hover": {
color: "primary",
},
":focus": {
outline: "none",
},
":active": {
color: "secondary",
},
...tileStyle,
}}
{...tileProps}
>
<AddIcon /> <AddIcon />
</Flex> </Flex>
</Flex> </Flex>

View File

@ -82,6 +82,15 @@ function AddMapModal({ isOpen, onRequestClose, onDone }) {
} }
} }
function handleMapSelect(mapId) {
setCurrentMap(mapId);
setGridX(maps[mapId].gridX);
setGridY(maps[mapId].gridY);
}
/**
* Drag and Drop
*/
const [dragging, setDragging] = useState(false); const [dragging, setDragging] = useState(false);
function handleImageDragEnter(event) { function handleImageDragEnter(event) {
event.preventDefault(); event.preventDefault();
@ -130,7 +139,12 @@ function AddMapModal({ isOpen, onRequestClose, onDone }) {
<Label pt={2} pb={1}> <Label pt={2} pb={1}>
Add map Add map
</Label> </Label>
<MapSelect maps={maps} onMapAdd={openImageDialog} /> <MapSelect
maps={maps}
onMapAdd={openImageDialog}
selectedMap={currentMap}
onMapSelected={handleMapSelect}
/>
<Flex> <Flex>
<Box mb={2} mr={1} sx={{ flexGrow: 1 }}> <Box mb={2} mr={1} sx={{ flexGrow: 1 }}>
<Label htmlFor="gridX">Columns</Label> <Label htmlFor="gridX">Columns</Label>

View File

@ -39,7 +39,7 @@ function Game() {
function handleMapChange(newMap) { function handleMapChange(newMap) {
setMap(newMap); setMap(newMap);
for (let peer of Object.values(peers)) { for (let peer of Object.values(peers)) {
peer.connection.send({ id: "map", data: map }); peer.connection.send({ id: "map", data: newMap });
} }
} }
@ -156,9 +156,15 @@ function Game() {
} }
} }
if (data.id === "map") { if (data.id === "map") {
const file = new Blob([data.data.file]); // If we have a file convert it to a url
const source = URL.createObjectURL(file); // TODO clear the file url of the previous map if it's a blob
setMap({ ...data.data, file, source }); if (data.data.file) {
const file = new Blob([data.data.file]);
const source = URL.createObjectURL(file);
setMap({ ...data.data, file, source });
} else {
setMap(data.data);
}
} }
if (data.id === "tokenEdit") { if (data.id === "tokenEdit") {
setMapTokens((prevMapTokens) => ({ setMapTokens((prevMapTokens) => ({