| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.Controllers.LoadingScreenV2 |
| | 8 | | { |
| | 9 | | public class HintViewManager : IHintViewManager |
| | 10 | | { |
| 0 | 11 | | private readonly TimeSpan SHOWING_TIME_HINTS = TimeSpan.FromSeconds(5); |
| | 12 | | private readonly List<HintView> hintViewList; |
| | 13 | |
|
| | 14 | | private CancellationTokenSource cancellationTokenSource; |
| | 15 | |
|
| | 16 | | internal bool isIteratingHints = false; |
| | 17 | | internal int currentHintIndex = 0; |
| | 18 | |
|
| | 19 | | public event Action OnHintChanged; |
| | 20 | |
|
| 0 | 21 | | public HintViewManager(List<HintView> hintViewList) |
| | 22 | | { |
| 0 | 23 | | this.hintViewList = hintViewList; |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public void StartCarousel() |
| | 27 | | { |
| 0 | 28 | | if (isIteratingHints || hintViewList.Count == 0) |
| 0 | 29 | | return; |
| | 30 | |
|
| 0 | 31 | | isIteratingHints = true; |
| 0 | 32 | | ScheduleNextUpdate(CancellationToken.None).Forget(); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | public void StopCarousel() |
| | 36 | | { |
| 0 | 37 | | isIteratingHints = false; |
| 0 | 38 | | cancellationTokenSource?.Cancel(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | public void CarouselNextHint() |
| | 42 | | { |
| 0 | 43 | | if (hintViewList.Count == 0) |
| 0 | 44 | | return; |
| | 45 | |
|
| 0 | 46 | | SetHint((currentHintIndex + 1) % hintViewList.Count); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void CarouselPreviousHint() |
| | 50 | | { |
| 0 | 51 | | if (hintViewList.Count == 0) |
| 0 | 52 | | return; |
| | 53 | |
|
| 0 | 54 | | SetHint((currentHintIndex - 1 + hintViewList.Count) % hintViewList.Count); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public void SetSpecificHint(int index) |
| | 58 | | { |
| 0 | 59 | | if (hintViewList.Count == 0) |
| 0 | 60 | | return; |
| | 61 | |
|
| 0 | 62 | | SetHint(index); |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | private void SetHint(int index) |
| | 66 | | { |
| 0 | 67 | | hintViewList[currentHintIndex].ToggleHint(false); |
| 0 | 68 | | currentHintIndex = index; |
| 0 | 69 | | UpdateHintView(); |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | private async UniTask ScheduleNextUpdate(CancellationToken token) |
| | 73 | | { |
| 0 | 74 | | cancellationTokenSource?.Cancel(); // cancel previous timer |
| 0 | 75 | | cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token); |
| | 76 | |
|
| | 77 | | try |
| | 78 | | { |
| 0 | 79 | | await UniTask.Delay(SHOWING_TIME_HINTS, cancellationToken: cancellationTokenSource.Token); |
| 0 | 80 | | CarouselNextHint(); |
| 0 | 81 | | } |
| 0 | 82 | | catch (OperationCanceledException) |
| | 83 | | { |
| | 84 | | // Operation was cancelled |
| 0 | 85 | | } |
| | 86 | | catch (Exception ex) |
| | 87 | | { |
| 0 | 88 | | Debug.LogException(ex); |
| 0 | 89 | | } |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | private void UpdateHintView() |
| | 93 | | { |
| 0 | 94 | | hintViewList[currentHintIndex].ToggleHint(true); |
| | 95 | |
|
| 0 | 96 | | if (isIteratingHints) |
| | 97 | | { |
| 0 | 98 | | ScheduleNextUpdate(cancellationTokenSource.Token).Forget(); |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | OnHintChanged?.Invoke(); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public void Dispose() |
| | 105 | | { |
| 0 | 106 | | StopCarousel(); |
| 0 | 107 | | cancellationTokenSource?.Cancel(); |
| 0 | 108 | | cancellationTokenSource?.Dispose(); |
| | 109 | |
|
| 0 | 110 | | foreach (var hintView in hintViewList) |
| | 111 | | { |
| 0 | 112 | | DCL.Helpers.Utils.SafeDestroy(hintView.gameObject); |
| | 113 | | } |
| | 114 | |
|
| 0 | 115 | | hintViewList.Clear(); |
| 0 | 116 | | } |
| | 117 | | } |
| | 118 | | } |