| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using System.Linq; |
| | 5 | |
|
| | 6 | | namespace DCL.EmotesCustomization |
| | 7 | | { |
| | 8 | | public class EmoteSlotSelectorComponentView : BaseComponentView, IEmoteSlotSelectorComponentView, IComponentModelCon |
| | 9 | | { |
| | 10 | | [Header("Prefab References")] |
| | 11 | | [SerializeField] internal GridContainerComponentView emotesSlots; |
| | 12 | |
|
| | 13 | | [Header("Configuration")] |
| | 14 | | [SerializeField] internal EmoteSlotSelectorComponentModel model; |
| | 15 | |
|
| 0 | 16 | | public int selectedSlot => model.selectedSlot; |
| | 17 | |
|
| | 18 | | public event Action<int, string> onSlotSelected; |
| | 19 | |
|
| | 20 | | public override void Start() |
| | 21 | | { |
| 24 | 22 | | base.Start(); |
| | 23 | |
|
| 24 | 24 | | ConfigureSlotButtons(); |
| 24 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Configure(EmoteSlotSelectorComponentModel newModel) |
| | 28 | | { |
| 1 | 29 | | if (model == newModel) |
| 0 | 30 | | return; |
| | 31 | |
|
| 1 | 32 | | model = newModel; |
| 1 | 33 | | RefreshControl(); |
| 1 | 34 | | } |
| | 35 | |
|
| | 36 | | public override void RefreshControl() |
| | 37 | | { |
| 1 | 38 | | if (model == null) |
| 0 | 39 | | return; |
| | 40 | |
|
| 1 | 41 | | SelectSlot(model.selectedSlot); |
| 1 | 42 | | emotesSlots.RefreshControl(); |
| 1 | 43 | | } |
| | 44 | |
|
| | 45 | | public override void Dispose() |
| | 46 | | { |
| 97 | 47 | | base.Dispose(); |
| | 48 | |
|
| 97 | 49 | | UnsubscribeSlotButtons(); |
| 97 | 50 | | } |
| | 51 | |
|
| | 52 | | public void SelectSlot(int slotNumber) |
| | 53 | | { |
| 11 | 54 | | model.selectedSlot = slotNumber; |
| | 55 | |
|
| 11 | 56 | | if (emotesSlots == null) |
| 0 | 57 | | return; |
| | 58 | |
|
| 11 | 59 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 242 | 60 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 61 | | { |
| 110 | 62 | | if (slot.model.slotNumber == slotNumber) |
| | 63 | | { |
| 10 | 64 | | slot.SetEmoteAsSelected(true); |
| 10 | 65 | | onSlotSelected?.Invoke(slotNumber, slot.model.emoteId); |
| 6 | 66 | | } |
| | 67 | | else |
| | 68 | | { |
| 100 | 69 | | slot.SetEmoteAsSelected(false); |
| | 70 | | } |
| | 71 | | } |
| 11 | 72 | | } |
| | 73 | |
|
| | 74 | | public void AssignEmoteIntoSlot(int slotNumber, string emoteId, string emoteName, Sprite pictureSprite, string p |
| | 75 | | { |
| 10 | 76 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 220 | 77 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 78 | | { |
| 100 | 79 | | if (slot.model.slotNumber == slotNumber) |
| | 80 | | { |
| 9 | 81 | | slot.SetEmoteName(emoteName); |
| 9 | 82 | | if (pictureSprite != null) |
| | 83 | | { |
| 6 | 84 | | slot.SetEmotePicture(pictureSprite); |
| 6 | 85 | | slot.model.pictureUri = pictureUri; |
| 6 | 86 | | } |
| | 87 | | else |
| 3 | 88 | | slot.SetEmotePicture(pictureUri); |
| 9 | 89 | | slot.SetEmoteId(emoteId); |
| 9 | 90 | | slot.SetRarity(rarity); |
| 9 | 91 | | } |
| 91 | 92 | | else if (slot.model.emoteId == emoteId) |
| | 93 | | { |
| 37 | 94 | | slot.SetEmoteId(string.Empty); |
| 37 | 95 | | slot.SetEmoteName(string.Empty); |
| 37 | 96 | | slot.SetRarity(string.Empty); |
| | 97 | | } |
| | 98 | | } |
| 10 | 99 | | } |
| | 100 | |
|
| | 101 | | internal void ConfigureSlotButtons() |
| | 102 | | { |
| 27 | 103 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 594 | 104 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 105 | | { |
| 273 | 106 | | slot.onClick.AddListener(() => SelectSlot(slot.model.slotNumber)); |
| | 107 | | } |
| 27 | 108 | | } |
| | 109 | |
|
| | 110 | | internal void UnsubscribeSlotButtons() |
| | 111 | | { |
| 100 | 112 | | IEnumerable<EmoteSlotCardComponentView> currentSlots = emotesSlots |
| | 113 | | .GetItems() |
| 1000 | 114 | | .Select(x => x as EmoteSlotCardComponentView); |
| | 115 | |
|
| 2200 | 116 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 117 | | { |
| 1000 | 118 | | slot.onClick.RemoveAllListeners(); |
| | 119 | | } |
| 100 | 120 | | } |
| | 121 | |
|
| | 122 | | internal List<EmoteSlotCardComponentView> GetAllSlots() |
| | 123 | | { |
| 1461 | 124 | | return emotesSlots |
| | 125 | | .GetItems() |
| 14610 | 126 | | .Select(x => x as EmoteSlotCardComponentView) |
| | 127 | | .ToList(); |
| | 128 | | } |
| | 129 | | } |
| | 130 | | } |