-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathCenterPanel.tsx
More file actions
430 lines (393 loc) Β· 15.3 KB
/
CenterPanel.tsx
File metadata and controls
430 lines (393 loc) Β· 15.3 KB
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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
import * as React from 'react'
import type { Wearable } from 'decentraland-ecs'
import { BodyShape, PreviewEmote, WearableCategory } from '@dcl/schemas'
import { Dropdown, DropdownProps, Popup, Icon, Loader, Center, DropdownItemProps, Button } from 'decentraland-ui'
import { AnimationControls, WearablePreview, EmoteControls, ZoomControls } from 'decentraland-ui2'
import { SocialEmoteAnimation } from '@dcl/schemas/dist/dapps/preview/social-emote-animation'
import { getAnalytics } from 'decentraland-dapps/dist/modules/analytics/utils'
import { t } from 'decentraland-dapps/dist/modules/translation/utils'
import { Color4 } from 'lib/colors'
import { isDevelopment } from 'lib/environment'
import { extractThirdPartyTokenId, extractTokenId, isThirdParty } from 'lib/urn'
import { isTPCollection } from 'modules/collection/utils'
import { EmoteData, ItemType } from 'modules/item/types'
import { isEmote } from 'modules/item/utils'
import { toBase64, toHex } from 'modules/editor/utils'
import { getSkinColors, getEyeColors, getHairColors } from 'modules/editor/avatar'
import BuilderIcon from 'components/Icon'
import AvatarColorDropdown from './AvatarColorDropdown'
import AvatarWearableDropdown from './AvatarWearableDropdown'
import { Props, State } from './CenterPanel.types'
import './CenterPanel.css'
export default class CenterPanel extends React.PureComponent<Props, State> {
state = {
showSceneBoundaries: false,
isShowingAvatarAttributes: false,
isLoading: false,
socialEmote: undefined,
hasBeenUpdatedWearablePreview: false
}
analytics = getAnalytics()
componentDidMount() {
const {
address,
collection,
emotes,
selectedBaseWearables: bodyShapeBaseWearables,
hasUserOrphanItems,
onFetchBaseWearables,
onFetchOrphanItems,
onFetchCollectionItems
} = this.props
const hasEmotesLoaded = emotes.length > 0
if (!bodyShapeBaseWearables) {
onFetchBaseWearables()
}
// Fetch emotes created by the user to show them in the Play Emote dropdown
if (!hasEmotesLoaded) {
// The TP collections wouldn't have emotes soon, for this reason, we are fetching only standard collections to show in the Play Emote dropdown
if (collection && !isTPCollection(collection)) {
onFetchCollectionItems(collection.id)
} else if (address && hasUserOrphanItems) {
// TODO: Remove this call when there are no users with orphan items
onFetchOrphanItems(address)
}
}
// Setting the editor as loading as soon as it mounts. It will be turned of by the WearablePreview component once it's ready.
this.setState({ isLoading: true })
}
componentWillUnmount() {
const { onSetWearablePreviewController } = this.props
onSetWearablePreviewController(null)
}
componentDidUpdate(_prevProps: Props, prevState: State) {
if (prevState.socialEmote !== this.state.socialEmote) {
this.setState({ hasBeenUpdatedWearablePreview: false })
}
}
handleToggleShowingAvatarAttributes = () => {
this.setState({ isShowingAvatarAttributes: !this.state.isShowingAvatarAttributes })
}
handleBodyShapeChange = (_event: React.SyntheticEvent<HTMLElement, Event>, { value }: DropdownProps) => {
const { onSetBodyShape } = this.props
onSetBodyShape(value as BodyShape)
}
handleAnimationChange = (_event: React.SyntheticEvent<HTMLElement, Event>, { value }: DropdownItemProps) => {
const { emotes, visibleItems, onSetAvatarAnimation, onSetItems } = this.props
const emote = emotes.find(emote => emote.id === value)
const newVisibleItems = visibleItems.filter(item => item.type !== ItemType.EMOTE)
newVisibleItems.forEach(item => {
this.analytics?.track('Play Emote', {
EMOTE_PLAYED_BASE: !emote,
EMOTE_PLAYED_ITEM_ID: emote?.urn ? extractTokenId(emote.urn) : null,
EMOTE_PLAYED_NAME: emote ? emote.name : value,
PREVIEWED_WEARABLE_ITEM_ID: item?.urn
? isThirdParty(item.urn)
? extractThirdPartyTokenId(item.urn)
: extractTokenId(item.urn)
: null,
PREVIEWED_WEARABLE_NAME: item.name
})
})
if (emote) {
newVisibleItems.push(emote)
} else {
onSetAvatarAnimation(value as PreviewEmote)
}
onSetItems(newVisibleItems)
}
handleSkinColorChange = (color: Color4) => {
const { onSetSkinColor } = this.props
onSetSkinColor(color)
}
handleEyeColorChange = (color: Color4) => {
const { onSetEyeColor } = this.props
onSetEyeColor(color)
}
handleHairColorChange = (color: Color4) => {
const { onSetHairColor } = this.props
onSetHairColor(color)
}
handleWearableChange = (category: WearableCategory, bodyShape: BodyShape, wearable: Wearable | null) => {
const { onSetBaseWearable } = this.props
onSetBaseWearable(category, bodyShape, wearable)
}
renderSelectTrigger(label: string, value: string) {
return (
<>
<div className="label">{label}</div>
<div className="value">{value}</div>
<div className="handle" />
</>
)
}
getDropdownAttributes<T>(value: T, options: any[], label: string) {
const selected = options.find(option => option.value === value)
return {
value,
options,
trigger: this.renderSelectTrigger(label, selected ? selected.text : '')
}
}
handleWearablePreviewLoad = () => {
const { wearableController, onSetWearablePreviewController } = this.props
if (!wearableController) {
onSetWearablePreviewController(WearablePreview.createController('wearable-editor'))
}
this.setState({ isLoading: false })
}
handlePlayEmote = () => {
const { wearableController, isPlayingEmote, visibleItems, onSetAvatarAnimation, onSetItems } = this.props
const newVisibleItems = visibleItems.filter(item => item.type !== ItemType.EMOTE)
if (isPlayingEmote) {
onSetAvatarAnimation(PreviewEmote.IDLE)
onSetItems(newVisibleItems)
} else {
wearableController?.emote.play() as void
}
}
handleSocialEmoteSelect = (animation: SocialEmoteAnimation) => {
this.setState({ socialEmote: animation })
}
renderEmotePlayButton = () => {
const { isPlayingEmote } = this.props
const icon = isPlayingEmote ? 'stop' : 'play'
const text = isPlayingEmote ? t('item_editor.center_panel.stop') : t('item_editor.center_panel.play_emote')
return (
<Button icon onClick={this.handlePlayEmote}>
<Icon name={icon} />
<span>{text}</span>
</Button>
)
}
renderEmoteDropdownButton = () => {
const { collection, emotes, isPlayingEmote } = this.props
const areEmotesFromCollection = !!collection
const hasEmotes = emotes.length > 0
if (isPlayingEmote) return null
return (
<Dropdown className="avatar-animation button icon" floating scrolling>
<Dropdown.Menu>
{hasEmotes && (
<>
<Dropdown.Header
content={areEmotesFromCollection ? t('item_editor.center_panel.from_collection') : t('item_editor.center_panel.from_items')}
/>
<Dropdown.Divider />
{emotes.map(value => (
<Dropdown.Item key={value.id} value={value.id} text={value.name} onClick={this.handleAnimationChange} />
))}
<Dropdown.Divider />
<Dropdown.Header content={t('item_editor.center_panel.default')} />
<Dropdown.Divider />
</>
)}
{PreviewEmote.schema.enum.map((value: PreviewEmote) => (
<Dropdown.Item key={value} value={value} text={t(`emotes.${value}`)} onClick={this.handleAnimationChange} />
))}
</Dropdown.Menu>
</Dropdown>
)
}
renderEmoteSelector = () => {
return (
<Popup
content={t('item_editor.center_panel.disabled_animation_dropdown')}
disabled
position="top center"
trigger={
<div className="avatar-animation-dropdown-wrapper option">
<Button.Group>
{this.renderEmotePlayButton()}
{this.renderEmoteDropdownButton()}
</Button.Group>
</div>
}
/>
)
}
render() {
const {
bodyShape,
skinColor,
eyeColor,
hairColor,
emote,
selectedBaseWearables,
selectedItem,
visibleItems,
isImportFilesModalOpen,
wearableController
} = this.props
const { isShowingAvatarAttributes, showSceneBoundaries, isLoading, socialEmote, hasBeenUpdatedWearablePreview } = this.state
const isRenderingAnEmote = visibleItems.some(isEmote) && selectedItem?.type === ItemType.EMOTE
const zoom = emote === PreviewEmote.JUMP ? 1 : undefined
let _socialEmote = undefined
if (!socialEmote && selectedItem?.type === ItemType.EMOTE && (selectedItem.data as unknown as EmoteData).startAnimation) {
_socialEmote = { title: 'Start Animation', ...(selectedItem.data as unknown as EmoteData).startAnimation, loop: true }
}
return (
<div className={`CenterPanel ${isImportFilesModalOpen ? 'import-files-modal-is-open' : ''}`}>
<WearablePreview
id="wearable-editor"
profile="default"
bodyShape={bodyShape}
emote={emote}
zoom={zoom}
skin={toHex(skinColor)}
eyes={toHex(eyeColor)}
hair={toHex(hairColor)}
urns={
selectedBaseWearables
? Object.values(selectedBaseWearables)
.map(wearable => (wearable ? wearable.id : null))
.filter(urn => urn !== null)
: []
}
base64s={visibleItems.map(toBase64)}
disableAutoRotate
disableBackground
wheelZoom={1.5}
wheelStart={100}
dev={isDevelopment}
onUpdate={() => this.setState({ isLoading: true, hasBeenUpdatedWearablePreview: true })}
onLoad={this.handleWearablePreviewLoad}
disableDefaultEmotes={isRenderingAnEmote}
showSceneBoundaries={showSceneBoundaries}
socialEmote={socialEmote || _socialEmote}
/>
{isRenderingAnEmote && hasBeenUpdatedWearablePreview && wearableController ? (
<ZoomControls className="zoom-controls" wearablePreviewId="wearable-editor" wearablePreviewController={wearableController} />
) : null}
{isLoading && (
<Center>
<Loader active />
</Center>
)}
<div className="footer">
{isRenderingAnEmote && !isLoading && hasBeenUpdatedWearablePreview && wearableController ? (
<div className="emote-controls-container">
<EmoteControls
className="emote-controls"
wearablePreviewId="wearable-editor"
wearablePreviewController={wearableController}
/>
</div>
) : null}
<div className="options">
<div className={`option ${isShowingAvatarAttributes ? 'active' : ''}`} onClick={this.handleToggleShowingAvatarAttributes}>
<Icon name="user" />
</div>
{isRenderingAnEmote ? (
hasBeenUpdatedWearablePreview && wearableController ? (
<AnimationControls
className="animation-controls"
wearablePreviewId="wearable-editor"
wearablePreviewController={wearableController as any}
selectedAnimation={socialEmote}
onSelectAnimation={this.handleSocialEmoteSelect}
/>
) : null
) : (
this.renderEmoteSelector()
)}
<div className={`option ${showSceneBoundaries ? 'active' : ''}`}>
<BuilderIcon
name="cylinder"
onClick={() => this.setState(prevState => ({ showSceneBoundaries: !prevState.showSceneBoundaries }))}
/>
</div>
</div>
<div className={`avatar-attributes ${isShowingAvatarAttributes ? 'active' : ''}`}>
<div className="dropdown-container">
<Dropdown
inline
direction="right"
className="Select"
onChange={this.handleBodyShapeChange}
{...this.getDropdownAttributes(
bodyShape,
[
{ value: BodyShape.MALE, text: t('body_shapes.male') },
{ value: BodyShape.FEMALE, text: t('body_shapes.female') }
],
t('wearable.category.body_shape')
)}
/>
</div>
<div className="dropdown-container">
<AvatarColorDropdown
currentColor={skinColor}
colors={getSkinColors()}
label={t('wearable.color.skin')}
onChange={this.handleSkinColorChange}
/>
</div>
<div className="dropdown-container">
<AvatarColorDropdown
currentColor={eyeColor}
colors={getEyeColors()}
label={t('wearable.color.eye')}
onChange={this.handleEyeColorChange}
/>
</div>
<div className="dropdown-container">
<AvatarColorDropdown
currentColor={hairColor}
colors={getHairColors()}
label={t('wearable.color.hair')}
onChange={this.handleHairColorChange}
/>
</div>
<div className="dropdown-container">
{selectedBaseWearables && (
<AvatarWearableDropdown
wearable={selectedBaseWearables[WearableCategory.HAIR]}
category={WearableCategory.HAIR}
bodyShape={bodyShape}
label={t('wearable.category.hair')}
onChange={this.handleWearableChange}
isNullable
/>
)}
</div>
<div className="dropdown-container">
{selectedBaseWearables && (
<AvatarWearableDropdown
wearable={selectedBaseWearables[WearableCategory.FACIAL_HAIR]}
category={WearableCategory.FACIAL_HAIR}
bodyShape={bodyShape}
label={t('wearable.category.facial_hair')}
onChange={this.handleWearableChange}
isNullable
/>
)}
</div>
<div className="dropdown-container">
{selectedBaseWearables && (
<AvatarWearableDropdown
wearable={selectedBaseWearables[WearableCategory.UPPER_BODY]}
category={WearableCategory.UPPER_BODY}
bodyShape={bodyShape}
label={t('wearable.category.upper_body')}
onChange={this.handleWearableChange}
/>
)}
</div>
<div className="dropdown-container">
{selectedBaseWearables && (
<AvatarWearableDropdown
wearable={selectedBaseWearables[WearableCategory.LOWER_BODY]}
category={WearableCategory.LOWER_BODY}
bodyShape={bodyShape}
label={t('wearable.category.lower_body')}
onChange={this.handleWearableChange}
/>
)}
</div>
</div>
</div>
</div>
)
}
}