| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.CompilerServices; |
| | 5 | | using System.Threading; |
| | 6 | | using Cysharp.Threading.Tasks; |
| | 7 | | using DCL; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.UI; |
| | 10 | | #pragma warning disable CS4014 |
| | 11 | |
|
| | 12 | | [assembly: InternalsVisibleTo("AvatarEditorHUDTests")] |
| | 13 | |
|
| | 14 | | public class ItemSelector : MonoBehaviour |
| | 15 | | { |
| | 16 | | private const int MIN_SCREEN_SIZE = 200; |
| | 17 | | private const int AVATAR_MARGIN = 450; |
| | 18 | | private const float ASPECT_RATIO_PER_COLUMN = 0.15f; |
| | 19 | | private const int TOTAL_ROWS_OF_ITEMS = 3; |
| | 20 | | private const int MIN_AMOUNT_OF_COLUMNS = 3; |
| | 21 | |
|
| | 22 | | [SerializeField] internal UIPageSelector pageSelector; |
| | 23 | | [SerializeField] internal ItemToggleContainer itemToggleContainer; |
| | 24 | |
|
| | 25 | | public event Action<string> OnItemClicked; |
| | 26 | | public event Action<string> OnSellClicked; |
| | 27 | |
|
| 900 | 28 | | internal readonly Dictionary<string, ItemToggle> itemToggles = new Dictionary<string, ItemToggle>(); |
| 900 | 29 | | internal readonly Dictionary<string, WearableSettings> totalWearables = new Dictionary<string, WearableSettings>(); |
| 900 | 30 | | internal List<WearableSettings> availableWearables = new List<WearableSettings>(); |
| 900 | 31 | | internal readonly List<string> selectedItems = new List<string>(); |
| | 32 | |
|
| | 33 | | private string currentBodyShape; |
| 900 | 34 | | private int maxVisibleWearables = 9; |
| | 35 | | private int lastPage; |
| | 36 | |
|
| 900 | 37 | | private CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); |
| | 38 | |
|
| | 39 | | private void Awake() |
| | 40 | | { |
| 94 | 41 | | Application.quitting += () => |
| | 42 | | { |
| 0 | 43 | | OnItemClicked = null; |
| 0 | 44 | | }; |
| | 45 | |
|
| 94 | 46 | | pageSelector.OnValueChanged += UpdateWearableList; |
| 94 | 47 | | DataStore.i.screen.size.OnChange += OnScreenSizeChanged; |
| 94 | 48 | | } |
| | 49 | |
|
| | 50 | | private void OnEnable() |
| | 51 | | { |
| 94 | 52 | | CheckScreenSize(); |
| 94 | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnDestroy() |
| | 56 | | { |
| 94 | 57 | | cancellationTokenSource.Cancel(); |
| 94 | 58 | | cancellationTokenSource.Dispose(); |
| 94 | 59 | | } |
| | 60 | |
|
| | 61 | | private void OnScreenSizeChanged(Vector2Int current, Vector2Int previous) |
| | 62 | | { |
| 0 | 63 | | CheckScreenSize(); |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | private void CheckScreenSize() |
| | 67 | | { |
| 94 | 68 | | cancellationTokenSource.Cancel(); |
| 94 | 69 | | cancellationTokenSource.Dispose(); |
| 94 | 70 | | cancellationTokenSource = new CancellationTokenSource(); |
| 94 | 71 | | CheckScreenSizeAsync(cancellationTokenSource.Token); |
| 94 | 72 | | } |
| | 73 | |
|
| | 74 | | private async UniTask CheckScreenSizeAsync(CancellationToken cancellationToken) |
| | 75 | | { |
| | 76 | | try { |
| 94 | 77 | | RectTransform rt = (RectTransform)transform; |
| 94 | 78 | | LayoutRebuilder.MarkLayoutForRebuild(rt); |
| | 79 | |
|
| 282 | 80 | | await UniTask.NextFrame(PlayerLoopTiming.LastPostLateUpdate, cancellationToken); |
| | 81 | |
|
| 0 | 82 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 83 | |
|
| 0 | 84 | | var rect = rt.rect; |
| 0 | 85 | | var width = Mathf.Max(rect.width, MIN_SCREEN_SIZE); |
| 0 | 86 | | float itemAndSpaceSize = 130 + 32f; |
| 0 | 87 | | var columns = Mathf.Max(Mathf.CeilToInt(width / itemAndSpaceSize), MIN_AMOUNT_OF_COLUMNS); |
| | 88 | |
|
| 0 | 89 | | maxVisibleWearables = TOTAL_ROWS_OF_ITEMS * columns; |
| | 90 | |
|
| 0 | 91 | | SetupWearablePagination(); |
| 0 | 92 | | } |
| 188 | 93 | | catch (OperationCanceledException) { } |
| 94 | 94 | | } |
| | 95 | |
|
| | 96 | | private void SetupWearablePagination() |
| | 97 | | { |
| 6688 | 98 | | itemToggleContainer.Setup(maxVisibleWearables); |
| 6688 | 99 | | pageSelector.Setup(GetMaxPages()); |
| 6688 | 100 | | UpdateWearableList(lastPage); |
| 6688 | 101 | | } |
| | 102 | |
|
| 6688 | 103 | | private int GetMaxPages() => Mathf.CeilToInt(availableWearables.Count / (float)maxVisibleWearables); |
| | 104 | |
|
| | 105 | | private void UpdateWearableList( int page ) |
| | 106 | | { |
| 7184 | 107 | | lastPage = page; |
| 7184 | 108 | | itemToggles.Clear(); |
| 143680 | 109 | | for (int itemToggleIndex = 0; itemToggleIndex < maxVisibleWearables; itemToggleIndex++) |
| | 110 | | { |
| 64656 | 111 | | var baseIndex = page * maxVisibleWearables; |
| 64656 | 112 | | var wearableIndex = itemToggleIndex + baseIndex; |
| | 113 | |
|
| 64656 | 114 | | if (wearableIndex < availableWearables.Count) |
| | 115 | | { |
| 6447 | 116 | | WearableSettings wearableSettings = availableWearables[wearableIndex]; |
| 6447 | 117 | | var item = wearableSettings.Item; |
| 6447 | 118 | | var itemToggle = itemToggleContainer.LoadItem(itemToggleIndex, wearableSettings); |
| 6447 | 119 | | itemToggle.SetCallbacks(ToggleClicked, SellClicked); |
| 6447 | 120 | | itemToggle.SetLoadingSpinner(wearableSettings.isLoading); |
| | 121 | |
|
| 6447 | 122 | | if (selectedItems.Contains(item.id)) |
| 442 | 123 | | itemToggle.selected = true; |
| | 124 | |
|
| 6447 | 125 | | itemToggles[item.id] = itemToggle; |
| 6447 | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 58209 | 129 | | itemToggleContainer.HideItem(itemToggleIndex); |
| | 130 | | } |
| | 131 | | } |
| 7184 | 132 | | } |
| | 133 | |
|
| | 134 | | public void AddWearable( |
| | 135 | | WearableItem item, |
| | 136 | | string collectionName, |
| | 137 | | int amount, |
| | 138 | | Func<WearableItem, bool> hideOtherWearablesToastStrategy, |
| | 139 | | Func<WearableItem, bool> replaceOtherWearablesToastStrategy, |
| | 140 | | Func<WearableItem, bool> incompatibleWearableToastStrategy) |
| | 141 | | { |
| 4242 | 142 | | if (item == null) |
| 0 | 143 | | return; |
| | 144 | |
|
| 4242 | 145 | | if (totalWearables.ContainsKey(item.id)) |
| 0 | 146 | | return; |
| | 147 | |
|
| 4242 | 148 | | WearableSettings wearableSettings = new WearableSettings(item, collectionName, amount, hideOtherWearablesToastSt |
| 4242 | 149 | | totalWearables.Add(item.id, wearableSettings); |
| | 150 | |
|
| 4242 | 151 | | availableWearables.Add(wearableSettings); |
| 4242 | 152 | | } |
| | 153 | |
|
| | 154 | | public void RemoveWearable(string itemID) |
| | 155 | | { |
| 0 | 156 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 157 | | return; |
| | 158 | |
|
| 0 | 159 | | totalWearables.Remove(itemID); |
| 0 | 160 | | itemToggles.Remove(itemID); |
| | 161 | |
|
| 0 | 162 | | RefreshAvailableWearables(); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | public void RemoveAllWearables() |
| | 166 | | { |
| 2718 | 167 | | totalWearables.Clear(); |
| 2718 | 168 | | availableWearables.Clear(); |
| 2718 | 169 | | itemToggles.Clear(); |
| | 170 | |
|
| 2718 | 171 | | UpdateSelectorLayout(); |
| 2718 | 172 | | } |
| | 173 | |
|
| | 174 | | public void SetBodyShape(string bodyShape) |
| | 175 | | { |
| 1054 | 176 | | if (currentBodyShape == bodyShape) |
| 0 | 177 | | return; |
| | 178 | |
|
| 1054 | 179 | | currentBodyShape = bodyShape; |
| 1054 | 180 | | RefreshAvailableWearables(); |
| 1054 | 181 | | } |
| | 182 | |
|
| | 183 | | public void UpdateSelectorLayout() |
| | 184 | | { |
| 5634 | 185 | | SetupWearablePagination(); |
| 5634 | 186 | | } |
| | 187 | |
|
| | 188 | | private void RefreshAvailableWearables() |
| | 189 | | { |
| 1054 | 190 | | availableWearables = totalWearables.Values.ToList(); |
| 1054 | 191 | | SetupWearablePagination(); |
| 1054 | 192 | | } |
| | 193 | |
|
| | 194 | | public void Select(string itemID) |
| | 195 | | { |
| 2242 | 196 | | selectedItems.Add(itemID); |
| 2242 | 197 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 2242 | 198 | | if (toggle != null) |
| 1152 | 199 | | toggle.selected = true; |
| 2242 | 200 | | } |
| | 201 | |
|
| | 202 | | public void SetWearableLoadingSpinner(string wearableID, bool isActive) |
| | 203 | | { |
| 3108 | 204 | | if (totalWearables.ContainsKey(wearableID)) |
| | 205 | | { |
| 1554 | 206 | | totalWearables[wearableID].isLoading = isActive; |
| | 207 | | } |
| | 208 | |
|
| 3108 | 209 | | ItemToggle toggle = GetItemToggleByID(wearableID); |
| 3108 | 210 | | if (toggle != null) |
| 1554 | 211 | | toggle.SetLoadingSpinner(isActive); |
| 3108 | 212 | | } |
| | 213 | |
|
| | 214 | | public void Unselect(string itemID) |
| | 215 | | { |
| 928 | 216 | | selectedItems.Remove(itemID); |
| 928 | 217 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 928 | 218 | | if (toggle != null) |
| 464 | 219 | | toggle.selected = false; |
| 928 | 220 | | } |
| | 221 | |
|
| | 222 | | public void UnselectAll() |
| | 223 | | { |
| 1813 | 224 | | selectedItems.Clear(); |
| 1813 | 225 | | using (var iterator = itemToggles.GetEnumerator()) |
| | 226 | | { |
| 4635 | 227 | | while (iterator.MoveNext()) |
| | 228 | | { |
| 2822 | 229 | | iterator.Current.Value.selected = false; |
| | 230 | | } |
| 1813 | 231 | | } |
| 1813 | 232 | | } |
| | 233 | |
|
| 0 | 234 | | private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); } |
| | 235 | |
|
| 0 | 236 | | private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); } |
| | 237 | |
|
| | 238 | | private ItemToggle GetItemToggleByID(string itemID) |
| | 239 | | { |
| 6278 | 240 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 241 | | return null; |
| 6278 | 242 | | return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null; |
| | 243 | | } |
| | 244 | | } |