#
Junjie
2024-03-11 b6468c97166a649147c476767cfc78cd86ac9b41
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as React from 'react'
import * as PIXI from 'pixi.js';
import { FormattedMessage, useIntl, useModel } from '@umijs/max';
import { Layout, Button, Flex, Row, Col, FloatButton } from 'antd';
const { Header, Content } = Layout;
import {
    AppstoreAddOutlined,
    FileAddOutlined,
    CompressOutlined,
} from '@ant-design/icons';
import './index.css'
import { createStyles } from 'antd-style';
import Edit from './components/device'
import * as Utils from './utils'
import Player from './player';
 
const useStyles = createStyles(({ token }) => {
    let dark = token.colorBgBase === '#000';
    return {
        dark: dark,
        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)'
        },
    };
});
 
let player;
 
const Map = () => {
    const { initialState, setInitialState } = useModel('@@initialState');
    const { styles } = useStyles();
    const mapRef = React.useRef();
    const contentRef = React.useRef();
 
    const [deviceVisible, setDeviceVisible] = 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);
    const [mapEditModel, setMapEditModel] = React.useState(false);
 
    React.useEffect(() => {
        player = new Player(mapRef.current, styles.dark);
        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])
 
    React.useEffect(() => {
        if (!mapContainer) {
            return;
        }
        if (mapEditModel) {
            player.showGridlines();
        } else {
            player.hideGridlines();
        }
    }, [mapEditModel]);
 
    const onDrop = (sprite, x, y) => {
        const { mapX, mapY } = Utils.getRealPosition(x, y, mapContainer);
        sprite.x = mapX;
        sprite.y = mapY;
        mapContainer.addChild(sprite);
    };
 
    return (
        <>
            <Layout className={styles.layout}>
                <Header className={styles.header}>
                    <Row style={{ height: '100%' }}>
                        <Col span={8} style={{ backgroundColor: '#3C40C6' }}></Col>
                        <Col span={16} style={{ backgroundColor: '#3C40C6' }}>
                            <Flex className={styles.flex} gap={'large'} justify={'flex-end'} align={'center'}>
                                <Button onClick={() => setMapEditModel(!mapEditModel)} size={'large'}>
                                    {!mapEditModel
                                        ? <span style={{ fontWeight: 'bold' }}><FormattedMessage id='map.edit' defaultMessage='编辑地图' /></span>
                                        : <span style={{ color: 'red', fontWeight: 'bold' }}><FormattedMessage id='map.edit.close' defaultMessage='退出编辑' /></span>
                                    }
                                </Button>
                            </Flex>
                        </Col>
                    </Row>
                </Header>
                <Content ref={contentRef} className={styles.content}>
                    <div ref={mapRef} style={{ position: "relative" }} />
 
                    <FloatButton.Group
                        shape="square"
                        style={{
                            left: 35,
                            bottom: 35
                        }}
                    >
                        <FloatButton
                            icon={<CompressOutlined />}
                        />
                        <FloatButton.BackTop visibilityHeight={0} />
                    </FloatButton.Group>
 
                    <FloatButton.Group
                        hidden={!mapEditModel}
                        trigger="hover"
                        style={{
                            right: 35,
                            bottom: 35
                        }}
                        icon={<AppstoreAddOutlined />}
                    >
                        <FloatButton
                            tooltip={<div><FormattedMessage id='map.device.add' defaultMessage='添加设备' /></div>}
                            icon={<FileAddOutlined />}
                            onClick={() => {
                                setDeviceVisible(true);
                            }}
                        />
                    </FloatButton.Group>
                </Content>
            </Layout>
 
            <Edit
                open={deviceVisible}
                onCancel={() => {
                    setDeviceVisible(false);
                }}
                refCurr={mapRef.current}
                onDrop={onDrop}
            />
        </>
    )
}
 
export default Map;