| | 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 | | { |
| 0 | 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 | | [SerializeField] internal GameObject soundIcon; |
| | 27 | | [SerializeField] internal GameObject secondaryItem; |
| | 28 | | [SerializeField] internal GameObject amountLabel; |
| | 29 | | [SerializeField] internal TMP_Text amountText; |
| | 30 | |
|
| | 31 | | [Header("Configuration")] |
| | 32 | | [SerializeField] internal Sprite defaultEmotePicture; |
| | 33 | | [SerializeField] internal Sprite nonEmoteAssignedPicture; |
| | 34 | | [SerializeField] internal List<EmoteRarity> rarityColors; |
| | 35 | | [SerializeField] internal EmoteCardComponentModel model; |
| | 36 | |
|
| 0 | 37 | | public Button.ButtonClickedEvent onMainClick => mainButton?.onClick; |
| 0 | 38 | | public Button.ButtonClickedEvent onInfoClick => infoButton?.onClick; |
| | 39 | |
|
| | 40 | | public event Action<string> onEmoteSelected; |
| | 41 | |
|
| | 42 | | public override void Awake() |
| | 43 | | { |
| 0 | 44 | | base.Awake(); |
| | 45 | |
|
| 0 | 46 | | if (emoteImage != null) |
| 0 | 47 | | emoteImage.OnLoaded += OnEmoteImageLoaded; |
| | 48 | |
|
| 0 | 49 | | if (cardSelectionFrame != null) |
| 0 | 50 | | cardSelectionFrame.SetActive(false); |
| | 51 | |
|
| 0 | 52 | | SetSoundIcon(false); |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Configure(EmoteCardComponentModel newModel) |
| | 56 | | { |
| 0 | 57 | | if (model == newModel) |
| 0 | 58 | | return; |
| | 59 | |
|
| 0 | 60 | | model = newModel; |
| 0 | 61 | | RefreshControl(); |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | public override void RefreshControl() |
| | 65 | | { |
| 0 | 66 | | if (model == null) |
| 0 | 67 | | return; |
| | 68 | |
|
| 0 | 69 | | amountText.text = model.amount; |
| 0 | 70 | | secondaryItem.SetActive(!string.IsNullOrEmpty(amountText.text)); |
| 0 | 71 | | amountLabel.SetActive(!string.IsNullOrEmpty(amountText.text)); |
| 0 | 72 | | SetEmoteId(model.id); |
| 0 | 73 | | SetEmoteName(model.name); |
| 0 | 74 | | SetEmoteDescription(model.description); |
| | 75 | |
|
| 0 | 76 | | if (model.pictureSprite != null) |
| 0 | 77 | | SetEmotePicture(model.pictureSprite); |
| 0 | 78 | | else if (!string.IsNullOrEmpty(model.pictureUri)) |
| 0 | 79 | | SetEmotePicture(model.pictureUri); |
| | 80 | | else |
| 0 | 81 | | OnEmoteImageLoaded(null); |
| | 82 | |
|
| 0 | 83 | | SetEmoteAsAssignedInSelectedSlot(model.isAssignedInSelectedSlot); |
| 0 | 84 | | AssignSlot(model.assignedSlot); |
| 0 | 85 | | SetEmoteAsSelected(model.isSelected); |
| 0 | 86 | | SetRarity(model.rarity); |
| 0 | 87 | | SetIsInL2(model.isInL2); |
| 0 | 88 | | SetIsCollectible(model.isCollectible); |
| 0 | 89 | | SetAsLoading(model.isLoading); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public override void OnEnable() |
| | 93 | | { |
| 0 | 94 | | base.OnEnable(); |
| | 95 | |
|
| 0 | 96 | | RefreshControl(); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | public override void OnFocus() |
| | 100 | | { |
| 0 | 101 | | base.OnFocus(); |
| | 102 | |
|
| 0 | 103 | | if (emoteNameContainer != null) |
| 0 | 104 | | emoteNameContainer.SetActive(true); |
| | 105 | |
|
| 0 | 106 | | SetEmoteAsSelected(true); |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | public override void OnLoseFocus() |
| | 110 | | { |
| 0 | 111 | | base.OnLoseFocus(); |
| | 112 | |
|
| 0 | 113 | | if (emoteNameContainer != null) |
| 0 | 114 | | emoteNameContainer.SetActive(false); |
| | 115 | |
|
| 0 | 116 | | SetEmoteAsSelected(false); |
| 0 | 117 | | } |
| | 118 | |
|
| | 119 | | public override void Dispose() |
| | 120 | | { |
| 0 | 121 | | base.Dispose(); |
| | 122 | |
|
| 0 | 123 | | if (emoteImage != null) |
| | 124 | | { |
| 0 | 125 | | emoteImage.OnLoaded -= OnEmoteImageLoaded; |
| 0 | 126 | | emoteImage.Dispose(); |
| | 127 | | } |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | public void SetEmoteId(string id) |
| | 131 | | { |
| 0 | 132 | | model.id = id; |
| | 133 | |
|
| 0 | 134 | | if (string.IsNullOrEmpty(id)) |
| | 135 | | { |
| 0 | 136 | | if (nonEmoteAssignedPicture != null) |
| 0 | 137 | | SetEmotePicture(nonEmoteAssignedPicture); |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetEmoteName(string name) |
| | 142 | | { |
| 0 | 143 | | model.name = name; |
| | 144 | |
|
| 0 | 145 | | if (emoteNameText == null) |
| 0 | 146 | | return; |
| | 147 | |
|
| 0 | 148 | | emoteNameText.text = name; |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | public void SetEmoteDescription(string description) |
| | 152 | | { |
| 0 | 153 | | model.description = description; |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | public void SetEmotePicture(Sprite sprite) |
| | 157 | | { |
| 0 | 158 | | if (sprite == null && defaultEmotePicture != null) |
| 0 | 159 | | sprite = defaultEmotePicture; |
| | 160 | |
|
| 0 | 161 | | model.pictureSprite = sprite; |
| | 162 | |
|
| 0 | 163 | | if (emoteImage == null) |
| 0 | 164 | | return; |
| | 165 | |
|
| 0 | 166 | | emoteImage.SetImage(sprite); |
| 0 | 167 | | } |
| | 168 | |
|
| | 169 | | public void SetEmotePicture(string uri) |
| | 170 | | { |
| 0 | 171 | | if (string.IsNullOrEmpty(uri) && defaultEmotePicture != null) |
| | 172 | | { |
| 0 | 173 | | SetEmotePicture(defaultEmotePicture); |
| 0 | 174 | | return; |
| | 175 | | } |
| | 176 | |
|
| 0 | 177 | | model.pictureUri = uri; |
| | 178 | |
|
| 0 | 179 | | if (!Application.isPlaying) |
| 0 | 180 | | return; |
| | 181 | |
|
| 0 | 182 | | if (emoteImage == null) |
| 0 | 183 | | return; |
| | 184 | |
|
| 0 | 185 | | emoteImage.SetImage(uri); |
| 0 | 186 | | } |
| | 187 | |
|
| | 188 | | public void SetEmoteAsAssignedInSelectedSlot(bool isAssigned) |
| | 189 | | { |
| 0 | 190 | | model.isAssignedInSelectedSlot = isAssigned; |
| | 191 | |
|
| 0 | 192 | | RefreshVisualCardStatus(); |
| 0 | 193 | | } |
| | 194 | |
|
| | 195 | | public void AssignSlot(int slotNumber) |
| | 196 | | { |
| 0 | 197 | | model.assignedSlot = slotNumber; |
| | 198 | |
|
| 0 | 199 | | if (assignedSlotNumberText == null) |
| 0 | 200 | | return; |
| | 201 | |
|
| 0 | 202 | | assignedSlotNumberText.text = slotNumber.ToString(); |
| | 203 | |
|
| 0 | 204 | | RefreshVisualCardStatus(); |
| 0 | 205 | | } |
| | 206 | |
|
| 0 | 207 | | public void UnassignSlot() { AssignSlot(-1); } |
| | 208 | |
|
| | 209 | | public void SetEmoteAsSelected(bool isSelected) |
| | 210 | | { |
| 0 | 211 | | model.isSelected = isSelected; |
| | 212 | |
|
| 0 | 213 | | if (selectionAnimator != null) |
| 0 | 214 | | selectionAnimator.SetBool(ON_SELECTED_CARD_COMPONENT_BOOL, isSelected); |
| | 215 | |
|
| 0 | 216 | | RefreshVisualCardStatus(); |
| | 217 | |
|
| 0 | 218 | | if (isSelected) |
| 0 | 219 | | onEmoteSelected?.Invoke(model.id); |
| | 220 | | else |
| 0 | 221 | | onEmoteSelected?.Invoke(null); |
| 0 | 222 | | } |
| | 223 | |
|
| | 224 | | public void SetRarity(string rarity) |
| | 225 | | { |
| 0 | 226 | | model.rarity = rarity; |
| | 227 | |
|
| 0 | 228 | | if (rarityMark == null) |
| 0 | 229 | | return; |
| | 230 | |
|
| 0 | 231 | | EmoteRarity emoteRarity = rarityColors.FirstOrDefault(x => x.rarity == rarity); |
| 0 | 232 | | rarityMark.gameObject.SetActive(emoteRarity != null); |
| 0 | 233 | | rarityMark.color = emoteRarity != null ? emoteRarity.markColor : Color.white; |
| 0 | 234 | | } |
| | 235 | |
|
| | 236 | | public void SetIsInL2(bool isInL2) |
| | 237 | | { |
| 0 | 238 | | model.isInL2 = isInL2; |
| 0 | 239 | | } |
| | 240 | |
|
| | 241 | | public void SetIsCollectible(bool isCollectible) |
| | 242 | | { |
| 0 | 243 | | model.isCollectible = isCollectible; |
| | 244 | |
|
| 0 | 245 | | RefreshVisualCardStatus(); |
| 0 | 246 | | } |
| | 247 | |
|
| | 248 | | public void SetAsLoading(bool isLoading) |
| | 249 | | { |
| 0 | 250 | | model.isLoading = isLoading; |
| | 251 | |
|
| 0 | 252 | | if (loadingSpinnerGO != null) |
| 0 | 253 | | loadingSpinnerGO.SetActive(isLoading); |
| | 254 | |
|
| 0 | 255 | | if (mainButton != null) |
| 0 | 256 | | mainButton.gameObject.SetActive(!isLoading); |
| 0 | 257 | | } |
| | 258 | |
|
| | 259 | | internal void RefreshVisualCardStatus() |
| | 260 | | { |
| 0 | 261 | | RefreshAssignedSlotTextVisibility(); |
| 0 | 262 | | RefreshSelectionFrameVisibility(); |
| 0 | 263 | | RefreshCardButtonsVisibility(); |
| 0 | 264 | | } |
| | 265 | |
|
| | 266 | | internal void RefreshAssignedSlotTextVisibility() |
| | 267 | | { |
| 0 | 268 | | if (assignedSlotNumberText == null) |
| 0 | 269 | | return; |
| | 270 | |
|
| 0 | 271 | | assignedSlotNumberText.gameObject.SetActive(model.assignedSlot >= 0); |
| 0 | 272 | | } |
| | 273 | |
|
| | 274 | | internal void RefreshSelectionFrameVisibility() |
| | 275 | | { |
| 0 | 276 | | if (cardSelectionFrame == null) |
| 0 | 277 | | return; |
| | 278 | |
|
| 0 | 279 | | cardSelectionFrame.SetActive(model.isSelected || model.isAssignedInSelectedSlot); |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | internal void RefreshCardButtonsVisibility() |
| | 283 | | { |
| 0 | 284 | | if (infoButton != null) |
| 0 | 285 | | infoButton.gameObject.SetActive(model.isCollectible); |
| 0 | 286 | | } |
| | 287 | |
|
| | 288 | | internal void OnEmoteImageLoaded(Sprite sprite) |
| | 289 | | { |
| 0 | 290 | | if (sprite != null) |
| 0 | 291 | | SetEmotePicture(sprite); |
| | 292 | | else |
| 0 | 293 | | SetEmotePicture(sprite: null); |
| 0 | 294 | | } |
| | 295 | |
|
| | 296 | | public void SetSoundIcon(bool active) |
| | 297 | | { |
| 0 | 298 | | soundIcon.gameObject.SetActive(active); |
| 0 | 299 | | } |
| | 300 | | } |
| | 301 | | } |