2020-04-23 13:31:39 -07:00
|
|
|
import React from "react";
|
2020-04-24 00:28:00 -07:00
|
|
|
import { CSSTransition, TransitionGroup } from "react-transition-group";
|
2020-04-23 13:31:39 -07:00
|
|
|
import gql from "graphql-tag";
|
|
|
|
import { useQuery } from "@apollo/react-hooks";
|
2020-04-24 23:13:28 -07:00
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Flex,
|
|
|
|
Icon,
|
|
|
|
IconButton,
|
|
|
|
Image,
|
|
|
|
PseudoBox,
|
|
|
|
Spinner,
|
|
|
|
Text,
|
|
|
|
Tooltip,
|
|
|
|
} from "@chakra-ui/core";
|
2020-04-23 13:31:39 -07:00
|
|
|
|
|
|
|
import { Delay } from "./util";
|
2020-04-25 05:29:27 -07:00
|
|
|
import OutfitResetModal from "./OutfitResetModal";
|
2020-04-25 04:33:05 -07:00
|
|
|
import SpeciesColorPicker from "./SpeciesColorPicker";
|
2020-04-23 13:31:39 -07:00
|
|
|
|
2020-04-24 00:28:00 -07:00
|
|
|
import "./OutfitPreview.css";
|
|
|
|
|
2020-04-24 20:28:40 -07:00
|
|
|
export const itemAppearanceFragment = gql`
|
|
|
|
fragment AppearanceForOutfitPreview on Appearance {
|
|
|
|
layers {
|
|
|
|
id
|
|
|
|
imageUrl(size: SIZE_600)
|
|
|
|
zone {
|
|
|
|
id
|
|
|
|
depth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restrictedZones {
|
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-04-25 04:33:05 -07:00
|
|
|
function OutfitPreview({ outfitState, dispatchToOutfit }) {
|
2020-04-24 19:16:24 -07:00
|
|
|
const { wornItemIds, speciesId, colorId } = outfitState;
|
2020-04-25 04:33:05 -07:00
|
|
|
const [hasFocus, setHasFocus] = React.useState(false);
|
2020-04-25 05:29:27 -07:00
|
|
|
const [showResetModal, setShowResetModal] = React.useState(false);
|
2020-04-24 19:16:24 -07:00
|
|
|
|
2020-04-23 13:31:39 -07:00
|
|
|
const { loading, error, data } = useQuery(
|
|
|
|
gql`
|
2020-04-24 19:16:24 -07:00
|
|
|
query($wornItemIds: [ID!]!, $speciesId: ID!, $colorId: ID!) {
|
2020-04-23 14:23:46 -07:00
|
|
|
petAppearance(speciesId: $speciesId, colorId: $colorId) {
|
2020-04-24 20:28:40 -07:00
|
|
|
...AppearanceForOutfitPreview
|
2020-04-23 14:23:46 -07:00
|
|
|
}
|
|
|
|
|
2020-04-24 19:16:24 -07:00
|
|
|
items(ids: $wornItemIds) {
|
2020-04-23 13:31:39 -07:00
|
|
|
id
|
|
|
|
appearanceOn(speciesId: $speciesId, colorId: $colorId) {
|
2020-04-24 20:28:40 -07:00
|
|
|
...AppearanceForOutfitPreview
|
2020-04-23 13:31:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-24 20:28:40 -07:00
|
|
|
${itemAppearanceFragment}
|
2020-04-23 13:31:39 -07:00
|
|
|
`,
|
2020-04-23 23:43:39 -07:00
|
|
|
{
|
2020-04-24 19:16:24 -07:00
|
|
|
variables: { wornItemIds, speciesId, colorId },
|
2020-04-23 23:43:39 -07:00
|
|
|
}
|
2020-04-23 13:31:39 -07:00
|
|
|
);
|
|
|
|
|
2020-04-24 23:13:28 -07:00
|
|
|
const visibleLayers = getVisibleLayers(data);
|
|
|
|
const [downloadImageUrl, prepareDownload] = useDownloadableImage(
|
|
|
|
visibleLayers
|
|
|
|
);
|
|
|
|
|
2020-04-23 13:31:39 -07:00
|
|
|
if (error) {
|
|
|
|
return (
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Text color="gray.50" d="flex" alignItems="center">
|
|
|
|
<Icon name="warning" />
|
|
|
|
<Box width={2} />
|
|
|
|
Could not load preview. Try again?
|
|
|
|
</Text>
|
|
|
|
</FullScreenCenter>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-23 15:19:36 -07:00
|
|
|
return (
|
2020-04-24 23:13:28 -07:00
|
|
|
<PseudoBox role="group" pos="relative" height="100%" width="100%">
|
2020-04-24 00:28:00 -07:00
|
|
|
<TransitionGroup>
|
2020-04-24 23:13:28 -07:00
|
|
|
{visibleLayers.map((layer) => (
|
2020-04-24 00:28:00 -07:00
|
|
|
<CSSTransition
|
|
|
|
key={layer.id}
|
|
|
|
classNames={{
|
|
|
|
exit: "outfit-preview-layer-exit",
|
|
|
|
exitActive: "outfit-preview-layer-exit-active",
|
|
|
|
}}
|
|
|
|
timeout={200}
|
|
|
|
>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Image
|
|
|
|
src={layer.imageUrl}
|
|
|
|
objectFit="contain"
|
|
|
|
maxWidth="100%"
|
|
|
|
maxHeight="100%"
|
|
|
|
className="outfit-preview-layer-image"
|
2020-04-24 23:13:28 -07:00
|
|
|
// This sets up the cache to not need to reload images during
|
|
|
|
// download!
|
2020-04-25 03:02:11 -07:00
|
|
|
// TODO: Re-enable this once we get our change into Chakra
|
|
|
|
// main. For now, this will make Downloads a bit slower, which
|
|
|
|
// is fine!
|
|
|
|
// crossOrigin="Anonymous"
|
2020-04-24 00:28:00 -07:00
|
|
|
/>
|
|
|
|
</FullScreenCenter>
|
|
|
|
</CSSTransition>
|
|
|
|
))}
|
|
|
|
</TransitionGroup>
|
2020-04-23 15:19:36 -07:00
|
|
|
{loading && (
|
|
|
|
<Delay>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Box
|
|
|
|
width="100%"
|
|
|
|
height="100%"
|
|
|
|
backgroundColor="gray.900"
|
|
|
|
opacity="0.8"
|
|
|
|
/>
|
|
|
|
</FullScreenCenter>
|
|
|
|
<FullScreenCenter>
|
|
|
|
<Spinner color="green.400" size="xl" />
|
|
|
|
</FullScreenCenter>
|
|
|
|
</Delay>
|
|
|
|
)}
|
2020-04-25 05:29:27 -07:00
|
|
|
<Box pos="absolute" left="2" top="2">
|
|
|
|
<IconButton
|
|
|
|
icon="arrow-back"
|
|
|
|
aria-label="Leave this outfit"
|
|
|
|
variant="unstyled"
|
|
|
|
d="flex"
|
|
|
|
alignItems="center"
|
|
|
|
justifyContent="center"
|
|
|
|
color="gray.50"
|
|
|
|
opacity={hasFocus ? 1 : 0}
|
|
|
|
transition="all 0.2s"
|
|
|
|
_groupHover={{
|
|
|
|
opacity: 1,
|
|
|
|
}}
|
|
|
|
onFocus={() => setHasFocus(true)}
|
|
|
|
onBlur={() => setHasFocus(false)}
|
|
|
|
onClick={() => setShowResetModal(true)}
|
|
|
|
/>
|
|
|
|
</Box>
|
2020-04-25 02:32:41 -07:00
|
|
|
<Box
|
2020-04-25 04:33:05 -07:00
|
|
|
// Bottom toolbar on small screens, top on large screens
|
2020-04-25 02:32:41 -07:00
|
|
|
pos="absolute"
|
2020-04-25 04:33:05 -07:00
|
|
|
left="0"
|
|
|
|
right="0"
|
2020-04-25 02:32:41 -07:00
|
|
|
bottom={{ base: "2", lg: "auto" }}
|
|
|
|
top={{ base: "auto", lg: "2" }}
|
2020-04-25 04:33:05 -07:00
|
|
|
// Grid layout for the content!
|
|
|
|
display="grid"
|
|
|
|
gridTemplateAreas={`"space picker download"`}
|
|
|
|
gridTemplateColumns="minmax(0, 1fr) auto 1fr"
|
|
|
|
alignItems="center"
|
2020-04-25 02:32:41 -07:00
|
|
|
>
|
2020-04-25 04:33:05 -07:00
|
|
|
<Box gridArea="space"></Box>
|
|
|
|
<PseudoBox
|
|
|
|
gridArea="picker"
|
|
|
|
opacity={hasFocus ? 1 : 0}
|
|
|
|
_groupHover={{ opacity: 1 }}
|
|
|
|
transition="opacity 0.2s"
|
|
|
|
>
|
|
|
|
<SpeciesColorPicker
|
|
|
|
outfitState={outfitState}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
|
|
|
onFocus={() => setHasFocus(true)}
|
|
|
|
onBlur={() => setHasFocus(false)}
|
2020-04-25 02:32:41 -07:00
|
|
|
/>
|
2020-04-25 04:33:05 -07:00
|
|
|
</PseudoBox>
|
|
|
|
<Flex gridArea="download" justify="flex-end">
|
|
|
|
<Tooltip label="Download" placement="left">
|
|
|
|
<IconButton
|
|
|
|
icon="download"
|
|
|
|
aria-label="Download"
|
|
|
|
isRound
|
|
|
|
as="a"
|
|
|
|
// eslint-disable-next-line no-script-url
|
|
|
|
href={downloadImageUrl || "javascript:void 0"}
|
|
|
|
download={(outfitState.name || "Outfit") + ".png"}
|
|
|
|
onMouseEnter={prepareDownload}
|
|
|
|
onFocus={() => {
|
|
|
|
prepareDownload();
|
|
|
|
setHasFocus(true);
|
|
|
|
}}
|
|
|
|
onBlur={() => setHasFocus(false)}
|
|
|
|
cursor={!downloadImageUrl && "wait"}
|
|
|
|
variant="unstyled"
|
|
|
|
backgroundColor="gray.600"
|
|
|
|
color="gray.50"
|
|
|
|
boxShadow="md"
|
|
|
|
d="flex"
|
|
|
|
alignItems="center"
|
|
|
|
justifyContent="center"
|
|
|
|
opacity={hasFocus ? 1 : 0}
|
|
|
|
transition="all 0.2s"
|
|
|
|
_groupHover={{
|
|
|
|
opacity: 1,
|
|
|
|
}}
|
|
|
|
_focus={{
|
|
|
|
opacity: 1,
|
|
|
|
backgroundColor: "gray.500",
|
|
|
|
}}
|
|
|
|
_hover={{
|
|
|
|
backgroundColor: "gray.500",
|
|
|
|
}}
|
|
|
|
outline="initial"
|
|
|
|
/>
|
|
|
|
</Tooltip>
|
|
|
|
</Flex>
|
2020-04-25 02:32:41 -07:00
|
|
|
</Box>
|
2020-04-25 05:29:27 -07:00
|
|
|
<OutfitResetModal
|
|
|
|
isOpen={showResetModal}
|
|
|
|
onClose={() => setShowResetModal(false)}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
|
|
|
/>
|
2020-04-24 23:13:28 -07:00
|
|
|
</PseudoBox>
|
2020-04-23 15:19:36 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getVisibleLayers(data) {
|
|
|
|
if (!data) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-04-23 14:44:06 -07:00
|
|
|
const allAppearances = [
|
|
|
|
data.petAppearance,
|
2020-04-23 23:43:39 -07:00
|
|
|
...(data.items || []).map((i) => i.appearanceOn),
|
|
|
|
].filter((a) => a);
|
|
|
|
let allLayers = allAppearances.map((a) => a.layers).flat();
|
|
|
|
|
|
|
|
// Clean up our data a bit, by ensuring only one layer per zone. This
|
|
|
|
// shouldn't happen in theory, but sometimes our database doesn't clean up
|
|
|
|
// after itself correctly :(
|
|
|
|
allLayers = allLayers.filter((l, i) => {
|
|
|
|
return allLayers.findIndex((l2) => l2.zone.id === l.zone.id) === i;
|
|
|
|
});
|
2020-04-23 14:44:06 -07:00
|
|
|
|
|
|
|
const allRestrictedZoneIds = allAppearances
|
|
|
|
.map((l) => l.restrictedZones)
|
|
|
|
.flat()
|
|
|
|
.map((z) => z.id);
|
|
|
|
|
|
|
|
const visibleLayers = allLayers.filter(
|
|
|
|
(l) => !allRestrictedZoneIds.includes(l.zone.id)
|
|
|
|
);
|
|
|
|
visibleLayers.sort((a, b) => a.zone.depth - b.zone.depth);
|
2020-04-23 14:23:46 -07:00
|
|
|
|
2020-04-23 15:19:36 -07:00
|
|
|
return visibleLayers;
|
2020-04-23 13:31:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function FullScreenCenter({ children }) {
|
|
|
|
return (
|
|
|
|
<Flex
|
2020-04-23 15:19:36 -07:00
|
|
|
pos="absolute"
|
|
|
|
top="0"
|
|
|
|
right="0"
|
|
|
|
bottom="0"
|
|
|
|
left="0"
|
2020-04-23 13:31:39 -07:00
|
|
|
alignItems="center"
|
|
|
|
justifyContent="center"
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:13:28 -07:00
|
|
|
function useDownloadableImage(visibleLayers) {
|
|
|
|
const [downloadImageUrl, setDownloadImageUrl] = React.useState(null);
|
|
|
|
const [preparedForLayerIds, setPreparedForLayerIds] = React.useState([]);
|
|
|
|
|
|
|
|
const prepareDownload = React.useCallback(async () => {
|
|
|
|
// Skip if the current image URL is already correct for these layers.
|
|
|
|
const layerIds = visibleLayers.map((l) => l.id);
|
|
|
|
if (layerIds.join(",") === preparedForLayerIds.join(",")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-25 04:33:05 -07:00
|
|
|
setDownloadImageUrl(null);
|
|
|
|
|
2020-04-24 23:13:28 -07:00
|
|
|
const imagePromises = visibleLayers.map(
|
|
|
|
(layer) =>
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const image = new window.Image();
|
|
|
|
image.crossOrigin = "Anonymous"; // Requires S3 CORS config!
|
|
|
|
image.addEventListener("load", () => resolve(image), false);
|
|
|
|
image.addEventListener("error", (e) => reject(e), false);
|
2020-04-25 03:02:11 -07:00
|
|
|
image.src = layer.imageUrl + "&xoxo";
|
2020-04-24 23:13:28 -07:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const images = await Promise.all(imagePromises);
|
|
|
|
|
|
|
|
const canvas = document.createElement("canvas");
|
|
|
|
const context = canvas.getContext("2d");
|
|
|
|
canvas.width = 600;
|
|
|
|
canvas.height = 600;
|
|
|
|
|
|
|
|
for (const image of images) {
|
|
|
|
context.drawImage(image, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
"Generated image for download",
|
|
|
|
layerIds,
|
|
|
|
canvas.toDataURL("image/png")
|
|
|
|
);
|
|
|
|
setDownloadImageUrl(canvas.toDataURL("image/png"));
|
|
|
|
setPreparedForLayerIds(layerIds);
|
|
|
|
}, [preparedForLayerIds, visibleLayers]);
|
|
|
|
|
|
|
|
return [downloadImageUrl, prepareDownload];
|
|
|
|
}
|
|
|
|
|
2020-04-23 13:31:39 -07:00
|
|
|
export default OutfitPreview;
|