| | | 1 | | using System; |
| | | 2 | | using DCL.Helpers; |
| | | 3 | | using DCL.QuestsController; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using UnityEngine; |
| | | 6 | | |
| | | 7 | | namespace DCL.Huds.QuestsPanel |
| | | 8 | | { |
| | | 9 | | public class QuestsPanelHUDController : IHUD |
| | | 10 | | { |
| | | 11 | | internal IQuestsPanelHUDView view; |
| | | 12 | | internal IQuestsController questsController; |
| | 0 | 13 | | private static BaseDictionary<string, QuestModel> quests => DataStore.i.Quests.quests; |
| | | 14 | | |
| | | 15 | | public void Initialize(IQuestsController newQuestsController) |
| | | 16 | | { |
| | 10 | 17 | | questsController = newQuestsController; |
| | 10 | 18 | | view = CreateView(); |
| | 10 | 19 | | SetViewActive(false); |
| | | 20 | | |
| | 10 | 21 | | questsController.OnQuestUpdated += OnQuestUpdated; |
| | 10 | 22 | | quests.OnAdded += OnQuestAdded; |
| | 10 | 23 | | quests.OnRemoved += OnQuestRemoved; |
| | 10 | 24 | | quests.OnSet += OnQuestSet; |
| | | 25 | | |
| | 10 | 26 | | DataStore.i.HUDs.questsPanelVisible.OnChange -= OnQuestPanelVisibleChanged; |
| | 10 | 27 | | DataStore.i.HUDs.questsPanelVisible.OnChange += OnQuestPanelVisibleChanged; |
| | | 28 | | |
| | 10 | 29 | | OnQuestSet(quests.Get()); |
| | 10 | 30 | | } |
| | | 31 | | |
| | 2 | 32 | | private void OnQuestPanelVisibleChanged(bool current, bool previous) { SetViewActive(current); } |
| | | 33 | | |
| | | 34 | | private void OnQuestUpdated(string questId, bool hasProgress) |
| | | 35 | | { |
| | 4 | 36 | | if (!quests.TryGetValue(questId, out QuestModel model) || model.status == QuestsLiterals.Status.BLOCKED || ( |
| | | 37 | | { |
| | 3 | 38 | | view.RemoveQuest(questId); |
| | 3 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | 1 | 42 | | view.RequestAddOrUpdateQuest(questId); |
| | 1 | 43 | | } |
| | | 44 | | |
| | | 45 | | private void OnQuestAdded(string questId, QuestModel questModel) |
| | | 46 | | { |
| | 13 | 47 | | if (questModel.status == QuestsLiterals.Status.BLOCKED || (questModel.visibility == QuestsLiterals.Visibilit |
| | 3 | 48 | | return; |
| | 10 | 49 | | view.RequestAddOrUpdateQuest(questId); |
| | 10 | 50 | | } |
| | | 51 | | |
| | 2 | 52 | | private void OnQuestRemoved(string questId, QuestModel questModel) { view.RemoveQuest(questId); } |
| | | 53 | | |
| | | 54 | | private void OnQuestSet(IEnumerable<KeyValuePair<string, QuestModel>> quests) |
| | | 55 | | { |
| | 13 | 56 | | view.ClearQuests(); |
| | 52 | 57 | | foreach ((string key, QuestModel value) in quests) |
| | | 58 | | { |
| | 13 | 59 | | OnQuestAdded(key, value); |
| | | 60 | | } |
| | 13 | 61 | | } |
| | | 62 | | |
| | 2 | 63 | | public void SetVisibility(bool visible) { DataStore.i.HUDs.questsPanelVisible.Set(visible); } |
| | | 64 | | |
| | | 65 | | private void SetViewActive(bool visible) |
| | | 66 | | { |
| | 11 | 67 | | if ( CommonScriptableObjects.rendererState.Get() ) |
| | | 68 | | { |
| | 11 | 69 | | if (visible) |
| | 1 | 70 | | Utils.UnlockCursor(); |
| | | 71 | | else |
| | 10 | 72 | | Utils.LockCursor(); |
| | | 73 | | } |
| | 11 | 74 | | view?.SetVisibility(visible); |
| | 11 | 75 | | } |
| | | 76 | | |
| | 1 | 77 | | internal virtual IQuestsPanelHUDView CreateView() => QuestsPanelHUDView.Create(); |
| | | 78 | | |
| | | 79 | | public void Dispose() |
| | | 80 | | { |
| | 1 | 81 | | view.Dispose(); |
| | 1 | 82 | | if (questsController != null) |
| | 1 | 83 | | questsController.OnQuestUpdated -= OnQuestUpdated; |
| | 1 | 84 | | quests.OnAdded -= OnQuestAdded; |
| | 1 | 85 | | quests.OnRemoved -= OnQuestRemoved; |
| | 1 | 86 | | quests.OnSet -= OnQuestSet; |
| | 1 | 87 | | DataStore.i.HUDs.questsPanelVisible.OnChange -= OnQuestPanelVisibleChanged; |
| | 1 | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |