| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | namespace DCL.Huds.QuestsPanel |
| | 9 | | { |
| | 10 | | public interface IQuestsPanelHUDView |
| | 11 | | { |
| | 12 | | void RequestAddOrUpdateQuest(string questId); |
| | 13 | | void RemoveQuest(string questId); |
| | 14 | | void ClearQuests(); |
| | 15 | | void SetVisibility(bool active); |
| | 16 | | bool isVisible { get; } |
| | 17 | | void Dispose(); |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public class QuestsPanelHUDView : MonoBehaviour, IQuestsPanelHUDView |
| | 21 | | { |
| 1 | 22 | | internal static int ENTRIES_PER_FRAME { get; set; } = 5; |
| | 23 | | private const string VIEW_PATH = "QuestsPanelHUD"; |
| | 24 | |
|
| | 25 | | [SerializeField] internal RectTransform availableQuestsContainer; |
| | 26 | | [SerializeField] internal RectTransform completedQuestsContainer; |
| | 27 | | [SerializeField] private GameObject questsContainerSeparators; |
| | 28 | | [SerializeField] private GameObject questPrefab; |
| | 29 | | [SerializeField] private GameObject noQuestsTitle; |
| | 30 | | [SerializeField] internal QuestsPanelPopup questPopup; |
| | 31 | | [SerializeField] private Button closeButton; |
| | 32 | | [SerializeField] private DynamicScrollSensitivity dynamicScrollSensitivity; |
| | 33 | |
|
| 0 | 34 | | private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests; |
| | 35 | |
|
| 21 | 36 | | private string currentQuestInPopup = ""; |
| 21 | 37 | | internal readonly Dictionary<string, QuestsPanelEntry> questEntries = new Dictionary<string, QuestsPanelEntry>( |
| | 38 | | private bool orderQuestsRequested = false; |
| | 39 | | private bool layoutRebuildRequested = false; |
| 21 | 40 | | internal readonly List<string> questsToBeAdded = new List<string>(); |
| | 41 | | private bool isDestroyed = false; |
| | 42 | |
|
| | 43 | | internal static QuestsPanelHUDView Create() |
| | 44 | | { |
| 1 | 45 | | var view = Instantiate(Resources.Load<GameObject>(VIEW_PATH)).GetComponent<QuestsPanelHUDView>(); |
| | 46 | | #if UNITY_EDITOR |
| 1 | 47 | | view.gameObject.name = "_QuestsPanelHUDView"; |
| | 48 | | #endif |
| 1 | 49 | | return view; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public void Awake() |
| | 53 | | { |
| 19 | 54 | | questPopup.gameObject.SetActive(false); |
| 19 | 55 | | noQuestsTitle.SetActive(false); |
| 19 | 56 | | closeButton.onClick.AddListener(() => |
| | 57 | | { |
| 0 | 58 | | QuestsUIAnalytics.SendQuestLogVisibiltyChanged(false, "quest_log_close_button"); |
| 0 | 59 | | DataStore.i.HUDs.questsPanelVisible.Set(false); |
| 0 | 60 | | }); |
| 19 | 61 | | } |
| | 62 | |
|
| | 63 | | public void RequestAddOrUpdateQuest(string questId) |
| | 64 | | { |
| 4 | 65 | | if (questsToBeAdded.Contains(questId)) |
| | 66 | | { |
| 1 | 67 | | AddOrUpdateQuest(questId); |
| 1 | 68 | | return; |
| | 69 | | } |
| | 70 | |
|
| 3 | 71 | | questsToBeAdded.Add(questId); |
| 3 | 72 | | } |
| | 73 | |
|
| | 74 | | internal void AddOrUpdateQuest(string questId) |
| | 75 | | { |
| 18 | 76 | | if (!quests.TryGetValue(questId, out QuestModel quest)) |
| | 77 | | { |
| 1 | 78 | | Debug.LogError($"Couldn't find quest with ID {questId} in DataStore"); |
| 1 | 79 | | return; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | //Quest has no available tasks, we remove it. |
| 17 | 83 | | if (!quest.hasAvailableTasks) |
| | 84 | | { |
| 0 | 85 | | RemoveQuest(questId); |
| 0 | 86 | | return; |
| | 87 | | } |
| | 88 | |
|
| 17 | 89 | | if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry)) |
| | 90 | | { |
| 16 | 91 | | questEntry = Instantiate(questPrefab).GetComponent<QuestsPanelEntry>(); |
| 16 | 92 | | questEntry.OnReadMoreClicked += ShowQuestPopup; |
| 16 | 93 | | questEntries.Add(questId, questEntry); |
| | 94 | | } |
| | 95 | |
|
| 17 | 96 | | questEntry.transform.localScale = Vector3.one; |
| 17 | 97 | | questEntry.Populate(quest); |
| 17 | 98 | | if (currentQuestInPopup == questId) |
| 0 | 99 | | questPopup.Populate(quest); |
| | 100 | |
|
| 17 | 101 | | orderQuestsRequested = true; |
| 17 | 102 | | layoutRebuildRequested = true; |
| 17 | 103 | | } |
| | 104 | |
|
| | 105 | | public void RemoveQuest(string questId) |
| | 106 | | { |
| 2 | 107 | | questsToBeAdded.Remove(questId); |
| | 108 | |
|
| 2 | 109 | | if (!questEntries.TryGetValue(questId, out QuestsPanelEntry questEntry)) |
| 1 | 110 | | return; |
| 1 | 111 | | questEntries.Remove(questId); |
| 1 | 112 | | questEntry.Unparent(); |
| 1 | 113 | | questEntry.SelfDestroy(); |
| | 114 | |
|
| 1 | 115 | | if (currentQuestInPopup == questId) |
| 0 | 116 | | questPopup.Close(); |
| | 117 | |
|
| 1 | 118 | | questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0); |
| 1 | 119 | | noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 ); |
| 1 | 120 | | layoutRebuildRequested = true; |
| 1 | 121 | | } |
| | 122 | |
|
| | 123 | | public void ClearQuests() |
| | 124 | | { |
| 1 | 125 | | questPopup.Close(); |
| 2 | 126 | | foreach (QuestsPanelEntry questEntry in questEntries.Values) |
| | 127 | | { |
| 0 | 128 | | questEntry.transform.SetParent(null); |
| 0 | 129 | | Destroy(questEntry.gameObject); |
| | 130 | | } |
| | 131 | |
|
| 1 | 132 | | questEntries.Clear(); |
| 1 | 133 | | questsToBeAdded.Clear(); |
| 1 | 134 | | questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0); |
| 1 | 135 | | noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 ); |
| 1 | 136 | | layoutRebuildRequested = true; |
| 1 | 137 | | } |
| | 138 | |
|
| | 139 | | internal void ShowQuestPopup(string questId) |
| | 140 | | { |
| 8 | 141 | | if (!quests.TryGetValue(questId, out QuestModel quest)) |
| | 142 | | { |
| 1 | 143 | | Debug.Log($"Couldnt find quest with id {questId}"); |
| 1 | 144 | | return; |
| | 145 | | } |
| | 146 | |
|
| 7 | 147 | | questPopup.SetPositionToReadMoreButton(questEntries[questId].readMorePosition); |
| | 148 | |
|
| 7 | 149 | | currentQuestInPopup = questId; |
| 7 | 150 | | questPopup.Populate(quest); |
| 7 | 151 | | questPopup.Show(); |
| 7 | 152 | | } |
| | 153 | |
|
| | 154 | | internal void Update() |
| | 155 | | { |
| 4 | 156 | | if (layoutRebuildRequested) |
| | 157 | | { |
| 2 | 158 | | layoutRebuildRequested = false; |
| 2 | 159 | | Utils.ForceRebuildLayoutImmediate(GetComponent<RectTransform>()); |
| 2 | 160 | | dynamicScrollSensitivity?.RecalculateSensitivity(); |
| | 161 | | } |
| | 162 | |
|
| 4 | 163 | | if (orderQuestsRequested) |
| | 164 | | { |
| 1 | 165 | | orderQuestsRequested = false; |
| 1 | 166 | | OrderQuests(); |
| | 167 | | } |
| | 168 | |
|
| 8 | 169 | | for (int i = 0; i < ENTRIES_PER_FRAME && questsToBeAdded.Count > 0; i++) |
| | 170 | | { |
| 0 | 171 | | string questId = questsToBeAdded.First(); |
| 0 | 172 | | questsToBeAdded.RemoveAt(0); |
| 0 | 173 | | AddOrUpdateQuest(questId); |
| | 174 | | } |
| 4 | 175 | | } |
| | 176 | |
|
| | 177 | | private void OrderQuests() |
| | 178 | | { |
| 9 | 179 | | var questModels = questEntries.Keys.Select(x => quests.Get(x)); |
| | 180 | |
|
| 14 | 181 | | string[] availableIdsSorted = questModels.Where(x => !x.isCompleted).OrderBy(x => x.assignmentTime).ThenBy(x |
| 8 | 182 | | for (int i = 0; i < availableIdsSorted.Length; i++) |
| | 183 | | { |
| 3 | 184 | | questEntries[availableIdsSorted[i]].transform.SetParent(availableQuestsContainer); |
| 3 | 185 | | questEntries[availableIdsSorted[i]].transform.localScale = Vector3.one; |
| 3 | 186 | | questEntries[availableIdsSorted[i]].transform.SetSiblingIndex(i); |
| | 187 | | } |
| | 188 | |
|
| 8 | 189 | | string[] completedQuestsSorted = questModels.Where(x => x.isCompleted).OrderBy(x => x.completionTime).ThenBy |
| 4 | 190 | | for (int i = 0; i < completedQuestsSorted.Length; i++) |
| | 191 | | { |
| 1 | 192 | | questEntries[completedQuestsSorted[i]].transform.SetParent(completedQuestsContainer); |
| 1 | 193 | | questEntries[completedQuestsSorted[i]].transform.localScale = Vector3.one; |
| 1 | 194 | | questEntries[completedQuestsSorted[i]].transform.SetSiblingIndex(i); |
| | 195 | | } |
| | 196 | |
|
| 1 | 197 | | questsContainerSeparators.SetActive(completedQuestsContainer.childCount > 0); |
| 1 | 198 | | noQuestsTitle.SetActive(availableQuestsContainer.childCount == 0 ); |
| 1 | 199 | | } |
| | 200 | |
|
| | 201 | | public void SetVisibility(bool active) |
| | 202 | | { |
| 2 | 203 | | gameObject.SetActive(active); |
| | 204 | |
|
| 2 | 205 | | if (active) |
| 1 | 206 | | AudioScriptableObjects.dialogOpen.Play(); |
| | 207 | | else |
| 1 | 208 | | AudioScriptableObjects.dialogClose.Play(); |
| 1 | 209 | | } |
| | 210 | |
|
| 0 | 211 | | public bool isVisible => gameObject.activeSelf; |
| | 212 | |
|
| | 213 | | public void Dispose() |
| | 214 | | { |
| 1 | 215 | | if (!isDestroyed) |
| 1 | 216 | | Destroy(gameObject); |
| 1 | 217 | | } |
| | 218 | |
|
| 38 | 219 | | private void OnDestroy() { isDestroyed = true; } |
| | 220 | | } |
| | 221 | | } |