| | 1 | | using DCL.Helpers; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace DCL.Backpack |
| | 7 | | { |
| | 8 | | public class AvatarSlotsView : BaseComponentView, IAvatarSlotsView |
| | 9 | | { |
| | 10 | | [SerializeField] private RectTransform avatarSlotsContainer; |
| | 11 | |
|
| | 12 | | [Header("Prefab references")] |
| | 13 | | [SerializeField] private GameObject avatarSlotSectionPrefab; |
| | 14 | | [SerializeField] private GameObject avatarSlotPrefab; |
| | 15 | | [SerializeField] private GameObject sectionSeparator; |
| | 16 | |
|
| 37 | 17 | | private readonly Dictionary<string, Transform> avatarSlotSections = new (); |
| 37 | 18 | | private readonly Dictionary<string, IAvatarSlotComponentView> avatarSlots = new (); |
| | 19 | |
|
| | 20 | | public event IAvatarSlotsView.ToggleAvatarSlotDelegate OnToggleAvatarSlot; |
| | 21 | | public event Action<string> OnUnequipFromSlot; |
| | 22 | | public event Action<string, bool> OnHideUnhidePressed; |
| | 23 | |
|
| | 24 | | private Dictionary<string, HashSet<string>> previouslyHidden; |
| | 25 | |
|
| | 26 | | public override void Awake() |
| | 27 | | { |
| 35 | 28 | | previouslyHidden = new Dictionary<string, HashSet<string>>(); |
| | 29 | |
|
| 1330 | 30 | | foreach (string category in WearableItem.CATEGORIES_PRIORITY) |
| 630 | 31 | | previouslyHidden.Add(category, new HashSet<string>()); |
| 35 | 32 | | } |
| | 33 | |
|
| | 34 | | public void CreateAvatarSlotSection(string sectionName, bool addSeparator) |
| | 35 | | { |
| 0 | 36 | | avatarSlotSections.Add(sectionName, Instantiate(avatarSlotSectionPrefab, avatarSlotsContainer).transform); |
| | 37 | |
|
| 0 | 38 | | if (addSeparator) |
| 0 | 39 | | Instantiate(sectionSeparator, avatarSlotsContainer); |
| 0 | 40 | | } |
| | 41 | |
|
| | 42 | | public void RebuildLayout() |
| | 43 | | { |
| | 44 | | // Needed because adding elements to a transform that contains a |
| | 45 | | // layout does not refresh the child placements |
| 0 | 46 | | Utils.ForceRebuildLayoutImmediate(avatarSlotsContainer); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void AddSlotToSection(string sectionName, string slotCategory, bool allowUnEquip) |
| | 50 | | { |
| 0 | 51 | | IAvatarSlotComponentView avatarSlot = Instantiate(avatarSlotPrefab, avatarSlotSections[sectionName]).GetComp |
| 0 | 52 | | avatarSlot.SetCategory(slotCategory); |
| 0 | 53 | | avatarSlot.SetUnEquipAllowed(allowUnEquip); |
| 0 | 54 | | avatarSlots.Add(slotCategory, avatarSlot); |
| 0 | 55 | | avatarSlot.OnSelectAvatarSlot += (slotModel, isToggled) => OnToggleAvatarSlot?.Invoke(slotModel.category, sl |
| 0 | 56 | | avatarSlot.OnUnEquip += (wearableId) => OnUnequipFromSlot?.Invoke(wearableId); |
| 0 | 57 | | avatarSlot.OnFocusHiddenBy += (hiddenBy) => avatarSlots[hiddenBy].ShakeAnimation(); |
| 0 | 58 | | avatarSlot.OnHideUnhidePressed += (category, isOverridden) => OnHideUnhidePressed?.Invoke(category, isOverri |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | public void DisablePreviousSlot(string category) => |
| 0 | 62 | | avatarSlots[category].OnPointerClickOnDifferentSlot(); |
| | 63 | |
|
| | 64 | | public void SetSlotContent(string category, WearableItem wearableItem, string bodyShape, HashSet<string> forceRe |
| | 65 | | { |
| 0 | 66 | | avatarSlots[category].SetRarity(wearableItem.rarity); |
| 0 | 67 | | avatarSlots[category].SetNftImage(wearableItem.ComposeThumbnailUrl()); |
| 0 | 68 | | avatarSlots[category].SetWearableId(wearableItem.id); |
| 0 | 69 | | avatarSlots[category].SetHideList(wearableItem.GetHidesList(bodyShape)); |
| 0 | 70 | | RecalculateHideList(forceRender); |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | public void ResetCategorySlot(string category, HashSet<string> forceRender) |
| | 74 | | { |
| 0 | 75 | | if (avatarSlots[category].GetHideList() != null) |
| 0 | 76 | | foreach (var slot in avatarSlots[category].GetHideList()) |
| 0 | 77 | | if (avatarSlots.ContainsKey(slot)) |
| 0 | 78 | | avatarSlots[slot].SetIsHidden(false, category); |
| | 79 | |
|
| 0 | 80 | | avatarSlots[category].ResetSlot(); |
| 0 | 81 | | RecalculateHideList(forceRender); |
| 0 | 82 | | } |
| | 83 | |
|
| | 84 | | public void RecalculateHideList(HashSet<string> forceRender) |
| | 85 | | { |
| 0 | 86 | | foreach (var avatarSlotComponentView in avatarSlots) |
| | 87 | | { |
| 0 | 88 | | bool isHidden = forceRender.Contains(avatarSlotComponentView.Key); |
| 0 | 89 | | avatarSlotComponentView.Value.SetForceRender(isHidden); |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | foreach (string category in WearableItem.CATEGORIES_PRIORITY) |
| 0 | 93 | | previouslyHidden[category] = new HashSet<string>(); |
| | 94 | |
|
| 0 | 95 | | foreach (string priorityCategory in WearableItem.CATEGORIES_PRIORITY) |
| | 96 | | { |
| 0 | 97 | | if (!avatarSlots.ContainsKey(priorityCategory) || avatarSlots[priorityCategory].GetHideList() == null) |
| | 98 | | continue; |
| | 99 | |
|
| 0 | 100 | | foreach (string categoryToHide in avatarSlots[priorityCategory].GetHideList()) |
| | 101 | | { |
| | 102 | | //if it hides a slot that doesn't exist, avoid processing hides |
| 0 | 103 | | if (!avatarSlots.ContainsKey(categoryToHide)) |
| | 104 | | continue; |
| | 105 | |
|
| | 106 | | //if category has already been processed, avoid processing hides |
| 0 | 107 | | if (previouslyHidden[categoryToHide].Contains(priorityCategory)) |
| | 108 | | { |
| 0 | 109 | | avatarSlots[categoryToHide].SetIsHidden(false, priorityCategory); |
| 0 | 110 | | continue; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | previouslyHidden[priorityCategory].Add(categoryToHide); |
| | 114 | |
|
| 0 | 115 | | if (forceRender != null && forceRender.Contains(categoryToHide)) |
| | 116 | | { |
| 0 | 117 | | avatarSlots[categoryToHide].SetIsHidden(false, priorityCategory); |
| 0 | 118 | | avatarSlots[categoryToHide].SetHideIconVisible(true); |
| 0 | 119 | | continue; |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | avatarSlots[categoryToHide].SetIsHidden(true, priorityCategory); |
| | 123 | | } |
| | 124 | | } |
| 0 | 125 | | } |
| | 126 | |
|
| | 127 | | public void SetHideUnhideStatus(string slotCategory, bool isOverridden) |
| | 128 | | { |
| 0 | 129 | | if (slotCategory == null) |
| 0 | 130 | | return; |
| | 131 | |
|
| 0 | 132 | | if (avatarSlots.TryGetValue(slotCategory, out var slot)) |
| 0 | 133 | | slot.SetForceRender(isOverridden); |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public void Select(string category, bool notify) |
| | 137 | | { |
| 0 | 138 | | if (avatarSlots.TryGetValue(category, out var slot)) |
| 0 | 139 | | slot.Select(notify); |
| 0 | 140 | | } |
| | 141 | |
|
| | 142 | | public void UnSelectAll(bool notify) |
| | 143 | | { |
| 0 | 144 | | foreach (KeyValuePair<string,IAvatarSlotComponentView> slot in avatarSlots) |
| 0 | 145 | | slot.Value.UnSelect(notify); |
| 0 | 146 | | } |
| | 147 | |
|
| 0 | 148 | | public override void RefreshControl() { } |
| | 149 | | } |
| | 150 | | } |