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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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import React, { useEffect, useMemo, useState } from 'react'
import * as Menubar from '@radix-ui/react-menubar'
import { CheckIcon, DotFilledIcon } from '@radix-ui/react-icons'

import classes from './styles.module.css'
import { BodyEditor } from '../../editor'
import i18n, { IsChina } from '../../i18n'
import { Helper } from '../../environments/online/helper'
import { uploadImage } from '../../utils/transfer'
import { getCurrentTime } from '../../utils/time'
import useForceUpdate from '../../hooks/useFoceUpdate'
import classNames from 'classnames'
import { useLanguageSelect } from '../../hooks'
import { ShowContextMenu } from '../ContextMenu'
import { ShowDialog } from '../Dialog'
import { SetCDNBase } from '../../utils/detect'

const {
    MenubarRoot,
    MenubarTrigger,
    MenubarContent,
    MenubarItem,
    MenubarCheckboxItem,
    MenubarRadioItem,
    MenubarItemIndicator,
    MenubarSeparator,
    RightSlot,
    Blue,
    inset,
} = classes
const MenubarDemo: React.FC<{
    editor: BodyEditor
    onChangeBackground: (url: string) => void
    onScreenShot: (data: Record<string, { src: string; title: string }>) => void
    style?: React.CSSProperties
}> = ({ editor, onChangeBackground, onScreenShot, style }) => {
    const forceUpdate = useForceUpdate()
    const helper = useMemo(() => new Helper(editor), [editor])
    const { current, changeLanguage, languagList } = useLanguageSelect()

    useEffect(() => {
        const show = (data: { mouseX: number; mouseY: number }) => {
            ShowContextMenu({ ...data, editor, onChangeBackground })
        }
        editor?.ContextMenuEventManager.AddEventListener(show)
        return () => {
            editor?.ContextMenuEventManager.RemoveEventListener(show)
        }
    }, [editor])

    return (
        <Menubar.Root className={MenubarRoot} style={style}>
            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    {i18n.t('File')}
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-3}
                    >
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.ResetScene()}
                        >
                            {i18n.t('Reset Scene')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.LoadScene()}
                        >
                            {i18n.t('Load Scene')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.SaveScene()}
                        >
                            {i18n.t('Save Scene')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => helper.LoadGesture()}
                        >
                            {i18n.t('Load Gesture')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => helper.SaveGesture()}
                        >
                            {i18n.t('Save Gesture')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => {
                                helper.GenerateSceneURL()
                            }}
                        >
                            {i18n.t('Generate Scene URL')}
                        </Menubar.Item>
                        <Menubar.Separator className={MenubarSeparator} />
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.RestoreLastSavedScene()}
                        >
                            {i18n.t('Restore Last Scene')}
                        </Menubar.Item>
                        <Menubar.Separator className={MenubarSeparator} />
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() =>
                                helper.DetectFromImage(onChangeBackground)
                            }
                        >
                            {i18n.t('Detect From Image')}
                        </Menubar.Item>
                        {IsChina() ? (
                            <Menubar.Item
                                className={MenubarItem}
                                onSelect={() => {
                                    SetCDNBase(false)
                                    helper.DetectFromImage(onChangeBackground)
                                }}
                            >
                                {i18n.t('Detect From Image') + ' [中国]'}
                            </Menubar.Item>
                        ) : undefined}
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => helper.SetRandomPose()}
                        >
                            {i18n.t('Set Random Pose')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={async () => {
                                const image = await uploadImage()
                                if (image) onChangeBackground(image)
                            }}
                        >
                            {i18n.t('Set Background Image')}
                        </Menubar.Item>
                        <Menubar.Item className={MenubarItem}>
                            v{__APP_VERSION__}
                        </Menubar.Item>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>

            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    {i18n.t('Edit')}
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-3}
                    >
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.Undo()}
                        >
                            {i18n.t('Undo')}
                            <div className={RightSlot}>⌘ Z</div>
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.Redo()}
                        >
                            {i18n.t('Redo')}
                            <div className={RightSlot}>⇧ ⌘ Z</div>
                        </Menubar.Item>
                        <Menubar.Separator className={MenubarSeparator} />
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.CopySelectedBody()}
                        >
                            {i18n.t('Duplicate Skeleton')}
                            <div className={RightSlot}>⇧ D</div>
                        </Menubar.Item>
                        <Menubar.Item
                            className={MenubarItem}
                            onSelect={() => editor.RemoveBody()}
                        >
                            {i18n.t('Delete Skeleton')}
                            <div className={RightSlot}>{i18n.t('Del')}</div>
                        </Menubar.Item>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>

            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    {i18n.t('View')}
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-14}
                    >
                        <Menubar.Item
                            className={classNames(MenubarItem, inset)}
                            onSelect={() => {
                                editor.LockView()
                            }}
                        >
                            {i18n.t('Lock View')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={classNames(MenubarItem, inset)}
                            onSelect={() => {
                                editor.UnlockView()
                            }}
                        >
                            {i18n.t('Unlock View')}
                        </Menubar.Item>
                        <Menubar.Item
                            className={classNames(MenubarItem, inset)}
                            onSelect={() => {
                                editor.RestoreView()
                            }}
                        >
                            {i18n.t('Restore View')}
                        </Menubar.Item>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>

            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    {i18n.t('Setting')}
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-14}
                    >
                        <Menubar.CheckboxItem
                            className={classNames(MenubarCheckboxItem, inset)}
                            checked={editor.MoveMode}
                            onCheckedChange={() => {
                                editor.MoveMode = !editor.MoveMode
                                forceUpdate()
                            }}
                        >
                            <Menubar.ItemIndicator
                                className={MenubarItemIndicator}
                            >
                                <CheckIcon />
                            </Menubar.ItemIndicator>
                            {i18n.t('Move Mode')}
                        </Menubar.CheckboxItem>
                        <Menubar.CheckboxItem
                            className={classNames(MenubarCheckboxItem, inset)}
                            checked={editor.FreeMode}
                            onCheckedChange={() => {
                                editor.FreeMode = !editor.FreeMode
                                forceUpdate()
                            }}
                        >
                            <Menubar.ItemIndicator
                                className={MenubarItemIndicator}
                            >
                                <CheckIcon />
                            </Menubar.ItemIndicator>
                            {i18n.t('Free Mode')}
                        </Menubar.CheckboxItem>
                        <Menubar.CheckboxItem
                            className={classNames(MenubarCheckboxItem, inset)}
                            checked={editor.OnlyHand}
                            onCheckedChange={() => {
                                editor.OnlyHand = !editor.OnlyHand
                                forceUpdate()
                            }}
                        >
                            <Menubar.ItemIndicator
                                className={MenubarItemIndicator}
                            >
                                <CheckIcon />
                            </Menubar.ItemIndicator>
                            {i18n.t('Only Hand')}
                        </Menubar.CheckboxItem>
                        <Menubar.CheckboxItem
                            className={classNames(MenubarCheckboxItem, inset)}
                            checked={editor.enablePreview}
                            onCheckedChange={() => {
                                editor.enablePreview = !editor.enablePreview
                                forceUpdate()
                            }}
                        >
                            <Menubar.ItemIndicator
                                className={MenubarItemIndicator}
                            >
                                <CheckIcon />
                            </Menubar.ItemIndicator>
                            {i18n.t('Show Preview')}
                        </Menubar.CheckboxItem>
                        <Menubar.CheckboxItem
                            className={classNames(MenubarCheckboxItem, inset)}
                            checked={editor.EnableHelper}
                            onCheckedChange={() => {
                                editor.EnableHelper = !editor.EnableHelper
                                forceUpdate()
                            }}
                        >
                            <Menubar.ItemIndicator
                                className={MenubarItemIndicator}
                            >
                                <CheckIcon />
                            </Menubar.ItemIndicator>
                            {i18n.t('Show Grid')}
                        </Menubar.CheckboxItem>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>

            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    {i18n.t('Feedback')}
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-14}
                    >
                        <Menubar.Item
                            className={classNames(MenubarItem, inset)}
                            onSelect={() => {
                                helper.FeedbackByGithub()
                            }}
                        >
                            Github
                        </Menubar.Item>
                        <Menubar.Item
                            className={classNames(MenubarItem, inset)}
                            onSelect={() => {
                                helper.FeedbackByQQ()
                            }}
                        >
                            QQ
                        </Menubar.Item>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>
            <Menubar.Menu>
                <Menubar.Trigger className={MenubarTrigger}>
                    Language
                </Menubar.Trigger>
                <Menubar.Portal>
                    <Menubar.Content
                        className={MenubarContent}
                        align="start"
                        sideOffset={5}
                        alignOffset={-14}
                    >
                        <Menubar.RadioGroup
                            value={current}
                            onValueChange={(v) => {
                                changeLanguage(v)
                            }}
                        >
                            {languagList.map((item) => (
                                <Menubar.RadioItem
                                    className={classNames(
                                        MenubarRadioItem,
                                        inset
                                    )}
                                    key={item}
                                    value={item}
                                >
                                    <Menubar.ItemIndicator
                                        className={MenubarItemIndicator}
                                    >
                                        <DotFilledIcon />
                                    </Menubar.ItemIndicator>
                                    {item}
                                </Menubar.RadioItem>
                            ))}
                        </Menubar.RadioGroup>
                    </Menubar.Content>
                </Menubar.Portal>
            </Menubar.Menu>
            <Menubar.Menu>
                <Menubar.Trigger
                    className={classNames(MenubarTrigger, Blue)}
                    onClick={async () => {
                        const image = editor.MakeImages()
                        const result = Object.fromEntries(
                            Object.entries(image).map(([name, imgData]) => [
                                name,
                                {
                                    src: imgData,
                                    title: name + '_' + getCurrentTime(),
                                },
                            ])
                        )
                        onScreenShot(result)
                    }}
                >
                    {i18n.t('Generate')}
                </Menubar.Trigger>
            </Menubar.Menu>
        </Menubar.Root>
    )
}

export default MenubarDemo