Skip to content
This repository was archived by the owner on Aug 21, 2024. It is now read-only.

Commit 57f0031

Browse files
authored
show uploaded avatar preview in admin avatar create (#9379)
* show uploaded avatar preview in admin avatar create * remove console log
1 parent abb3c23 commit 57f0031

File tree

1 file changed

+14
-96
lines changed

1 file changed

+14
-96
lines changed

packages/client-core/src/admin/components/Avatars/AvatarDrawer.tsx

Lines changed: 14 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ All portions of the code written by the Ethereal Engine team are Copyright © 20
2323
Ethereal Engine. All Rights Reserved.
2424
*/
2525

26-
import React, { useEffect, useRef } from 'react'
26+
import React, { useEffect } from 'react'
2727
import { useTranslation } from 'react-i18next'
2828

2929
import ConfirmDialog from '@etherealengine/client-core/src/common/components/ConfirmDialog'
3030
import InputFile from '@etherealengine/client-core/src/common/components/InputFile'
3131
import InputRadio from '@etherealengine/client-core/src/common/components/InputRadio'
3232
import InputText from '@etherealengine/client-core/src/common/components/InputText'
33-
import LoadingView from '@etherealengine/client-core/src/common/components/LoadingView'
3433
import { getCanvasBlob, isValidHttpUrl } from '@etherealengine/client-core/src/common/utils'
3534
import {
3635
AVATAR_FILE_ALLOWED_EXTENSIONS,
@@ -43,8 +42,6 @@ import {
4342
THUMBNAIL_WIDTH
4443
} from '@etherealengine/common/src/constants/AvatarConstants'
4544
import { AssetLoader } from '@etherealengine/engine/src/assets/classes/AssetLoader'
46-
import { AvatarRigComponent } from '@etherealengine/engine/src/avatar/components/AvatarAnimationComponent'
47-
import { getOptionalComponent } from '@etherealengine/engine/src/ecs/functions/ComponentFunctions'
4845
import { AvatarType } from '@etherealengine/engine/src/schemas/user/avatar.schema'
4946
import { useHookstate } from '@etherealengine/hyperflux'
5047
import Box from '@etherealengine/ui/src/primitives/mui/Box'
@@ -55,12 +52,10 @@ import DialogTitle from '@etherealengine/ui/src/primitives/mui/DialogTitle'
5552
import FormControl from '@etherealengine/ui/src/primitives/mui/FormControl'
5653
import FormHelperText from '@etherealengine/ui/src/primitives/mui/FormHelperText'
5754
import Icon from '@etherealengine/ui/src/primitives/mui/Icon'
58-
import Tooltip from '@etherealengine/ui/src/primitives/mui/Tooltip'
5955
import Typography from '@etherealengine/ui/src/primitives/mui/Typography'
6056

57+
import AvatarPreview from '../../../common/components/AvatarPreview'
6158
import { NotificationService } from '../../../common/services/NotificationService'
62-
import { loadAvatarForPreview, resetAnimationLogic } from '../../../user/components/Panel3D/helperFunctions'
63-
import { useRender3DPanelSystem } from '../../../user/components/Panel3D/useRender3DPanelSystem'
6459
import { AvatarService } from '../../../user/services/AvatarService'
6560
import { userHasAccess } from '../../../user/userHasAccess'
6661
import DrawerView from '../../common/DrawerView'
@@ -102,14 +97,11 @@ const defaultState = {
10297

10398
const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) => {
10499
const { t } = useTranslation()
105-
const panelRef = useRef() as React.MutableRefObject<HTMLDivElement>
106100
const editMode = useHookstate(false)
107101
const state = useHookstate({ ...defaultState })
108102
const avatarLoading = useHookstate(false)
109103
const showConfirm = useHookstate(ConfirmState.None)
110-
111-
const renderPanel = useRender3DPanelSystem(panelRef)
112-
const { entity, camera, scene, renderer } = renderPanel.state
104+
const avatarSrc = useHookstate('')
113105

114106
const hasWriteAccess = userHasAccess('static_resource:write')
115107
const viewMode = mode === AvatarDrawerMode.ViewEdit && !editMode.value
@@ -165,22 +157,7 @@ const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) =>
165157
}
166158
}
167159

168-
if (url) {
169-
avatarLoading.set(true)
170-
resetAnimationLogic(entity.value)
171-
const avatar = await loadAvatarForPreview(entity.value, url)
172-
const avatarRigComponent = getOptionalComponent(entity.value, AvatarRigComponent)
173-
if (avatarRigComponent) {
174-
avatarRigComponent.rig.head.node.getWorldPosition(camera.value.position)
175-
camera.value.position.y += 0.2
176-
camera.value.position.z = 0.6
177-
}
178-
avatarLoading.set(false)
179-
if (avatar) {
180-
avatar.name = 'avatar'
181-
scene.value.add(avatar)
182-
}
183-
}
160+
avatarSrc.set(url)
184161
}
185162

