fix long press down

This commit is contained in:
Viktor Rådberg
2024-03-16 10:23:15 +01:00
parent 1bff41bc10
commit 677fd79bee

View File

@@ -66,8 +66,7 @@ export const CommanderDamage = ({
}: CommanderDamageButtonComponentProps) => { }: CommanderDamageButtonComponentProps) => {
const { updatePlayer } = usePlayers(); const { updatePlayer } = usePlayers();
const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined); const timeoutRef = useRef<NodeJS.Timeout | undefined>(undefined);
const [timeoutFinished, setTimeoutFinished] = useState(false); const [downLongPressed, setDownLongPressed] = useState(false);
const [hasPressedDown, setHasPressedDown] = useState(false);
const downPositionRef = useRef({ x: 0, y: 0 }); const downPositionRef = useRef({ x: 0, y: 0 });
const handleCommanderDamageChange = ( const handleCommanderDamageChange = (
@@ -109,16 +108,16 @@ export const CommanderDamage = ({
const handleDownInput = ({ opponentIndex, isPartner, event }: InputProps) => { const handleDownInput = ({ opponentIndex, isPartner, event }: InputProps) => {
downPositionRef.current = { x: event.clientX, y: event.clientY }; downPositionRef.current = { x: event.clientX, y: event.clientY };
setTimeoutFinished(false); setDownLongPressed(false);
setHasPressedDown(true);
timeoutRef.current = setTimeout(() => { timeoutRef.current = setTimeout(() => {
setTimeoutFinished(true); setDownLongPressed(true);
handleCommanderDamageChange(opponentIndex, -1, isPartner); handleCommanderDamageChange(opponentIndex, -1, isPartner);
}, decrementTimeoutMs); }, decrementTimeoutMs);
}; };
const handleUpInput = ({ opponentIndex, isPartner, event }: InputProps) => { const handleUpInput = ({ opponentIndex, isPartner, event }: InputProps) => {
if (!(hasPressedDown && !timeoutFinished)) { if (downLongPressed) {
return; return;
} }
@@ -134,14 +133,14 @@ export const CommanderDamage = ({
return; return;
} }
clearTimeout(timeoutRef.current);
handleCommanderDamageChange(opponentIndex, 1, isPartner); handleCommanderDamageChange(opponentIndex, 1, isPartner);
setHasPressedDown(false);
}; };
const handleLeaveInput = () => { const handleLeaveInput = () => {
setTimeoutFinished(true); setDownLongPressed(true);
clearTimeout(timeoutRef.current); clearTimeout(timeoutRef.current);
setHasPressedDown(false);
}; };
const opponentIndex = opponent.index; const opponentIndex = opponent.index;