2020-04-26 17:12:15 +10:00
|
|
|
import React, { useState } from "react";
|
2020-04-26 12:39:56 +10:00
|
|
|
import { Flex, Image as UIImage, IconButton, Box, Text } from "theme-ui";
|
2020-04-23 20:32:33 +10:00
|
|
|
|
|
|
|
|
import RemoveMapIcon from "../../icons/RemoveMapIcon";
|
|
|
|
|
import ResetMapIcon from "../../icons/ResetMapIcon";
|
|
|
|
|
import ExpandMoreDotIcon from "../../icons/ExpandMoreDotIcon";
|
|
|
|
|
|
2020-04-24 15:50:05 +10:00
|
|
|
import useDataSource from "../../helpers/useDataSource";
|
|
|
|
|
import { mapSources as defaultMapSources } from "../../maps";
|
|
|
|
|
|
2020-04-23 22:12:50 +10:00
|
|
|
function MapTile({
|
|
|
|
|
map,
|
2020-04-26 17:12:15 +10:00
|
|
|
mapState,
|
2020-04-23 22:12:50 +10:00
|
|
|
isSelected,
|
|
|
|
|
onMapSelect,
|
|
|
|
|
onMapRemove,
|
|
|
|
|
onMapReset,
|
|
|
|
|
onSubmit,
|
|
|
|
|
}) {
|
2020-04-24 15:50:05 +10:00
|
|
|
const mapSource = useDataSource(map, defaultMapSources);
|
2020-04-23 20:32:33 +10:00
|
|
|
const [isMapTileMenuOpen, setIsTileMenuOpen] = useState(false);
|
2020-04-24 15:50:05 +10:00
|
|
|
const isDefault = map.type === "default";
|
2020-04-26 17:12:15 +10:00
|
|
|
const hasMapState =
|
|
|
|
|
mapState &&
|
|
|
|
|
(Object.values(mapState.tokens).length > 0 ||
|
|
|
|
|
mapState.drawActions.length > 0);
|
2020-04-23 20:32:33 +10:00
|
|
|
|
|
|
|
|
const expandButton = (
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="Show Map Actions"
|
|
|
|
|
title="Show Map Actions"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setIsTileMenuOpen(true);
|
|
|
|
|
}}
|
|
|
|
|
bg="overlay"
|
|
|
|
|
sx={{ borderRadius: "50%" }}
|
|
|
|
|
m={1}
|
|
|
|
|
>
|
|
|
|
|
<ExpandMoreDotIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
function removeButton(map) {
|
|
|
|
|
return (
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="Remove Map"
|
|
|
|
|
title="Remove Map"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setIsTileMenuOpen(false);
|
|
|
|
|
onMapRemove(map.id);
|
|
|
|
|
}}
|
|
|
|
|
bg="overlay"
|
|
|
|
|
sx={{ borderRadius: "50%" }}
|
|
|
|
|
m={1}
|
|
|
|
|
>
|
|
|
|
|
<RemoveMapIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function resetButton(map) {
|
|
|
|
|
return (
|
|
|
|
|
<IconButton
|
|
|
|
|
aria-label="Reset Map"
|
|
|
|
|
title="Reset Map"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setIsTileMenuOpen(false);
|
|
|
|
|
onMapReset(map.id);
|
|
|
|
|
}}
|
|
|
|
|
bg="overlay"
|
|
|
|
|
sx={{ borderRadius: "50%" }}
|
|
|
|
|
m={1}
|
|
|
|
|
>
|
|
|
|
|
<ResetMapIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex
|
|
|
|
|
key={map.id}
|
|
|
|
|
sx={{
|
|
|
|
|
borderColor: "primary",
|
|
|
|
|
borderStyle: isSelected ? "solid" : "none",
|
|
|
|
|
borderWidth: "4px",
|
|
|
|
|
position: "relative",
|
|
|
|
|
width: "150px",
|
|
|
|
|
height: "150px",
|
|
|
|
|
borderRadius: "4px",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
cursor: "pointer",
|
|
|
|
|
}}
|
|
|
|
|
m={2}
|
|
|
|
|
bg="muted"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setIsTileMenuOpen(false);
|
2020-04-26 19:22:30 +10:00
|
|
|
if (!isSelected) {
|
|
|
|
|
onMapSelect(map);
|
|
|
|
|
}
|
2020-04-23 20:32:33 +10:00
|
|
|
}}
|
2020-04-23 22:12:50 +10:00
|
|
|
onDoubleClick={(e) => {
|
|
|
|
|
if (!isMapTileMenuOpen) {
|
|
|
|
|
onSubmit(e);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2020-04-23 20:32:33 +10:00
|
|
|
>
|
|
|
|
|
<UIImage
|
|
|
|
|
sx={{ width: "100%", height: "100%", objectFit: "contain" }}
|
2020-04-24 15:50:05 +10:00
|
|
|
src={mapSource}
|
2020-04-23 20:32:33 +10:00
|
|
|
/>
|
2020-04-26 12:39:56 +10:00
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
background:
|
2020-04-26 19:04:18 +10:00
|
|
|
"linear-gradient(to bottom, rgba(0,0,0,0) 70%, rgba(0,0,0,0.65) 100%);",
|
2020-04-26 12:39:56 +10:00
|
|
|
alignItems: "flex-end",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
}}
|
|
|
|
|
p={2}
|
|
|
|
|
>
|
2020-04-26 19:04:18 +10:00
|
|
|
<Text as="p" variant="heading" color="hsl(210, 50%, 96%)">
|
2020-04-26 12:39:56 +10:00
|
|
|
{map.name}
|
|
|
|
|
</Text>
|
|
|
|
|
</Flex>
|
2020-04-23 20:32:33 +10:00
|
|
|
{/* Show expand button only if both reset and remove is available */}
|
|
|
|
|
{isSelected && (
|
|
|
|
|
<Box sx={{ position: "absolute", top: 0, right: 0 }}>
|
2020-04-24 15:50:05 +10:00
|
|
|
{isDefault && hasMapState && resetButton(map)}
|
|
|
|
|
{!isDefault && hasMapState && !isMapTileMenuOpen && expandButton}
|
|
|
|
|
{!isDefault && !hasMapState && removeButton(map)}
|
2020-04-23 20:32:33 +10:00
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
{/* Tile menu for two actions */}
|
2020-04-24 15:50:05 +10:00
|
|
|
{!isDefault && isMapTileMenuOpen && isSelected && (
|
2020-04-23 20:32:33 +10:00
|
|
|
<Flex
|
|
|
|
|
sx={{
|
|
|
|
|
position: "absolute",
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
}}
|
|
|
|
|
bg="muted"
|
|
|
|
|
onClick={() => setIsTileMenuOpen(false)}
|
|
|
|
|
>
|
2020-04-24 15:50:05 +10:00
|
|
|
{!isDefault && removeButton(map)}
|
2020-04-23 20:32:33 +10:00
|
|
|
{hasMapState && resetButton(map)}
|
|
|
|
|
</Flex>
|
|
|
|
|
)}
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default MapTile;
|