#
luxiaotao1123
2024-10-10 cbd9fde3a2f8d4f5c45bea1a5215ad843e8dabc6
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import React, { useEffect } from 'react';
import { useForm, Controller } from 'react-hook-form';
import {
    Box,
    Grid,
    Typography,
    TextField,
    Slider,
    Button,
    Select,
    MenuItem,
    InputLabel,
    FormControl,
    FormHelperText,
    Checkbox,
    FormControlLabel,
    Stack,
} from '@mui/material';
import { useTranslate } from 'react-admin';
 
const MapSettings = (props) => {
    const { sprite, onSubmit } = props;
    const translate = useTranslate();
 
    const { control, handleSubmit, reset, watch, setValue } = useForm({
        defaultValues: {
            x: sprite?.position.x || 0,
            y: sprite?.position.y || 0,
            scaleX: sprite?.scale.x || 1,
            scaleY: sprite?.scale.y || 1,
            rotation: (sprite?.rotation * 180) / Math.PI || 0,
            copyDirection: 'right',
            copyCount: 1,
            // 其他默认值...
        },
    });
 
    // 监听表单值的变化
    const watchAllFields = watch();
 
    useEffect(() => {
        if (sprite) {
            reset({
                x: sprite.position.x,
                y: sprite.position.y,
                scaleX: sprite.scale.x,
                scaleY: sprite.scale.y,
                rotation: (sprite.rotation * 180) / Math.PI,
                // 设置其他字段的初始值
            });
        }
    }, [sprite, reset]);
 
    // 更新精灵属性的函数
    const updateSprite = (data) => {
        if (sprite) {
            sprite.position.x = data.x;
            sprite.position.y = data.y;
            sprite.scale.x = data.scaleX;
            sprite.scale.y = data.scaleY;
            sprite.rotation = (data.rotation * Math.PI) / 180;
            // 更新其他属性...
        }
    };
 
    // 处理表单提交
    const onFormSubmit = (data) => {
        updateSprite(data);
        if (onSubmit) {
            onSubmit(data);
        }
    };
 
    return (
        <Box component="form" onSubmit={handleSubmit(onFormSubmit)} noValidate sx={{ mt: 2 }}>
            <Grid container spacing={2}>
                {/* 位置 */}
                <Grid item xs={12}>
                    <Typography variant="h6">{translate('map.settings.position')}</Typography>
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="x"
                        control={control}
                        render={({ field }) => (
                            <TextField
                                {...field}
                                label="X"
                                type="number"
                                fullWidth
                                onChange={(e) => {
                                    field.onChange(e);
                                    updateSprite({ ...watchAllFields, x: parseFloat(e.target.value) });
                                }}
                            />
                        )}
                    />
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="y"
                        control={control}
                        render={({ field }) => (
                            <TextField
                                {...field}
                                label="Y"
                                type="number"
                                fullWidth
                                onChange={(e) => {
                                    field.onChange(e);
                                    updateSprite({ ...watchAllFields, y: parseFloat(e.target.value) });
                                }}
                            />
                        )}
                    />
                </Grid>
 
                {/* 缩放 */}
                <Grid item xs={12}>
                    <Typography variant="h6">{translate('map.settings.scale')}</Typography>
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="scaleX"
                        control={control}
                        render={({ field }) => (
                            <TextField
                                {...field}
                                label="Scale X"
                                type="number"
                                fullWidth
                                inputProps={{ step: 0.1, min: 0.1, max: 10 }}
                                onChange={(e) => {
                                    field.onChange(e);
                                    updateSprite({ ...watchAllFields, scaleX: parseFloat(e.target.value) });
                                }}
                            />
                        )}
                    />
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="scaleY"
                        control={control}
                        render={({ field }) => (
                            <TextField
                                {...field}
                                label="Scale Y"
                                type="number"
                                fullWidth
                                inputProps={{ step: 0.1, min: 0.1, max: 10 }}
                                onChange={(e) => {
                                    field.onChange(e);
                                    updateSprite({ ...watchAllFields, scaleY: parseFloat(e.target.value) });
                                }}
                            />
                        )}
                    />
                </Grid>
 
                {/* 旋转 */}
                <Grid item xs={12}>
                    <Typography variant="h6">{translate('map.settings.rotation')}</Typography>
                </Grid>
                <Grid item xs={12}>
                    <Controller
                        name="rotation"
                        control={control}
                        render={({ field }) => (
                            <Slider
                                {...field}
                                min={0}
                                max={360}
                                step={1}
                                valueLabelDisplay="auto"
                                onChange={(e, value) => {
                                    field.onChange(value);
                                    updateSprite({ ...watchAllFields, rotation: value });
                                }}
                            />
                        )}
                    />
                </Grid>
 
                {/* 复制 */}
                <Grid item xs={12}>
                    <Typography variant="h6">{translate('map.settings.copy')}</Typography>
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="copyDirection"
                        control={control}
                        render={({ field }) => (
                            <FormControl fullWidth>
                                <InputLabel>{translate('map.settings.copyDirection')}</InputLabel>
                                <Select {...field} label={translate('map.settings.copyDirection')}>
                                    <MenuItem value="left">{translate('map.settings.left')}</MenuItem>
                                    <MenuItem value="right">{translate('map.settings.right')}</MenuItem>
                                    <MenuItem value="up">{translate('map.settings.up')}</MenuItem>
                                    <MenuItem value="down">{translate('map.settings.down')}</MenuItem>
                                </Select>
                            </FormControl>
                        )}
                    />
                </Grid>
                <Grid item xs={6}>
                    <Controller
                        name="copyCount"
                        control={control}
                        render={({ field }) => (
                            <TextField
                                {...field}
                                label={translate('map.settings.copyCount')}
                                type="number"
                                fullWidth
                                inputProps={{ min: 1 }}
                            />
                        )}
                    />
                </Grid>
 
                {/* 提交按钮 */}
                <Grid item xs={12}>
                    <Stack direction="row" spacing={2}>
                        <Button type="submit" variant="contained" color="primary">
                            {translate('common.submit')}
                        </Button>
                        <Button variant="outlined" color="secondary" onClick={() => reset()}>
                            {translate('common.reset')}
                        </Button>
                    </Stack>
                </Grid>
            </Grid>
        </Box>
    );
};
 
export default MapSettings;