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