| | 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 | | internal InputAction_Trigger toggleQuestsPanel; |
| | 16 | |
|
| | 17 | | public void Initialize(IQuestsController newQuestsController) |
| | 18 | | { |
| 12 | 19 | | questsController = newQuestsController; |
| 12 | 20 | | view = CreateView(); |
| 12 | 21 | | SetViewActive(false); |
| | 22 | |
|
| 12 | 23 | | toggleQuestsPanel = Resources.Load<InputAction_Trigger>("ToggleQuestsPanelHud"); |
| 12 | 24 | | toggleQuestsPanel.OnTriggered += OnToggleActionTriggered; |
| | 25 | |
|
| 12 | 26 | | questsController.OnQuestUpdated += OnQuestUpdated; |
| 12 | 27 | | quests.OnAdded += OnQuestAdded; |
| 12 | 28 | | quests.OnRemoved += OnQuestRemoved; |
| 12 | 29 | | quests.OnSet += OnQuestSet; |
| | 30 | |
|
| 12 | 31 | | DataStore.i.HUDs.questsPanelVisible.OnChange -= OnQuestPanelVisibleChanged; |
| 12 | 32 | | DataStore.i.HUDs.questsPanelVisible.OnChange += OnQuestPanelVisibleChanged; |
| | 33 | |
|
| 12 | 34 | | OnQuestSet(quests.Get()); |
| 12 | 35 | | } |
| | 36 | |
|
| 44 | 37 | | private void OnQuestPanelVisibleChanged(bool current, bool previous) { SetViewActive(current); } |
| | 38 | |
|
| | 39 | | private void OnToggleActionTriggered(DCLAction_Trigger action) |
| | 40 | | { |
| 21 | 41 | | bool value = !DataStore.i.HUDs.questsPanelVisible.Get(); |
| 21 | 42 | | QuestsUIAnalytics.SendQuestLogVisibiltyChanged(value, "input_action"); |
| 21 | 43 | | SetVisibility(value); |
| 21 | 44 | | } |
| | 45 | |
|
| | 46 | | private void OnQuestUpdated(string questId, bool hasProgress) |
| | 47 | | { |
| 4 | 48 | | if (!quests.TryGetValue(questId, out QuestModel model) || model.status == QuestsLiterals.Status.BLOCKED || ( |
| | 49 | | { |
| 3 | 50 | | view.RemoveQuest(questId); |
| 3 | 51 | | return; |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | view.RequestAddOrUpdateQuest(questId); |
| 1 | 55 | | } |
| | 56 | |
|
| | 57 | | private void OnQuestAdded(string questId, QuestModel questModel) |
| | 58 | | { |
| 15 | 59 | | if (questModel.status == QuestsLiterals.Status.BLOCKED || (questModel.visibility == QuestsLiterals.Visibilit |
| 3 | 60 | | return; |
| 12 | 61 | | view.RequestAddOrUpdateQuest(questId); |
| 12 | 62 | | } |
| | 63 | |
|
| 2 | 64 | | private void OnQuestRemoved(string questId, QuestModel questModel) { view.RemoveQuest(questId); } |
| | 65 | |
|
| | 66 | | private void OnQuestSet(IEnumerable<KeyValuePair<string, QuestModel>> quests) |
| | 67 | | { |
| 15 | 68 | | view.ClearQuests(); |
| 60 | 69 | | foreach ((string key, QuestModel value) in quests) |
| | 70 | | { |
| 15 | 71 | | OnQuestAdded(key, value); |
| | 72 | | } |
| 15 | 73 | | } |
| | 74 | |
|
| | 75 | | public void SetVisibility(bool visible) |
| | 76 | | { |
| 22 | 77 | | if ( CommonScriptableObjects.rendererState.Get() ) |
| | 78 | | { |
| 22 | 79 | | if (visible) |
| 12 | 80 | | Utils.UnlockCursor(); |
| | 81 | | else |
| 10 | 82 | | Utils.LockCursor(); |
| | 83 | | } |
| | 84 | |
|
| 22 | 85 | | DataStore.i.HUDs.questsPanelVisible.Set(visible); |
| 22 | 86 | | } |
| | 87 | |
|
| 68 | 88 | | private void SetViewActive(bool visible) { view?.SetVisibility(visible); } |
| | 89 | |
|
| 1 | 90 | | internal virtual IQuestsPanelHUDView CreateView() => QuestsPanelHUDView.Create(); |
| | 91 | |
|
| | 92 | | public void Dispose() |
| | 93 | | { |
| 1 | 94 | | view.Dispose(); |
| 1 | 95 | | toggleQuestsPanel.OnTriggered -= OnToggleActionTriggered; |
| 1 | 96 | | if (questsController != null) |
| 1 | 97 | | questsController.OnQuestUpdated -= OnQuestUpdated; |
| 1 | 98 | | quests.OnAdded -= OnQuestAdded; |
| 1 | 99 | | quests.OnRemoved -= OnQuestRemoved; |
| 1 | 100 | | quests.OnSet -= OnQuestSet; |
| 1 | 101 | | DataStore.i.HUDs.questsPanelVisible.OnChange -= OnQuestPanelVisibleChanged; |
| 1 | 102 | | } |
| | 103 | | } |
| | 104 | | } |