| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | [assembly: InternalsVisibleTo("AvatarEditorHUDTests")] |
| | 10 | |
|
| | 11 | | public class ItemSelector : MonoBehaviour |
| | 12 | | { |
| | 13 | | private const string DECENTRALAND_COLLECTION_ID = "Decentraland"; |
| | 14 | |
|
| | 15 | | [SerializeField] |
| | 16 | | internal ItemToggleFactory itemToggleFactory; |
| | 17 | |
|
| | 18 | | [SerializeField] |
| | 19 | | internal CollectionGroup collectionGroupPrefab; |
| | 20 | |
|
| | 21 | | [SerializeField] |
| | 22 | | internal RectTransform content; |
| | 23 | |
|
| | 24 | | [SerializeField] |
| | 25 | | internal GameObject loadingSpinner; |
| | 26 | |
|
| | 27 | | [SerializeField] |
| | 28 | | internal GameObject loadingRetry; |
| | 29 | |
|
| | 30 | | [SerializeField] |
| | 31 | | internal Button loadingRetryButton; |
| | 32 | |
|
| | 33 | | public event System.Action<string> OnItemClicked; |
| | 34 | | public event System.Action<string> OnSellClicked; |
| | 35 | | public event System.Action OnRetryClicked; |
| | 36 | |
|
| 900 | 37 | | internal Dictionary<string, ItemToggle> itemToggles = new Dictionary<string, ItemToggle>(); |
| 900 | 38 | | internal Dictionary<string, CollectionGroup> currentCollectionGroups = new Dictionary<string, CollectionGroup>(); |
| | 39 | |
|
| | 40 | | private string currentBodyShape; |
| | 41 | |
|
| | 42 | | private void Awake() |
| | 43 | | { |
| 94 | 44 | | Application.quitting += () => |
| | 45 | | { |
| 0 | 46 | | OnItemClicked = null; |
| 0 | 47 | | }; |
| | 48 | |
|
| 94 | 49 | | loadingRetryButton.onClick.AddListener(RetryLoading); |
| 94 | 50 | | } |
| | 51 | |
|
| 188 | 52 | | private void OnDestroy() { loadingRetryButton.onClick.RemoveListener(RetryLoading); } |
| | 53 | |
|
| | 54 | | public void AddItemToggle( |
| | 55 | | WearableItem item, |
| | 56 | | string collectionName, |
| | 57 | | int amount, |
| | 58 | | Func<WearableItem, bool> hideOtherWearablesToastStrategy, |
| | 59 | | Func<WearableItem, bool> replaceOtherWearablesToastStrategy) |
| | 60 | | { |
| | 61 | | CollectionGroup collectionGroup; |
| 4242 | 62 | | if (item.IsFromThirdPartyCollection) |
| 0 | 63 | | collectionGroup = CreateCollectionGroupIfNeeded(item.ThirdPartyCollectionId, collectionName); |
| | 64 | | else |
| 4242 | 65 | | collectionGroup = CreateCollectionGroupIfNeeded(DECENTRALAND_COLLECTION_ID, DECENTRALAND_COLLECTION_ID); |
| | 66 | |
|
| 4242 | 67 | | if (item == null) |
| 0 | 68 | | return; |
| 4242 | 69 | | if (itemToggles.ContainsKey(item.id)) |
| 0 | 70 | | return; |
| | 71 | |
|
| | 72 | | ItemToggle newToggle; |
| 4242 | 73 | | if (item.IsCollectible()) |
| | 74 | | { |
| 42 | 75 | | newToggle = itemToggleFactory.CreateItemToggleFromRarity(item.rarity, collectionGroup.itemContainer); |
| 42 | 76 | | newToggle.transform.SetAsFirstSibling(); |
| 42 | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 4200 | 80 | | newToggle = itemToggleFactory.CreateBaseWearable(collectionGroup.itemContainer); |
| | 81 | | } |
| | 82 | |
|
| 4242 | 83 | | newToggle.Initialize(item, false, amount); |
| 4242 | 84 | | newToggle.SetHideOtherWerablesToastStrategy(hideOtherWearablesToastStrategy); |
| 4242 | 85 | | newToggle.SetReplaceOtherWearablesToastStrategy(replaceOtherWearablesToastStrategy); |
| 4242 | 86 | | newToggle.OnClicked += ToggleClicked; |
| 4242 | 87 | | newToggle.OnSellClicked += SellClicked; |
| 4242 | 88 | | newToggle.collectionId = collectionGroup.collectionId; |
| 4242 | 89 | | itemToggles.Add(item.id, newToggle); |
| | 90 | |
|
| 4242 | 91 | | bool active = string.IsNullOrEmpty(currentBodyShape) || item.SupportsBodyShape(currentBodyShape); |
| 4242 | 92 | | newToggle.gameObject.SetActive(active); |
| 4242 | 93 | | } |
| | 94 | |
|
| | 95 | | public void RemoveItemToggle(string itemID) |
| | 96 | | { |
| 0 | 97 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 98 | | return; |
| | 99 | |
|
| 0 | 100 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 0 | 101 | | if (toggle == null) |
| 0 | 102 | | return; |
| | 103 | |
|
| 0 | 104 | | itemToggles.Remove(itemID); |
| 0 | 105 | | Destroy(toggle.gameObject); |
| 0 | 106 | | RemoveCollectionGroupIfNeeded(toggle.collectionId); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public void RemoveAllItemToggle() |
| | 110 | | { |
| 2718 | 111 | | using (var it = itemToggles.GetEnumerator()) |
| | 112 | | { |
| 5602 | 113 | | while (it.MoveNext()) |
| | 114 | | { |
| 2884 | 115 | | Destroy(it.Current.Value.gameObject); |
| 2884 | 116 | | RemoveCollectionGroupIfNeeded(it.Current.Value.collectionId); |
| | 117 | | } |
| 2718 | 118 | | } |
| | 119 | |
|
| 2718 | 120 | | itemToggles.Clear(); |
| 2718 | 121 | | } |
| | 122 | |
|
| | 123 | | public void SetBodyShape(string bodyShape) |
| | 124 | | { |
| 901 | 125 | | if (currentBodyShape == bodyShape) |
| 0 | 126 | | return; |
| | 127 | |
|
| 901 | 128 | | currentBodyShape = bodyShape; |
| 901 | 129 | | ShowCompatibleWithBodyShape(); |
| 901 | 130 | | } |
| | 131 | |
|
| 188 | 132 | | private void OnEnable() { UpdateSelectorLayout(); } |
| | 133 | |
|
| 5624 | 134 | | public void UpdateSelectorLayout() { Utils.ForceUpdateLayout(content); } |
| | 135 | |
|
| | 136 | | private void ShowCompatibleWithBodyShape() |
| | 137 | | { |
| 901 | 138 | | using (Dictionary<string, ItemToggle>.Enumerator iterator = itemToggles.GetEnumerator()) |
| | 139 | | { |
| 2279 | 140 | | while (iterator.MoveNext()) |
| | 141 | | { |
| 1378 | 142 | | ItemToggle current = iterator.Current.Value; |
| 1378 | 143 | | bool active = current.wearableItem.SupportsBodyShape(currentBodyShape); |
| 1378 | 144 | | current.gameObject.SetActive(active); |
| | 145 | | } |
| 901 | 146 | | } |
| 901 | 147 | | } |
| | 148 | |
|
| | 149 | | public void Select(string itemID) |
| | 150 | | { |
| 2107 | 151 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 2107 | 152 | | if (toggle != null) |
| 1080 | 153 | | toggle.selected = true; |
| 2107 | 154 | | } |
| | 155 | |
|
| | 156 | | public void SetWearableLoadingSpinner(string wearableID, bool isActive) |
| | 157 | | { |
| 2982 | 158 | | ItemToggle toggle = GetItemToggleByID(wearableID); |
| 2982 | 159 | | if (toggle != null) |
| 1491 | 160 | | toggle.SetLoadingSpinner(isActive); |
| 2982 | 161 | | } |
| | 162 | |
|
| | 163 | | public void Unselect(string itemID) |
| | 164 | | { |
| 928 | 165 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 928 | 166 | | if (toggle != null) |
| 464 | 167 | | toggle.selected = false; |
| 928 | 168 | | } |
| | 169 | |
|
| | 170 | | public void UnselectAll() |
| | 171 | | { |
| 1821 | 172 | | using (var iterator = itemToggles.GetEnumerator()) |
| | 173 | | { |
| 4651 | 174 | | while (iterator.MoveNext()) |
| | 175 | | { |
| 2830 | 176 | | iterator.Current.Value.selected = false; |
| | 177 | | } |
| 1821 | 178 | | } |
| 1821 | 179 | | } |
| | 180 | |
|
| 0 | 181 | | private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); } |
| | 182 | |
|
| 0 | 183 | | private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); } |
| | 184 | |
|
| | 185 | | private ItemToggle GetItemToggleByID(string itemID) |
| | 186 | | { |
| 6017 | 187 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 188 | | return null; |
| 6017 | 189 | | return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | public void ShowLoading(bool isActive) |
| | 193 | | { |
| 0 | 194 | | loadingSpinner.SetActive(isActive); |
| 0 | 195 | | loadingSpinner.transform.SetAsLastSibling(); |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | public void ShowRetryLoading(bool isActive) |
| | 199 | | { |
| 0 | 200 | | loadingRetry.SetActive(isActive); |
| 0 | 201 | | loadingRetry.transform.SetAsLastSibling(); |
| 0 | 202 | | } |
| | 203 | |
|
| 0 | 204 | | private void RetryLoading() { OnRetryClicked?.Invoke(); } |
| | 205 | |
|
| | 206 | | private CollectionGroup CreateCollectionGroupIfNeeded(string collectionId, string collectionName) |
| | 207 | | { |
| 4242 | 208 | | if (currentCollectionGroups.ContainsKey(collectionId)) |
| 3706 | 209 | | return currentCollectionGroups[collectionId]; |
| | 210 | |
|
| 536 | 211 | | CollectionGroup newCollectionGroup = Instantiate(collectionGroupPrefab, content); |
| 536 | 212 | | newCollectionGroup.Configure(collectionId, collectionName); |
| 536 | 213 | | currentCollectionGroups.Add(collectionId, newCollectionGroup); |
| | 214 | |
|
| 536 | 215 | | if (collectionId == DECENTRALAND_COLLECTION_ID) |
| 536 | 216 | | newCollectionGroup.transform.SetAsFirstSibling(); |
| | 217 | | else |
| 0 | 218 | | newCollectionGroup.transform.SetAsLastSibling(); |
| | 219 | |
|
| 536 | 220 | | return newCollectionGroup; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | private bool RemoveCollectionGroupIfNeeded(string collectionId) |
| | 224 | | { |
| 2884 | 225 | | currentCollectionGroups.TryGetValue(collectionId, out CollectionGroup collectionGroupToRemove); |
| 11536 | 226 | | if (collectionGroupToRemove != null && itemToggles.Count(x => x.Value.collectionId == collectionId) == 0) |
| | 227 | | { |
| 0 | 228 | | currentCollectionGroups.Remove(collectionId); |
| 0 | 229 | | Destroy(collectionGroupToRemove.gameObject); |
| 0 | 230 | | return true; |
| | 231 | | } |
| | 232 | |
|
| 2884 | 233 | | return false; |
| | 234 | | } |
| | 235 | | } |