import * as React from 'react'
|
import * as PIXI from 'pixi.js';
|
import { FormattedMessage, useIntl, useModel } from '@umijs/max';
|
import { Layout, Button, Flex, } from 'antd';
|
const { Header, Content } = Layout;
|
import './index.css'
|
import { createStyles } from 'antd-style';
|
import Edit from './components/edit'
|
import * as Utils from './utils'
|
import Player from './player';
|
|
const useStyles = createStyles(({ token }) => {
|
let dark = token.colorBgBase === '#000';
|
return {
|
layout: {
|
overflow: 'hidden',
|
},
|
header: {
|
height: 64,
|
paddingInline: 48,
|
lineHeight: '64px',
|
padding: 0,
|
},
|
flex: {
|
width: '100%',
|
height: '100%',
|
padding: '0 30px',
|
},
|
content: {
|
backgroundColor: '#F8FAFB',
|
height: 'calc(100vh - 120px)'
|
}
|
};
|
});
|
|
const Map = () => {
|
const { initialState, setInitialState } = useModel('@@initialState');
|
const { styles } = useStyles();
|
const mapRef = React.useRef();
|
const contentRef = React.useRef();
|
|
const [editModalVisible, setEditModalVisible] = React.useState(false);
|
const [windowSize, setWindowSize] = React.useState({
|
width: window.innerWidth,
|
height: window.innerHeight,
|
});
|
const [app, setApp] = React.useState(null)
|
const [mapContainer, setMapContainer] = React.useState(null)
|
|
React.useEffect(() => {
|
const player = new Player(mapRef.current);
|
setApp(player.app);
|
setMapContainer(player.mapContainer);
|
Utils.syncApp(player.app);
|
Utils.syncMapContainer(player.mapContainer);
|
|
const handleResize = () => {
|
setWindowSize({
|
width: window.innerWidth,
|
height: window.innerHeight,
|
});
|
};
|
window.addEventListener('resize', handleResize);
|
}, []);
|
|
React.useEffect(() => {
|
if (!app) {
|
return;
|
}
|
const width = contentRef.current.offsetWidth;
|
const height = contentRef.current.offsetHeight;
|
app.renderer.resize(width, height);
|
}, [app, mapContainer, windowSize])
|
|
const editHandle = () => {
|
setEditModalVisible(true);
|
}
|
|
const onDrop = (sprite, x, y) => {
|
const { mapX, mapY } = Utils.getRealPosition(x, y);
|
sprite.x = mapX;
|
sprite.y = mapY;
|
mapContainer.addChild(sprite);
|
}
|
|
return (
|
<>
|
<Layout className={styles.layout}>
|
<Header className={styles.header}>
|
<Flex className={styles.flex} gap={'large'} justify={'flex-end'} align={'center'}>
|
<Button onClick={editHandle} size={'large'}><FormattedMessage id='map.edit' defaultMessage='编辑地图' /></Button>
|
</Flex>
|
</Header>
|
<Content ref={contentRef} className={styles.content}>
|
<div ref={mapRef} style={{ position: "relative" }} />
|
</Content>
|
</Layout>
|
|
<Edit
|
open={editModalVisible}
|
onCancel={() => {
|
setEditModalVisible(false);
|
}}
|
refCurr={mapRef.current}
|
onDrop={onDrop}
|
/>
|
</>
|
)
|
}
|
|
export default Map;
|