| | 1 | | using MainScripts.DCL.Helpers.Utils; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using TMPro; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Quests |
| | 9 | | { |
| | 10 | | public class QuestLogComponentView : BaseComponentView, IQuestLogComponentView |
| | 11 | | { |
| | 12 | | private const int MAX_QUESTS_COUNT = 10; |
| | 13 | | private const string IN_PROGRESS_TITLE = "Active quests"; |
| | 14 | | private const string COMPLETED_TITLE = "Completed quests"; |
| | 15 | | private const int IN_PROGRESS_SECTION_INDEX = 0; |
| | 16 | | private const int COMPLETED_SECTION_INDEX = 1; |
| | 17 | |
|
| | 18 | | [SerializeField] internal SectionSelectorComponentView sectionSelector; |
| | 19 | | [SerializeField] internal QuestDetailsComponentView questDetailsComponentView; |
| | 20 | | [SerializeField] internal TMP_Text headerText; |
| | 21 | | [SerializeField] internal GameObject emptyState; |
| | 22 | | [SerializeField] internal GameObject emptyActiveState; |
| | 23 | | [SerializeField] internal GameObject emptyCompletedState; |
| | 24 | | [SerializeField] internal Transform activeQuestsContainer; |
| | 25 | | [SerializeField] internal ActiveQuestComponentView activeQuestPrefab; |
| | 26 | |
|
| | 27 | | public event Action<string, bool> OnPinChange; |
| | 28 | | public event Action<Vector2Int> OnJumpIn; |
| | 29 | | public event Action<string> OnQuestAbandon; |
| | 30 | |
|
| | 31 | | private Dictionary<string, ActiveQuestComponentView> activeQuests; |
| | 32 | | private Dictionary<string, ActiveQuestComponentView> completedQuests; |
| | 33 | | private UnityObjectPool<ActiveQuestComponentView> questsPool; |
| | 34 | | private string previouslyActiveSelectedQuest; |
| | 35 | | private string previouslyCompletedSelectedQuest; |
| | 36 | |
|
| | 37 | | public override void Awake() |
| | 38 | | { |
| 0 | 39 | | activeQuests = new (); |
| 0 | 40 | | completedQuests = new (); |
| 0 | 41 | | questsPool = new UnityObjectPool<ActiveQuestComponentView>(activeQuestPrefab, activeQuestsContainer, actionO |
| 0 | 42 | | questsPool.Prewarm(MAX_QUESTS_COUNT); |
| 0 | 43 | | InitialiseSectionSelector(); |
| | 44 | |
|
| 0 | 45 | | questDetailsComponentView.OnJumpIn += (coords) => OnJumpIn?.Invoke(coords); |
| 0 | 46 | | questDetailsComponentView.OnPinChange += (questId, isPinned) => OnPinChange?.Invoke(questId, isPinned); |
| 0 | 47 | | questDetailsComponentView.OnQuestAbandon += (questId) => OnQuestAbandon?.Invoke(questId); |
| | 48 | |
|
| 0 | 49 | | emptyState.SetActive(true); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | private void InitialiseSectionSelector() |
| | 53 | | { |
| 0 | 54 | | sectionSelector.Awake(); |
| 0 | 55 | | sectionSelector.GetSection(IN_PROGRESS_SECTION_INDEX).onSelect.RemoveAllListeners(); |
| 0 | 56 | | sectionSelector.GetSection(COMPLETED_SECTION_INDEX).onSelect.RemoveAllListeners(); |
| 0 | 57 | | sectionSelector.GetSection(IN_PROGRESS_SECTION_INDEX).onSelect.AddListener(InProgressSectionOnSelect); |
| 0 | 58 | | sectionSelector.GetSection(COMPLETED_SECTION_INDEX).onSelect.AddListener(CompletedSectionOnSelect); |
| 0 | 59 | | } |
| | 60 | |
|
| | 61 | | private void CompletedSectionOnSelect(bool isSelected) |
| | 62 | | { |
| 0 | 63 | | if (!isSelected) |
| 0 | 64 | | return; |
| | 65 | |
|
| 0 | 66 | | headerText.text = COMPLETED_TITLE; |
| 0 | 67 | | ShowActiveOrCompletedQuests(false); |
| | 68 | |
|
| 0 | 69 | | if (completedQuests.Count == 0) |
| | 70 | | { |
| 0 | 71 | | emptyState.SetActive(true); |
| 0 | 72 | | emptyActiveState.SetActive(false); |
| 0 | 73 | | emptyCompletedState.SetActive(true); |
| | 74 | | } |
| | 75 | | else |
| | 76 | | { |
| 0 | 77 | | emptyState.SetActive(false); |
| 0 | 78 | | HandleCompletedQuestSelection(completedQuests.First().Key); |
| | 79 | | } |
| 0 | 80 | | } |
| | 81 | |
|
| | 82 | | private void InProgressSectionOnSelect(bool isSelected) |
| | 83 | | { |
| 0 | 84 | | if (!isSelected) |
| 0 | 85 | | return; |
| | 86 | |
|
| 0 | 87 | | headerText.text = IN_PROGRESS_TITLE; |
| 0 | 88 | | ShowActiveOrCompletedQuests(true); |
| | 89 | |
|
| 0 | 90 | | if (activeQuests.Count == 0) |
| | 91 | | { |
| 0 | 92 | | emptyState.SetActive(true); |
| 0 | 93 | | emptyActiveState.SetActive(true); |
| 0 | 94 | | emptyCompletedState.SetActive(false); |
| | 95 | | } |
| | 96 | | else |
| | 97 | | { |
| 0 | 98 | | emptyState.SetActive(false); |
| 0 | 99 | | HandleActiveQuestSelection(activeQuests.First().Key); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private void ShowActiveOrCompletedQuests(bool active) |
| | 104 | | { |
| 0 | 105 | | foreach (var activeQuest in activeQuests.Values) |
| | 106 | | { |
| 0 | 107 | | activeQuest.gameObject.SetActive(active); |
| | 108 | |
|
| | 109 | | } |
| 0 | 110 | | foreach (var completedQuest in completedQuests.Values) |
| | 111 | | { |
| 0 | 112 | | completedQuest.gameObject.SetActive(!active); |
| | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | public void AddActiveQuest(QuestDetailsComponentModel activeQuest, string creatorName) |
| | 117 | | { |
| 0 | 118 | | emptyState.SetActive(false); |
| | 119 | |
|
| 0 | 120 | | if (activeQuests.ContainsKey(activeQuest.questId)) |
| | 121 | | { |
| 0 | 122 | | questsPool.Release(activeQuests[activeQuest.questId]); |
| 0 | 123 | | activeQuests.Remove(activeQuest.questId); |
| | 124 | | } |
| | 125 | |
|
| 0 | 126 | | activeQuests.TryAdd(activeQuest.questId, questsPool.Get()); |
| | 127 | |
|
| 0 | 128 | | ActiveQuestComponentView activeQuestComponentView = activeQuests[activeQuest.questId]; |
| 0 | 129 | | activeQuestComponentView.OnActiveQuestSelected -= SelectedQuest; |
| 0 | 130 | | activeQuestComponentView.SetModel(new ActiveQuestComponentModel() |
| | 131 | | { |
| | 132 | | questId = activeQuest.questId, |
| | 133 | | questCreator = creatorName, |
| | 134 | | questName = activeQuest.questName, |
| | 135 | | questImageUri = activeQuest.questImageUri, |
| | 136 | | isPinned = activeQuest.isPinned, |
| | 137 | | questModel = activeQuest |
| | 138 | | }); |
| 0 | 139 | | activeQuestComponentView.OnActiveQuestSelected += SelectedQuest; |
| 0 | 140 | | HandleActiveQuestSelection(activeQuest.questId); |
| 0 | 141 | | sectionSelector.GetSection(IN_PROGRESS_SECTION_INDEX).SelectToggle(true); |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | public void AddCompletedQuest(QuestDetailsComponentModel completedQuest, string creatorName) |
| | 145 | | { |
| 0 | 146 | | emptyState.SetActive(false); |
| | 147 | |
|
| 0 | 148 | | if (activeQuests.ContainsKey(completedQuest.questId)) |
| | 149 | | { |
| 0 | 150 | | questsPool.Release(activeQuests[completedQuest.questId]); |
| 0 | 151 | | activeQuests.Remove(completedQuest.questId); |
| | 152 | | } |
| | 153 | |
|
| 0 | 154 | | if (!completedQuests.ContainsKey(completedQuest.questId)) |
| 0 | 155 | | completedQuests.Add(completedQuest.questId, questsPool.Get()); |
| | 156 | |
|
| 0 | 157 | | ActiveQuestComponentView completedQuestComponentView = completedQuests[completedQuest.questId]; |
| 0 | 158 | | completedQuestComponentView.OnActiveQuestSelected -= SelectedCompletedQuest; |
| 0 | 159 | | completedQuestComponentView.SetModel(new ActiveQuestComponentModel() |
| | 160 | | { |
| | 161 | | questId = completedQuest.questId, |
| | 162 | | questCreator = creatorName, |
| | 163 | | questName = completedQuest.questName, |
| | 164 | | questImageUri = completedQuest.questImageUri, |
| | 165 | | isPinned = false, |
| | 166 | | questModel = completedQuest |
| | 167 | | }); |
| 0 | 168 | | completedQuestComponentView.OnActiveQuestSelected += SelectedCompletedQuest; |
| 0 | 169 | | HandleCompletedQuestSelection(completedQuest.questId); |
| 0 | 170 | | sectionSelector.GetSection(IN_PROGRESS_SECTION_INDEX).SelectToggle(true); |
| 0 | 171 | | } |
| | 172 | |
|
| | 173 | | public void RemoveQuestIfExists(string questId) |
| | 174 | | { |
| 0 | 175 | | if (!activeQuests.ContainsKey(questId)) return; |
| | 176 | |
|
| 0 | 177 | | questsPool.Release(activeQuests[questId]); |
| 0 | 178 | | activeQuests.Remove(questId); |
| 0 | 179 | | if (activeQuests.Count == 0) |
| | 180 | | { |
| 0 | 181 | | emptyState.SetActive(true); |
| 0 | 182 | | emptyActiveState.SetActive(true); |
| 0 | 183 | | emptyCompletedState.SetActive(false); |
| | 184 | | } |
| | 185 | | else |
| | 186 | | { |
| 0 | 187 | | HandleActiveQuestSelection(activeQuests.First().Key); |
| | 188 | | } |
| 0 | 189 | | } |
| | 190 | |
|
| | 191 | | public void SetAsFullScreenMenuMode(Transform parentTransform) |
| | 192 | | { |
| 0 | 193 | | if (parentTransform == null) |
| 0 | 194 | | return; |
| | 195 | |
|
| | 196 | | Transform panelTransform; |
| 0 | 197 | | (panelTransform = transform).SetParent(parentTransform); |
| 0 | 198 | | panelTransform.localScale = Vector3.one; |
| | 199 | |
|
| 0 | 200 | | RectTransform rectTransform = panelTransform as RectTransform; |
| 0 | 201 | | if (rectTransform == null) return; |
| 0 | 202 | | rectTransform.anchorMin = Vector2.zero; |
| 0 | 203 | | rectTransform.anchorMax = Vector2.one; |
| 0 | 204 | | rectTransform.pivot = new Vector2(0.5f, 0.5f); |
| 0 | 205 | | rectTransform.localPosition = Vector2.zero; |
| 0 | 206 | | rectTransform.offsetMax = Vector2.zero; |
| 0 | 207 | | rectTransform.offsetMin = Vector2.zero; |
| 0 | 208 | | gameObject.SetActive(true); |
| 0 | 209 | | } |
| | 210 | |
|
| | 211 | | private void HandleActiveQuestSelection(string questId) |
| | 212 | | { |
| 0 | 213 | | if(!string.IsNullOrEmpty(previouslyActiveSelectedQuest)) |
| 0 | 214 | | activeQuests[previouslyActiveSelectedQuest].Deselect(); |
| 0 | 215 | | activeQuests[questId].OnPointerClick(null); |
| 0 | 216 | | previouslyActiveSelectedQuest = questId; |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | private void HandleCompletedQuestSelection(string questId) |
| | 220 | | { |
| 0 | 221 | | if(!string.IsNullOrEmpty(previouslyCompletedSelectedQuest)) |
| 0 | 222 | | completedQuests[previouslyCompletedSelectedQuest].Deselect(); |
| 0 | 223 | | completedQuests[questId].OnPointerClick(null); |
| 0 | 224 | | previouslyCompletedSelectedQuest = questId; |
| 0 | 225 | | } |
| | 226 | |
|
| | 227 | | private void SelectedQuest(QuestDetailsComponentModel questModel) |
| | 228 | | { |
| 0 | 229 | | if(!string.IsNullOrEmpty(previouslyActiveSelectedQuest) && activeQuests.TryGetValue(previouslyActiveSelected |
| 0 | 230 | | quest.Deselect(); |
| | 231 | |
|
| 0 | 232 | | questDetailsComponentView.SetModel(questModel); |
| 0 | 233 | | questDetailsComponentView.SetFooter(true); |
| 0 | 234 | | previouslyActiveSelectedQuest = questModel.questId; |
| 0 | 235 | | } |
| | 236 | |
|
| | 237 | | private void SelectedCompletedQuest(QuestDetailsComponentModel questModel) |
| | 238 | | { |
| 0 | 239 | | if(!string.IsNullOrEmpty(previouslyCompletedSelectedQuest) && completedQuests.TryGetValue(previouslyComplete |
| 0 | 240 | | quest.Deselect(); |
| | 241 | |
|
| 0 | 242 | | questDetailsComponentView.SetFooter(false); |
| 0 | 243 | | questDetailsComponentView.SetModel(questModel); |
| 0 | 244 | | previouslyCompletedSelectedQuest = questModel.questId; |
| 0 | 245 | | } |
| | 246 | |
|
| | 247 | | public void SetIsGuest(bool isGuest) => |
| 0 | 248 | | questDetailsComponentView.SetIsGuest(isGuest); |
| | 249 | |
|
| | 250 | | public override void RefreshControl() |
| | 251 | | { |
| 0 | 252 | | } |
| | 253 | | } |
| | 254 | | } |