| | 1 | | using DCL.Helpers; |
| | 2 | | using MainScripts.DCL.Helpers.Utils; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using TMPro; |
| | 6 | | using UIComponents.Scripts.Components; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.UI; |
| | 9 | |
|
| | 10 | | namespace DCL.Quests |
| | 11 | | { |
| | 12 | | public class QuestDetailsComponentView : BaseComponentView<QuestDetailsComponentModel>, IQuestDetailsComponentView |
| | 13 | | { |
| | 14 | | private const int MAX_REWARDS_COUNT = 5; |
| | 15 | | private const int MAX_STEPS_COUNT = 10; |
| | 16 | | private const string COMPLETED_PANEL = "CompletedQuests"; |
| | 17 | | private const string PROGRESS_PANEL = "InProgressQuests"; |
| | 18 | |
|
| | 19 | | [SerializeField] internal TMP_Text questName; |
| | 20 | | [SerializeField] internal TMP_Text questCreator; |
| | 21 | | [SerializeField] internal TMP_Text questDescription; |
| | 22 | | [SerializeField] internal Transform stepsParent; |
| | 23 | | [SerializeField] internal Transform rewardsParent; |
| | 24 | | [SerializeField] internal Button pinButton; |
| | 25 | | [SerializeField] internal TMP_Text pinButtonText; |
| | 26 | | [SerializeField] internal Button abandonButton; |
| | 27 | | [SerializeField] internal GameObject rewardsSection; |
| | 28 | | [SerializeField] internal GameObject guestSection; |
| | 29 | | [SerializeField] internal GameObject footer; |
| | 30 | | [SerializeField] internal RectTransform parentContent; |
| | 31 | | private VerticalLayoutGroup layoutGroup; |
| | 32 | |
|
| | 33 | | [SerializeField] internal QuestRewardComponentView rewardPrefab; |
| | 34 | | [SerializeField] internal QuestStepComponentView stepPrefab; |
| | 35 | |
|
| | 36 | | public event Action<Vector2Int> OnJumpIn; |
| | 37 | | public event Action<string, bool> OnPinChange; |
| | 38 | | public event Action<string> OnQuestAbandon; |
| | 39 | |
|
| | 40 | | private UnityObjectPool<QuestStepComponentView> stepsPool; |
| | 41 | | private UnityObjectPool<QuestRewardComponentView> rewardsPool; |
| | 42 | |
|
| 12 | 43 | | private readonly List<QuestRewardComponentView> usedRewards = new (); |
| 12 | 44 | | private readonly List<QuestStepComponentView> usedSteps = new (); |
| | 45 | |
|
| | 46 | | public override void Awake() |
| | 47 | | { |
| 11 | 48 | | pinButton.onClick.RemoveAllListeners(); |
| 11 | 49 | | pinButton.onClick.AddListener(() => |
| | 50 | | { |
| 2 | 51 | | model.isPinned = !model.isPinned; |
| 2 | 52 | | pinButtonText.text = model.isPinned ? "PINNED" : "PIN"; |
| 2 | 53 | | OnPinChange?.Invoke(model.questId, model.isPinned); |
| 2 | 54 | | }); |
| 11 | 55 | | abandonButton.onClick.RemoveAllListeners(); |
| 12 | 56 | | abandonButton.onClick.AddListener(() => OnQuestAbandon?.Invoke(model.questId)); |
| 11 | 57 | | InitializePools(); |
| 11 | 58 | | } |
| | 59 | |
|
| | 60 | | public override void Dispose() |
| | 61 | | { |
| 49 | 62 | | foreach (var pooledReward in usedRewards) |
| 3 | 63 | | rewardsPool.Release(pooledReward); |
| 21 | 64 | | rewardsPool.Clear(); |
| | 65 | |
|
| 50 | 66 | | foreach (var pooledSteps in usedSteps) |
| 5 | 67 | | stepsPool.Release(pooledSteps); |
| 19 | 68 | | stepsPool.Clear(); |
| 19 | 69 | | } |
| | 70 | |
|
| | 71 | | public override void RefreshControl() |
| | 72 | | { |
| 0 | 73 | | SetIsPinned(model.isPinned); |
| 0 | 74 | | SetQuestId(model.questId); |
| 0 | 75 | | SetQuestName(model.questName); |
| 0 | 76 | | SetQuestCreator(model.questCreator); |
| 0 | 77 | | SetQuestDescription(model.questDescription); |
| 0 | 78 | | SetCoordinates(model.coordinates); |
| 0 | 79 | | SetQuestSteps(model.questSteps); |
| 0 | 80 | | SetQuestRewards(model.questRewards); |
| | 81 | |
|
| 0 | 82 | | Utils.ForceRebuildLayoutImmediate(parentContent); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public void SetFooter(bool isFooterVisible) |
| | 86 | | { |
| 0 | 87 | | abandonButton.gameObject.SetActive(isFooterVisible); |
| 0 | 88 | | pinButton.gameObject.SetActive(isFooterVisible); |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | public void SetQuestName(string nameText) |
| | 92 | | { |
| 1 | 93 | | model.questName = nameText; |
| 1 | 94 | | questName.text = nameText; |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | public void SetQuestCreator(string creatorText) |
| | 98 | | { |
| 1 | 99 | | model.questCreator = creatorText; |
| 1 | 100 | | questCreator.text = creatorText; |
| 1 | 101 | | } |
| | 102 | |
|
| | 103 | | public void SetQuestDescription(string description) |
| | 104 | | { |
| 1 | 105 | | model.questDescription = description; |
| 1 | 106 | | questDescription.text = description; |
| 1 | 107 | | } |
| | 108 | |
|
| | 109 | | public void SetQuestId(string questId) => |
| 3 | 110 | | model.questId = questId; |
| | 111 | |
|
| | 112 | | public void SetCoordinates(Vector2Int coordinates) => |
| 0 | 113 | | model.coordinates = coordinates; |
| | 114 | |
|
| | 115 | | public void SetIsPinned(bool isPinned) |
| | 116 | | { |
| 2 | 117 | | model.isPinned = isPinned; |
| 2 | 118 | | pinButtonText.text = isPinned ? "PINNED" : "PIN"; |
| 2 | 119 | | } |
| | 120 | |
|
| | 121 | | public void SetQuestSteps(List<QuestStepComponentModel> questSteps) |
| | 122 | | { |
| 2 | 123 | | model.questSteps = questSteps; |
| | 124 | |
|
| 4 | 125 | | foreach (var pooledStep in usedSteps) |
| 0 | 126 | | stepsPool.Release(pooledStep); |
| | 127 | |
|
| 2 | 128 | | usedSteps.Clear(); |
| | 129 | |
|
| 10 | 130 | | foreach (var stepModel in questSteps) |
| | 131 | | { |
| 3 | 132 | | QuestStepComponentView pooledStep = stepsPool.Get(); |
| 3 | 133 | | pooledStep.OnJumpIn -= InvokeJumpIn; |
| 3 | 134 | | pooledStep.OnJumpIn += InvokeJumpIn; |
| 3 | 135 | | pooledStep.SetModel(stepModel); |
| 3 | 136 | | usedSteps.Add(pooledStep); |
| | 137 | | } |
| 2 | 138 | | Utils.ForceRebuildLayoutImmediate(parentContent); |
| 2 | 139 | | } |
| | 140 | |
|
| | 141 | | public void SetQuestRewards(List<QuestRewardComponentModel> questRewards) |
| | 142 | | { |
| 1 | 143 | | model.questRewards = questRewards; |
| | 144 | |
|
| 1 | 145 | | if (questRewards == null || questRewards.Count == 0) |
| | 146 | | { |
| 0 | 147 | | rewardsSection.SetActive(false); |
| 0 | 148 | | return; |
| | 149 | | } |
| | 150 | |
|
| 1 | 151 | | rewardsSection.SetActive(true); |
| | 152 | |
|
| 2 | 153 | | foreach (var pooledReward in usedRewards) |
| 0 | 154 | | rewardsPool.Release(pooledReward); |
| | 155 | |
|
| 1 | 156 | | usedRewards.Clear(); |
| | 157 | |
|
| 6 | 158 | | foreach (var rewardModel in questRewards) |
| | 159 | | { |
| 2 | 160 | | QuestRewardComponentView pooledReward = rewardsPool.Get(); |
| 2 | 161 | | pooledReward.SetModel(rewardModel); |
| 2 | 162 | | usedRewards.Add(pooledReward); |
| | 163 | | } |
| 1 | 164 | | Utils.ForceRebuildLayoutImmediate(parentContent); |
| 1 | 165 | | } |
| | 166 | |
|
| | 167 | | public void SetIsGuest(bool isGuest) => |
| 2 | 168 | | guestSection.SetActive(isGuest); |
| | 169 | |
|
| | 170 | | public void SetPanel(string panelName) |
| | 171 | | { |
| 0 | 172 | | footer.SetActive(panelName.Equals(PROGRESS_PANEL)); |
| 0 | 173 | | footer.SetActive(panelName.Equals(COMPLETED_PANEL)); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | private void InvokeJumpIn(Vector2Int coordinates) => |
| 1 | 177 | | OnJumpIn?.Invoke(coordinates); |
| | 178 | |
|
| | 179 | | private void InitializePools() |
| | 180 | | { |
| 11 | 181 | | rewardsPool = new UnityObjectPool<QuestRewardComponentView>(rewardPrefab, rewardsParent); |
| 11 | 182 | | rewardsPool.Prewarm(MAX_REWARDS_COUNT); |
| 11 | 183 | | stepsPool = new UnityObjectPool<QuestStepComponentView>(stepPrefab, stepsParent); |
| 11 | 184 | | stepsPool.Prewarm(MAX_STEPS_COUNT); |
| 11 | 185 | | } |
| | 186 | | } |
| | 187 | | } |