#
luxiaotao1123
2024-03-03 b5a0c724fe1afbcfef4276b50b7121ca08459b17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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;