| | 1 | | using DCL.Helpers; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using TMPro; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Huds.QuestsPanel |
| | 9 | | { |
| | 10 | | public class QuestsPanelPopup : MonoBehaviour |
| | 11 | | { |
| | 12 | | private const int ARROW_OFFSET = 220; |
| | 13 | |
|
| | 14 | | [SerializeField] internal TextMeshProUGUI questName; |
| | 15 | | [SerializeField] internal TextMeshProUGUI description; |
| | 16 | | [SerializeField] internal RectTransform sectionsContainer; |
| | 17 | | [SerializeField] internal GameObject sectionPrefab; |
| | 18 | | [SerializeField] internal Button closeButton; |
| | 19 | | [SerializeField] internal Toggle pinQuestToggle; |
| | 20 | | [SerializeField] internal RawImage thumbnailImage; |
| | 21 | | [SerializeField] internal Button closePopupAreaButton; |
| | 22 | | [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity; |
| | 23 | | [SerializeField] internal GameObject rewardsPanel; |
| | 24 | | [SerializeField] internal TextMeshProUGUI rewardsAmount; |
| | 25 | |
|
| | 26 | | private AssetPromise_Texture thumbnailPromise; |
| | 27 | | private bool forceRebuildLayout = false; |
| | 28 | |
|
| | 29 | | internal QuestModel quest; |
| 20 | 30 | | internal readonly List<QuestsPanelSection> sections = new List<QuestsPanelSection>(); |
| 42 | 31 | | private static BaseCollection<string> pinnedQuests => DataStore.i.Quests.pinnedQuests; |
| | 32 | | private RectTransform rectTransform; |
| | 33 | |
|
| | 34 | | private void Awake() |
| | 35 | | { |
| 7 | 36 | | rectTransform = GetComponent<RectTransform>(); |
| | 37 | |
|
| 7 | 38 | | closeButton.onClick.AddListener(Close); |
| | 39 | |
|
| 7 | 40 | | pinQuestToggle.onValueChanged.AddListener(OnPinToggleValueChanged); |
| 7 | 41 | | pinnedQuests.OnAdded += OnPinnedQuests; |
| 7 | 42 | | pinnedQuests.OnRemoved += OnUnpinnedQuest; |
| | 43 | |
|
| 7 | 44 | | closePopupAreaButton.onClick.AddListener(Close); |
| 7 | 45 | | } |
| | 46 | |
|
| | 47 | | public void Populate(QuestModel newQuest) |
| | 48 | | { |
| 7 | 49 | | quest = newQuest; |
| 21 | 50 | | QuestSection[] availableSections = quest.sections.Where(x => x.tasks.Any(y => y.status != QuestsLiterals.Sta |
| 7 | 51 | | PrepareSections(availableSections.Length); |
| | 52 | |
|
| 7 | 53 | | questName.text = quest.name; |
| 7 | 54 | | description.text = quest.description; |
| 7 | 55 | | SetThumbnail(quest.thumbnail_banner); |
| 28 | 56 | | for (int i = 0; i < availableSections.Length; i++) |
| | 57 | | { |
| 7 | 58 | | sections[i].Populate(quest, availableSections[i]); |
| | 59 | | } |
| | 60 | |
|
| 7 | 61 | | pinQuestToggle.SetIsOnWithoutNotify(pinnedQuests.Contains(quest.id)); |
| 7 | 62 | | pinQuestToggle.gameObject.SetActive(!quest.isCompleted); |
| 7 | 63 | | forceRebuildLayout = true; |
| | 64 | |
|
| 7 | 65 | | SetRewards(quest.rewards?.Length ?? 0); |
| 7 | 66 | | } |
| | 67 | |
|
| | 68 | | private void OnPinToggleValueChanged(bool isOn) |
| | 69 | | { |
| 0 | 70 | | if (quest == null) |
| 0 | 71 | | return; |
| | 72 | |
|
| 0 | 73 | | if (isOn) |
| | 74 | | { |
| 0 | 75 | | if (!pinnedQuests.Contains(quest.id)) |
| 0 | 76 | | pinnedQuests.Add(quest.id); |
| 0 | 77 | | } |
| | 78 | | else |
| | 79 | | { |
| 0 | 80 | | if (pinnedQuests.Contains(quest.id)) |
| 0 | 81 | | pinnedQuests.Remove(quest.id); |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | QuestsUIAnalytics.SendQuestPinChanged(quest.id, isOn, QuestsUIAnalytics.UIContext.QuestDetails); |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | private void OnPinnedQuests(string questId) |
| | 88 | | { |
| 0 | 89 | | if (quest != null && quest.id == questId) |
| 0 | 90 | | pinQuestToggle.SetIsOnWithoutNotify(true); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | private void OnUnpinnedQuest(string questId) |
| | 94 | | { |
| 0 | 95 | | if (quest != null && quest.id == questId) |
| 0 | 96 | | pinQuestToggle.SetIsOnWithoutNotify(false); |
| 0 | 97 | | } |
| | 98 | |
|
| | 99 | | internal void SetThumbnail(string thumbnailURL) |
| | 100 | | { |
| 7 | 101 | | if (thumbnailPromise != null) |
| | 102 | | { |
| 0 | 103 | | thumbnailPromise.ClearEvents(); |
| 0 | 104 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| | 105 | | } |
| | 106 | |
|
| 7 | 107 | | if (string.IsNullOrEmpty(thumbnailURL)) |
| | 108 | | { |
| 7 | 109 | | thumbnailImage.gameObject.SetActive(false); |
| 7 | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | thumbnailPromise = new AssetPromise_Texture(thumbnailURL); |
| 0 | 114 | | thumbnailPromise.OnSuccessEvent += OnThumbnailReady; |
| 0 | 115 | | thumbnailPromise.OnFailEvent += (x, error) => |
| | 116 | | { |
| 0 | 117 | | thumbnailImage.gameObject.SetActive(false); |
| 0 | 118 | | Debug.Log($"Error downloading quest panel popup thumbnail: {thumbnailURL}, Exception: {error}"); |
| 0 | 119 | | }; |
| | 120 | |
|
| 0 | 121 | | AssetPromiseKeeper_Texture.i.Keep(thumbnailPromise); |
| 0 | 122 | | } |
| | 123 | |
|
| | 124 | | private void OnThumbnailReady(Asset_Texture assetTexture) |
| | 125 | | { |
| 0 | 126 | | thumbnailImage.gameObject.SetActive(true); |
| 0 | 127 | | thumbnailImage.texture = assetTexture.texture; |
| 0 | 128 | | } |
| | 129 | |
|
| 14 | 130 | | internal void CreateSection() { sections.Add(Instantiate(sectionPrefab, sectionsContainer).GetComponent<QuestsPa |
| | 131 | |
|
| | 132 | | internal void PrepareSections(int sectionsAmount) |
| | 133 | | { |
| 7 | 134 | | if (sections.Count == sectionsAmount) |
| 0 | 135 | | return; |
| | 136 | |
|
| 7 | 137 | | if (sections.Count < sectionsAmount) |
| | 138 | | { |
| 14 | 139 | | while (sections.Count < sectionsAmount) |
| 7 | 140 | | CreateSection(); |
| 7 | 141 | | } |
| | 142 | | else |
| | 143 | | { |
| 0 | 144 | | while (sections.Count > sectionsAmount) |
| | 145 | | { |
| 0 | 146 | | var section = sections.Last(); |
| 0 | 147 | | sections.RemoveAt(sections.Count - 1); |
| 0 | 148 | | Destroy(section.gameObject); |
| | 149 | | } |
| | 150 | | } |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public void Show() |
| | 154 | | { |
| 7 | 155 | | gameObject.SetActive(true); |
| 7 | 156 | | AudioScriptableObjects.dialogOpen.Play(); |
| 7 | 157 | | } |
| | 158 | |
|
| | 159 | | public void Close() |
| | 160 | | { |
| 21 | 161 | | gameObject.SetActive(false); |
| 21 | 162 | | AudioScriptableObjects.dialogClose.Play(); |
| 21 | 163 | | } |
| | 164 | |
|
| | 165 | | private void Update() |
| | 166 | | { |
| 0 | 167 | | if (forceRebuildLayout) |
| | 168 | | { |
| 0 | 169 | | forceRebuildLayout = false; |
| 0 | 170 | | rectTransform.ForceUpdateLayout(); |
| 0 | 171 | | dynamicScrollSensitivity?.RecalculateSensitivity(); |
| | 172 | | } |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | private void SetRewards(int amount) |
| | 176 | | { |
| 7 | 177 | | rewardsPanel.SetActive(amount > 0); |
| 7 | 178 | | rewardsAmount.text = amount.ToString(); |
| 7 | 179 | | } |
| | 180 | |
|
| | 181 | | private void OnDestroy() |
| | 182 | | { |
| 7 | 183 | | if (thumbnailPromise != null) |
| | 184 | | { |
| 0 | 185 | | thumbnailPromise.ClearEvents(); |
| 0 | 186 | | AssetPromiseKeeper_Texture.i.Forget(thumbnailPromise); |
| | 187 | | } |
| | 188 | |
|
| 7 | 189 | | if (pinnedQuests != null) |
| | 190 | | { |
| 7 | 191 | | pinnedQuests.OnAdded -= OnPinnedQuests; |
| 7 | 192 | | pinnedQuests.OnRemoved -= OnUnpinnedQuest; |
| | 193 | | } |
| 7 | 194 | | } |
| | 195 | |
|
| | 196 | | public void SetPositionToReadMoreButton(Vector3 position) |
| | 197 | | { |
| 7 | 198 | | Vector3 pos = transform.position; |
| 7 | 199 | | pos.y = Mathf.Max(0, position.y - ARROW_OFFSET); |
| 7 | 200 | | transform.position = pos; |
| 7 | 201 | | } |
| | 202 | | } |
| | 203 | | } |