-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBackpackController.cs
More file actions
346 lines (298 loc) · 14.4 KB
/
BackpackController.cs
File metadata and controls
346 lines (298 loc) · 14.4 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
using Arch.Core;
using CommunicationData.URLHelpers;
using Cysharp.Threading.Tasks;
using DCL.AvatarRendering.AvatarShape.Components;
using DCL.AvatarRendering.Wearables;
using DCL.AvatarRendering.Wearables.Equipped;
using DCL.AvatarRendering.Wearables.Helpers;
using DCL.Backpack.AvatarSection.Outfits;
using DCL.Backpack.AvatarSection.Outfits.Commands;
using DCL.Backpack.AvatarSection.Outfits.Logger;
using DCL.Backpack.AvatarSection.Outfits.Repository;
using DCL.Backpack.AvatarSection.Outfits.Services;
using DCL.Backpack.AvatarSection.Outfits.Slots;
using DCL.Backpack.BackpackBus;
using DCL.Backpack.CharacterPreview;
using DCL.Backpack.EmotesSection;
using DCL.Browser;
using DCL.CharacterPreview;
using DCL.Input;
using DCL.Multiplayer.Connections.DecentralandUrls;
using DCL.Profiles;
using DCL.Profiles.Self;
using DCL.UI;
using DCL.WebRequests;
using ECS;
using Runtime.Wearables;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using UnityEngine;
using Utility;
using Avatar = DCL.Profiles.Avatar;
namespace DCL.Backpack
{
public class BackpackController : ISection, IDisposable
{
private readonly BackpackView view;
private readonly BackpackCommandBus backpackCommandBus;
private readonly BackpackInfoPanelController emoteInfoPanelController;
private readonly RectTransform rectTransform;
private readonly AvatarController avatarController;
private readonly BackpackCharacterPreviewController backpackCharacterPreviewController;
private readonly ICursor cursor;
private readonly World world;
private readonly Entity playerEntity;
private readonly BackpackEmoteGridController backpackEmoteGridController;
private readonly BackpackGridController backpackGridController;
private readonly EmotesController emotesController;
private readonly Dictionary<BackpackSections, ISection> backpackSections;
private readonly SectionSelectorController<BackpackSections> sectionSelectorController;
private readonly Dictionary<BackpackSections, TabSelectorView> tabsBySections;
private readonly IBackpackEventBus backpackEventBus;
private readonly BackpackSections currentSection = BackpackSections.Avatar;
private readonly IRealmData realmData;
private BackpackSections lastShownSection;
private CancellationTokenSource? animationCts;
private CancellationTokenSource? profileLoadingCts;
private bool isAvatarLoaded;
private bool instantSectionToggle;
public BackpackController(
BackpackView view,
ISelfProfile selfProfile,
IWebBrowser webBrowser,
AvatarView avatarView,
NftTypeIconSO rarityInfoPanelBackgrounds,
BackpackCommandBus backpackCommandBus,
IBackpackEventBus backpackEventBus,
BackpackGridController backpackGridController,
BackpackInfoPanelController wearableInfoPanelController,
BackpackInfoPanelController emoteInfoPanelController,
World world, Entity playerEntity,
BackpackEmoteGridController backpackEmoteGridController,
AvatarSlotView[] avatarSlotViews,
EmotesController emotesController,
BackpackCharacterPreviewController backpackCharacterPreviewController,
IThumbnailProvider thumbnailProvider,
IInputBlock inputBlock,
ICursor cursor,
OutfitsRepository outfitsRepository,
IRealmData realmData,
IWebRequestController webController,
IEquippedWearables equippedWearables,
IWearableStorage wearableStorage,
IWearablesProvider wearablesProvider,
INftNamesProvider nftNamesProvider,
IEventBus eventBus,
Sprite deleteIcon,
IDecentralandUrlsSource decentralandUrlsSource)
{
this.view = view;
this.backpackCommandBus = backpackCommandBus;
this.emoteInfoPanelController = emoteInfoPanelController;
this.world = world;
this.playerEntity = playerEntity;
this.backpackEmoteGridController = backpackEmoteGridController;
this.backpackGridController = backpackGridController;
this.emotesController = emotesController;
this.backpackEventBus = backpackEventBus;
this.backpackCharacterPreviewController = backpackCharacterPreviewController;
rectTransform = view.transform.parent.GetComponent<RectTransform>();
var categoriesPresenter = new CategoriesPresenter(avatarView.CategoriesView,
backpackGridController,
backpackCommandBus,
backpackEventBus,
inputBlock);
var screenshotService = new AvatarScreenshotService(selfProfile);
var outfitsLogger = new OutfitsLogger(wearableStorage, realmData);
var outfitSlotFactory = new OutfitSlotPresenterFactory(screenshotService);
var outfitsCollection = new OutfitsCollection();
var outfitApplier = new OutfitApplier(backpackCommandBus);
var loadOutfitsCommand = new LoadOutfitsCommand(webController,
selfProfile,
realmData,
outfitsLogger);
var saveOutfitCommand = new SaveOutfitCommand(selfProfile,
outfitsRepository,
wearableStorage,
eventBus,
outfitsLogger);
var deleteOutfitCommand = new DeleteOutfitCommand(selfProfile, outfitsRepository, screenshotService, deleteIcon);
var checkOutfitsBannerCommand = new CheckOutfitsBannerVisibilityCommand(selfProfile, nftNamesProvider);
var prewarmWearablesCacheCommand = new PrewarmWearablesCacheCommand(wearablesProvider, wearableStorage);
var previewOutfitCommand = new PreviewOutfitCommand(outfitApplier,
equippedWearables,
selfProfile,
wearableStorage,
outfitsLogger);
var outfitsPresenter = new OutfitsPresenter(avatarView.OutfitsView,
eventBus,
outfitApplier,
outfitsCollection,
webBrowser,
equippedWearables,
loadOutfitsCommand,
saveOutfitCommand,
deleteOutfitCommand,
checkOutfitsBannerCommand,
prewarmWearablesCacheCommand,
previewOutfitCommand,
screenshotService,
backpackCharacterPreviewController,
outfitSlotFactory);
avatarController = new AvatarController(
avatarView,
webBrowser,
avatarSlotViews,
rarityInfoPanelBackgrounds,
backpackCommandBus,
backpackEventBus,
wearableInfoPanelController,
backpackGridController,
categoriesPresenter,
outfitsPresenter,
thumbnailProvider,
decentralandUrlsSource);
backpackSections = new Dictionary<BackpackSections, ISection>
{
{ BackpackSections.Avatar, avatarController },
{ BackpackSections.Emotes, emotesController },
};
foreach (KeyValuePair<BackpackSections, ISection> keyValuePair in backpackSections)
keyValuePair.Value.Deactivate();
sectionSelectorController = new SectionSelectorController<BackpackSections>(backpackSections, BackpackSections.Avatar);
tabsBySections = view.TabSelectorMappedViews.ToDictionary(map => map.Section, map => map.TabSelectorViews);
foreach ((BackpackSections section, TabSelectorView? tabSelector) in tabsBySections)
{
tabSelector.TabSelectorToggle.onValueChanged.RemoveAllListeners();
tabSelector.TabSelectorToggle.onValueChanged.AddListener(
isOn =>
{
ToggleSection(isOn, tabSelector, section, true);
if (isOn)
{
backpackEventBus.SendChangedBackpackSectionEvent(section);
}
}
);
}
this.cursor = cursor;
view.TipsButton.onClick.AddListener(ToggleTipsContent);
view.TipsPanelDeselectable.OnDeselectEvent += ToggleTipsContent;
}
private void ToggleSection(bool isOn, TabSelectorView tabSelectorView, BackpackSections shownSection, bool animate)
{
if(isOn && animate && shownSection != lastShownSection)
sectionSelectorController.SetAnimationState(false, tabsBySections[lastShownSection]);
animationCts.SafeCancelAndDispose();
animationCts = new CancellationTokenSource();
sectionSelectorController.OnTabSelectorToggleValueChangedAsync(isOn, tabSelectorView, shownSection, animationCts.Token, animate).Forget();
if (isOn)
{
lastShownSection = shownSection;
backpackEventBus.SendChangedBackpackSectionEvent(shownSection);
}
}
public void Dispose()
{
view.TipsPanelDeselectable.OnDeselectEvent -= ToggleTipsContent;
avatarController.Dispose();
emotesController.Dispose();
backpackEmoteGridController.Dispose();
animationCts.SafeCancelAndDispose();
profileLoadingCts.SafeCancelAndDispose();
backpackCharacterPreviewController.Dispose();
emoteInfoPanelController.Dispose();
}
private void ToggleTipsContent()
{
if (!view.TipsPanelDeselectable.gameObject.activeInHierarchy)
view.TipsPanelDeselectable.SelectElement();
view.TipsPanelDeselectable.gameObject.SetActive(!view.TipsPanelDeselectable.gameObject.activeInHierarchy);
}
private async UniTaskVoid AwaitForProfileAsync(CancellationToken ct)
{
if (ct.IsCancellationRequested) return;
isAvatarLoaded = false;
var avatarShapeComponent = world.Get<AvatarShapeComponent>(playerEntity);
Avatar avatar = world.Get<Profile>(playerEntity).Avatar;
backpackGridController.RequestPage(1, true);
backpackEmoteGridController.RequestAndFillEmotes(1, true);
backpackCharacterPreviewController.Initialize(avatar, CharacterPreviewUtils.AVATAR_POSITION_1);
while (!avatarShapeComponent.WearablePromise.IsConsumed)
{
avatarShapeComponent = world.Get<AvatarShapeComponent>(playerEntity);
await UniTask.Yield();
}
if (ct.IsCancellationRequested) return;
backpackCommandBus.SendCommand(new BackpackUnEquipAllCommand());
backpackCommandBus.SendCommand(new BackpackChangeColorCommand(avatar.HairColor, WearableCategories.Categories.HAIR));
backpackCommandBus.SendCommand(new BackpackChangeColorCommand(avatar.EyesColor, WearableCategories.Categories.EYES));
backpackCommandBus.SendCommand(new BackpackChangeColorCommand(avatar.SkinColor, WearableCategories.Categories.BODY_SHAPE));
backpackCommandBus.SendCommand(new BackpackHideCommand(avatar.ForceRender, true));
backpackCommandBus.SendCommand(new BackpackEquipWearableCommand(avatar.BodyShape.Value, false));
foreach (URN w in avatar.Wearables)
backpackCommandBus.SendCommand(new BackpackEquipWearableCommand(w.Shorten(), false));
for (var i = 0; i < avatar.Emotes.Count; i++)
{
URN avatarEmote = avatar.Emotes[i];
if (avatarEmote.IsNullOrEmpty()) continue;
backpackCommandBus.SendCommand(new BackpackEquipEmoteCommand(avatarEmote.Shorten(), i, false));
}
isAvatarLoaded = true;
}
public void Activate()
{
profileLoadingCts = profileLoadingCts.SafeRestart();
AwaitForProfileAsync(profileLoadingCts.Token).Forget();
backpackSections[currentSection].Activate();
view.gameObject.SetActive(true);
backpackCharacterPreviewController.OnBeforeShow();
backpackCharacterPreviewController.OnShow();
foreach ((BackpackSections section, TabSelectorView? tab) in tabsBySections)
ToggleSection(section == BackpackSections.Avatar, tab, section, true);
sectionSelectorController.SetAnimationState(true, tabsBySections[BackpackSections.Avatar]);
cursor.Unlock();
}
public void Deactivate()
{
foreach (ISection backpackSectionsValue in backpackSections.Values)
backpackSectionsValue.Deactivate();
//Resets the tab selector to the default state (Avatar selected and open)
foreach (BackpackPanelTabSelectorMapping tabSelector in view.TabSelectorMappedViews)
tabSelector.TabSelectorViews.TabSelectorToggle.isOn = tabSelector.Section == BackpackSections.Avatar;
profileLoadingCts.SafeCancelAndDispose();
if (isAvatarLoaded)
backpackCommandBus.SendCommand(new BackpackPublishProfileCommand());
view.gameObject.SetActive(false);
backpackCharacterPreviewController.OnHide();
backpackEventBus.SendBackpackDeactivateEvent();
}
public void Animate(int triggerId)
{
view.PanelAnimator.SetTrigger(triggerId);
view.HeaderAnimator.SetTrigger(triggerId);
}
public void ResetAnimator()
{
view.PanelAnimator.Rebind();
view.HeaderAnimator.Rebind();
view.PanelAnimator.Update(0);
view.HeaderAnimator.Update(0);
}
public RectTransform GetRectTransform() =>
rectTransform;
public void Toggle(BackpackSections section)
{
bool tmp = instantSectionToggle;
instantSectionToggle = true;
foreach (BackpackPanelTabSelectorMapping tabSelector in view.TabSelectorMappedViews)
{
if (tabSelector.Section != section) continue;
tabSelector.TabSelectorViews.TabSelectorToggle.isOn = true;
}
instantSectionToggle = tmp;
}
}
}