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