| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using TMPro; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.UI; |
| | 6 | |
|
| | 7 | | namespace DCL.EmotesCustomization |
| | 8 | | { |
| | 9 | | public class EmoteSlotCardComponentView : BaseComponentView, IEmoteSlotCardComponentView, IComponentModelConfig<Emot |
| | 10 | | { |
| | 11 | | internal const int SLOT_VIEWER_ROTATION_ANGLE = 36; |
| | 12 | | internal const string EMPTY_SLOT_TEXT = "None"; |
| | 13 | |
|
| | 14 | | [Header("Prefab References")] |
| | 15 | | [SerializeField] internal ImageComponentView emoteImage; |
| | 16 | | [SerializeField] internal Image nonEmoteImage; |
| | 17 | | [SerializeField] internal TMP_Text emoteNameText; |
| | 18 | | [SerializeField] internal TMP_Text slotNumberText; |
| | 19 | | [SerializeField] internal Image slotViewerImage; |
| | 20 | | [SerializeField] internal ButtonComponentView mainButton; |
| | 21 | | [SerializeField] internal Image defaultBackgroundImage; |
| | 22 | | [SerializeField] internal Image selectedBackgroundImage; |
| | 23 | | [SerializeField] internal GameObject separatorGO; |
| | 24 | | [SerializeField] internal Image rarityMark; |
| | 25 | |
|
| | 26 | | [Header("Configuration")] |
| | 27 | | [SerializeField] internal Sprite defaultEmotePicture; |
| | 28 | | [SerializeField] internal Color defaultBackgroundColor; |
| | 29 | | [SerializeField] internal Color selectedBackgroundColor; |
| | 30 | | [SerializeField] internal Color defaultSlotNumberColor; |
| | 31 | | [SerializeField] internal Color selectedSlotNumberColor; |
| | 32 | | [SerializeField] internal Color defaultEmoteNameColor; |
| | 33 | | [SerializeField] internal Color selectedEmoteNameColor; |
| | 34 | | [SerializeField] internal List<EmoteRarity> rarityColors; |
| | 35 | | [SerializeField] internal EmoteSlotCardComponentModel model; |
| | 36 | |
|
| 0 | 37 | | public Button.ButtonClickedEvent onClick => mainButton?.onClick; |
| | 38 | |
|
| | 39 | | public override void Awake() |
| | 40 | | { |
| 899 | 41 | | base.Awake(); |
| | 42 | |
|
| 899 | 43 | | if (emoteImage != null) |
| 899 | 44 | | emoteImage.OnLoaded += OnEmoteImageLoaded; |
| 899 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Configure(EmoteSlotCardComponentModel newModel) |
| | 48 | | { |
| 1 | 49 | | if (model == newModel) |
| 0 | 50 | | return; |
| | 51 | |
|
| 1 | 52 | | model = newModel; |
| 1 | 53 | | RefreshControl(); |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | public override void RefreshControl() |
| | 57 | | { |
| 1 | 58 | | if (model == null) |
| 0 | 59 | | return; |
| | 60 | |
|
| 1 | 61 | | if (model.pictureSprite != null) |
| 1 | 62 | | SetEmotePicture(model.pictureSprite); |
| 0 | 63 | | else if (!string.IsNullOrEmpty(model.pictureUri)) |
| 0 | 64 | | SetEmotePicture(model.pictureUri); |
| | 65 | | else |
| 0 | 66 | | OnEmoteImageLoaded(null); |
| | 67 | |
|
| 1 | 68 | | SetEmoteId(model.emoteId); |
| 1 | 69 | | SetEmoteName(model.emoteName); |
| 1 | 70 | | SetEmoteAsSelected(model.isSelected); |
| 1 | 71 | | SetSlotNumber(model.slotNumber); |
| 1 | 72 | | SetSeparatorActive(model.hasSeparator); |
| 1 | 73 | | SetRarity(model.rarity); |
| 1 | 74 | | } |
| | 75 | |
|
| | 76 | | public override void OnFocus() |
| | 77 | | { |
| 1 | 78 | | base.OnFocus(); |
| | 79 | |
|
| 1 | 80 | | if (!model.isSelected) |
| | 81 | | { |
| 1 | 82 | | SetSelectedVisualsForHovering(true); |
| | 83 | | } |
| 1 | 84 | | } |
| | 85 | |
|
| | 86 | | public override void OnLoseFocus() |
| | 87 | | { |
| 1760 | 88 | | base.OnLoseFocus(); |
| | 89 | |
|
| 1760 | 90 | | if (!model.isSelected) |
| | 91 | | { |
| 1739 | 92 | | SetSelectedVisualsForHovering(false); |
| | 93 | | } |
| 1760 | 94 | | } |
| | 95 | |
|
| | 96 | | public override void Dispose() |
| | 97 | | { |
| 948 | 98 | | base.Dispose(); |
| | 99 | |
|
| 948 | 100 | | if (emoteImage != null) |
| | 101 | | { |
| 948 | 102 | | emoteImage.OnLoaded -= OnEmoteImageLoaded; |
| 948 | 103 | | emoteImage.Dispose(); |
| | 104 | | } |
| 948 | 105 | | } |
| | 106 | |
|
| | 107 | | public void SetEmoteId(string id) |
| | 108 | | { |
| 49 | 109 | | model.emoteId = id; |
| | 110 | |
|
| 49 | 111 | | if (nonEmoteImage != null) |
| 49 | 112 | | nonEmoteImage.gameObject.SetActive(string.IsNullOrEmpty(id)); |
| | 113 | |
|
| 49 | 114 | | if (emoteImage != null) |
| 49 | 115 | | emoteImage.gameObject.SetActive(!string.IsNullOrEmpty(id)); |
| 49 | 116 | | } |
| | 117 | |
|
| | 118 | | public void SetEmoteName(string name) |
| | 119 | | { |
| 49 | 120 | | model.emoteName = name; |
| | 121 | |
|
| 49 | 122 | | if (emoteNameText == null) |
| 0 | 123 | | return; |
| | 124 | |
|
| 49 | 125 | | emoteNameText.text = string.IsNullOrEmpty(name) ? EMPTY_SLOT_TEXT : name; |
| 49 | 126 | | } |
| | 127 | |
|
| | 128 | | public void SetEmotePicture(Sprite sprite) |
| | 129 | | { |
| 15 | 130 | | if (sprite == null && defaultEmotePicture != null) |
| 2 | 131 | | sprite = defaultEmotePicture; |
| | 132 | |
|
| 15 | 133 | | model.pictureSprite = sprite; |
| | 134 | |
|
| 15 | 135 | | if (emoteImage == null) |
| 0 | 136 | | return; |
| | 137 | |
|
| 15 | 138 | | emoteImage.SetImage(sprite); |
| 15 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetEmotePicture(string uri) |
| | 142 | | { |
| 5 | 143 | | if (string.IsNullOrEmpty(uri) && defaultEmotePicture != null) |
| | 144 | | { |
| 4 | 145 | | SetEmotePicture(defaultEmotePicture); |
| 4 | 146 | | return; |
| | 147 | | } |
| | 148 | |
|
| 1 | 149 | | model.pictureUri = uri; |
| | 150 | |
|
| 1 | 151 | | if (!Application.isPlaying) |
| 0 | 152 | | return; |
| | 153 | |
|
| 1 | 154 | | if (emoteImage == null) |
| 0 | 155 | | return; |
| | 156 | |
|
| 1 | 157 | | emoteImage.SetImage(uri); |
| 1 | 158 | | } |
| | 159 | |
|
| | 160 | | public void SetEmoteAsSelected(bool isSelected) |
| | 161 | | { |
| 113 | 162 | | model.isSelected = isSelected; |
| | 163 | |
|
| 113 | 164 | | SetSelectedVisualsForClicking(isSelected); |
| 113 | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetSlotNumber(int slotNumber) |
| | 168 | | { |
| 2 | 169 | | model.slotNumber = Mathf.Clamp(slotNumber, 0, 9); |
| | 170 | |
|
| 2 | 171 | | if (slotNumberText != null) |
| 2 | 172 | | slotNumberText.text = model.slotNumber.ToString(); |
| | 173 | |
|
| 2 | 174 | | if (slotViewerImage != null) |
| 2 | 175 | | slotViewerImage.transform.rotation = Quaternion.Euler(0, 0, -model.slotNumber * SLOT_VIEWER_ROTATION_ANG |
| 2 | 176 | | } |
| | 177 | |
|
| | 178 | | public void SetSeparatorActive(bool isActive) |
| | 179 | | { |
| 3 | 180 | | model.hasSeparator = isActive; |
| | 181 | |
|
| 3 | 182 | | if (separatorGO == null) |
| 0 | 183 | | return; |
| | 184 | |
|
| 3 | 185 | | separatorGO.SetActive(isActive); |
| 3 | 186 | | } |
| | 187 | |
|
| | 188 | | public void SetRarity(string rarity) |
| | 189 | | { |
| 49 | 190 | | model.rarity = rarity; |
| | 191 | |
|
| 49 | 192 | | if (rarityMark == null) |
| 0 | 193 | | return; |
| | 194 | |
|
| 362 | 195 | | EmoteRarity emoteRarity = rarityColors.FirstOrDefault(x => x.rarity == rarity); |
| 49 | 196 | | rarityMark.gameObject.SetActive(emoteRarity != null); |
| 49 | 197 | | rarityMark.color = emoteRarity != null ? emoteRarity.markColor : Color.white; |
| 49 | 198 | | } |
| | 199 | |
|
| | 200 | | internal void OnEmoteImageLoaded(Sprite sprite) |
| | 201 | | { |
| 2 | 202 | | if (sprite != null) |
| 1 | 203 | | SetEmotePicture(sprite); |
| | 204 | | else |
| 1 | 205 | | SetEmotePicture(sprite: null); |
| 1 | 206 | | } |
| | 207 | |
|
| | 208 | | internal void SetSelectedVisualsForClicking(bool isSelected) |
| | 209 | | { |
| 115 | 210 | | if (defaultBackgroundImage != null) |
| | 211 | | { |
| 115 | 212 | | defaultBackgroundImage.gameObject.SetActive(!isSelected); |
| 115 | 213 | | defaultBackgroundImage.color = defaultBackgroundColor; |
| | 214 | | } |
| | 215 | |
|
| 115 | 216 | | if (selectedBackgroundImage != null) |
| | 217 | | { |
| 115 | 218 | | selectedBackgroundImage.gameObject.SetActive(isSelected); |
| 115 | 219 | | selectedBackgroundImage.color = selectedBackgroundColor; |
| | 220 | | } |
| | 221 | |
|
| 115 | 222 | | if (slotNumberText != null) |
| 115 | 223 | | slotNumberText.color = isSelected ? selectedSlotNumberColor : defaultSlotNumberColor; |
| | 224 | |
|
| 115 | 225 | | if (emoteNameText != null) |
| 115 | 226 | | emoteNameText.color = isSelected ? selectedEmoteNameColor : defaultEmoteNameColor; |
| | 227 | |
|
| 115 | 228 | | if (slotViewerImage != null) |
| 115 | 229 | | slotViewerImage.gameObject.SetActive(isSelected); |
| 115 | 230 | | } |
| | 231 | |
|
| | 232 | | internal void SetSelectedVisualsForHovering(bool isSelected) |
| | 233 | | { |
| 1742 | 234 | | if (defaultBackgroundImage != null) |
| 1742 | 235 | | defaultBackgroundImage.color = isSelected ? selectedBackgroundColor : defaultBackgroundColor; |
| | 236 | |
|
| 1742 | 237 | | if (slotNumberText != null) |
| 1742 | 238 | | slotNumberText.color = isSelected ? selectedSlotNumberColor : defaultSlotNumberColor; |
| | 239 | |
|
| 1742 | 240 | | if (emoteNameText != null) |
| 1742 | 241 | | emoteNameText.color = isSelected ? selectedEmoteNameColor : defaultEmoteNameColor; |
| | 242 | |
|
| 1742 | 243 | | if (slotViewerImage != null) |
| 1742 | 244 | | slotViewerImage.gameObject.SetActive(isSelected); |
| 1742 | 245 | | } |
| | 246 | | } |
| | 247 | | } |