| | 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, bool> onSlotSelected; |
| | 19 | |
|
| | 20 | | public void Start() |
| | 21 | | { |
| 0 | 22 | | ConfigureSlotButtons(); |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | public void Configure(EmoteSlotSelectorComponentModel newModel) |
| | 26 | | { |
| 0 | 27 | | if (model == newModel) |
| 0 | 28 | | return; |
| | 29 | |
|
| 0 | 30 | | model = newModel; |
| 0 | 31 | | RefreshControl(); |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | public override void RefreshControl() |
| | 35 | | { |
| 0 | 36 | | if (model == null) |
| 0 | 37 | | return; |
| | 38 | |
|
| 0 | 39 | | SelectSlot(model.selectedSlot); |
| 0 | 40 | | emotesSlots.RefreshControl(); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public override void Dispose() |
| | 44 | | { |
| 0 | 45 | | base.Dispose(); |
| | 46 | |
|
| 0 | 47 | | UnsubscribeSlotButtons(); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public void SelectSlot(int slotNumber) |
| | 51 | | { |
| 0 | 52 | | bool isInitialized = model.selectedSlot < 0; |
| | 53 | |
|
| 0 | 54 | | model.selectedSlot = slotNumber; |
| | 55 | |
|
| 0 | 56 | | if (emotesSlots == null) |
| 0 | 57 | | return; |
| | 58 | |
|
| 0 | 59 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 0 | 60 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 61 | | { |
| 0 | 62 | | if (slot.model.slotNumber == slotNumber) |
| | 63 | | { |
| 0 | 64 | | slot.SetEmoteAsSelected(true); |
| | 65 | |
|
| 0 | 66 | | onSlotSelected?.Invoke(slotNumber, slot.model.emoteId, !isInitialized); |
| | 67 | | } |
| | 68 | | else |
| | 69 | | { |
| 0 | 70 | | slot.SetEmoteAsSelected(false); |
| | 71 | | } |
| | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public void AssignEmoteIntoSlot(int slotNumber, string emoteId, string emoteName, Sprite pictureSprite, string p |
| | 76 | | { |
| 0 | 77 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 0 | 78 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 79 | | { |
| 0 | 80 | | if (slot.model.slotNumber == slotNumber) |
| | 81 | | { |
| 0 | 82 | | slot.SetEmoteName(emoteName); |
| 0 | 83 | | if (pictureSprite != null) |
| | 84 | | { |
| 0 | 85 | | slot.SetEmotePicture(pictureSprite); |
| 0 | 86 | | slot.model.pictureUri = pictureUri; |
| | 87 | | } |
| | 88 | | else |
| 0 | 89 | | slot.SetEmotePicture(pictureUri); |
| 0 | 90 | | slot.SetEmoteId(emoteId); |
| 0 | 91 | | slot.SetRarity(rarity); |
| | 92 | | } |
| 0 | 93 | | else if (slot.model.emoteId == emoteId) |
| | 94 | | { |
| 0 | 95 | | slot.SetEmoteId(string.Empty); |
| 0 | 96 | | slot.SetEmoteName(string.Empty); |
| 0 | 97 | | slot.SetRarity(string.Empty); |
| | 98 | | } |
| | 99 | | } |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | internal void ConfigureSlotButtons() |
| | 103 | | { |
| 0 | 104 | | List<EmoteSlotCardComponentView> currentSlots = GetAllSlots(); |
| 0 | 105 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 106 | | { |
| 0 | 107 | | slot.onClick.AddListener(() => SelectSlot(slot.model.slotNumber)); |
| | 108 | | } |
| 0 | 109 | | } |
| | 110 | |
|
| | 111 | | internal void UnsubscribeSlotButtons() |
| | 112 | | { |
| 0 | 113 | | IEnumerable<EmoteSlotCardComponentView> currentSlots = emotesSlots |
| | 114 | | .GetItems() |
| 0 | 115 | | .Select(x => x as EmoteSlotCardComponentView); |
| | 116 | |
|
| 0 | 117 | | foreach (EmoteSlotCardComponentView slot in currentSlots) |
| | 118 | | { |
| 0 | 119 | | slot.onClick.RemoveAllListeners(); |
| | 120 | | } |
| 0 | 121 | | } |
| | 122 | |
|
| | 123 | | internal List<EmoteSlotCardComponentView> GetAllSlots() |
| | 124 | | { |
| 0 | 125 | | if (emotesSlots == null) |
| | 126 | | { |
| 0 | 127 | | Debug.LogError("EmotesSlotSelectorComponentView: emotesSlots are accessed before serialized reference wa |
| 0 | 128 | | return new List<EmoteSlotCardComponentView>(); |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | return emotesSlots |
| | 132 | | .GetItems() |
| 0 | 133 | | .Select(x => x as EmoteSlotCardComponentView) |
| | 134 | | .ToList(); |
| | 135 | | } |
| | 136 | | } |
| | 137 | | } |