2023-08-10 15:56:36 -07:00
|
|
|
import React from "react";
|
|
|
|
import { ClassNames } from "@emotion/react";
|
|
|
|
import {
|
|
|
|
Box,
|
|
|
|
Button,
|
|
|
|
DarkMode,
|
|
|
|
Flex,
|
|
|
|
FormControl,
|
|
|
|
FormHelperText,
|
|
|
|
FormLabel,
|
|
|
|
HStack,
|
|
|
|
IconButton,
|
|
|
|
ListItem,
|
2023-10-15 13:38:04 -07:00
|
|
|
Menu,
|
|
|
|
MenuItem,
|
|
|
|
MenuList,
|
2023-08-10 15:56:36 -07:00
|
|
|
Popover,
|
|
|
|
PopoverArrow,
|
|
|
|
PopoverBody,
|
|
|
|
PopoverContent,
|
|
|
|
PopoverTrigger,
|
|
|
|
Portal,
|
|
|
|
Stack,
|
|
|
|
Switch,
|
|
|
|
Tooltip,
|
|
|
|
UnorderedList,
|
2024-02-01 05:31:34 -08:00
|
|
|
useBreakpointValue,
|
2023-08-10 15:56:36 -07:00
|
|
|
useClipboard,
|
|
|
|
useToast,
|
|
|
|
} from "@chakra-ui/react";
|
|
|
|
import {
|
|
|
|
ArrowBackIcon,
|
|
|
|
CheckIcon,
|
|
|
|
ChevronDownIcon,
|
|
|
|
DownloadIcon,
|
|
|
|
LinkIcon,
|
|
|
|
SettingsIcon,
|
|
|
|
} from "@chakra-ui/icons";
|
|
|
|
import { MdPause, MdPlayArrow } from "react-icons/md";
|
|
|
|
|
|
|
|
import { getBestImageUrlForLayer } from "../components/OutfitPreview";
|
|
|
|
import HTML5Badge, { layerUsesHTML5 } from "../components/HTML5Badge";
|
|
|
|
import PosePicker from "./PosePicker";
|
|
|
|
import SpeciesColorPicker from "../components/SpeciesColorPicker";
|
2023-10-15 14:20:55 -07:00
|
|
|
import { loadImage, loadable, useLocalStorage } from "../util";
|
2023-08-10 15:56:36 -07:00
|
|
|
import useCurrentUser from "../components/useCurrentUser";
|
|
|
|
import useOutfitAppearance from "../components/useOutfitAppearance";
|
|
|
|
import OutfitKnownGlitchesBadge from "./OutfitKnownGlitchesBadge";
|
|
|
|
import usePreferArchive from "../components/usePreferArchive";
|
|
|
|
|
2023-10-15 14:20:55 -07:00
|
|
|
const LoadableLayersInfoModal = loadable(() => import("./LayersInfoModal"));
|
|
|
|
|
2023-08-10 15:56:36 -07:00
|
|
|
/**
|
|
|
|
* OutfitControls is the set of controls layered over the outfit preview, to
|
|
|
|
* control things like species/color and sharing links!
|
|
|
|
*/
|
|
|
|
function OutfitControls({
|
|
|
|
outfitState,
|
|
|
|
dispatchToOutfit,
|
|
|
|
showAnimationControls,
|
|
|
|
appearance,
|
|
|
|
}) {
|
|
|
|
const [focusIsLocked, setFocusIsLocked] = React.useState(false);
|
|
|
|
const onLockFocus = React.useCallback(
|
|
|
|
() => setFocusIsLocked(true),
|
2023-10-24 16:45:49 -07:00
|
|
|
[setFocusIsLocked],
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
const onUnlockFocus = React.useCallback(
|
|
|
|
() => setFocusIsLocked(false),
|
2023-10-24 16:45:49 -07:00
|
|
|
[setFocusIsLocked],
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
// HACK: As of 1.0.0-rc.0, Chakra's `toast` function rebuilds unnecessarily,
|
|
|
|
// which triggers unnecessary rebuilds of the `onSpeciesColorChange`
|
|
|
|
// callback, which causes the `React.memo` on `SpeciesColorPicker` to
|
|
|
|
// fail, which harms performance. But it seems to work just fine if we
|
|
|
|
// hold onto the first copy of the function we get! :/
|
|
|
|
const _toast = useToast();
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
const toast = React.useMemo(() => _toast, []);
|
|
|
|
|
2024-02-01 05:31:34 -08:00
|
|
|
const speciesColorPickerSize = useBreakpointValue({ base: "sm", md: "md" });
|
|
|
|
|
2023-08-10 15:56:36 -07:00
|
|
|
const onSpeciesColorChange = React.useCallback(
|
|
|
|
(species, color, isValid, closestPose) => {
|
|
|
|
if (isValid) {
|
|
|
|
dispatchToOutfit({
|
|
|
|
type: "setSpeciesAndColor",
|
|
|
|
speciesId: species.id,
|
|
|
|
colorId: color.id,
|
|
|
|
pose: closestPose,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// NOTE: This shouldn't be possible to trigger, because the
|
|
|
|
// `stateMustAlwaysBeValid` prop should prevent it. But we have
|
|
|
|
// it as a fallback, just in case!
|
|
|
|
toast({
|
|
|
|
title: `We haven't seen a ${color.name} ${species.name} before! 😓`,
|
|
|
|
status: "warning",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2023-10-24 16:45:49 -07:00
|
|
|
[dispatchToOutfit, toast],
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const maybeUnlockFocus = (e) => {
|
|
|
|
// We lock focus when a touch-device user taps the area. When they tap
|
|
|
|
// empty space, we treat that as a toggle and release the focus lock.
|
|
|
|
if (e.target === e.currentTarget) {
|
|
|
|
onUnlockFocus();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ClassNames>
|
|
|
|
{({ css, cx }) => (
|
2023-10-15 13:38:04 -07:00
|
|
|
<OutfitControlsContextMenu outfitState={outfitState}>
|
|
|
|
<Box
|
|
|
|
role="group"
|
|
|
|
pos="absolute"
|
|
|
|
left="0"
|
|
|
|
right="0"
|
|
|
|
top="0"
|
|
|
|
bottom="0"
|
|
|
|
height="100%" // Required for Safari to size the grid correctly
|
|
|
|
padding={{ base: 2, lg: 6 }}
|
|
|
|
display="grid"
|
|
|
|
overflow="auto"
|
|
|
|
gridTemplateAreas={`"back play-pause sharing"
|
2023-08-10 15:56:36 -07:00
|
|
|
"space space space"
|
|
|
|
"picker picker picker"`}
|
2023-10-15 13:38:04 -07:00
|
|
|
gridTemplateRows="auto minmax(1rem, 1fr) auto"
|
|
|
|
className={cx(
|
|
|
|
css`
|
|
|
|
opacity: 0;
|
|
|
|
transition: opacity 0.2s;
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2023-10-15 13:38:04 -07:00
|
|
|
&:focus-within,
|
|
|
|
&.focus-is-locked {
|
2023-08-10 15:56:36 -07:00
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
|
2023-10-15 13:38:04 -07:00
|
|
|
/* Ignore simulated hovers, only reveal for _real_ hovers. This helps
|
|
|
|
* us avoid state conflicts with the focus-lock from clicks. */
|
|
|
|
@media (hover: hover) {
|
|
|
|
&:hover {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
2023-10-24 16:45:49 -07:00
|
|
|
focusIsLocked && "focus-is-locked",
|
2023-10-15 13:38:04 -07:00
|
|
|
)}
|
|
|
|
onClickCapture={(e) => {
|
|
|
|
const opacity = parseFloat(
|
2023-10-24 16:45:49 -07:00
|
|
|
getComputedStyle(e.currentTarget).opacity,
|
2023-10-15 13:38:04 -07:00
|
|
|
);
|
|
|
|
if (opacity < 0.5) {
|
|
|
|
// If the controls aren't visible right now, then clicks on them are
|
|
|
|
// probably accidental. Ignore them! (We prevent default to block
|
|
|
|
// built-in behaviors like link nav, and we stop propagation to block
|
|
|
|
// our own custom click handlers. I don't know if I can prevent the
|
|
|
|
// select clicks though?)
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2023-08-10 15:56:36 -07:00
|
|
|
|
2023-10-15 13:38:04 -07:00
|
|
|
// We also show the controls, by locking focus. We'll undo this when
|
|
|
|
// the user taps elsewhere (because it will trigger a blur event from
|
|
|
|
// our child components), in `maybeUnlockFocus`.
|
|
|
|
setFocusIsLocked(true);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
data-test-id="wardrobe-outfit-controls"
|
2023-08-10 15:56:36 -07:00
|
|
|
>
|
2023-10-15 13:38:04 -07:00
|
|
|
<Box gridArea="back" onClick={maybeUnlockFocus}>
|
|
|
|
<BackButton outfitState={outfitState} />
|
2023-08-10 15:56:36 -07:00
|
|
|
</Box>
|
2023-10-15 13:38:04 -07:00
|
|
|
|
|
|
|
<Flex
|
|
|
|
gridArea="play-pause"
|
|
|
|
// HACK: Better visual centering with other controls
|
|
|
|
paddingTop="0.3rem"
|
|
|
|
direction="column"
|
|
|
|
align="center"
|
|
|
|
>
|
|
|
|
{showAnimationControls && <PlayPauseButton />}
|
|
|
|
<Box height="2" />
|
|
|
|
<HStack spacing="2" align="center" justify="center">
|
|
|
|
<OutfitHTML5Badge appearance={appearance} />
|
|
|
|
<OutfitKnownGlitchesBadge appearance={appearance} />
|
|
|
|
<SettingsButton
|
|
|
|
onLockFocus={onLockFocus}
|
|
|
|
onUnlockFocus={onUnlockFocus}
|
|
|
|
/>
|
|
|
|
</HStack>
|
|
|
|
</Flex>
|
|
|
|
<Stack
|
|
|
|
gridArea="sharing"
|
|
|
|
alignSelf="flex-end"
|
|
|
|
spacing={{ base: "2", lg: "4" }}
|
|
|
|
align="flex-end"
|
|
|
|
onClick={maybeUnlockFocus}
|
|
|
|
>
|
|
|
|
<Box>
|
|
|
|
<DownloadButton outfitState={outfitState} />
|
|
|
|
</Box>
|
|
|
|
<Box>
|
|
|
|
<CopyLinkButton outfitState={outfitState} />
|
|
|
|
</Box>
|
|
|
|
</Stack>
|
|
|
|
<Box gridArea="space" onClick={maybeUnlockFocus} />
|
|
|
|
{outfitState.speciesId && outfitState.colorId && (
|
2023-08-10 15:56:36 -07:00
|
|
|
<Flex
|
2023-10-15 13:38:04 -07:00
|
|
|
gridArea="picker"
|
2024-02-01 05:31:34 -08:00
|
|
|
align="center"
|
2023-10-15 13:38:04 -07:00
|
|
|
justify="center"
|
|
|
|
onClick={maybeUnlockFocus}
|
|
|
|
>
|
|
|
|
{/**
|
|
|
|
* We try to center the species/color picker, but the left spacer will
|
|
|
|
* shrink more than the pose picker container if we run out of space!
|
|
|
|
*/}
|
|
|
|
<Box flex="0 0 auto">
|
|
|
|
<DarkMode>
|
|
|
|
<SpeciesColorPicker
|
|
|
|
speciesId={outfitState.speciesId}
|
|
|
|
colorId={outfitState.colorId}
|
|
|
|
idealPose={outfitState.pose}
|
|
|
|
onChange={onSpeciesColorChange}
|
|
|
|
stateMustAlwaysBeValid
|
2024-02-01 05:31:34 -08:00
|
|
|
size={speciesColorPickerSize}
|
2023-10-15 13:38:04 -07:00
|
|
|
speciesTestId="wardrobe-species-picker"
|
|
|
|
colorTestId="wardrobe-color-picker"
|
|
|
|
/>
|
|
|
|
</DarkMode>
|
|
|
|
</Box>
|
2024-01-30 05:34:20 -08:00
|
|
|
<Flex flex="0 0 auto" align="center" pl="2">
|
2023-10-15 13:38:04 -07:00
|
|
|
<PosePicker
|
2023-08-10 15:56:36 -07:00
|
|
|
speciesId={outfitState.speciesId}
|
|
|
|
colorId={outfitState.colorId}
|
2023-10-15 13:38:04 -07:00
|
|
|
pose={outfitState.pose}
|
2024-01-30 06:21:32 -08:00
|
|
|
altStyleId={outfitState.altStyleId}
|
2023-10-15 13:38:04 -07:00
|
|
|
appearanceId={outfitState.appearanceId}
|
|
|
|
dispatchToOutfit={dispatchToOutfit}
|
|
|
|
onLockFocus={onLockFocus}
|
|
|
|
onUnlockFocus={onUnlockFocus}
|
|
|
|
data-test-id="wardrobe-pose-picker"
|
2023-08-10 15:56:36 -07:00
|
|
|
/>
|
2023-10-15 13:38:04 -07:00
|
|
|
</Flex>
|
2023-08-10 15:56:36 -07:00
|
|
|
</Flex>
|
2023-10-15 13:38:04 -07:00
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
</OutfitControlsContextMenu>
|
2023-08-10 15:56:36 -07:00
|
|
|
)}
|
|
|
|
</ClassNames>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-15 13:38:04 -07:00
|
|
|
function OutfitControlsContextMenu({ outfitState, children }) {
|
|
|
|
// NOTE: We track these separately, rather than in one atomic state object,
|
|
|
|
// because I want to still keep the menu in the right position when it's
|
|
|
|
// animating itself closed!
|
|
|
|
const [isOpen, setIsOpen] = React.useState(false);
|
|
|
|
const [position, setPosition] = React.useState({ x: 0, y: 0 });
|
|
|
|
|
2023-10-15 14:20:55 -07:00
|
|
|
const [layersInfoModalIsOpen, setLayersInfoModalIsOpen] =
|
|
|
|
React.useState(false);
|
|
|
|
|
2023-10-15 13:38:04 -07:00
|
|
|
const { visibleLayers } = useOutfitAppearance(outfitState);
|
|
|
|
const [downloadImageUrl, prepareDownload] =
|
|
|
|
useDownloadableImage(visibleLayers);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
onContextMenuCapture={(e) => {
|
|
|
|
setIsOpen(true);
|
|
|
|
setPosition({ x: e.pageX, y: e.pageY });
|
|
|
|
e.preventDefault();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
<Menu isOpen={isOpen} onClose={() => setIsOpen(false)}>
|
|
|
|
<Portal>
|
|
|
|
<MenuList position="absolute" left={position.x} top={position.y}>
|
|
|
|
<MenuItem
|
|
|
|
icon={<DownloadIcon />}
|
|
|
|
as="a"
|
|
|
|
// eslint-disable-next-line no-script-url
|
|
|
|
href={downloadImageUrl || "#"}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (!downloadImageUrl) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
download={(outfitState.name || "Outfit") + ".png"}
|
|
|
|
onMouseEnter={prepareDownload}
|
|
|
|
onFocus={prepareDownload}
|
|
|
|
cursor={!downloadImageUrl && "wait"}
|
|
|
|
>
|
|
|
|
Download
|
|
|
|
</MenuItem>
|
2023-10-15 14:20:55 -07:00
|
|
|
<MenuItem
|
|
|
|
icon={<LinkIcon />}
|
|
|
|
onClick={() => setLayersInfoModalIsOpen(true)}
|
|
|
|
>
|
|
|
|
Layers (SWF, PNG)
|
|
|
|
</MenuItem>
|
2023-10-15 13:38:04 -07:00
|
|
|
</MenuList>
|
|
|
|
</Portal>
|
|
|
|
</Menu>
|
2023-10-15 14:20:55 -07:00
|
|
|
<LoadableLayersInfoModal
|
|
|
|
isOpen={layersInfoModalIsOpen}
|
|
|
|
onClose={() => setLayersInfoModalIsOpen(false)}
|
|
|
|
visibleLayers={visibleLayers}
|
|
|
|
/>
|
2023-10-15 13:38:04 -07:00
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:56:36 -07:00
|
|
|
function OutfitHTML5Badge({ appearance }) {
|
|
|
|
const petIsUsingHTML5 =
|
|
|
|
appearance.petAppearance?.layers.every(layerUsesHTML5);
|
|
|
|
|
|
|
|
const itemsNotUsingHTML5 = appearance.items.filter((item) =>
|
2023-10-24 16:45:49 -07:00
|
|
|
item.appearance.layers.some((l) => !layerUsesHTML5(l)),
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
itemsNotUsingHTML5.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
|
|
|
|
const usesHTML5 = petIsUsingHTML5 && itemsNotUsingHTML5.length === 0;
|
|
|
|
|
|
|
|
let tooltipLabel;
|
|
|
|
if (usesHTML5) {
|
|
|
|
tooltipLabel = (
|
|
|
|
<>This outfit is converted to HTML5, and ready to use on Neopets.com!</>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
tooltipLabel = (
|
|
|
|
<Box>
|
|
|
|
<Box as="p">
|
|
|
|
This outfit isn't converted to HTML5 yet, so it might not appear in
|
|
|
|
Neopets.com customization yet. Once it's ready, it could look a bit
|
|
|
|
different than our temporary preview here. It might even be animated!
|
|
|
|
</Box>
|
|
|
|
{!petIsUsingHTML5 && (
|
|
|
|
<Box as="p" marginTop="1em" fontWeight="bold">
|
|
|
|
This pet is not yet converted.
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
{itemsNotUsingHTML5.length > 0 && (
|
|
|
|
<>
|
|
|
|
<Box as="header" marginTop="1em" fontWeight="bold">
|
|
|
|
The following items aren't yet converted:
|
|
|
|
</Box>
|
|
|
|
<UnorderedList>
|
|
|
|
{itemsNotUsingHTML5.map((item) => (
|
|
|
|
<ListItem key={item.id}>{item.name}</ListItem>
|
|
|
|
))}
|
|
|
|
</UnorderedList>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<HTML5Badge
|
|
|
|
usesHTML5={usesHTML5}
|
|
|
|
isLoading={appearance.loading}
|
|
|
|
tooltipLabel={tooltipLabel}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BackButton takes you back home, or to Your Outfits if this outfit is yours.
|
|
|
|
*/
|
|
|
|
function BackButton({ outfitState }) {
|
|
|
|
const currentUser = useCurrentUser();
|
|
|
|
const outfitBelongsToCurrentUser =
|
|
|
|
outfitState.creator && outfitState.creator.id === currentUser.id;
|
|
|
|
|
|
|
|
return (
|
2023-08-10 16:31:39 -07:00
|
|
|
<ControlButton
|
|
|
|
as="a"
|
|
|
|
href={outfitBelongsToCurrentUser ? "/your-outfits" : "/"}
|
|
|
|
icon={<ArrowBackIcon />}
|
|
|
|
aria-label="Leave this outfit"
|
|
|
|
d="inline-flex" // Not sure why <a> requires this to style right! ^^`
|
|
|
|
data-test-id="wardrobe-nav-back-button"
|
|
|
|
/>
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DownloadButton downloads the outfit as an image!
|
|
|
|
*/
|
|
|
|
function DownloadButton({ outfitState }) {
|
|
|
|
const { visibleLayers } = useOutfitAppearance(outfitState);
|
|
|
|
|
|
|
|
const [downloadImageUrl, prepareDownload] =
|
|
|
|
useDownloadableImage(visibleLayers);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip label="Download" placement="left">
|
|
|
|
<Box>
|
|
|
|
<ControlButton
|
|
|
|
icon={<DownloadIcon />}
|
|
|
|
aria-label="Download"
|
|
|
|
as="a"
|
|
|
|
// eslint-disable-next-line no-script-url
|
|
|
|
href={downloadImageUrl || "#"}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (!downloadImageUrl) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
download={(outfitState.name || "Outfit") + ".png"}
|
|
|
|
onMouseEnter={prepareDownload}
|
|
|
|
onFocus={prepareDownload}
|
|
|
|
cursor={!downloadImageUrl && "wait"}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* CopyLinkButton copies the outfit URL to the clipboard!
|
|
|
|
*/
|
|
|
|
function CopyLinkButton({ outfitState }) {
|
|
|
|
const { onCopy, hasCopied } = useClipboard(outfitState.url);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Tooltip label={hasCopied ? "Copied!" : "Copy link"} placement="left">
|
|
|
|
<Box>
|
|
|
|
<ControlButton
|
|
|
|
icon={hasCopied ? <CheckIcon /> : <LinkIcon />}
|
|
|
|
aria-label="Copy link"
|
|
|
|
onClick={onCopy}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function PlayPauseButton() {
|
|
|
|
const [isPaused, setIsPaused] = useLocalStorage("DTIOutfitIsPaused", true);
|
|
|
|
|
|
|
|
// We show an intro animation if this mounts while paused. Whereas if we're
|
|
|
|
// not paused, we initialize as if we had already finished.
|
|
|
|
const [blinkInState, setBlinkInState] = React.useState(
|
2023-10-24 16:45:49 -07:00
|
|
|
isPaused ? { type: "ready" } : { type: "done" },
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
const buttonRef = React.useRef(null);
|
|
|
|
|
|
|
|
React.useLayoutEffect(() => {
|
|
|
|
if (blinkInState.type === "ready" && buttonRef.current) {
|
|
|
|
setBlinkInState({
|
|
|
|
type: "started",
|
|
|
|
position: {
|
|
|
|
left: buttonRef.current.offsetLeft,
|
|
|
|
top: buttonRef.current.offsetTop,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, [blinkInState, setBlinkInState]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ClassNames>
|
|
|
|
{({ css }) => (
|
|
|
|
<>
|
|
|
|
<PlayPauseButtonContent
|
|
|
|
isPaused={isPaused}
|
|
|
|
setIsPaused={setIsPaused}
|
|
|
|
ref={buttonRef}
|
|
|
|
/>
|
|
|
|
{blinkInState.type === "started" && (
|
|
|
|
<Portal>
|
|
|
|
<PlayPauseButtonContent
|
|
|
|
isPaused={isPaused}
|
|
|
|
setIsPaused={setIsPaused}
|
|
|
|
position="absolute"
|
|
|
|
left={blinkInState.position.left}
|
|
|
|
top={blinkInState.position.top}
|
|
|
|
backgroundColor="gray.600"
|
|
|
|
borderColor="gray.50"
|
|
|
|
color="gray.50"
|
|
|
|
onAnimationEnd={() => setBlinkInState({ type: "done" })}
|
|
|
|
// Don't disrupt the hover state of the controls! (And the button
|
|
|
|
// doesn't seem to click correctly, not sure why, but instead of
|
|
|
|
// debugging I'm adding this :p)
|
|
|
|
pointerEvents="none"
|
|
|
|
className={css`
|
|
|
|
@keyframes fade-in-out {
|
|
|
|
0% {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
10% {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
90% {
|
|
|
|
opacity: 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
100% {
|
|
|
|
opacity: 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opacity: 0;
|
|
|
|
animation: fade-in-out 2s;
|
|
|
|
`}
|
|
|
|
/>
|
|
|
|
</Portal>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</ClassNames>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const PlayPauseButtonContent = React.forwardRef(
|
|
|
|
({ isPaused, setIsPaused, ...props }, ref) => {
|
|
|
|
return (
|
|
|
|
<TranslucentButton
|
|
|
|
ref={ref}
|
|
|
|
leftIcon={isPaused ? <MdPause /> : <MdPlayArrow />}
|
|
|
|
onClick={() => setIsPaused(!isPaused)}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{isPaused ? <>Paused</> : <>Playing</>}
|
|
|
|
</TranslucentButton>
|
|
|
|
);
|
2023-10-24 16:45:49 -07:00
|
|
|
},
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
function SettingsButton({ onLockFocus, onUnlockFocus }) {
|
|
|
|
return (
|
|
|
|
<Popover onOpen={onLockFocus} onClose={onUnlockFocus}>
|
|
|
|
<PopoverTrigger>
|
|
|
|
<TranslucentButton size="xs" aria-label="Settings">
|
|
|
|
<SettingsIcon />
|
|
|
|
<Box width="1" />
|
|
|
|
<ChevronDownIcon />
|
|
|
|
</TranslucentButton>
|
|
|
|
</PopoverTrigger>
|
|
|
|
<Portal>
|
|
|
|
<PopoverContent width="25ch">
|
|
|
|
<PopoverArrow />
|
|
|
|
<PopoverBody>
|
|
|
|
<HiResModeSetting />
|
|
|
|
</PopoverBody>
|
|
|
|
</PopoverContent>
|
|
|
|
</Portal>
|
|
|
|
</Popover>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function HiResModeSetting() {
|
|
|
|
const [hiResMode, setHiResMode] = useLocalStorage("DTIHiResMode", false);
|
|
|
|
const [preferArchive, setPreferArchive] = usePreferArchive();
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box>
|
|
|
|
<FormControl>
|
|
|
|
<Flex>
|
|
|
|
<Box>
|
|
|
|
<FormLabel htmlFor="hi-res-mode-setting" fontSize="sm" margin="0">
|
|
|
|
Hi-res mode (SVG)
|
|
|
|
</FormLabel>
|
|
|
|
<FormHelperText marginTop="0" fontSize="xs">
|
|
|
|
Crisper at higher resolutions, but not always accurate
|
|
|
|
</FormHelperText>
|
|
|
|
</Box>
|
|
|
|
<Box width="2" />
|
|
|
|
<Switch
|
|
|
|
id="hi-res-mode-setting"
|
|
|
|
size="sm"
|
|
|
|
marginTop="0.1rem"
|
|
|
|
isChecked={hiResMode}
|
|
|
|
onChange={(e) => setHiResMode(e.target.checked)}
|
|
|
|
/>
|
|
|
|
</Flex>
|
|
|
|
</FormControl>
|
|
|
|
<Box height="2" />
|
|
|
|
<FormControl>
|
|
|
|
<Flex>
|
|
|
|
<Box>
|
|
|
|
<FormLabel
|
|
|
|
htmlFor="prefer-archive-setting"
|
|
|
|
fontSize="sm"
|
|
|
|
margin="0"
|
|
|
|
>
|
|
|
|
Use DTI's image archive
|
|
|
|
</FormLabel>
|
|
|
|
<FormHelperText marginTop="0" fontSize="xs">
|
|
|
|
Turn this on when images.neopets.com is slow!
|
|
|
|
</FormHelperText>
|
|
|
|
</Box>
|
|
|
|
<Box width="2" />
|
|
|
|
<Switch
|
|
|
|
id="prefer-archive-setting"
|
|
|
|
size="sm"
|
|
|
|
marginTop="0.1rem"
|
|
|
|
isChecked={preferArchive ?? false}
|
|
|
|
onChange={(e) => setPreferArchive(e.target.checked)}
|
|
|
|
/>
|
|
|
|
</Flex>
|
|
|
|
</FormControl>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const TranslucentButton = React.forwardRef(({ children, ...props }, ref) => {
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
ref={ref}
|
|
|
|
size="sm"
|
|
|
|
color="gray.100"
|
|
|
|
variant="outline"
|
|
|
|
borderColor="gray.200"
|
|
|
|
borderRadius="full"
|
|
|
|
backgroundColor="blackAlpha.600"
|
|
|
|
boxShadow="md"
|
|
|
|
_hover={{
|
|
|
|
backgroundColor: "gray.600",
|
|
|
|
borderColor: "gray.50",
|
|
|
|
color: "gray.50",
|
|
|
|
}}
|
|
|
|
_focus={{
|
|
|
|
backgroundColor: "gray.600",
|
|
|
|
borderColor: "gray.50",
|
|
|
|
color: "gray.50",
|
|
|
|
}}
|
|
|
|
{...props}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ControlButton is a UI helper to render the cute round buttons we use in
|
|
|
|
* OutfitControls!
|
|
|
|
*/
|
|
|
|
function ControlButton({ icon, "aria-label": ariaLabel, ...props }) {
|
|
|
|
return (
|
|
|
|
<IconButton
|
|
|
|
icon={icon}
|
|
|
|
aria-label={ariaLabel}
|
|
|
|
isRound
|
|
|
|
variant="unstyled"
|
|
|
|
backgroundColor="gray.600"
|
|
|
|
color="gray.50"
|
|
|
|
boxShadow="md"
|
|
|
|
d="flex"
|
|
|
|
alignItems="center"
|
|
|
|
justifyContent="center"
|
|
|
|
transition="backgroundColor 0.2s"
|
|
|
|
_focus={{ backgroundColor: "gray.500" }}
|
|
|
|
_hover={{ backgroundColor: "gray.500" }}
|
|
|
|
outline="initial"
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useDownloadableImage loads the image data and generates the downloadable
|
|
|
|
* image URL.
|
|
|
|
*/
|
|
|
|
function useDownloadableImage(visibleLayers) {
|
|
|
|
const [hiResMode] = useLocalStorage("DTIHiResMode", false);
|
|
|
|
const [preferArchive] = usePreferArchive();
|
|
|
|
|
|
|
|
const [downloadImageUrl, setDownloadImageUrl] = React.useState(null);
|
|
|
|
const [preparedForLayerIds, setPreparedForLayerIds] = React.useState([]);
|
|
|
|
const toast = useToast();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip if there are no layers. (This probably means we're still loading!)
|
|
|
|
if (layerIds.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDownloadImageUrl(null);
|
|
|
|
|
|
|
|
// NOTE: You could argue that we may as well just always use PNGs here,
|
|
|
|
// regardless of hi-res mode… but using the same src will help both
|
|
|
|
// performance (can use cached SVG), and predictability (image will
|
|
|
|
// look like what you see here).
|
|
|
|
const imagePromises = visibleLayers.map((layer) =>
|
|
|
|
loadImage(getBestImageUrlForLayer(layer, { hiResMode }), {
|
|
|
|
crossOrigin: "anonymous",
|
|
|
|
preferArchive,
|
2023-10-24 16:45:49 -07:00
|
|
|
}),
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
let images;
|
|
|
|
try {
|
|
|
|
images = await Promise.all(imagePromises);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Error building downloadable image", e);
|
|
|
|
toast({
|
|
|
|
status: "error",
|
|
|
|
title: "Oops, sorry, we couldn't download the image!",
|
|
|
|
description:
|
|
|
|
"Check your connection, then reload the page and try again.",
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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.debug(
|
|
|
|
"Generated image for download",
|
|
|
|
layerIds,
|
2023-10-24 16:45:49 -07:00
|
|
|
canvas.toDataURL("image/png"),
|
2023-08-10 15:56:36 -07:00
|
|
|
);
|
|
|
|
setDownloadImageUrl(canvas.toDataURL("image/png"));
|
|
|
|
setPreparedForLayerIds(layerIds);
|
|
|
|
}, [preparedForLayerIds, visibleLayers, toast, hiResMode, preferArchive]);
|
|
|
|
|
|
|
|
return [downloadImageUrl, prepareDownload];
|
|
|
|
}
|
|
|
|
|
|
|
|
export default OutfitControls;
|