feat: popover custom shell

This commit is contained in:
tresabhi committed 2024-11-10 21:09:09 -06:00
1 parent dbc3d0df3a
commit 3e2bfc453a
2 files changed
+68 -75

No files matched your search

@@ -1,16 +1,15 @@
import { imgur, ShellType } from '@blitzkit/core'; import { imgur, ShellType } from '@blitzkit/core';
import { import {
Button, Button,
Dialog,
Flex, Flex,
IconButton, IconButton,
Popover,
Select, Select,
Slider, Slider,
Text, Text,
TextField, TextField,
} from '@radix-ui/themes'; } from '@radix-ui/themes';
import { invalidate } from '@react-three/fiber'; import { type ChangeEvent } from 'react';
import { useEffect, useState, type ChangeEvent } from 'react';
import { Duel } from '../../../../../../stores/duel'; import { Duel } from '../../../../../../stores/duel';
import { TankopediaEphemeral } from '../../../../../../stores/tankopediaEphemeral'; import { TankopediaEphemeral } from '../../../../../../stores/tankopediaEphemeral';
@@ -18,17 +17,18 @@ export function CustomShellButton() {
const customShell = TankopediaEphemeral.use((state) => state.customShell); const customShell = TankopediaEphemeral.use((state) => state.customShell);
const antagonistShell = Duel.use((state) => state.antagonist.shell); const antagonistShell = Duel.use((state) => state.antagonist.shell);
const mutateTankopediaEphemeral = TankopediaEphemeral.useMutation(); const mutateTankopediaEphemeral = TankopediaEphemeral.useMutation();
const [customShellDraft, setCustomShellDraft] = useState(
customShell ?? antagonistShell,
);
useEffect(() => {
setCustomShellDraft(customShell ?? antagonistShell);
}, [antagonistShell, customShell]);
return ( return (
<Dialog.Root> <Popover.Root
<Dialog.Trigger> onOpenChange={(open) => {
if (!open) return;
mutateTankopediaEphemeral((draft) => {
draft.customShell = antagonistShell;
});
}}
>
<Popover.Trigger>
<IconButton <IconButton
color={customShell ? undefined : 'gray'} color={customShell ? undefined : 'gray'}
variant="soft" variant="soft"
@@ -44,19 +44,21 @@ export function CustomShellButton() {
}} }}
/> />
</IconButton> </IconButton>
</Dialog.Trigger> </Popover.Trigger>
<Dialog.Content> <Popover.Content>
<Dialog.Title>Custom shell</Dialog.Title> {/* <Popover.Title>Custom shell</Popover.Title> */}
{customShell && (
<>
<Flex direction="column" gap="2"> <Flex direction="column" gap="2">
<Flex align="center" justify="between"> <Flex align="center" justify="between" gap="4">
<Text>Type</Text> <Text>Type</Text>
<Select.Root <Select.Root
value={`${customShellDraft.type}`} value={`${customShell.type}`}
onValueChange={(value) => { onValueChange={(value) => {
const type = Number(value) as ShellType; const type = Number(value) as ShellType;
const modified = { ...customShellDraft, type }; const modified = { ...customShell, type };
switch (type) { switch (type) {
case ShellType.AP: { case ShellType.AP: {
@@ -77,7 +79,9 @@ export function CustomShellButton() {
} }
} }
setCustomShellDraft(modified); mutateTankopediaEphemeral((draft) => {
draft.customShell = modified;
});
}} }}
> >
<Select.Trigger /> <Select.Trigger />
@@ -91,16 +95,15 @@ export function CustomShellButton() {
</Select.Root> </Select.Root>
</Flex> </Flex>
<Flex align="center" justify="between"> <Flex align="center" justify="between" gap="4">
<Text>Caliber</Text> <Text>Caliber</Text>
<TextField.Root <TextField.Root
style={{ maxWidth: '6rem' }} style={{ maxWidth: '6rem' }}
defaultValue={customShellDraft.caliber} defaultValue={customShell.caliber}
type="number" type="number"
onBlur={(event: ChangeEvent<HTMLInputElement>) => { onChange={(event) => {
setCustomShellDraft({ mutateTankopediaEphemeral((draft) => {
...customShellDraft, draft.customShell!.caliber = event.target.valueAsNumber;
caliber: event.target.valueAsNumber,
}); });
}} }}
onFocus={(event) => event.target.select()} onFocus={(event) => event.target.select()}
@@ -109,7 +112,7 @@ export function CustomShellButton() {
(event.target as HTMLInputElement).blur(); (event.target as HTMLInputElement).blur();
} else if (event.key === 'Escape') { } else if (event.key === 'Escape') {
(event.target as HTMLInputElement).valueAsNumber = (event.target as HTMLInputElement).valueAsNumber =
customShellDraft.caliber; customShell.caliber;
(event.target as HTMLInputElement).blur(); (event.target as HTMLInputElement).blur();
} }
}} }}
@@ -118,19 +121,16 @@ export function CustomShellButton() {
</TextField.Root> </TextField.Root>
</Flex> </Flex>
<Flex align="center" justify="between"> <Flex align="center" justify="between" gap="4">
<Text>Penetration</Text> <Text>Penetration</Text>
<TextField.Root <TextField.Root
style={{ maxWidth: '6rem' }} style={{ maxWidth: '6rem' }}
defaultValue={customShellDraft.penetration.near} defaultValue={customShell.penetration.near}
type="number" type="number"
onBlur={(event: ChangeEvent<HTMLInputElement>) => { onChange={(event: ChangeEvent<HTMLInputElement>) => {
setCustomShellDraft({ mutateTankopediaEphemeral((draft) => {
...customShellDraft, draft.customShell!.penetration.near =
penetration: { event.target.valueAsNumber;
near: event.target.valueAsNumber,
far: event.target.valueAsNumber,
},
}); });
}} }}
onFocus={(event) => event.target.select()} onFocus={(event) => event.target.select()}
@@ -139,7 +139,7 @@ export function CustomShellButton() {
(event.target as HTMLInputElement).blur(); (event.target as HTMLInputElement).blur();
} else if (event.key === 'Escape') { } else if (event.key === 'Escape') {
(event.target as HTMLInputElement).valueAsNumber = (event.target as HTMLInputElement).valueAsNumber =
customShellDraft.penetration.near; customShell.penetration.near;
(event.target as HTMLInputElement).blur(); (event.target as HTMLInputElement).blur();
} }
}} }}
@@ -148,26 +148,25 @@ export function CustomShellButton() {
</TextField.Root> </TextField.Root>
</Flex> </Flex>
{(customShellDraft.type === ShellType.AP || {(customShell.type === ShellType.AP ||
customShellDraft.type === ShellType.APCR) && ( customShell.type === ShellType.APCR) && (
<> <>
<Flex align="center" justify="between" gap="4" py="1"> <Flex align="center" justify="between" gap="4" py="1">
<Text>Normalization</Text> <Text>Normalization</Text>
<Flex align="center" gap="2" flexGrow="1" maxWidth="15rem"> <Flex align="center" gap="2" flexGrow="1" maxWidth="15rem">
<Text color="gray"> <Text color="gray">
{customShellDraft.normalization ?? 0}° {customShell.normalization ?? 0}°
</Text> </Text>
<Slider <Slider
min={0} min={0}
max={90} max={90}
value={[customShellDraft.normalization ?? 0]} value={[customShell.normalization ?? 0]}
onValueChange={([value]) => onValueChange={([value]) => {
setCustomShellDraft({ mutateTankopediaEphemeral((draft) => {
...customShellDraft, draft.customShell!.normalization = value;
normalization: value, });
}) }}
}
/> />
</Flex> </Flex>
</Flex> </Flex>
@@ -176,17 +175,16 @@ export function CustomShellButton() {
<Text>Ricochet</Text> <Text>Ricochet</Text>
<Flex align="center" gap="2" flexGrow="1" maxWidth="15rem"> <Flex align="center" gap="2" flexGrow="1" maxWidth="15rem">
<Text color="gray">{customShellDraft.ricochet ?? 90}°</Text> <Text color="gray">{customShell.ricochet ?? 90}°</Text>
<Slider <Slider
min={0} min={0}
max={90} max={90}
value={[customShellDraft.ricochet ?? 90]} value={[customShell.ricochet ?? 90]}
onValueChange={([value]) => onValueChange={([value]) => {
setCustomShellDraft({ mutateTankopediaEphemeral((draft) => {
...customShellDraft, draft.customShell!.ricochet = value;
ricochet: value, });
}) }}
}
/> />
</Flex> </Flex>
</Flex> </Flex>
@@ -195,34 +193,25 @@ export function CustomShellButton() {
</Flex> </Flex>
<Flex justify="end" mt="6" gap="2"> <Flex justify="end" mt="6" gap="2">
<Dialog.Close> <Popover.Close>
<Button color="red" variant="outline"> <Button color="red" variant="outline">
Cancel Exit
</Button> </Button>
</Dialog.Close> </Popover.Close>
<Button <Button
color="red" color="red"
onClick={() => { onClick={() => {
setCustomShellDraft(antagonistShell); mutateTankopediaEphemeral((draft) => {
draft.customShell = antagonistShell;
});
}} }}
> >
Reset Reset
</Button> </Button>
<Dialog.Close>
<Button
onClick={() => {
invalidate();
mutateTankopediaEphemeral((draft) => {
draft.shot = undefined;
draft.customShell = customShellDraft;
});
}}
>
Apply
</Button>
</Dialog.Close>
</Flex> </Flex>
</Dialog.Content> </>
</Dialog.Root> )}
</Popover.Content>
</Popover.Root>
); );
} }
@@ -53,7 +53,9 @@ interface OptionsProps {
} }
export function Options({ thicknessRange, canvas }: OptionsProps) { export function Options({ thicknessRange, canvas }: OptionsProps) {
const customShell = TankopediaEphemeral.use((state) => state.customShell); const hasCustomShell = TankopediaEphemeral.use(
(state) => state.customShell !== undefined,
);
const display = TankopediaPersistent.useDeferred( const display = TankopediaPersistent.useDeferred(
(state) => state.display, (state) => state.display,
TankopediaDisplay.Model, TankopediaDisplay.Model,
@@ -101,6 +103,7 @@ export function Options({ thicknessRange, canvas }: OptionsProps) {
style={{ position: 'absolute', transform: 'translateY(-50%)' }} style={{ position: 'absolute', transform: 'translateY(-50%)' }}
align="end" align="end"
> >
{!hasCustomShell && (
<Text color="gray" size={{ initial: '1', sm: '2' }}> <Text color="gray" size={{ initial: '1', sm: '2' }}>
{( {(
resolvePenetrationCoefficient( resolvePenetrationCoefficient(
@@ -110,6 +113,7 @@ export function Options({ thicknessRange, canvas }: OptionsProps) {
).toFixed(0)} ).toFixed(0)}
mm mm
</Text> </Text>
)}
{antagonistUniqueGuns.length > 1 && ( {antagonistUniqueGuns.length > 1 && (
<Popover.Root> <Popover.Root>
<Popover.Trigger> <Popover.Trigger>
@@ -189,7 +193,7 @@ export function Options({ thicknessRange, canvas }: OptionsProps) {
{antagonistGun.gun_type!.value.base.shells.map((thisShell) => ( {antagonistGun.gun_type!.value.base.shells.map((thisShell) => (
<IconButton <IconButton
color={ color={
thisShell.id === antagonistShell.id && !customShell thisShell.id === antagonistShell.id && !hasCustomShell
? undefined ? undefined
: 'gray' : 'gray'
} }