mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-11-25 20:34:47 +00:00
Closing the radial menu during the transition from the more pages would cause the menu to be stuck open
199 lines
6.5 KiB
TypeScript
199 lines
6.5 KiB
TypeScript
import { Box, createStyles } from '@mantine/core';
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
import { useEffect, useState } from 'react';
|
|
import { useNuiEvent } from '../../../hooks/useNuiEvent';
|
|
import { fetchNui } from '../../../utils/fetchNui';
|
|
import ScaleFade from '../../../transitions/ScaleFade';
|
|
import type { RadialMenuItem } from '../../../typings';
|
|
import { useLocales } from '../../../providers/LocaleProvider';
|
|
|
|
const useStyles = createStyles((theme) => ({
|
|
wrapper: {
|
|
position: 'absolute',
|
|
top: '50%',
|
|
left: '50%',
|
|
transform: 'translate(-50%, -50%)',
|
|
},
|
|
sector: {
|
|
fill: theme.colors.dark[6],
|
|
color: theme.colors.dark[0],
|
|
|
|
'&:hover': {
|
|
fill: theme.fn.primaryColor(),
|
|
'> g > text, > g > svg > path': {
|
|
fill: '#fff',
|
|
},
|
|
},
|
|
'> g > text': {
|
|
fill: theme.colors.dark[0],
|
|
},
|
|
},
|
|
backgroundCircle: {
|
|
fill: theme.colors.dark[6],
|
|
},
|
|
centerCircle: {
|
|
fill: theme.fn.primaryColor(),
|
|
color: '#fff',
|
|
stroke: theme.colors.dark[6],
|
|
strokeWidth: 4,
|
|
'&:hover': {
|
|
fill: theme.colors[theme.primaryColor][theme.fn.primaryShade() - 1],
|
|
},
|
|
},
|
|
centerIconContainer: {
|
|
position: 'absolute',
|
|
top: '50%',
|
|
left: '50%',
|
|
transform: 'translate(-50%, -50%)',
|
|
pointerEvents: 'none',
|
|
},
|
|
centerIcon: {
|
|
color: '#fff',
|
|
},
|
|
}));
|
|
|
|
// PAGE_ITEMS + 1 = More... button
|
|
const PAGE_ITEMS = 7;
|
|
|
|
const degToRad = (deg: number) => deg * (Math.PI / 180);
|
|
|
|
const RadialMenu: React.FC = () => {
|
|
const { classes } = useStyles();
|
|
const { locale } = useLocales();
|
|
const [visible, setVisible] = useState(false);
|
|
const [menuItems, setMenuItems] = useState<RadialMenuItem[]>([]);
|
|
const [menu, setMenu] = useState<{ items: RadialMenuItem[]; sub?: boolean; page: number }>({
|
|
items: [],
|
|
sub: false,
|
|
page: 1,
|
|
});
|
|
|
|
const changePage = async (increment?: boolean) => {
|
|
setVisible(false);
|
|
|
|
const didTransition: boolean = await fetchNui('radialTransition');
|
|
|
|
if (!didTransition) return;
|
|
|
|
setVisible(true);
|
|
setMenu({ ...menu, page: increment ? menu.page + 1 : menu.page - 1 });
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (menu.items.length < PAGE_ITEMS) return setMenuItems(menu.items);
|
|
const items = menu.items.slice(PAGE_ITEMS * (menu.page - 1), PAGE_ITEMS * menu.page);
|
|
PAGE_ITEMS * menu.page < menu.items.length &&
|
|
items.push({ icon: 'ellipsis-h', label: locale.ui.more, isMore: true });
|
|
setMenuItems(items);
|
|
}, [menu.items, menu.page]);
|
|
|
|
useNuiEvent(
|
|
'openRadialMenu',
|
|
async (data: { items: RadialMenuItem[]; sub?: boolean; page?: number } | false | number) => {
|
|
if (!data || typeof data === 'number') {
|
|
setVisible(false);
|
|
if (typeof data === 'number') setMenu((ps) => ({ ...ps, page: data }));
|
|
return;
|
|
}
|
|
// data.page currently unused but might need later?
|
|
setMenu((state) => ({ ...data, page: data.page || state.page }));
|
|
setVisible(true);
|
|
}
|
|
);
|
|
|
|
useNuiEvent('refreshItems', (data: RadialMenuItem[]) => {
|
|
setMenu({ ...menu, items: data });
|
|
});
|
|
|
|
return (
|
|
<>
|
|
<Box
|
|
className={classes.wrapper}
|
|
onContextMenu={async () => {
|
|
if (menu.sub) fetchNui('radialBack');
|
|
else if (menu.page > 1) await changePage();
|
|
}}
|
|
>
|
|
<ScaleFade visible={visible}>
|
|
<svg width="350px" height="350px" transform="rotate(90)">
|
|
{/*Fixed issues with background circle extending the circle when there's less than 3 items*/}
|
|
<g transform="translate(175, 175)">
|
|
<circle r={175} className={classes.backgroundCircle} />
|
|
</g>
|
|
{menuItems.map((item, index) => {
|
|
// Always draw full circle to avoid elipse circles with 2 or less items
|
|
const pieAngle = 360 / (menuItems.length < 3 ? 3 : menuItems.length);
|
|
const angle = degToRad(pieAngle / 2 + 90);
|
|
const gap = 0;
|
|
const radius = 175 * 0.65 - gap;
|
|
const sinAngle = Math.sin(angle);
|
|
const cosAngle = Math.cos(angle);
|
|
const iconX = 175 + sinAngle * radius;
|
|
const iconY = 175 + cosAngle * radius;
|
|
|
|
return (
|
|
<>
|
|
<g
|
|
transform={`rotate(-${index * pieAngle} 175 175) translate(${sinAngle * gap}, ${cosAngle * gap})`}
|
|
className={classes.sector}
|
|
onClick={async () => {
|
|
const clickIndex = menu.page === 1 ? index : PAGE_ITEMS * (menu.page - 1) + index;
|
|
if (!item.isMore) fetchNui('radialClick', clickIndex);
|
|
else {
|
|
await changePage(true);
|
|
}
|
|
}}
|
|
>
|
|
<path
|
|
d={`M175.01,175.01 l${175 - gap},0 A175.01,175.01 0 0,0 ${
|
|
175 + (175 - gap) * Math.cos(-degToRad(pieAngle))
|
|
}, ${175 + (175 - gap) * Math.sin(-degToRad(pieAngle))} z`}
|
|
/>
|
|
<g transform={`rotate(${index * pieAngle - 90} ${iconX} ${iconY})`} pointerEvents="none">
|
|
<FontAwesomeIcon
|
|
x={iconX - 12.5}
|
|
y={iconY - 17.5}
|
|
icon={item.icon}
|
|
width={25}
|
|
height={25}
|
|
fixedWidth
|
|
/>
|
|
<text x={iconX} y={iconY + 25} fill="#fff" textAnchor="middle" pointerEvents="none">
|
|
{item.label}
|
|
</text>
|
|
</g>
|
|
</g>
|
|
</>
|
|
);
|
|
})}
|
|
<g
|
|
transform={`translate(175, 175)`}
|
|
onClick={async () => {
|
|
if (menu.sub) fetchNui('radialBack');
|
|
else if (menu.page > 1) await changePage();
|
|
else {
|
|
setVisible(false);
|
|
fetchNui('radialClose');
|
|
}
|
|
}}
|
|
>
|
|
<circle r={32} className={classes.centerCircle} />
|
|
</g>
|
|
</svg>
|
|
<div className={classes.centerIconContainer}>
|
|
<FontAwesomeIcon
|
|
icon={!menu.sub && menu.page < 2 ? 'xmark' : 'arrow-rotate-left'}
|
|
fixedWidth
|
|
className={classes.centerIcon}
|
|
color="#fff"
|
|
size="2x"
|
|
/>
|
|
</div>
|
|
</ScaleFade>
|
|
</Box>
|
|
</>
|
|
);
|
|
};
|
|
|
|
export default RadialMenu;
|