safe rotation in settings menu, reset game icon

This commit is contained in:
Viktor Rådberg
2023-09-26 16:54:23 +02:00
parent de7bde1b2b
commit 7d60a668c6
3 changed files with 393 additions and 363 deletions

View File

@@ -30,7 +30,7 @@ const Container = styled.div<{ $rotation: Rotation }>`
}} }}
`; `;
const ExtraCountersGrid = styled.div<{ $rotation: Rotation }>` export const ExtraCountersGrid = styled.div<{ $rotation: Rotation }>`
display: flex; display: flex;
position: absolute; position: absolute;
width: 100%; width: 100%;

View File

@@ -1,7 +1,111 @@
import { Button } from '@mui/material'; import { Button, Checkbox } from '@mui/material';
import styled, { css } from 'styled-components'; import styled, { css } from 'styled-components';
import { Player, Rotation } from '../../Types/Player'; import { Player, Rotation } from '../../Types/Player';
import Settings from './Settings'; import { theme } from '../../Data/theme';
import { useGlobalSettings } from '../../Hooks/useGlobalSettings';
import { usePlayers } from '../../Hooks/usePlayers';
import {
PartnerTax,
Poison,
Energy,
Experience,
Exit,
FullscreenOff,
FullscreenOn,
Cross,
ResetGame,
} from '../../Icons/generated';
import { useEffect, useRef } from 'react';
import { Spacer } from '../Misc/Spacer';
const SettingsContainer = styled.div<{
$rotation: Rotation;
}>`
display: flex;
flex-direction: row;
flex-wrap: wrap;
height: 100%;
width: 100%;
${(props) => {
if (
props.$rotation === Rotation.SideFlipped ||
props.$rotation === Rotation.Side
) {
return css`
flex-direction: column-reverse;
height: 100%;
width: 100%;
`;
}
}}
${(props) => {
if (props.$rotation === Rotation.Side) {
return css`
rotate: ${props.$rotation - 180}deg;
`;
} else if (props.$rotation === Rotation.SideFlipped) {
return css`
rotate: ${props.$rotation - 180}deg;
`;
}
}}
`;
const BetterRowContainer = styled.div`
display: flex;
flex-direction: column;
flex-grow: 1;
width: 100%;
height: 100%;
justify-content: end;
align-items: stretch;
`;
const TogglesSection = styled.div`
display: flex;
position: relative;
flex-direction: row;
gap: 0.5rem;
justify-content: space-evenly;
`;
const ButtonsSections = styled.div`
position: relative;
display: flex;
gap: 1rem;
justify-content: space-between;
padding: 3% 3%;
align-items: center;
`;
const ColorPicker = styled.input`
position: absolute;
top: 5%;
left: 5%;
height: 8vmax;
width: 8vmax;
border: none;
outline: none;
cursor: pointer;
background-color: transparent;
user-select: none;
color: #ffffff;
`;
const CheckboxContainer = styled.div<{ $rotation: Rotation }>`
${(props) => {
if (
props.$rotation === Rotation.SideFlipped ||
props.$rotation === Rotation.Side
) {
return css`
/* rotate: ${props.$rotation - 180}deg; */
`;
}
}}
`;
const PlayerMenuWrapper = styled.div<{ const PlayerMenuWrapper = styled.div<{
$rotation: Rotation; $rotation: Rotation;
@@ -66,25 +170,304 @@ type PlayerMenuProps = {
}; };
const PlayerMenu = ({ player, setShowPlayerMenu }: PlayerMenuProps) => { const PlayerMenu = ({ player, setShowPlayerMenu }: PlayerMenuProps) => {
const settingsContainerRef = useRef<HTMLDivElement | null>(null);
const isSide =
player.settings.rotation === Rotation.Side ||
player.settings.rotation === Rotation.SideFlipped;
useEffect(() => {
if (!settingsContainerRef.current) {
return;
}
if (isSide) {
//set height to 100% of the width of the parent
settingsContainerRef.current.style.height = `${settingsContainerRef.current.parentElement?.clientWidth}px`;
//set width to 100% of the height of the parent
settingsContainerRef.current.style.width = `${settingsContainerRef.current.parentElement?.clientHeight}px`;
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [settingsContainerRef]);
const handleOnClick = () => { const handleOnClick = () => {
setShowPlayerMenu(false); setShowPlayerMenu(false);
}; };
const { fullscreen, wakeLock, goToStart } = useGlobalSettings();
const { updatePlayer, resetCurrentGame } = usePlayers();
const handleColorChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const updatedPlayer = { ...player, color: event.target.value };
updatePlayer(updatedPlayer);
};
const handleSettingsChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, checked } = event.target;
const updatedSettings = { ...player.settings, [name]: checked };
const updatedPlayer = { ...player, settings: updatedSettings };
updatePlayer(updatedPlayer);
};
const handleResetGame = () => {
resetCurrentGame();
setShowPlayerMenu(false);
};
const toggleFullscreen = () => {
if (fullscreen.isFullscreen) {
fullscreen.disableFullscreen();
} else {
fullscreen.enableFullscreen();
}
};
const buttonFontSize = isSide ? '1.5vmax' : '3vmin';
const iconSize = isSide ? '6vmin' : '3vmax';
const extraCountersSize = isSide ? '8vmin' : '4vmax';
const closeButtonSize = isSide ? '6vmin' : '3vmax';
return ( return (
<PlayerMenuWrapper $rotation={player.settings.rotation}> <PlayerMenuWrapper $rotation={player.settings.rotation}>
<CloseButton $rotation={player.settings.rotation}> <CloseButton $rotation={player.settings.rotation}>
<Button <Button
style={{ variant="text"
padding: '0 8px',
minWidth: '0',
}}
variant="outlined"
onClick={handleOnClick} onClick={handleOnClick}
style={{
margin: 0,
padding: 0,
height: closeButtonSize,
width: closeButtonSize,
}}
> >
X <Cross size={closeButtonSize} />
</Button> </Button>
</CloseButton> </CloseButton>
<Settings player={player} setShowPlayerMenu={setShowPlayerMenu} /> <SettingsContainer
$rotation={player.settings.rotation}
ref={settingsContainerRef}
>
<ColorPicker
type="color"
value={player.color}
onChange={handleColorChange}
role="button"
aria-label="Color picker"
/>
<BetterRowContainer>
<TogglesSection>
{player.settings.useCommanderDamage && (
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="usePartner"
checked={player.settings.usePartner}
icon={
<PartnerTax
size={extraCountersSize}
color="black"
stroke="white"
strokeWidth="30"
/>
}
checkedIcon={
<PartnerTax
size={extraCountersSize}
color={player.color}
stroke="white"
strokeWidth="30"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.usePartner}
aria-label="Partner"
/>
</CheckboxContainer>
)}
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="usePoison"
checked={player.settings.usePoison}
icon={
<Poison
size={extraCountersSize}
color="black"
stroke="white"
strokeWidth="30"
/>
}
checkedIcon={
<Poison
size={extraCountersSize}
color={player.color}
stroke="white"
strokeWidth="30"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.usePoison}
aria-label="Poison"
/>
</CheckboxContainer>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="useEnergy"
checked={player.settings.useEnergy}
icon={
<Energy
size={extraCountersSize}
color="black"
stroke="white"
strokeWidth="15"
/>
}
checkedIcon={
<Energy
size={extraCountersSize}
color={player.color}
stroke="white"
strokeWidth="15"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.useEnergy}
aria-label="Energy"
/>
</CheckboxContainer>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="useExperience"
checked={player.settings.useExperience}
icon={
<Experience
size={extraCountersSize}
color="black"
stroke="white"
strokeWidth="15"
/>
}
checkedIcon={
<Experience
size={extraCountersSize}
color={player.color}
stroke="white"
strokeWidth="15"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.useExperience}
aria-label="Experience"
/>
</CheckboxContainer>
</TogglesSection>
<Spacer height="1rem" />
<ButtonsSections>
<Button
variant="text"
style={{
cursor: 'pointer',
userSelect: 'none',
}}
onClick={goToStart}
aria-label="Back to start"
>
<Exit size={iconSize} style={{ rotate: '180deg' }} />
</Button>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="fullscreen"
checked={document.fullscreenElement ? true : false}
icon={
<FullscreenOff
size={iconSize}
color={theme.palette.primary.main}
/>
}
checkedIcon={<FullscreenOn size={iconSize} />}
onChange={toggleFullscreen}
role="checkbox"
aria-checked={document.fullscreenElement ? true : false}
aria-label="Fullscreen"
/>
</CheckboxContainer>
<Button
variant={wakeLock.active ? 'contained' : 'outlined'}
style={{
cursor: 'pointer',
userSelect: 'none',
fontSize: buttonFontSize,
padding: '0 4px 0 4px',
}}
onClick={wakeLock.toggleWakeLock}
role="checkbox"
aria-checked={wakeLock.active}
aria-label="Keep awake"
>
Keep Awake
</Button>
<Button
style={{
cursor: 'pointer',
userSelect: 'none',
fontSize: buttonFontSize,
padding: '4px',
}}
onClick={() => {
settingsContainerRef.current?.querySelector(`dialog`)?.show();
}}
role="checkbox"
aria-checked={wakeLock.active}
aria-label="Reset Game"
>
<ResetGame size={iconSize} />
</Button>
</ButtonsSections>
</BetterRowContainer>
<dialog
id={`reset-game-${player.index}`}
style={{
borderRadius: '1rem',
backgroundColor: theme.palette.primary.main,
position: 'absolute',
bottom: '0',
}}
>
<h3>Reset Game?</h3>
<div
style={{
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-evenly',
}}
>
<button
onClick={() => {
settingsContainerRef.current?.querySelector(`dialog`)?.close();
}}
>
No
</button>
<button
onClick={() => {
handleResetGame();
settingsContainerRef.current?.querySelector(`dialog`)?.close();
}}
>
Yes
</button>
</div>
</dialog>
</SettingsContainer>
</PlayerMenuWrapper> </PlayerMenuWrapper>
); );
}; };

View File

@@ -1,353 +0,0 @@
import { Button, Checkbox } from '@mui/material';
import styled, { css } from 'styled-components';
import { theme } from '../../Data/theme';
import { useGlobalSettings } from '../../Hooks/useGlobalSettings';
import {
Energy,
Exit,
Experience,
FullscreenOff,
FullscreenOn,
PartnerTax,
Poison,
} from '../../Icons/generated';
import { Player, Rotation } from '../../Types/Player';
import { usePlayers } from '../../Hooks/usePlayers';
const SettingsContainer = styled.div<{
$rotation: Rotation;
}>`
display: flex;
position: relative;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
height: 100%;
width: 80%;
${(props) => {
if (
props.$rotation === Rotation.SideFlipped ||
props.$rotation === Rotation.Side
) {
return css`
flex-direction: column-reverse;
height: 100%;
width: 100%;
`;
}
}}
`;
const TogglesSection = styled.div<{ $rotation: Rotation }>`
display: flex;
position: absolute;
flex-direction: row;
justify-content: space-evenly;
gap: 0.5rem;
${(props) => {
if (
props.$rotation === Rotation.SideFlipped ||
props.$rotation === Rotation.Side
) {
return css`
flex-direction: column-reverse;
`;
}
}}
`;
const ButtonsSections = styled.div<{ $rotation: Rotation }>`
position: absolute;
display: flex;
gap: 1rem;
bottom: 16px;
${(props) => {
if (props.$rotation === Rotation.Side) {
return css`
bottom: auto;
right: -6rem;
rotate: ${props.$rotation - 180}deg;
`;
} else if (props.$rotation === Rotation.SideFlipped) {
return css`
bottom: auto;
left: -6rem;
rotate: ${props.$rotation - 180}deg;
`;
}
}}
`;
const ColorPicker = styled.input<{
$rotation: Rotation;
}>`
position: absolute;
top: 5%;
left: 5%;
height: 8vmax;
width: 8vmax;
border: none;
outline: none;
cursor: pointer;
background-color: transparent;
user-select: none;
color: #ffffff;
${(props) => {
if (props.$rotation === Rotation.Side) {
return css`
rotate: ${props.$rotation - 180}deg;
bottom: 5%;
top: auto;
`;
} else if (props.$rotation === Rotation.SideFlipped) {
return css`
rotate: ${props.$rotation - 180}deg;
top: 5%;
left: auto;
right: 5%;
`;
}
}}
`;
const CheckboxContainer = styled.div<{ $rotation: Rotation }>`
${(props) => {
if (
props.$rotation === Rotation.SideFlipped ||
props.$rotation === Rotation.Side
) {
return css`
rotate: ${props.$rotation - 180}deg;
`;
}
}}
`;
type SettingsProps = {
player: Player;
setShowPlayerMenu: (showPlayerMenu: boolean) => void;
};
const Settings = ({ player, setShowPlayerMenu }: SettingsProps) => {
const { fullscreen, wakeLock, goToStart } = useGlobalSettings();
const { updatePlayer, resetCurrentGame } = usePlayers();
const isSide =
player.settings.rotation === Rotation.Side ||
player.settings.rotation === Rotation.SideFlipped;
const handleColorChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const updatedPlayer = { ...player, color: event.target.value };
updatePlayer(updatedPlayer);
};
const handleSettingsChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { name, checked } = event.target;
const updatedSettings = { ...player.settings, [name]: checked };
const updatedPlayer = { ...player, settings: updatedSettings };
updatePlayer(updatedPlayer);
};
const handleResetGame = () => {
resetCurrentGame();
setShowPlayerMenu(false);
};
const toggleFullscreen = () => {
if (fullscreen.isFullscreen) {
fullscreen.disableFullscreen();
} else {
fullscreen.enableFullscreen();
}
};
const buttonFontSize = isSide ? '1.5vmax' : '3vmin';
return (
<SettingsContainer $rotation={player.settings.rotation}>
<ColorPicker
$rotation={player.settings.rotation}
type="color"
value={player.color}
onChange={handleColorChange}
role="button"
aria-label="Color picker"
/>
<TogglesSection $rotation={player.settings.rotation}>
{player.settings.useCommanderDamage && (
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="usePartner"
checked={player.settings.usePartner}
icon={
<PartnerTax
size="6vmax"
color="black"
stroke="white"
strokeWidth="30"
/>
}
checkedIcon={
<PartnerTax
size="6vmax"
color={player.color}
stroke="white"
strokeWidth="30"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.usePartner}
aria-label="Partner"
/>
</CheckboxContainer>
)}
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="usePoison"
checked={player.settings.usePoison}
icon={
<Poison
size="6vmax"
color="black"
stroke="white"
strokeWidth="30"
/>
}
checkedIcon={
<Poison
size="6vmax"
color={player.color}
stroke="white"
strokeWidth="30"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.usePoison}
aria-label="Poison"
/>
</CheckboxContainer>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="useEnergy"
checked={player.settings.useEnergy}
icon={
<Energy
size="6vmax"
color="black"
stroke="white"
strokeWidth="15"
/>
}
checkedIcon={
<Energy
size="6vmax"
color={player.color}
stroke="white"
strokeWidth="15"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.useEnergy}
aria-label="Energy"
/>
</CheckboxContainer>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="useExperience"
checked={player.settings.useExperience}
icon={
<Experience
size="6vmax"
color="black"
stroke="white"
strokeWidth="15"
/>
}
checkedIcon={
<Experience
size="6vmax"
color={player.color}
stroke="white"
strokeWidth="15"
/>
}
onChange={handleSettingsChange}
role="checkbox"
aria-checked={player.settings.useExperience}
aria-label="Experience"
/>
</CheckboxContainer>
</TogglesSection>
<ButtonsSections $rotation={player.settings.rotation}>
<Button
variant="text"
style={{
cursor: 'pointer',
userSelect: 'none',
}}
onClick={goToStart}
aria-label="Back to start"
>
<Exit size="4vmax" style={{ rotate: '180deg' }} />
</Button>
<CheckboxContainer $rotation={player.settings.rotation}>
<Checkbox
name="fullscreen"
checked={document.fullscreenElement ? true : false}
icon={
<FullscreenOff size="4vmax" color={theme.palette.primary.main} />
}
checkedIcon={<FullscreenOn size="4vmax" />}
onChange={toggleFullscreen}
role="checkbox"
aria-checked={document.fullscreenElement ? true : false}
aria-label="Fullscreen"
/>
</CheckboxContainer>
<Button
variant={wakeLock.active ? 'contained' : 'outlined'}
style={{
cursor: 'pointer',
userSelect: 'none',
fontSize: buttonFontSize,
padding: '0 4px 0 4px',
}}
onClick={wakeLock.toggleWakeLock}
role="checkbox"
aria-checked={wakeLock.active}
aria-label="Keep awake"
>
Keep Awake
</Button>
<Button
variant="contained"
style={{
cursor: 'pointer',
userSelect: 'none',
fontSize: buttonFontSize,
padding: '0 4px 0 4px',
}}
onClick={handleResetGame}
role="checkbox"
aria-checked={wakeLock.active}
aria-label="Reset Game"
>
Reset Game
</Button>
</ButtonsSections>
</SettingsContainer>
);
};
export default Settings;