Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zoomandmobilecontrolimprovement #992

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions components/level/game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ export default function Game({
return;
}

if (event.touches.length !== 1) {
validTouchStart.current = false;

return;
} else if (event.touches[0].clientX < 20 || event.touches[0].clientX > window.innerWidth - 20) {
// disables back and forward navigation on mobile... hopefully on all browsers

event.preventDefault();
}

console.log(event.changedTouches, event.touches);

// NB: must start the touch event within the game layout
const isValid = event.composedPath().some(e => (e as HTMLElement).id === `grid-${level._id.toString()}`);

Expand All @@ -535,7 +547,6 @@ export default function Game({
touchYDown.current = event.touches[0].clientY;
isSwiping.current = false;
lastTouchTimestamp.current = Date.now();
event.preventDefault();
}
}, [level._id, preventKeyDownEvent]);

Expand All @@ -554,6 +565,14 @@ export default function Game({
handleKeyDown(code);
}, [handleKeyDown, lastMovetimestamp]);

const handleWheelEvent = useCallback((event: WheelEvent) => {
console.log(event);

if (preventKeyDownEvent) {
return;
}
}, [preventKeyDownEvent]);

const handleTouchMoveEvent = useCallback((event: TouchEvent) => {
if (!validTouchStart.current || preventKeyDownEvent) {
return;
Expand Down Expand Up @@ -640,22 +659,27 @@ export default function Game({

useEffect(() => {
window.addEventListener('blur', handleBlurEvent);
window.addEventListener('wheel', handleWheelEvent);
document.addEventListener('keydown', handleKeyDownEvent);
document.addEventListener('keyup', handleKeyUpEvent);
//getsture

// NB: even though the default value for passive is false, you have to specifically set it to false here in order to prevent swipe navigation in the browser

document.addEventListener('touchstart', handleTouchStartEvent, { passive: false });
document.addEventListener('touchmove', handleTouchMoveEvent, { passive: false });
document.addEventListener('touchend', handleTouchEndEvent, { passive: false });

return () => {
window.removeEventListener('wheel', handleWheelEvent);
window.removeEventListener('blur', handleBlurEvent);
document.removeEventListener('keydown', handleKeyDownEvent);
document.removeEventListener('keyup', handleKeyUpEvent);
document.removeEventListener('touchstart', handleTouchStartEvent);
document.removeEventListener('touchmove', handleTouchMoveEvent);
document.removeEventListener('touchend', handleTouchEndEvent);
};
}, [handleBlurEvent, handleKeyDownEvent, handleKeyUpEvent, handleTouchMoveEvent, handleTouchStartEvent, handleTouchEndEvent]);
}, [handleBlurEvent, handleKeyDownEvent, handleKeyUpEvent, handleTouchMoveEvent, handleTouchStartEvent, handleTouchEndEvent, handleWheelEvent]);

const [controls, setControls] = useState<Control[]>([]);

Expand Down
4 changes: 4 additions & 0 deletions pages/level/[username]/[slugName].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PagePath from '@root/constants/pagePath';
import isPro from '@root/helpers/isPro';
import { useTour } from '@root/hooks/useTour';
import { GetServerSidePropsContext, NextApiRequest } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { NextSeo } from 'next-seo';
import { ParsedUrlQuery } from 'querystring';
Expand Down Expand Up @@ -197,6 +198,9 @@ export default function LevelPage({ _level, reqUser }: LevelProps) {

return (
<>
<Head>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=1' />
</Head>
{tour}
<NextSeo
title={`${level.name} - Pathology`}
Expand Down
Loading