|
| 1 | +import { Icon } from '@blueprintjs/core'; |
| 2 | +import { IconNames } from '@blueprintjs/icons'; |
| 3 | +import { useFullscreen } from '@mantine/hooks'; |
1 | 4 | import React from 'react';
|
2 | 5 | import { useDispatch } from 'react-redux';
|
3 | 6 | import { useTypedSelector } from 'src/commons/utils/Hooks';
|
@@ -50,9 +53,93 @@ function Game() {
|
50 | 53 | }
|
51 | 54 | }, [session, achievements, goals]);
|
52 | 55 |
|
| 56 | + // This is a custom hook imported from @mantine/hooks that handles the fullscreen logic |
| 57 | + // It returns a ref to attach to the element that should be fullscreened, |
| 58 | + // a function to toggle fullscreen and a boolean indicating whether the element is fullscreen |
| 59 | + const { |
| 60 | + ref: fullscreenRef, |
| 61 | + toggle: toggleFullscreen, |
| 62 | + fullscreen: isFullscreen |
| 63 | + } = useFullscreen<HTMLDivElement>(); |
| 64 | + |
| 65 | + // This function is a wrapper around toggleFullscreen that also locks the screen orientation |
| 66 | + // to landscape when entering fullscreen and unlocks it when exiting fullscreen |
| 67 | + const enhancedToggleFullscreen = async () => { |
| 68 | + toggleFullscreen(); |
| 69 | + |
| 70 | + if (window.screen.orientation) { |
| 71 | + if (!isFullscreen) { |
| 72 | + window.screen.orientation.lock('landscape'); |
| 73 | + } else { |
| 74 | + window.screen.orientation.unlock(); |
| 75 | + } |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + const gameDisplayRef = React.useRef<HTMLDivElement | null>(null); |
| 80 | + |
| 81 | + // This function sets the gameDisplayRef and also calls the ref callback from useFullscreen |
| 82 | + // to attach the fullscreen logic to the game display element |
| 83 | + const setGameDisplayRefs = React.useCallback( |
| 84 | + (node: HTMLDivElement | null) => { |
| 85 | + // Refs returned by useRef() |
| 86 | + gameDisplayRef.current = node; |
| 87 | + |
| 88 | + // Ref callback from useFullscreen |
| 89 | + fullscreenRef(node); |
| 90 | + }, |
| 91 | + [fullscreenRef] |
| 92 | + ); |
| 93 | + |
| 94 | + // Logic for the fullscreen button to dynamically adjust its size, position and padding |
| 95 | + // based on the size of the game display. |
| 96 | + const [iconSize, setIconSize] = React.useState(0); |
| 97 | + const [iconLeft, setIconLeft] = React.useState('0px'); |
| 98 | + const [iconPadding, setIconPadding] = React.useState('0px'); |
| 99 | + |
| 100 | + React.useEffect(() => { |
| 101 | + const handleResize = () => { |
| 102 | + if (gameDisplayRef.current) { |
| 103 | + const aspectRatio = 16 / 9; |
| 104 | + const height = gameDisplayRef.current.offsetHeight; |
| 105 | + const width = gameDisplayRef.current.offsetWidth; |
| 106 | + const size = height / 40; |
| 107 | + const padding = height / 50; |
| 108 | + const leftOffset = |
| 109 | + isFullscreen || height * aspectRatio > width ? 0 : (width - height * aspectRatio) / 2; |
| 110 | + setIconSize(size); |
| 111 | + setIconPadding(`${padding}px`); |
| 112 | + setIconLeft(`${leftOffset}px`); |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + // When exiting fullscreen, the browser might not have completed the transition |
| 117 | + // at the time handleResize is called, so the height of gameDisplayRef.current |
| 118 | + // is still the fullscreen height. |
| 119 | + // To fix this, we delay handleResize by 100ms. |
| 120 | + const delayedHandleResize = () => { |
| 121 | + setTimeout(handleResize, 100); |
| 122 | + }; |
| 123 | + |
| 124 | + window.addEventListener('resize', delayedHandleResize); |
| 125 | + delayedHandleResize(); |
| 126 | + |
| 127 | + return () => window.removeEventListener('resize', delayedHandleResize); |
| 128 | + }, [isFullscreen]); |
| 129 | + |
53 | 130 | return (
|
54 | 131 | <>
|
55 |
| - <div id="game-display"></div> |
| 132 | + <div id="game-display" ref={setGameDisplayRefs}> |
| 133 | + <Icon |
| 134 | + id="fullscreen-button" |
| 135 | + icon={isFullscreen ? IconNames.MINIMIZE : IconNames.FULLSCREEN} |
| 136 | + color="white" |
| 137 | + htmlTitle={isFullscreen ? 'Exit full screen' : 'Full screen'} |
| 138 | + size={iconSize} |
| 139 | + onClick={enhancedToggleFullscreen} |
| 140 | + style={{ left: iconLeft, padding: iconPadding }} |
| 141 | + /> |
| 142 | + </div> |
56 | 143 | {isTestStudent && (
|
57 | 144 | <div className="Horizontal">
|
58 | 145 | <button
|
|
0 commit comments