| | 1 | | using DCL.Helpers; |
| | 2 | | using DCL.Interface; |
| | 3 | | using System; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.UI; |
| | 8 | |
|
| | 9 | | namespace DCL.Huds.QuestsPanel |
| | 10 | | { |
| | 11 | | public class QuestsPanelEntry : MonoBehaviour |
| | 12 | | { |
| 1 | 13 | | private static readonly int LOADED_ANIM_TRIGGER = Animator.StringToHash("Loaded"); |
| | 14 | | public event Action<string> OnReadMoreClicked; |
| | 15 | |
|
| | 16 | | [SerializeField] internal TextMeshProUGUI questName; |
| | 17 | | [SerializeField] internal TextMeshProUGUI description; |
| | 18 | | [SerializeField] internal Button readMoreButton; |
| | 19 | | [SerializeField] internal Toggle pinQuestToggle; |
| | 20 | | [SerializeField] internal Image progressInTitle; |
| | 21 | | [SerializeField] internal RectTransform completedProgressInTitle; |
| | 22 | | [SerializeField] internal RectTransform completedMarkInTitle; |
| | 23 | | [SerializeField] internal RawImage thumbnailImage; |
| | 24 | | [SerializeField] internal Button jumpInButton; |
| | 25 | | [SerializeField] internal Animator animator; |
| | 26 | | [SerializeField] internal GameObject rewardsPanel; |
| | 27 | | [SerializeField] internal TextMeshProUGUI rewardsAmount; |
| | 28 | |
|
| | 29 | | private AssetPromise_Texture thumbnailPromise; |
| | 30 | |
|
| | 31 | | private QuestModel quest; |
| | 32 | | private string currentThumbnail; |
| | 33 | |
|
| | 34 | | internal Action readMoreDelegate; |
| 81 | 35 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| | 36 | |
|
| | 37 | | private Action jumpInDelegate; |
| 7 | 38 | | public Vector3 readMorePosition => readMoreButton.transform.position; |
| | 39 | |
|
| | 40 | | private bool isDestroyed = false; |
| | 41 | |
|
| | 42 | | private void Awake() |
| | 43 | | { |
| 16 | 44 | | jumpInButton.onClick.AddListener(() => { jumpInDelegate?.Invoke(); }); |
| 16 | 45 | | readMoreButton.onClick.AddListener(() => readMoreDelegate?.Invoke()); |
| 16 | 46 | | pinQuestToggle.onValueChanged.AddListener(OnPinToggleValueChanged); |
| 16 | 47 | | pinnedQuests.OnAdded += OnPinnedQuests; |
| 16 | 48 | | pinnedQuests.OnRemoved += OnUnpinnedQuest; |
| 16 | 49 | | } |
| | 50 | |
|
| | 51 | | private void OnEnable() |
| | 52 | | { |
| 20 | 53 | | if (quest == null || string.IsNullOrEmpty(currentThumbnail) || (thumbnailPromise != null && thumbnailPromise |
| 20 | 54 | | animator?.SetTrigger(LOADED_ANIM_TRIGGER); |
| 20 | 55 | | } |
| | 56 | |
|
| | 57 | | public void Populate(QuestModel newQuest) |
| | 58 | | { |
| 17 | 59 | | quest = newQuest; |
| | 60 | |
|
| 50 | 61 | | QuestTask incompletedTask = quest.sections.FirstOrDefault(x => x.progress < 1)?.tasks.FirstOrDefault(x => x. |
| 17 | 62 | | jumpInButton.gameObject.SetActive(incompletedTask != null && !string.IsNullOrEmpty(incompletedTask?.coordina |
| 17 | 63 | | jumpInDelegate = () => |
| | 64 | | { |
| 0 | 65 | | if (incompletedTask == null) |
| 0 | 66 | | return; |
| | 67 | |
|
| 0 | 68 | | QuestsUIAnalytics.SendJumpInPressed(quest.id, incompletedTask.id, incompletedTask.coordinates, QuestsUIA |
| 0 | 69 | | WebInterface.SendChatMessage(new ChatMessage |
| | 70 | | { |
| | 71 | | messageType = ChatMessage.Type.NONE, |
| | 72 | | recipient = string.Empty, |
| | 73 | | body = $"/goto {incompletedTask.coordinates}", |
| | 74 | | }); |
| | 75 | |
|
| 0 | 76 | | DataStore.i.HUDs.questsPanelVisible.Set(false); |
| 0 | 77 | | }; |
| | 78 | |
|
| 17 | 79 | | readMoreDelegate = () => OnReadMoreClicked?.Invoke(quest.id); |
| 17 | 80 | | questName.text = quest.name; |
| 17 | 81 | | description.text = quest.description; |
| 17 | 82 | | SetThumbnail(quest.thumbnail_entry); |
| 17 | 83 | | pinQuestToggle.SetIsOnWithoutNotify(pinnedQuests.Contains(quest.id)); |
| | 84 | |
|
| 17 | 85 | | pinQuestToggle.gameObject.SetActive(!quest.isCompleted); |
| 17 | 86 | | progressInTitle.transform.localScale = new Vector3(quest.progress, 1, 1); |
| 17 | 87 | | completedProgressInTitle.gameObject.SetActive(quest.isCompleted); |
| 17 | 88 | | completedMarkInTitle.gameObject.SetActive(quest.isCompleted); |
| | 89 | |
|
| 17 | 90 | | SetRewards(quest.rewards?.Length ?? 0); |
| 17 | 91 | | } |
| | 92 | |
|
| | 93 | | private void OnPinToggleValueChanged(bool isOn) |
| | 94 | | { |
| 0 | 95 | | if (quest == null) |
| 0 | 96 | | return; |
| | 97 | |
|
| 0 | 98 | | if (!quest.canBePinned) |
| | 99 | | { |
| 0 | 100 | | pinnedQuests.Remove(quest.id); |
| 0 | 101 | | pinQuestToggle.SetIsOnWithoutNotify(false); |
| 0 | 102 | | return; |
| | 103 | | } |
| | 104 | |
|
| 0 | 105 | | if (isOn) |
| | 106 | | { |
| 0 | 107 | | if (!pinnedQuests.Contains(quest.id)) |
| 0 | 108 | | pinnedQuests.Add(quest.id); |
| 0 | 109 | | } |
| | 110 | | else |
| | 111 | | { |
| 0 | 112 | | pinnedQuests.Remove(quest.id); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestsLog); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | private void OnPinnedQuests(string questId) |
| | 119 | | { |
| 16 | 120 | | if (quest != null && quest.id == questId) |
| 12 | 121 | | pinQuestToggle.SetIsOnWithoutNotify(true); |
| 16 | 122 | | } |
| | 123 | |
|
| | 124 | | private void OnUnpinnedQuest(string questId) |
| | 125 | | { |
| 0 | 126 | | if (quest != null && quest.id == questId) |
| 0 | 127 | | pinQuestToggle.SetIsOnWithoutNotify(false); |
| 0 | 128 | | } |
| | 129 | |
|
| | 130 | | internal void SetThumbnail(string thumbnailURL) |
| | 131 | | { |
| 17 | 132 | | if (thumbnailURL == currentThumbnail) |
| | 133 | | { |
| 17 | 134 | | animator.SetTrigger(LOADED_ANIM_TRIGGER); |
| 17 | 135 | | return; |
| | 136 | | } |
| | 137 | |
|
| 0 | 138 | | currentThumbnail = thumbnailURL; |
| 0 | 139 | | if (thumbnailPromise != null) |
| | 140 | | { |
| 0 | 141 | | thumbnailPromise.ClearEvents(); |
| 0 | 142 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| | 143 | | } |
| | 144 | |
|
| 0 | 145 | | if (string.IsNullOrEmpty(currentThumbnail)) |
| | 146 | | { |
| 0 | 147 | | thumbnailImage.gameObject.SetActive(false); |
| 0 | 148 | | animator.SetTrigger(LOADED_ANIM_TRIGGER); |
| 0 | 149 | | return; |
| | 150 | | } |
| | 151 | |
|
| 0 | 152 | | thumbnailPromise = new AssetPromise_Texture(currentThumbnail); |
| 0 | 153 | | thumbnailPromise.OnSuccessEvent += OnThumbnailReady; |
| 0 | 154 | | thumbnailPromise.OnFailEvent += (x, error) => |
| | 155 | | { |
| 0 | 156 | | thumbnailImage.gameObject.SetActive(false); |
| 0 | 157 | | animator.SetTrigger(LOADED_ANIM_TRIGGER); |
| 0 | 158 | | Debug.LogError($"Error downloading quest panel entry thumbnail: {currentThumbnail}, Exception: {error}") |
| 0 | 159 | | }; |
| | 160 | |
|
| 0 | 161 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| 0 | 162 | | } |
| | 163 | |
|
| | 164 | | private void OnThumbnailReady(Asset_Texture assetTexture) |
| | 165 | | { |
| 0 | 166 | | thumbnailImage.gameObject.SetActive(true); |
| 0 | 167 | | thumbnailImage.texture = assetTexture.texture; |
| 0 | 168 | | animator.SetTrigger(LOADED_ANIM_TRIGGER); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | private void SetRewards(int amount) |
| | 172 | | { |
| 17 | 173 | | rewardsPanel.SetActive(amount > 0); |
| 17 | 174 | | rewardsAmount.text = amount.ToString(); |
| 17 | 175 | | } |
| | 176 | |
|
| | 177 | | private void OnDestroy() |
| | 178 | | { |
| 16 | 179 | | if (thumbnailPromise != null) |
| | 180 | | { |
| 0 | 181 | | thumbnailPromise.ClearEvents(); |
| 0 | 182 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| | 183 | | } |
| | 184 | |
|
| 16 | 185 | | pinnedQuests.OnAdded -= OnUnpinnedQuest; |
| 16 | 186 | | pinnedQuests.OnRemoved -= OnPinnedQuests; |
| 16 | 187 | | isDestroyed = true; |
| 16 | 188 | | } |
| | 189 | |
|
| | 190 | | public void Unparent() |
| | 191 | | { |
| 1 | 192 | | if (isDestroyed) |
| 0 | 193 | | return; |
| 1 | 194 | | transform.parent = null; |
| 1 | 195 | | } |
| | 196 | |
|
| | 197 | | public void SelfDestroy() |
| | 198 | | { |
| 1 | 199 | | if (isDestroyed) |
| 0 | 200 | | return; |
| 1 | 201 | | Destroy(gameObject); |
| 1 | 202 | | } |
| | 203 | | } |
| | 204 | | } |