| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Components.Interfaces; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCLPlugins.UIRefresherPlugin |
| | 8 | | { |
| | 9 | | public class UIRefresherController : IDisposable |
| | 10 | | { |
| | 11 | | private const float DIRTY_WATCHER_UPDATE_BUDGET = 2/1000f; |
| | 12 | |
|
| | 13 | | private readonly IUpdateEventHandler eventHandler; |
| | 14 | | private readonly StringVariable sceneID; |
| | 15 | | private readonly BaseVariable<Dictionary<string, Queue<IUIRefreshable>>> dirtyShapes; |
| | 16 | |
|
| 51 | 17 | | public UIRefresherController( IUpdateEventHandler updateEventHandler, |
| | 18 | | StringVariable sceneIDVariable, |
| | 19 | | BaseVariable<Dictionary<string, Queue<IUIRefreshable>>> dirtyShapesVariable) |
| | 20 | | { |
| 51 | 21 | | eventHandler = updateEventHandler; |
| 51 | 22 | | sceneID = sceneIDVariable; |
| 51 | 23 | | dirtyShapes = dirtyShapesVariable; |
| 51 | 24 | | eventHandler.AddListener(IUpdateEventHandler.EventType.LateUpdate, OnLateUpdate); |
| 51 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Dispose() |
| | 28 | | { |
| 51 | 29 | | eventHandler.RemoveListener(IUpdateEventHandler.EventType.LateUpdate, OnLateUpdate); |
| 51 | 30 | | } |
| | 31 | |
|
| | 32 | | private void OnLateUpdate() |
| | 33 | | { |
| 997 | 34 | | var currentScene = sceneID.Get(); |
| | 35 | |
|
| 997 | 36 | | if (string.IsNullOrEmpty(currentScene)) |
| 645 | 37 | | currentScene = "default"; |
| | 38 | |
|
| 997 | 39 | | var dirtyShapesByScene = dirtyShapes.Get(); |
| 997 | 40 | | var startTime = Time.realtimeSinceStartup; |
| | 41 | |
|
| | 42 | | // prioritize current scene |
| 997 | 43 | | if (dirtyShapesByScene.ContainsKey(currentScene)) |
| | 44 | | { |
| 60 | 45 | | var queue = dirtyShapesByScene[sceneID]; |
| | 46 | |
|
| 102 | 47 | | while (queue.Count > 0) |
| | 48 | | { |
| 42 | 49 | | var uiShape = queue.Dequeue(); |
| 42 | 50 | | uiShape.Refresh(); |
| | 51 | |
|
| 42 | 52 | | if (!CanRefreshMore(startTime)) return; |
| | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| | 56 | | // update other scenes, this prevents hiccups when entering scenes with huge UIs that already loaded them |
| 2276 | 57 | | foreach (KeyValuePair<string,Queue<IUIRefreshable>> valuePair in dirtyShapesByScene) |
| | 58 | | { |
| 141 | 59 | | if (valuePair.Key == currentScene) continue; |
| | 60 | |
|
| 81 | 61 | | var queue = valuePair.Value; |
| | 62 | |
|
| 94 | 63 | | while (queue.Count > 0) |
| | 64 | | { |
| 13 | 65 | | var uiShape = queue.Dequeue(); |
| 13 | 66 | | uiShape.Refresh(); |
| | 67 | |
|
| 13 | 68 | | if (!CanRefreshMore(startTime)) return; |
| | 69 | | } |
| | 70 | | } |
| 997 | 71 | | } |
| | 72 | | private static bool CanRefreshMore(float startTime) |
| | 73 | | { |
| 110 | 74 | | if (Application.isBatchMode) return true; |
| 0 | 75 | | return Time.realtimeSinceStartup - startTime < DIRTY_WATCHER_UPDATE_BUDGET; |
| | 76 | | } |
| | 77 | | } |
| | 78 | | } |