| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Providers; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | namespace DCL.Controllers.LoadingScreenV2 |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Controller responsible of managing the hints views and requesting hints from the HintRequestService |
| | 13 | | /// And also responsible of showing the hints in the LoadingScreen using the HintViewManager carousel |
| | 14 | | /// </summary> |
| | 15 | | public class LoadingScreenHintsController |
| | 16 | | { |
| 0 | 17 | | private readonly TimeSpan SHOWING_TIME_HINTS = TimeSpan.FromSeconds(5); |
| 0 | 18 | | private readonly string SOURCE_HINT_ADDRESSABLE = "LoadingScreenV2HintView.prefab"; |
| | 19 | | private const int MAX_HINTS = 15; |
| | 20 | |
|
| | 21 | | private readonly HintRequestService hintRequestService; |
| | 22 | |
|
| | 23 | | internal HintView hintViewPrefab; |
| | 24 | | internal HintViewManager hintViewManager; |
| | 25 | | internal readonly List<HintView> hintViewPool; |
| | 26 | | internal Dictionary<int, Tuple<Hint, Texture2D>> hintsDictionary; |
| | 27 | | internal CancellationTokenSource cancellationTokenSource; |
| | 28 | |
|
| | 29 | | public event Action OnRequestHintsCompleted; |
| | 30 | |
|
| 0 | 31 | | public LoadingScreenHintsController(HintRequestService hintRequestService) |
| | 32 | | { |
| 0 | 33 | | this.hintRequestService = hintRequestService; |
| | 34 | |
|
| 0 | 35 | | hintsDictionary = new Dictionary<int, Tuple<Hint, Texture2D>>(); |
| 0 | 36 | | hintViewPool = new List<HintView>(); |
| | 37 | |
|
| 0 | 38 | | InitializeHintsAsync(); |
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | private async void InitializeHintsAsync() |
| | 42 | | { |
| 0 | 43 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 44 | |
|
| 0 | 45 | | IAddressableResourceProvider addressableProvider = new AddressableResourceProvider(); |
| 0 | 46 | | hintViewPrefab = await addressableProvider.GetAddressable<HintView>(SOURCE_HINT_ADDRESSABLE, cancellationTok |
| | 47 | |
|
| | 48 | | // Initializing empty hints views |
| 0 | 49 | | for (int i = 0; i < MAX_HINTS; i++) |
| | 50 | | { |
| 0 | 51 | | HintView newHintView = Object.Instantiate(hintViewPrefab); |
| 0 | 52 | | newHintView.ToggleHint(false); |
| 0 | 53 | | hintViewPool.Add(newHintView); |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | await RequestHints(cancellationTokenSource.Token); |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <summary> |
| | 60 | | /// Requests hints from the HintRequestService and initializes the hints views with the results |
| | 61 | | /// </summary> |
| | 62 | | /// <param name="ctx"></param> |
| | 63 | | public async UniTask RequestHints(CancellationToken ctx) |
| | 64 | | { |
| 0 | 65 | | hintsDictionary.Clear(); |
| | 66 | |
|
| 0 | 67 | | Dictionary<Hint, Texture2D> hintsResult = await hintRequestService.RequestHintsFromSources(ctx, MAX_HINTS); |
| | 68 | |
|
| 0 | 69 | | int index = 0; |
| 0 | 70 | | var intializedHints = new List<HintView>(); |
| 0 | 71 | | foreach (var hintResult in hintsResult) |
| | 72 | | { |
| 0 | 73 | | var hintTuple = new Tuple<Hint, Texture2D>(hintResult.Key, hintResult.Value); |
| 0 | 74 | | hintsDictionary.Add(index, hintTuple); |
| | 75 | |
|
| 0 | 76 | | if (index < hintViewPool.Count) |
| | 77 | | { |
| 0 | 78 | | hintViewPool[index].Initialize(hintResult.Key, hintResult.Value, index == 0); |
| 0 | 79 | | intializedHints.Add(hintViewPool[index]); |
| | 80 | | } |
| 0 | 81 | | index++; |
| | 82 | | } |
| | 83 | |
|
| 0 | 84 | | hintViewManager = new HintViewManager(intializedHints); |
| | 85 | |
|
| 0 | 86 | | StartHintsCarousel(); |
| 0 | 87 | | OnRequestHintsCompleted?.Invoke(); |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | public void StartHintsCarousel() |
| | 91 | | { |
| 0 | 92 | | hintViewManager.StartCarousel(); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public void StopHintsCarousel() |
| | 96 | | { |
| 0 | 97 | | hintViewManager.StopCarousel(); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | public void CarouselNextHint() |
| | 101 | | { |
| 0 | 102 | | hintViewManager.CarouselNextHint(); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | public void CarouselPreviousHint() |
| | 106 | | { |
| 0 | 107 | | hintViewManager.CarouselPreviousHint(); |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | public void SetHint(int index) |
| | 111 | | { |
| 0 | 112 | | hintViewManager.SetSpecificHint(index); |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public void Dispose() |
| | 116 | | { |
| 0 | 117 | | cancellationTokenSource?.Cancel(); |
| 0 | 118 | | hintViewManager.Dispose(); |
| 0 | 119 | | } |
| | 120 | | } |
| | 121 | | } |