| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.CompilerServices; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | [assembly: InternalsVisibleTo("AvatarEditorHUDTests")] |
| | 8 | |
|
| | 9 | | public class ItemSelector : MonoBehaviour |
| | 10 | | { |
| | 11 | | [SerializeField] |
| | 12 | | internal ItemToggleFactory itemToggleFactory; |
| | 13 | |
|
| | 14 | | [SerializeField] |
| | 15 | | private RectTransform itemContainer; |
| | 16 | |
|
| | 17 | | [SerializeField] |
| | 18 | | internal GameObject loadingSpinner; |
| | 19 | |
|
| | 20 | | [SerializeField] |
| | 21 | | internal GameObject loadingRetry; |
| | 22 | |
|
| | 23 | | [SerializeField] |
| | 24 | | internal Button loadingRetryButton; |
| | 25 | |
|
| | 26 | | public event System.Action<string> OnItemClicked; |
| | 27 | | public event System.Action<string> OnSellClicked; |
| | 28 | | public event System.Action OnRetryClicked; |
| | 29 | |
|
| 846 | 30 | | internal Dictionary<string, ItemToggle> itemToggles = new Dictionary<string, ItemToggle>(); |
| | 31 | |
|
| | 32 | | private string currentBodyShape; |
| | 33 | |
|
| | 34 | | private void Awake() |
| | 35 | | { |
| 136 | 36 | | Application.quitting += () => |
| | 37 | | { |
| 0 | 38 | | OnItemClicked = null; |
| 0 | 39 | | }; |
| | 40 | |
|
| 136 | 41 | | loadingRetryButton.onClick.AddListener(RetryLoading); |
| 136 | 42 | | } |
| | 43 | |
|
| 272 | 44 | | private void OnDestroy() { loadingRetryButton.onClick.RemoveListener(RetryLoading); } |
| | 45 | |
|
| | 46 | | public void AddItemToggle(WearableItem item, int amount, |
| | 47 | | Func<WearableItem, bool> hideOtherWearablesToastStrategy, |
| | 48 | | Func<WearableItem, bool> replaceOtherWearablesToastStrategy) |
| | 49 | | { |
| 4068 | 50 | | if (item == null) |
| 0 | 51 | | return; |
| 4068 | 52 | | if (itemToggles.ContainsKey(item.id)) |
| 0 | 53 | | return; |
| | 54 | |
|
| | 55 | | ItemToggle newToggle; |
| 4068 | 56 | | if (item.IsCollectible()) |
| | 57 | | { |
| 36 | 58 | | newToggle = itemToggleFactory.CreateItemToggleFromRarity(item.rarity, itemContainer); |
| 36 | 59 | | newToggle.transform.SetAsFirstSibling(); |
| 36 | 60 | | } |
| | 61 | | else |
| | 62 | | { |
| 4032 | 63 | | newToggle = itemToggleFactory.CreateBaseWearable(itemContainer); |
| | 64 | | } |
| | 65 | |
|
| 4068 | 66 | | newToggle.Initialize(item, false, amount); |
| 4068 | 67 | | newToggle.SetHideOtherWerablesToastStrategy(hideOtherWearablesToastStrategy); |
| 4068 | 68 | | newToggle.SetReplaceOtherWearablesToastStrategy(replaceOtherWearablesToastStrategy); |
| 4068 | 69 | | newToggle.OnClicked += ToggleClicked; |
| 4068 | 70 | | newToggle.OnSellClicked += SellClicked; |
| 4068 | 71 | | itemToggles.Add(item.id, newToggle); |
| | 72 | |
|
| 4068 | 73 | | bool active = string.IsNullOrEmpty(currentBodyShape) || item.SupportsBodyShape(currentBodyShape); |
| 4068 | 74 | | newToggle.gameObject.SetActive(active); |
| 4068 | 75 | | } |
| | 76 | |
|
| | 77 | | public void RemoveItemToggle(string itemID) |
| | 78 | | { |
| 0 | 79 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 80 | | return; |
| | 81 | |
|
| 0 | 82 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 0 | 83 | | if (toggle == null) |
| 0 | 84 | | return; |
| | 85 | |
|
| 0 | 86 | | itemToggles.Remove(itemID); |
| 0 | 87 | | Destroy(toggle.gameObject); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void RemoveAllItemToggle() |
| | 91 | | { |
| 2610 | 92 | | using (var it = itemToggles.GetEnumerator()) |
| | 93 | | { |
| 5382 | 94 | | while (it.MoveNext()) |
| | 95 | | { |
| 2772 | 96 | | Destroy(it.Current.Value.gameObject); |
| | 97 | | } |
| 2610 | 98 | | } |
| | 99 | |
|
| 2610 | 100 | | itemToggles.Clear(); |
| 2610 | 101 | | } |
| | 102 | |
|
| | 103 | | public void SetBodyShape(string bodyShape) |
| | 104 | | { |
| 850 | 105 | | if (currentBodyShape == bodyShape) |
| 0 | 106 | | return; |
| | 107 | |
|
| 850 | 108 | | currentBodyShape = bodyShape; |
| 850 | 109 | | ShowCompatibleWithBodyShape(); |
| 850 | 110 | | } |
| | 111 | |
|
| | 112 | | private void ShowCompatibleWithBodyShape() |
| | 113 | | { |
| 850 | 114 | | using (Dictionary<string, ItemToggle>.Enumerator iterator = itemToggles.GetEnumerator()) |
| | 115 | | { |
| 2150 | 116 | | while (iterator.MoveNext()) |
| | 117 | | { |
| 1300 | 118 | | ItemToggle current = iterator.Current.Value; |
| 1300 | 119 | | bool active = current.wearableItem.SupportsBodyShape(currentBodyShape); |
| 1300 | 120 | | current.gameObject.SetActive(active); |
| | 121 | | } |
| 850 | 122 | | } |
| 850 | 123 | | } |
| | 124 | |
|
| | 125 | | public void Select(string itemID) |
| | 126 | | { |
| 2006 | 127 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 2006 | 128 | | if (toggle != null) |
| 1028 | 129 | | toggle.selected = true; |
| 2006 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetWearableLoadingSpinner(string wearableID, bool isActive) |
| | 133 | | { |
| 3726 | 134 | | ItemToggle toggle = GetItemToggleByID(wearableID); |
| 3726 | 135 | | if (toggle != null) |
| 1859 | 136 | | toggle.SetLoadingSpinner(isActive); |
| 3726 | 137 | | } |
| | 138 | |
|
| | 139 | | public void Unselect(string itemID) |
| | 140 | | { |
| 886 | 141 | | ItemToggle toggle = GetItemToggleByID(itemID); |
| 886 | 142 | | if (toggle != null) |
| 443 | 143 | | toggle.selected = false; |
| 886 | 144 | | } |
| | 145 | |
|
| | 146 | | public void UnselectAll() |
| | 147 | | { |
| 1750 | 148 | | using (var iterator = itemToggles.GetEnumerator()) |
| | 149 | | { |
| 4470 | 150 | | while (iterator.MoveNext()) |
| | 151 | | { |
| 2720 | 152 | | iterator.Current.Value.selected = false; |
| | 153 | | } |
| 1750 | 154 | | } |
| 1750 | 155 | | } |
| | 156 | |
|
| 0 | 157 | | private void ToggleClicked(ItemToggle toggle) { OnItemClicked?.Invoke(toggle.wearableItem.id); } |
| | 158 | |
|
| 0 | 159 | | private void SellClicked(ItemToggle toggle) { OnSellClicked?.Invoke(toggle.wearableItem.id); } |
| | 160 | |
|
| | 161 | | private ItemToggle GetItemToggleByID(string itemID) |
| | 162 | | { |
| 6618 | 163 | | if (string.IsNullOrEmpty(itemID)) |
| 0 | 164 | | return null; |
| 6618 | 165 | | return itemToggles.ContainsKey(itemID) ? itemToggles[itemID] : null; |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public void ShowLoading(bool isActive) |
| | 169 | | { |
| 1 | 170 | | loadingSpinner.SetActive(isActive); |
| 1 | 171 | | loadingSpinner.transform.SetAsLastSibling(); |
| 1 | 172 | | } |
| | 173 | |
|
| | 174 | | public void ShowRetryLoading(bool isActive) |
| | 175 | | { |
| 1 | 176 | | loadingRetry.SetActive(isActive); |
| 1 | 177 | | loadingRetry.transform.SetAsLastSibling(); |
| 1 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | private void RetryLoading() { OnRetryClicked?.Invoke(); } |
| | 181 | | } |