| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.EmotesCustomization |
| | 9 | | { |
| | 10 | | public class EmoteCardComponentView : BaseComponentView, IEmoteCardComponentView, IComponentModelConfig<EmoteCardCom |
| | 11 | | { |
| 1 | 12 | | internal static readonly int ON_SELECTED_CARD_COMPONENT_BOOL = Animator.StringToHash("OnSelected"); |
| | 13 | |
|
| | 14 | | [Header("Prefab References")] |
| | 15 | | [SerializeField] internal ImageComponentView emoteImage; |
| | 16 | | [SerializeField] internal GameObject emoteNameContainer; |
| | 17 | | [SerializeField] internal TMP_Text emoteNameText; |
| | 18 | | [SerializeField] internal TMP_Text assignedSlotNumberText; |
| | 19 | | [SerializeField] internal ButtonComponentView mainButton; |
| | 20 | | [SerializeField] internal ButtonComponentView infoButton; |
| | 21 | | [SerializeField] internal GameObject cardSelectionFrame; |
| | 22 | | [SerializeField] internal Animator selectionAnimator; |
| | 23 | | [SerializeField] internal Image rarityMark; |
| | 24 | | [SerializeField] internal Transform emoteInfoAnchor; |
| | 25 | | [SerializeField] internal GameObject loadingSpinnerGO; |
| | 26 | |
|
| | 27 | | [Header("Configuration")] |
| | 28 | | [SerializeField] internal Sprite defaultEmotePicture; |
| | 29 | | [SerializeField] internal Sprite nonEmoteAssignedPicture; |
| | 30 | | [SerializeField] internal List<EmoteRarity> rarityColors; |
| | 31 | | [SerializeField] internal EmoteCardComponentModel model; |
| | 32 | |
|
| 0 | 33 | | public Button.ButtonClickedEvent onMainClick => mainButton?.onClick; |
| 0 | 34 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| | 35 | |
|
| | 36 | | public event Action<string> onEmoteSelected; |
| | 37 | |
|
| | 38 | | public override void Awake() |
| | 39 | | { |
| 997 | 40 | | base.Awake(); |
| | 41 | |
|
| 997 | 42 | | if (emoteImage != null) |
| 996 | 43 | | emoteImage.OnLoaded += OnEmoteImageLoaded; |
| | 44 | |
|
| 997 | 45 | | if (cardSelectionFrame != null) |
| 996 | 46 | | cardSelectionFrame.SetActive(false); |
| 997 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Configure(EmoteCardComponentModel newModel) |
| | 50 | | { |
| 932 | 51 | | if (model == newModel) |
| 0 | 52 | | return; |
| | 53 | |
|
| 932 | 54 | | model = newModel; |
| 932 | 55 | | RefreshControl(); |
| 932 | 56 | | } |
| | 57 | |
|
| | 58 | | public override void RefreshControl() |
| | 59 | | { |
| 3772 | 60 | | if (model == null) |
| 1 | 61 | | return; |
| | 62 | |
|
| 3771 | 63 | | SetEmoteId(model.id); |
| 3771 | 64 | | SetEmoteName(model.name); |
| 3771 | 65 | | SetEmoteDescription(model.description); |
| | 66 | |
|
| 3771 | 67 | | if (model.pictureSprite != null) |
| 3764 | 68 | | SetEmotePicture(model.pictureSprite); |
| 7 | 69 | | else if (!string.IsNullOrEmpty(model.pictureUri)) |
| 7 | 70 | | SetEmotePicture(model.pictureUri); |
| | 71 | | else |
| 0 | 72 | | OnEmoteImageLoaded(null); |
| | 73 | |
|
| 3771 | 74 | | SetEmoteAsAssignedInSelectedSlot(model.isAssignedInSelectedSlot); |
| 3771 | 75 | | AssignSlot(model.assignedSlot); |
| 3771 | 76 | | SetEmoteAsSelected(model.isSelected); |
| 3771 | 77 | | SetRarity(model.rarity); |
| 3771 | 78 | | SetIsInL2(model.isInL2); |
| 3771 | 79 | | SetIsCollectible(model.isCollectible); |
| 3771 | 80 | | SetAsLoading(model.isLoading); |
| 3771 | 81 | | } |
| | 82 | |
|
| | 83 | | public override void OnEnable() |
| | 84 | | { |
| 2840 | 85 | | base.OnEnable(); |
| | 86 | |
|
| 2840 | 87 | | RefreshControl(); |
| 2840 | 88 | | } |
| | 89 | |
|
| | 90 | | public override void OnFocus() |
| | 91 | | { |
| 1 | 92 | | base.OnFocus(); |
| | 93 | |
|
| 1 | 94 | | if (emoteNameContainer != null) |
| 1 | 95 | | emoteNameContainer.SetActive(true); |
| | 96 | |
|
| 1 | 97 | | SetEmoteAsSelected(true); |
| 1 | 98 | | } |
| | 99 | |
|
| | 100 | | public override void OnLoseFocus() |
| | 101 | | { |
| 2841 | 102 | | base.OnLoseFocus(); |
| | 103 | |
|
| 2841 | 104 | | if (emoteNameContainer != null) |
| 2840 | 105 | | emoteNameContainer.SetActive(false); |
| | 106 | |
|
| 2841 | 107 | | SetEmoteAsSelected(false); |
| 2841 | 108 | | } |
| | 109 | |
|
| | 110 | | public override void Dispose() |
| | 111 | | { |
| 1030 | 112 | | base.Dispose(); |
| | 113 | |
|
| 1030 | 114 | | if (emoteImage != null) |
| | 115 | | { |
| 1029 | 116 | | emoteImage.OnLoaded -= OnEmoteImageLoaded; |
| 1029 | 117 | | emoteImage.Dispose(); |
| | 118 | | } |
| 1030 | 119 | | } |
| | 120 | |
|
| | 121 | | public void SetEmoteId(string id) |
| | 122 | | { |
| 3772 | 123 | | model.id = id; |
| | 124 | |
|
| 3772 | 125 | | if (string.IsNullOrEmpty(id)) |
| | 126 | | { |
| 0 | 127 | | if (nonEmoteAssignedPicture != null) |
| 0 | 128 | | SetEmotePicture(nonEmoteAssignedPicture); |
| | 129 | | } |
| 3772 | 130 | | } |
| | 131 | |
|
| | 132 | | public void SetEmoteName(string name) |
| | 133 | | { |
| 3772 | 134 | | model.name = name; |
| | 135 | |
|
| 3772 | 136 | | if (emoteNameText == null) |
| 0 | 137 | | return; |
| | 138 | |
|
| 3772 | 139 | | emoteNameText.text = name; |
| 3772 | 140 | | } |
| | 141 | |
|
| | 142 | | public void SetEmoteDescription(string description) |
| | 143 | | { |
| 0 | 144 | | model.description = description; |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public void SetEmotePicture(Sprite sprite) |
| | 148 | | { |
| 3769 | 149 | | if (sprite == null && defaultEmotePicture != null) |
| 2 | 150 | | sprite = defaultEmotePicture; |
| | 151 | |
|
| 3769 | 152 | | model.pictureSprite = sprite; |
| | 153 | |
|
| 3769 | 154 | | if (emoteImage == null) |
| 0 | 155 | | return; |
| | 156 | |
|
| 3769 | 157 | | emoteImage.SetImage(sprite); |
| 3769 | 158 | | } |
| | 159 | |
|
| | 160 | | public void SetEmotePicture(string uri) |
| | 161 | | { |
| 9 | 162 | | if (string.IsNullOrEmpty(uri) && defaultEmotePicture != null) |
| | 163 | | { |
| 1 | 164 | | SetEmotePicture(defaultEmotePicture); |
| 1 | 165 | | return; |
| | 166 | | } |
| | 167 | |
|
| 8 | 168 | | model.pictureUri = uri; |
| | 169 | |
|
| 8 | 170 | | if (!Application.isPlaying) |
| 0 | 171 | | return; |
| | 172 | |
|
| 8 | 173 | | if (emoteImage == null) |
| 0 | 174 | | return; |
| | 175 | |
|
| 8 | 176 | | emoteImage.SetImage(uri); |
| 8 | 177 | | } |
| | 178 | |
|
| | 179 | | public void SetEmoteAsAssignedInSelectedSlot(bool isAssigned) |
| | 180 | | { |
| 3787 | 181 | | model.isAssignedInSelectedSlot = isAssigned; |
| | 182 | |
|
| 3787 | 183 | | RefreshVisualCardStatus(); |
| 3787 | 184 | | } |
| | 185 | |
|
| | 186 | | public void AssignSlot(int slotNumber) |
| | 187 | | { |
| 3783 | 188 | | model.assignedSlot = slotNumber; |
| | 189 | |
|
| 3783 | 190 | | if (assignedSlotNumberText == null) |
| 0 | 191 | | return; |
| | 192 | |
|
| 3783 | 193 | | assignedSlotNumberText.text = slotNumber.ToString(); |
| | 194 | |
|
| 3783 | 195 | | RefreshVisualCardStatus(); |
| 3783 | 196 | | } |
| | 197 | |
|
| 2 | 198 | | public void UnassignSlot() { AssignSlot(-1); } |
| | 199 | |
|
| | 200 | | public void SetEmoteAsSelected(bool isSelected) |
| | 201 | | { |
| 6615 | 202 | | model.isSelected = isSelected; |
| | 203 | |
|
| 6615 | 204 | | if (selectionAnimator != null) |
| 6614 | 205 | | selectionAnimator.SetBool(ON_SELECTED_CARD_COMPONENT_BOOL, isSelected); |
| | 206 | |
|
| 6615 | 207 | | RefreshVisualCardStatus(); |
| | 208 | |
|
| 6615 | 209 | | if (isSelected) |
| 84 | 210 | | onEmoteSelected?.Invoke(model.id); |
| | 211 | | else |
| 6531 | 212 | | onEmoteSelected?.Invoke(null); |
| 2792 | 213 | | } |
| | 214 | |
|
| | 215 | | public void SetRarity(string rarity) |
| | 216 | | { |
| 3773 | 217 | | model.rarity = rarity; |
| | 218 | |
|
| 3773 | 219 | | if (rarityMark == null) |
| 0 | 220 | | return; |
| | 221 | |
|
| 18534 | 222 | | EmoteRarity emoteRarity = rarityColors.FirstOrDefault(x => x.rarity == rarity); |
| 3773 | 223 | | rarityMark.gameObject.SetActive(emoteRarity != null); |
| 3773 | 224 | | rarityMark.color = emoteRarity != null ? emoteRarity.markColor : Color.white; |
| 3773 | 225 | | } |
| | 226 | |
|
| | 227 | | public void SetIsInL2(bool isInL2) |
| | 228 | | { |
| 0 | 229 | | model.isInL2 = isInL2; |
| 0 | 230 | | } |
| | 231 | |
|
| | 232 | | public void SetIsCollectible(bool isCollectible) |
| | 233 | | { |
| 3773 | 234 | | model.isCollectible = isCollectible; |
| | 235 | |
|
| 3773 | 236 | | RefreshVisualCardStatus(); |
| 3773 | 237 | | } |
| | 238 | |
|
| | 239 | | public void SetAsLoading(bool isLoading) |
| | 240 | | { |
| 4686 | 241 | | model.isLoading = isLoading; |
| | 242 | |
|
| 4686 | 243 | | if (loadingSpinnerGO != null) |
| 4685 | 244 | | loadingSpinnerGO.SetActive(isLoading); |
| | 245 | |
|
| 4686 | 246 | | if (mainButton != null) |
| 4685 | 247 | | mainButton.gameObject.SetActive(!isLoading); |
| 4686 | 248 | | } |
| | 249 | |
|
| | 250 | | internal void RefreshVisualCardStatus() |
| | 251 | | { |
| 17958 | 252 | | RefreshAssignedSlotTextVisibility(); |
| 17958 | 253 | | RefreshSelectionFrameVisibility(); |
| 17958 | 254 | | RefreshCardButtonsVisibility(); |
| 17958 | 255 | | } |
| | 256 | |
|
| | 257 | | internal void RefreshAssignedSlotTextVisibility() |
| | 258 | | { |
| 17960 | 259 | | if (assignedSlotNumberText == null) |
| 1 | 260 | | return; |
| | 261 | |
|
| 17959 | 262 | | assignedSlotNumberText.gameObject.SetActive(model.assignedSlot >= 0); |
| 17959 | 263 | | } |
| | 264 | |
|
| | 265 | | internal void RefreshSelectionFrameVisibility() |
| | 266 | | { |
| 17960 | 267 | | if (cardSelectionFrame == null) |
| 1 | 268 | | return; |
| | 269 | |
|
| 17959 | 270 | | cardSelectionFrame.SetActive(model.isSelected || model.isAssignedInSelectedSlot); |
| 17959 | 271 | | } |
| | 272 | |
|
| | 273 | | internal void RefreshCardButtonsVisibility() |
| | 274 | | { |
| 17960 | 275 | | if (infoButton != null) |
| 17959 | 276 | | infoButton.gameObject.SetActive(model.isCollectible); |
| 17960 | 277 | | } |
| | 278 | |
|
| | 279 | | internal void OnEmoteImageLoaded(Sprite sprite) |
| | 280 | | { |
| 2 | 281 | | if (sprite != null) |
| 1 | 282 | | SetEmotePicture(sprite); |
| | 283 | | else |
| 1 | 284 | | SetEmotePicture(sprite: null); |
| 1 | 285 | | } |
| | 286 | | } |
| | 287 | | } |