186163
const handleCancel = () => {
@@ -210,6 +187,7 @@ const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) =>
210187
})
211188
: ''
212189
})
190+
state.avatarFile.set(files[0])
213191
break
214192
}
215193
case 'thumbnailFile': {
@@ -222,6 +200,7 @@ const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) =>
222200
})
223201
: ''
224202
})
203+
state.thumbnailUrl.set(files[0].name)
225204
break
226205
}
227206
}
@@ -337,8 +316,10 @@ const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) =>
337316
canvas.width = THUMBNAIL_WIDTH
338317
canvas.height = THUMBNAIL_HEIGHT
339318

319+
const avatarCanvas = document.getElementById('stage')?.firstChild as CanvasImageSource
320+
340321
const newContext = canvas.getContext('2d')
341-
newContext?.drawImage(renderer.value.domElement, 0, 0, canvas.width, canvas.height)
322+
newContext?.drawImage(avatarCanvas, 0, 0, canvas.width, canvas.height)
342323

343324
const blob = await getCanvasBlob(canvas)
344325
if (isFile) {
@@ -418,74 +399,11 @@ const AvatarDrawerContent = ({ open, mode, selectedAvatar, onClose }: Props) =>
418399
/>
419400
)}
420401

421-
{editMode.value && (
422-
<Box
423-
className={styles.preview}
424-
style={{ width: THUMBNAIL_WIDTH + 'px', height: THUMBNAIL_HEIGHT + 'px', position: 'relative' }}
425-
>
426-
<div ref={panelRef} id="stage" style={{ width: THUMBNAIL_WIDTH + 'px', height: THUMBNAIL_HEIGHT + 'px' }} />
427-
428-
{avatarLoading.value && (
429-
<LoadingView
430-
title={t('admin:components.avatar.loading')}
431-
variant="body2"
432-
sx={{ position: 'absolute', top: 0 }}
433-
/>
434-
)}
435-
436-
{((state.source.value === 'file' && !state.avatarFile.value) ||
437-
(state.source.value === 'url' && !state.avatarUrl.value)) && (
438-
<Typography
439-
sx={{
440-
position: 'absolute',
441-
display: 'flex',
442-
justifyContent: 'center',
443-
alignItems: 'center',
444-
height: '100%',
445-
width: '100%',
446-
fontSize: 14,
447-
top: 0
448-
}}
449-
>
450-
{t('admin:components.avatar.avatarPreview')}
451-
</Typography>
452-
)}
453-
454-
<Tooltip
455-
arrow
456-
title={
457-
<Box sx={{ width: 100 }}>
458-
<Typography variant="subtitle2" sx={{ fontWeight: 'bold' }}>
459-
{t('user:avatar.rotate')}:
460-
</Typography>
461-
<Typography variant="body2" sx={{ display: 'flex', justifyContent: 'center' }}>
462-
{t('admin:components.avatar.leftClick')}
463-
<Icon type="Mouse" fontSize="small" />
464-
</Typography>
465-
466-
<br />
467-
468-
<Typography variant="subtitle2" sx={{ fontWeight: 'bold' }}>
469-
{t('user:avatar.pan')}:
470-
</Typography>
471-
<Typography variant="body2" sx={{ display: 'flex', justifyContent: 'center' }}>
472-
{t('admin:components.avatar.rightClick')} <Icon type="Mouse" fontSize="small" />
473-
</Typography>
474-
475-
<br />
476-
477-
<Typography variant="subtitle2" sx={{ fontWeight: 'bold' }}>
478-
{t('admin:components.avatar.zoom')}:
479-
</Typography>
480-
<Typography variant="body2" sx={{ display: 'flex', justifyContent: 'center' }}>
481-
{t('admin:components.avatar.scroll')} <Icon type="Mouse" fontSize="small" />
482-
</Typography>
483-
</Box>
484-
}
485-
>
486-
<Icon type="Help" sx={{ position: 'absolute', top: 0, right: 0, margin: 1 }} />
487-
</Tooltip>
488-
</Box>
402+
{!viewMode && (
403+
<AvatarPreview
404+
avatarUrl={avatarSrc.value}
405+
sx={{ width: `${THUMBNAIL_WIDTH}px`, height: `${THUMBNAIL_HEIGHT}px`, m: 'auto' }}
406+
/>
489407
)}
490408

491409
{state.source.value === 'file' && (

0 commit comments

Comments
 (0)