| | 1 | | using DCL; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Rendering; |
| | 9 | | using UnityEngine.Rendering.Universal; |
| | 10 | | using Categories = WearableLiterals.Categories; |
| | 11 | |
|
| | 12 | | public class AvatarEditorHUDController : IHUD |
| | 13 | | { |
| | 14 | | private const int LOADING_OWNED_WEARABLES_RETRIES = 3; |
| | 15 | | private const string LOADING_OWNED_WEARABLES_ERROR_MESSAGE = "There was a problem loading your wearables"; |
| | 16 | | private const string URL_MARKET_PLACE = "https://market.decentraland.org/browse?section=wearables"; |
| | 17 | | private const string URL_GET_A_WALLET = "https://docs.decentraland.org/get-a-wallet"; |
| | 18 | | private const string URL_SELL_COLLECTIBLE_GENERIC = "https://market.decentraland.org/account"; |
| | 19 | | private const string URL_SELL_SPECIFIC_COLLECTIBLE = "https://market.decentraland.org/contracts/{collectionId}/token |
| | 20 | |
|
| 1 | 21 | | protected static readonly string[] categoriesThatMustHaveSelection = { Categories.BODY_SHAPE, Categories.UPPER_BODY, |
| 1 | 22 | | protected static readonly string[] categoriesToRandomize = { Categories.HAIR, Categories.EYES, Categories.EYEBROWS, |
| | 23 | |
|
| | 24 | | [NonSerialized] |
| | 25 | | public bool bypassUpdateAvatarPreview = false; |
| | 26 | |
|
| | 27 | | private UserProfile userProfile; |
| | 28 | | private BaseDictionary<string, WearableItem> catalog; |
| 102 | 29 | | bool renderingEnabled => CommonScriptableObjects.rendererState.Get(); |
| 99 | 30 | | bool isPlayerRendererLoaded => DataStore.i.common.isPlayerRendererLoaded.Get(); |
| 175 | 31 | | BaseVariable<bool> avatarEditorVisible => DataStore.i.HUDs.avatarEditorVisible; |
| 133 | 32 | | BaseVariable<Transform> configureBackpackInFullscreenMenu => DataStore.i.exploreV2.configureBackpackInFullscreenMenu |
| 87 | 33 | | BaseVariable<bool> exploreV2IsOpen => DataStore.i.exploreV2.isOpen; |
| 46 | 34 | | private readonly Dictionary<string, List<WearableItem>> wearablesByCategory = new Dictionary<string, List<WearableIt |
| 46 | 35 | | protected readonly AvatarEditorHUDModel model = new AvatarEditorHUDModel(); |
| | 36 | |
|
| | 37 | | private ColorList skinColorList; |
| | 38 | | private ColorList eyeColorList; |
| | 39 | | private ColorList hairColorList; |
| | 40 | | private bool prevMouseLockState = false; |
| 46 | 41 | | private int ownedWearablesRemainingRequests = LOADING_OWNED_WEARABLES_RETRIES; |
| | 42 | | private bool ownedWearablesAlreadyLoaded = false; |
| 46 | 43 | | private List<Nft> ownedNftCollectionsL1 = new List<Nft>(); |
| 46 | 44 | | private List<Nft> ownedNftCollectionsL2 = new List<Nft>(); |
| | 45 | | private bool avatarIsDirty = false; |
| | 46 | | private float lastTimeOwnedWearablesChecked = 0; |
| | 47 | |
|
| | 48 | | public AvatarEditorHUDView view; |
| | 49 | |
|
| | 50 | | public event Action OnOpen; |
| | 51 | | public event Action OnClose; |
| | 52 | |
|
| 92 | 53 | | public AvatarEditorHUDController() { } |
| | 54 | |
|
| | 55 | | public void Initialize(UserProfile userProfile, BaseDictionary<string, WearableItem> catalog, bool bypassUpdateAvata |
| | 56 | | { |
| 46 | 57 | | this.userProfile = userProfile; |
| 46 | 58 | | this.bypassUpdateAvatarPreview = bypassUpdateAvatarPreview; |
| | 59 | |
|
| 46 | 60 | | view = AvatarEditorHUDView.Create(this); |
| | 61 | |
|
| 46 | 62 | | avatarEditorVisible.OnChange += OnAvatarEditorVisibleChanged; |
| 46 | 63 | | OnAvatarEditorVisibleChanged(avatarEditorVisible.Get(), false); |
| 46 | 64 | | view.OnCloseActionTriggered += DiscardAndClose; |
| | 65 | |
|
| 46 | 66 | | configureBackpackInFullscreenMenu.OnChange += ConfigureBackpackInFullscreenMenuChanged; |
| 46 | 67 | | ConfigureBackpackInFullscreenMenuChanged(configureBackpackInFullscreenMenu.Get(), null); |
| | 68 | |
|
| 46 | 69 | | exploreV2IsOpen.OnChange += ExploreV2IsOpenChanged; |
| | 70 | |
|
| 46 | 71 | | skinColorList = Resources.Load<ColorList>("SkinTone"); |
| 46 | 72 | | hairColorList = Resources.Load<ColorList>("HairColor"); |
| 46 | 73 | | eyeColorList = Resources.Load<ColorList>("EyeColor"); |
| 46 | 74 | | view.SetColors(skinColorList.colors, hairColorList.colors, eyeColorList.colors); |
| | 75 | |
|
| 46 | 76 | | SetCatalog(catalog); |
| | 77 | |
|
| 46 | 78 | | LoadUserProfile(userProfile, true); |
| 46 | 79 | | this.userProfile.OnUpdate += LoadUserProfile; |
| | 80 | |
|
| 46 | 81 | | DataStore.i.HUDs.isAvatarEditorInitialized.Set(true); |
| 46 | 82 | | } |
| | 83 | |
|
| | 84 | | public void SetCatalog(BaseDictionary<string, WearableItem> catalog) |
| | 85 | | { |
| 46 | 86 | | if (this.catalog != null) |
| | 87 | | { |
| 0 | 88 | | this.catalog.OnAdded -= AddWearable; |
| 0 | 89 | | this.catalog.OnRemoved -= RemoveWearable; |
| | 90 | | } |
| | 91 | |
|
| 46 | 92 | | this.catalog = catalog; |
| | 93 | |
|
| 46 | 94 | | ProcessCatalog(this.catalog); |
| 46 | 95 | | this.catalog.OnAdded += AddWearable; |
| 46 | 96 | | this.catalog.OnRemoved += RemoveWearable; |
| 46 | 97 | | } |
| | 98 | |
|
| | 99 | | private void LoadUserProfile(UserProfile userProfile) |
| | 100 | | { |
| 42 | 101 | | LoadUserProfile(userProfile, false); |
| 42 | 102 | | QueryNftCollections(userProfile.userId); |
| 42 | 103 | | } |
| | 104 | |
|
| | 105 | | private void LoadOwnedWereables(UserProfile userProfile) |
| | 106 | | { |
| | 107 | | // If there is more than 1 minute that we have checked the owned wearables, we try it again |
| | 108 | | // This is done in order to retrieved the wearables after you has claimed them |
| 41 | 109 | | if ((Time.realtimeSinceStartup < lastTimeOwnedWearablesChecked + 60 && |
| | 110 | | (ownedWearablesAlreadyLoaded || |
| | 111 | | ownedWearablesRemainingRequests <= 0)) || |
| | 112 | | string.IsNullOrEmpty(userProfile.userId)) |
| 40 | 113 | | return; |
| | 114 | |
|
| 1 | 115 | | view.ShowCollectiblesLoadingSpinner(true); |
| 1 | 116 | | view.ShowCollectiblesLoadingRetry(false); |
| 1 | 117 | | lastTimeOwnedWearablesChecked = Time.realtimeSinceStartup; |
| | 118 | |
|
| 1 | 119 | | CatalogController.RequestOwnedWearables(userProfile.userId) |
| | 120 | | .Then((ownedWearables) => |
| | 121 | | { |
| 0 | 122 | | ownedWearablesAlreadyLoaded = true; |
| 0 | 123 | | this.userProfile.SetInventory(ownedWearables.Select(x => x.id).ToArray()); |
| 0 | 124 | | LoadUserProfile(userProfile, true); |
| 0 | 125 | | view.ShowCollectiblesLoadingSpinner(false); |
| 0 | 126 | | }) |
| | 127 | | .Catch((error) => |
| | 128 | | { |
| 0 | 129 | | ownedWearablesRemainingRequests--; |
| 0 | 130 | | if (ownedWearablesRemainingRequests > 0) |
| | 131 | | { |
| 0 | 132 | | Debug.LogWarning("Retrying owned wereables loading..."); |
| 0 | 133 | | LoadOwnedWereables(userProfile); |
| 0 | 134 | | } |
| | 135 | | else |
| | 136 | | { |
| 0 | 137 | | NotificationsController.i.ShowNotification(new DCL.NotificationModel.Model |
| | 138 | | { |
| | 139 | | message = LOADING_OWNED_WEARABLES_ERROR_MESSAGE, |
| | 140 | | type = DCL.NotificationModel.Type.GENERIC, |
| | 141 | | timer = 10f, |
| | 142 | | destroyOnFinish = true |
| | 143 | | }); |
| | 144 | |
|
| 0 | 145 | | view.ShowCollectiblesLoadingSpinner(false); |
| 0 | 146 | | view.ShowCollectiblesLoadingRetry(true); |
| 0 | 147 | | Debug.LogError(error); |
| | 148 | | } |
| 0 | 149 | | }); |
| 1 | 150 | | } |
| | 151 | |
|
| | 152 | | private void QueryNftCollections(string userId) |
| | 153 | | { |
| 42 | 154 | | if (string.IsNullOrEmpty(userId)) |
| 42 | 155 | | return; |
| | 156 | |
|
| 0 | 157 | | DCL.Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfile.userId, NftCollectionsLayer |
| 0 | 158 | | .Then((nfts) => ownedNftCollectionsL1 = nfts) |
| 0 | 159 | | .Catch((error) => Debug.LogError(error)); |
| | 160 | |
|
| 0 | 161 | | DCL.Environment.i.platform.serviceProviders.theGraph.QueryNftCollections(userProfile.userId, NftCollectionsLayer |
| 0 | 162 | | .Then((nfts) => ownedNftCollectionsL2 = nfts) |
| 0 | 163 | | .Catch((error) => Debug.LogError(error)); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public void RetryLoadOwnedWearables() |
| | 167 | | { |
| 0 | 168 | | ownedWearablesRemainingRequests = LOADING_OWNED_WEARABLES_RETRIES; |
| 0 | 169 | | LoadOwnedWereables(userProfile); |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | private void PlayerRendererLoaded(bool current, bool previous) |
| | 173 | | { |
| 14 | 174 | | if (!current) |
| 0 | 175 | | return; |
| | 176 | |
|
| 14 | 177 | | if (!ownedWearablesAlreadyLoaded) |
| | 178 | | { |
| 14 | 179 | | List<string> equippedOwnedWearables = new List<string>(); |
| 58 | 180 | | for (int i = 0; i < userProfile.avatar.wearables.Count; i++) |
| | 181 | | { |
| 15 | 182 | | if (catalog.TryGetValue(userProfile.avatar.wearables[i], out WearableItem wearable) && |
| | 183 | | !wearable.data.tags.Contains("base-wearable")) |
| | 184 | | { |
| 0 | 185 | | equippedOwnedWearables.Add(userProfile.avatar.wearables[i]); |
| | 186 | | } |
| | 187 | | } |
| | 188 | |
|
| 14 | 189 | | userProfile.SetInventory(equippedOwnedWearables.ToArray()); |
| | 190 | | } |
| | 191 | |
|
| 14 | 192 | | LoadUserProfile(userProfile, true); |
| 14 | 193 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 14 | 194 | | } |
| | 195 | |
|
| | 196 | | public void LoadUserProfile(UserProfile userProfile, bool forceLoading) |
| | 197 | | { |
| 102 | 198 | | bool avatarEditorNotVisible = renderingEnabled && !view.isOpen; |
| 102 | 199 | | bool isPlaying = !Application.isBatchMode; |
| | 200 | |
|
| 102 | 201 | | if (!forceLoading) |
| | 202 | | { |
| 42 | 203 | | if (isPlaying && avatarEditorNotVisible) |
| 0 | 204 | | return; |
| | 205 | | } |
| | 206 | |
|
| 102 | 207 | | if (userProfile == null) |
| 0 | 208 | | return; |
| | 209 | |
|
| 102 | 210 | | if (userProfile.avatar == null || string.IsNullOrEmpty(userProfile.avatar.bodyShape)) |
| 3 | 211 | | return; |
| | 212 | |
|
| 99 | 213 | | CatalogController.wearableCatalog.TryGetValue(userProfile.avatar.bodyShape, out var bodyShape); |
| | 214 | |
|
| 99 | 215 | | if (bodyShape == null) |
| | 216 | | { |
| 0 | 217 | | return; |
| | 218 | | } |
| | 219 | |
|
| 99 | 220 | | view.SetIsWeb3(userProfile.hasConnectedWeb3); |
| | 221 | |
|
| 99 | 222 | | ProcessCatalog(this.catalog); |
| 99 | 223 | | EquipBodyShape(bodyShape); |
| 99 | 224 | | EquipSkinColor(userProfile.avatar.skinColor); |
| 99 | 225 | | EquipHairColor(userProfile.avatar.hairColor); |
| 99 | 226 | | EquipEyesColor(userProfile.avatar.eyeColor); |
| | 227 | |
|
| 99 | 228 | | model.wearables.Clear(); |
| 99 | 229 | | view.UnselectAllWearables(); |
| | 230 | |
|
| 99 | 231 | | int wearablesCount = userProfile.avatar.wearables.Count; |
| | 232 | |
|
| 99 | 233 | | if (isPlayerRendererLoaded) |
| | 234 | | { |
| 158 | 235 | | for (var i = 0; i < wearablesCount; i++) |
| | 236 | | { |
| 46 | 237 | | CatalogController.wearableCatalog.TryGetValue(userProfile.avatar.wearables[i], out var wearable); |
| 46 | 238 | | if (wearable == null) |
| | 239 | | { |
| 0 | 240 | | Debug.LogError($"Couldn't find wearable with ID {userProfile.avatar.wearables[i]}"); |
| 0 | 241 | | continue; |
| | 242 | | } |
| | 243 | |
|
| 46 | 244 | | EquipWearable(wearable); |
| | 245 | | } |
| | 246 | | } |
| | 247 | |
|
| 99 | 248 | | EnsureWearablesCategoriesNotEmpty(); |
| | 249 | |
|
| 99 | 250 | | UpdateAvatarPreview(); |
| 99 | 251 | | } |
| | 252 | |
|
| | 253 | | private void EnsureWearablesCategoriesNotEmpty() |
| | 254 | | { |
| 145 | 255 | | var categoriesInUse = model.wearables.Select(x => x.data.category).ToArray(); |
| 1584 | 256 | | for (var i = 0; i < categoriesThatMustHaveSelection.Length; i++) |
| | 257 | | { |
| 693 | 258 | | var category = categoriesThatMustHaveSelection[i]; |
| 693 | 259 | | if (category != Categories.BODY_SHAPE && !(categoriesInUse.Contains(category))) |
| | 260 | | { |
| | 261 | | WearableItem wearable; |
| 558 | 262 | | var defaultItemId = WearableLiterals.DefaultWearables.GetDefaultWearable(model.bodyShape.id, category); |
| 558 | 263 | | if (defaultItemId != null) |
| | 264 | | { |
| 558 | 265 | | CatalogController.wearableCatalog.TryGetValue(defaultItemId, out wearable); |
| 558 | 266 | | } |
| | 267 | | else |
| | 268 | | { |
| 0 | 269 | | wearable = wearablesByCategory[category].FirstOrDefault(x => x.SupportsBodyShape(model.bodyShape.id) |
| | 270 | | } |
| | 271 | |
|
| 558 | 272 | | if (wearable != null) |
| | 273 | | { |
| 558 | 274 | | EquipWearable(wearable); |
| | 275 | | } |
| | 276 | | } |
| | 277 | | } |
| 99 | 278 | | } |
| | 279 | |
|
| | 280 | | public void WearableClicked(string wearableId) |
| | 281 | | { |
| 20 | 282 | | CatalogController.wearableCatalog.TryGetValue(wearableId, out var wearable); |
| 20 | 283 | | if (wearable == null) |
| 3 | 284 | | return; |
| | 285 | |
|
| 17 | 286 | | if (wearable.data.category == Categories.BODY_SHAPE) |
| | 287 | | { |
| 3 | 288 | | if (wearable.id == model.bodyShape.id) |
| 0 | 289 | | return; |
| 3 | 290 | | EquipBodyShape(wearable); |
| 3 | 291 | | } |
| | 292 | | else |
| | 293 | | { |
| 14 | 294 | | if (model.wearables.Contains(wearable)) |
| | 295 | | { |
| 2 | 296 | | if (!categoriesThatMustHaveSelection.Contains(wearable.data.category)) |
| | 297 | | { |
| 0 | 298 | | UnequipWearable(wearable); |
| 0 | 299 | | } |
| | 300 | | else |
| | 301 | | { |
| 2 | 302 | | return; |
| | 303 | | } |
| | 304 | | } |
| | 305 | | else |
| | 306 | | { |
| 91 | 307 | | var sameCategoryEquipped = model.wearables.FirstOrDefault(x => x.data.category == wearable.data.category |
| 12 | 308 | | if (sameCategoryEquipped != null) |
| | 309 | | { |
| 2 | 310 | | UnequipWearable(sameCategoryEquipped); |
| | 311 | | } |
| | 312 | |
|
| 12 | 313 | | EquipWearable(wearable); |
| | 314 | | } |
| | 315 | | } |
| | 316 | |
|
| 15 | 317 | | UpdateAvatarPreview(); |
| 15 | 318 | | } |
| | 319 | |
|
| | 320 | | public void HairColorClicked(Color color) |
| | 321 | | { |
| 2 | 322 | | EquipHairColor(color); |
| 2 | 323 | | view.SelectHairColor(model.hairColor); |
| 2 | 324 | | UpdateAvatarPreview(); |
| 2 | 325 | | } |
| | 326 | |
|
| | 327 | | public void SkinColorClicked(Color color) |
| | 328 | | { |
| 2 | 329 | | EquipSkinColor(color); |
| 2 | 330 | | view.SelectSkinColor(model.skinColor); |
| 2 | 331 | | UpdateAvatarPreview(); |
| 2 | 332 | | } |
| | 333 | |
|
| | 334 | | public void EyesColorClicked(Color color) |
| | 335 | | { |
| 2 | 336 | | EquipEyesColor(color); |
| 2 | 337 | | view.SelectEyeColor(model.eyesColor); |
| 2 | 338 | | UpdateAvatarPreview(); |
| 2 | 339 | | } |
| | 340 | |
|
| | 341 | | protected virtual void UpdateAvatarPreview() |
| | 342 | | { |
| 121 | 343 | | if (!bypassUpdateAvatarPreview) |
| 121 | 344 | | view.UpdateAvatarPreview(model.ToAvatarModel()); |
| 121 | 345 | | } |
| | 346 | |
|
| | 347 | | private void EquipHairColor(Color color) |
| | 348 | | { |
| 102 | 349 | | var colorToSet = color; |
| 1042 | 350 | | if (!hairColorList.colors.Any(x => x.AproxComparison(colorToSet))) |
| | 351 | | { |
| 92 | 352 | | colorToSet = hairColorList.colors[hairColorList.defaultColor]; |
| | 353 | | } |
| | 354 | |
|
| 102 | 355 | | model.hairColor = colorToSet; |
| 102 | 356 | | view.SelectHairColor(model.hairColor); |
| 102 | 357 | | } |
| | 358 | |
|
| | 359 | | private void EquipEyesColor(Color color) |
| | 360 | | { |
| 102 | 361 | | var colorToSet = color; |
| 1041 | 362 | | if (!eyeColorList.colors.Any(x => x.AproxComparison(color))) |
| | 363 | | { |
| 92 | 364 | | colorToSet = eyeColorList.colors[eyeColorList.defaultColor]; |
| | 365 | | } |
| | 366 | |
|
| 102 | 367 | | model.eyesColor = colorToSet; |
| 102 | 368 | | view.SelectEyeColor(model.eyesColor); |
| 102 | 369 | | } |
| | 370 | |
|
| | 371 | | private void EquipSkinColor(Color color) |
| | 372 | | { |
| 101 | 373 | | var colorToSet = color; |
| 1033 | 374 | | if (!skinColorList.colors.Any(x => x.AproxComparison(colorToSet))) |
| | 375 | | { |
| 92 | 376 | | colorToSet = skinColorList.colors[skinColorList.defaultColor]; |
| | 377 | | } |
| | 378 | |
|
| 101 | 379 | | model.skinColor = colorToSet; |
| 101 | 380 | | view.SelectSkinColor(model.skinColor); |
| 101 | 381 | | } |
| | 382 | |
|
| | 383 | | private void EquipBodyShape(WearableItem bodyShape) |
| | 384 | | { |
| 102 | 385 | | if (bodyShape.data.category != Categories.BODY_SHAPE) |
| | 386 | | { |
| 0 | 387 | | Debug.LogError($"Item ({bodyShape.id} is not a body shape"); |
| 0 | 388 | | return; |
| | 389 | | } |
| | 390 | |
|
| 102 | 391 | | if (model.bodyShape == bodyShape) |
| 52 | 392 | | return; |
| | 393 | |
|
| 50 | 394 | | model.bodyShape = bodyShape; |
| 50 | 395 | | view.UpdateSelectedBody(bodyShape); |
| | 396 | |
|
| 50 | 397 | | int wearablesCount = model.wearables.Count; |
| 140 | 398 | | for (var i = wearablesCount - 1; i >= 0; i--) |
| | 399 | | { |
| 20 | 400 | | UnequipWearable(model.wearables[i]); |
| | 401 | | } |
| | 402 | |
|
| 50 | 403 | | var defaultWearables = WearableLiterals.DefaultWearables.GetDefaultWearables(bodyShape.id); |
| 810 | 404 | | for (var i = 0; i < defaultWearables.Length; i++) |
| | 405 | | { |
| 355 | 406 | | if (catalog.TryGetValue(defaultWearables[i], out var wearable)) |
| 355 | 407 | | EquipWearable(wearable); |
| | 408 | | } |
| 50 | 409 | | } |
| | 410 | |
|
| | 411 | | private void EquipWearable(WearableItem wearable) |
| | 412 | | { |
| 978 | 413 | | if (!wearablesByCategory.ContainsKey(wearable.data.category)) |
| 0 | 414 | | return; |
| | 415 | |
|
| 978 | 416 | | if (wearablesByCategory[wearable.data.category].Contains(wearable) && wearable.SupportsBodyShape(model.bodyShape |
| | 417 | | { |
| 978 | 418 | | var toReplace = GetWearablesReplacedBy(wearable); |
| 978 | 419 | | toReplace.ForEach(UnequipWearable); |
| 978 | 420 | | model.wearables.Add(wearable); |
| 978 | 421 | | view.EquipWearable(wearable); |
| 978 | 422 | | avatarIsDirty = true; |
| | 423 | | } |
| 978 | 424 | | } |
| | 425 | |
|
| | 426 | | private void UnequipWearable(WearableItem wearable) |
| | 427 | | { |
| 24 | 428 | | if (model.wearables.Contains(wearable)) |
| | 429 | | { |
| 24 | 430 | | model.wearables.Remove(wearable); |
| 24 | 431 | | view.UnequipWearable(wearable); |
| 24 | 432 | | avatarIsDirty = true; |
| | 433 | | } |
| 24 | 434 | | } |
| | 435 | |
|
| | 436 | | public void UnequipAllWearables() |
| | 437 | | { |
| 982 | 438 | | foreach (var wearable in model.wearables) |
| | 439 | | { |
| 419 | 440 | | view.UnequipWearable(wearable); |
| | 441 | | } |
| | 442 | |
|
| 72 | 443 | | model.wearables.Clear(); |
| 72 | 444 | | } |
| | 445 | |
|
| | 446 | | private void ProcessCatalog(BaseDictionary<string, WearableItem> catalog) |
| | 447 | | { |
| 145 | 448 | | wearablesByCategory.Clear(); |
| 145 | 449 | | view.RemoveAllWearables(); |
| 145 | 450 | | using (var iterator = catalog.Get().GetEnumerator()) |
| | 451 | | { |
| 5329 | 452 | | while (iterator.MoveNext()) |
| | 453 | | { |
| 5184 | 454 | | AddWearable(iterator.Current.Key, iterator.Current.Value); |
| | 455 | | } |
| 145 | 456 | | } |
| 145 | 457 | | } |
| | 458 | |
|
| | 459 | | private void AddWearable(string id, WearableItem wearable) |
| | 460 | | { |
| 5192 | 461 | | if (!wearable.data.tags.Contains("base-wearable") && userProfile.GetItemAmount(id) == 0) |
| | 462 | | { |
| 1142 | 463 | | return; |
| | 464 | | } |
| | 465 | |
|
| 4050 | 466 | | if (!wearablesByCategory.ContainsKey(wearable.data.category)) |
| | 467 | | { |
| 1584 | 468 | | wearablesByCategory.Add(wearable.data.category, new List<WearableItem>()); |
| | 469 | | } |
| | 470 | |
|
| 4050 | 471 | | wearablesByCategory[wearable.data.category].Add(wearable); |
| 4050 | 472 | | view.AddWearable(wearable, userProfile.GetItemAmount(id)); |
| 4050 | 473 | | } |
| | 474 | |
|
| | 475 | | private void RemoveWearable(string id, WearableItem wearable) |
| | 476 | | { |
| 0 | 477 | | if (wearablesByCategory.ContainsKey(wearable.data.category)) |
| | 478 | | { |
| 0 | 479 | | if (wearablesByCategory[wearable.data.category].Remove(wearable)) |
| | 480 | | { |
| 0 | 481 | | if (wearablesByCategory[wearable.data.category].Count == 0) |
| | 482 | | { |
| 0 | 483 | | wearablesByCategory.Remove(wearable.data.category); |
| | 484 | | } |
| | 485 | | } |
| | 486 | | } |
| | 487 | |
|
| 0 | 488 | | view.RemoveWearable(wearable); |
| 0 | 489 | | } |
| | 490 | |
|
| | 491 | | public void RandomizeWearables() |
| | 492 | | { |
| 1 | 493 | | EquipHairColor(hairColorList.colors[UnityEngine.Random.Range(0, hairColorList.colors.Count)]); |
| 1 | 494 | | EquipEyesColor(eyeColorList.colors[UnityEngine.Random.Range(0, eyeColorList.colors.Count)]); |
| | 495 | |
|
| 1 | 496 | | model.wearables.Clear(); |
| 1 | 497 | | view.UnselectAllWearables(); |
| 1 | 498 | | using (var iterator = wearablesByCategory.GetEnumerator()) |
| | 499 | | { |
| 12 | 500 | | while (iterator.MoveNext()) |
| | 501 | | { |
| 11 | 502 | | string category = iterator.Current.Key; |
| 11 | 503 | | if (!categoriesToRandomize.Contains(category)) |
| | 504 | | { |
| | 505 | | continue; |
| | 506 | | } |
| | 507 | |
|
| 29 | 508 | | var supportedWearables = iterator.Current.Value.Where(x => x.SupportsBodyShape(model.bodyShape.id)).ToAr |
| 7 | 509 | | if (supportedWearables.Length == 0) |
| | 510 | | { |
| 0 | 511 | | Debug.LogError($"Couldn't get any wearable for category {category} and bodyshape {model.bodyShape.id |
| | 512 | | } |
| | 513 | |
|
| 7 | 514 | | var wearable = supportedWearables[UnityEngine.Random.Range(0, supportedWearables.Length - 1)]; |
| 7 | 515 | | EquipWearable(wearable); |
| | 516 | | } |
| 1 | 517 | | } |
| | 518 | |
|
| 1 | 519 | | UpdateAvatarPreview(); |
| 1 | 520 | | } |
| | 521 | |
|
| | 522 | | public List<WearableItem> GetWearablesReplacedBy(WearableItem wearableItem) |
| | 523 | | { |
| 978 | 524 | | List<WearableItem> wearablesToReplace = new List<WearableItem>(); |
| | 525 | |
|
| 978 | 526 | | HashSet<string> categoriesToReplace = new HashSet<string>(wearableItem.GetReplacesList(model.bodyShape.id) ?? ne |
| | 527 | |
|
| 978 | 528 | | int wearableCount = model.wearables.Count; |
| 7420 | 529 | | for (int i = 0; i < wearableCount; i++) |
| | 530 | | { |
| 2732 | 531 | | var wearable = model.wearables[i]; |
| 2732 | 532 | | if (wearable == null) |
| | 533 | | continue; |
| | 534 | |
|
| 2732 | 535 | | if (categoriesToReplace.Contains(wearable.data.category)) |
| | 536 | | { |
| 2 | 537 | | wearablesToReplace.Add(wearable); |
| 2 | 538 | | } |
| | 539 | | else |
| | 540 | | { |
| | 541 | | //For retrocompatibility's sake we check current wearables against new one (compatibility matrix is symm |
| 2730 | 542 | | HashSet<string> replacesList = new HashSet<string>(wearable.GetReplacesList(model.bodyShape.id) ?? new s |
| 2730 | 543 | | if (replacesList.Contains(wearableItem.data.category)) |
| | 544 | | { |
| 0 | 545 | | wearablesToReplace.Add(wearable); |
| | 546 | | } |
| | 547 | | } |
| | 548 | | } |
| | 549 | |
|
| 978 | 550 | | return wearablesToReplace; |
| | 551 | | } |
| | 552 | |
|
| 46 | 553 | | private float prevRenderScale = 1.0f; |
| | 554 | | private Camera mainCamera; |
| | 555 | |
|
| 84 | 556 | | public void SetVisibility(bool visible) { avatarEditorVisible.Set(visible); } |
| | 557 | |
|
| 176 | 558 | | private void OnAvatarEditorVisibleChanged(bool current, bool previous) { SetVisibility_Internal(current); } |
| | 559 | |
|
| | 560 | | public void SetVisibility_Internal(bool visible) |
| | 561 | | { |
| 88 | 562 | | var currentRenderProfile = DCL.RenderProfileManifest.i.currentProfile; |
| | 563 | |
|
| 88 | 564 | | if (!visible && view.isOpen) |
| | 565 | | { |
| 1 | 566 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 567 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f); |
| | 568 | |
|
| 1 | 569 | | DCL.Environment.i.messaging.manager.paused = false; |
| 1 | 570 | | DataStore.i.skyboxConfig.avatarMatProfile.Set(AvatarMaterialProfile.InWorld); |
| 1 | 571 | | currentRenderProfile.avatarProfile.Apply(); |
| 1 | 572 | | if (prevMouseLockState && DataStore.i.common.isSignUpFlow.Get()) |
| | 573 | | { |
| 0 | 574 | | Utils.LockCursor(); |
| | 575 | | } |
| | 576 | |
|
| | 577 | | // NOTE(Brian): SSAO doesn't work correctly with the offseted avatar preview if the renderScale != 1.0 |
| 1 | 578 | | var asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset; |
| 1 | 579 | | asset.renderScale = prevRenderScale; |
| | 580 | |
|
| 1 | 581 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 582 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(false); |
| | 583 | |
|
| 1 | 584 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| | 585 | |
|
| 1 | 586 | | OnClose?.Invoke(); |
| 0 | 587 | | } |
| 87 | 588 | | else if (visible && !view.isOpen) |
| | 589 | | { |
| 41 | 590 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 591 | | DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(0f); |
| | 592 | |
|
| 41 | 593 | | LoadOwnedWereables(userProfile); |
| 41 | 594 | | DCL.Environment.i.messaging.manager.paused = DataStore.i.common.isSignUpFlow.Get(); |
| 41 | 595 | | DataStore.i.skyboxConfig.avatarMatProfile.Set(AvatarMaterialProfile.InEditor); |
| 41 | 596 | | currentRenderProfile.avatarProfile.Apply(); |
| | 597 | |
|
| 41 | 598 | | prevMouseLockState = Utils.isCursorLocked; |
| | 599 | |
|
| 41 | 600 | | if (DataStore.i.common.isSignUpFlow.Get() || !DataStore.i.exploreV2.isInitialized.Get()) |
| 41 | 601 | | Utils.UnlockCursor(); |
| | 602 | |
|
| | 603 | | // NOTE(Brian): SSAO doesn't work correctly with the offseted avatar preview if the renderScale != 1.0 |
| 41 | 604 | | var asset = GraphicsSettings.renderPipelineAsset as UniversalRenderPipelineAsset; |
| 41 | 605 | | prevRenderScale = asset.renderScale; |
| 41 | 606 | | asset.renderScale = 1.0f; |
| | 607 | |
|
| 41 | 608 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 609 | | CommonScriptableObjects.isFullscreenHUDOpen.Set(true); |
| | 610 | |
|
| 41 | 611 | | DataStore.i.common.isPlayerRendererLoaded.OnChange += PlayerRendererLoaded; |
| | 612 | |
|
| 41 | 613 | | OnOpen?.Invoke(); |
| | 614 | | } |
| | 615 | |
|
| 88 | 616 | | currentRenderProfile.avatarProfile.Apply(); |
| 88 | 617 | | view.SetVisibility(visible); |
| 88 | 618 | | } |
| | 619 | |
|
| | 620 | | public void Dispose() |
| | 621 | | { |
| 41 | 622 | | avatarEditorVisible.OnChange -= OnAvatarEditorVisibleChanged; |
| 41 | 623 | | configureBackpackInFullscreenMenu.OnChange -= ConfigureBackpackInFullscreenMenuChanged; |
| 41 | 624 | | view.OnCloseActionTriggered -= DiscardAndClose; |
| 41 | 625 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 41 | 626 | | exploreV2IsOpen.OnChange -= ExploreV2IsOpenChanged; |
| | 627 | |
|
| 41 | 628 | | CleanUp(); |
| 41 | 629 | | } |
| | 630 | |
|
| | 631 | | public void CleanUp() |
| | 632 | | { |
| 46 | 633 | | UnequipAllWearables(); |
| | 634 | |
|
| 46 | 635 | | if (view != null) |
| 46 | 636 | | view.CleanUp(); |
| | 637 | |
|
| 46 | 638 | | this.userProfile.OnUpdate -= LoadUserProfile; |
| 46 | 639 | | this.catalog.OnAdded -= AddWearable; |
| 46 | 640 | | this.catalog.OnRemoved -= RemoveWearable; |
| 46 | 641 | | DataStore.i.common.isPlayerRendererLoaded.OnChange -= PlayerRendererLoaded; |
| 46 | 642 | | } |
| | 643 | |
|
| 0 | 644 | | public void SetConfiguration(HUDConfiguration configuration) { SetVisibility(configuration.active); } |
| | 645 | |
|
| | 646 | | public void SaveAvatar(Texture2D faceSnapshot, Texture2D face128Snapshot, Texture2D face256Snapshot, Texture2D bodyS |
| | 647 | | { |
| 1 | 648 | | var avatarModel = model.ToAvatarModel(); |
| | 649 | |
|
| 1 | 650 | | WebInterface.SendSaveAvatar(avatarModel, faceSnapshot, face128Snapshot, face256Snapshot, bodySnapshot, DataStore |
| 1 | 651 | | userProfile.OverrideAvatar(avatarModel, face256Snapshot); |
| 1 | 652 | | if (DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 653 | | DataStore.i.HUDs.signupVisible.Set(true); |
| | 654 | |
|
| 1 | 655 | | avatarIsDirty = false; |
| 1 | 656 | | SetVisibility(false); |
| 1 | 657 | | } |
| | 658 | |
|
| | 659 | | public void DiscardAndClose() |
| | 660 | | { |
| 0 | 661 | | if (!view.isOpen) |
| 0 | 662 | | return; |
| | 663 | |
|
| 0 | 664 | | if (!DataStore.i.common.isSignUpFlow.Get()) |
| 0 | 665 | | LoadUserProfile(userProfile); |
| | 666 | | else |
| 0 | 667 | | WebInterface.SendCloseUserAvatar(true); |
| | 668 | |
|
| 0 | 669 | | SetVisibility(false); |
| 0 | 670 | | } |
| | 671 | |
|
| | 672 | | public void GoToMarketplace() |
| | 673 | | { |
| 0 | 674 | | if (userProfile.hasConnectedWeb3) |
| 0 | 675 | | WebInterface.OpenURL(URL_MARKET_PLACE); |
| | 676 | | else |
| 0 | 677 | | WebInterface.OpenURL(URL_GET_A_WALLET); |
| 0 | 678 | | } |
| | 679 | |
|
| | 680 | | public void SellCollectible(string collectibleId) |
| | 681 | | { |
| 0 | 682 | | var ownedCollectible = ownedNftCollectionsL1.FirstOrDefault(nft => nft.urn == collectibleId); |
| 0 | 683 | | if (ownedCollectible == null) |
| 0 | 684 | | ownedCollectible = ownedNftCollectionsL2.FirstOrDefault(nft => nft.urn == collectibleId); |
| | 685 | |
|
| 0 | 686 | | if (ownedCollectible != null) |
| 0 | 687 | | WebInterface.OpenURL(URL_SELL_SPECIFIC_COLLECTIBLE.Replace("{collectionId}", ownedCollectible.collectionId). |
| | 688 | | else |
| 0 | 689 | | WebInterface.OpenURL(URL_SELL_COLLECTIBLE_GENERIC); |
| 0 | 690 | | } |
| | 691 | |
|
| 0 | 692 | | public void ToggleVisibility() { SetVisibility(!view.isOpen); } |
| | 693 | |
|
| 92 | 694 | | private void ConfigureBackpackInFullscreenMenuChanged(Transform currentParentTransform, Transform previousParentTran |
| | 695 | |
|
| | 696 | | private void ExploreV2IsOpenChanged(bool current, bool previous) |
| | 697 | | { |
| 0 | 698 | | if (!current && avatarIsDirty) |
| | 699 | | { |
| 0 | 700 | | LoadUserProfile(userProfile, true); |
| 0 | 701 | | avatarIsDirty = false; |
| | 702 | | } |
| 0 | 703 | | } |
| | 704 | | } |