| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using DCL; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCLPlugins.DebugPlugins.Commons; |
| | 6 | |
|
| | 7 | | public class DebugShapesBoundingBoxDisplayer : IPlugin |
| | 8 | | { |
| | 9 | | private readonly IBaseDictionary<string, bool> isBoundingBoxEnabledForScene; |
| | 10 | | private readonly IWorldState worldState; |
| | 11 | | private readonly ISceneController sceneController; |
| 10 | 12 | | internal readonly Dictionary<string, WatchSceneHandler> scenesWatcher = new Dictionary<string, WatchSceneHandler>(); |
| 10 | 13 | | internal readonly List<string> pendingScenesId = new List<string>(); |
| | 14 | |
|
| 0 | 15 | | public DebugShapesBoundingBoxDisplayer() : this( |
| | 16 | | DataStore.i.debugConfig.showSceneBoundingBoxes, |
| | 17 | | Environment.i.world.state, Environment.i.world.sceneController) |
| | 18 | | { |
| 0 | 19 | | } |
| | 20 | |
|
| 10 | 21 | | internal DebugShapesBoundingBoxDisplayer( |
| | 22 | | IBaseDictionary<string, bool> isBoundingBoxEnabledVariable, |
| | 23 | | IWorldState state, ISceneController sceneController) |
| | 24 | | { |
| 10 | 25 | | this.isBoundingBoxEnabledForScene = isBoundingBoxEnabledVariable; |
| 10 | 26 | | this.worldState = state; |
| 10 | 27 | | this.sceneController = sceneController; |
| | 28 | |
|
| | 29 | | // NOTE: we search for scenes that might be added to the variable previous to this class instantiation |
| 10 | 30 | | using (var iterator = isBoundingBoxEnabledVariable.Get().GetEnumerator()) |
| | 31 | | { |
| 20 | 32 | | while (iterator.MoveNext() && iterator.Current.Value) |
| | 33 | | { |
| 10 | 34 | | IsBoundingBoxEnabledVariableOnOnAdded(iterator.Current.Key, iterator.Current.Value); |
| | 35 | | } |
| 10 | 36 | | } |
| | 37 | |
|
| 10 | 38 | | isBoundingBoxEnabledVariable.OnAdded += IsBoundingBoxEnabledVariableOnOnAdded; |
| 10 | 39 | | isBoundingBoxEnabledVariable.OnRemoved += IsBoundingBoxEnabledVariableOnOnRemoved; |
| 10 | 40 | | sceneController.OnNewSceneAdded += SceneControllerOnOnNewSceneAdded; |
| 10 | 41 | | } |
| | 42 | |
|
| | 43 | | public void Dispose() |
| | 44 | | { |
| 10 | 45 | | isBoundingBoxEnabledForScene.OnAdded -= IsBoundingBoxEnabledVariableOnOnAdded; |
| 10 | 46 | | isBoundingBoxEnabledForScene.OnRemoved -= IsBoundingBoxEnabledVariableOnOnRemoved; |
| 10 | 47 | | sceneController.OnNewSceneAdded -= SceneControllerOnOnNewSceneAdded; |
| | 48 | |
|
| 10 | 49 | | var scenesId = scenesWatcher.Keys.ToArray(); |
| 36 | 50 | | for (int i = 0; i < scenesId.Length; i++) |
| | 51 | | { |
| 8 | 52 | | KillWatchScene(scenesId[i]); |
| | 53 | | } |
| 10 | 54 | | } |
| | 55 | |
|
| | 56 | | private void KillWatchScene(string sceneId) |
| | 57 | | { |
| 10 | 58 | | if (!scenesWatcher.TryGetValue(sceneId, out WatchSceneHandler watchHandler)) |
| 0 | 59 | | return; |
| | 60 | |
|
| 10 | 61 | | watchHandler?.Dispose(); |
| 10 | 62 | | scenesWatcher.Remove(sceneId); |
| 10 | 63 | | } |
| | 64 | |
|
| | 65 | | private void WatchScene(string sceneId) |
| | 66 | | { |
| | 67 | | // NOTE: in case scene is not loaded yet, we add it to the "pending" list |
| 10 | 68 | | if (!worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene)) |
| | 69 | | { |
| 1 | 70 | | if (!pendingScenesId.Contains(sceneId)) |
| | 71 | | { |
| 1 | 72 | | pendingScenesId.Add(sceneId); |
| | 73 | | } |
| | 74 | |
|
| 1 | 75 | | return; |
| | 76 | | } |
| | 77 | |
|
| 9 | 78 | | WatchScene(scene); |
| 9 | 79 | | } |
| | 80 | |
|
| | 81 | | private void WatchScene(IParcelScene scene) |
| | 82 | | { |
| 10 | 83 | | if (scenesWatcher.TryGetValue(scene.sceneData.id, out WatchSceneHandler watchHandler)) |
| | 84 | | { |
| 0 | 85 | | watchHandler?.Dispose(); |
| | 86 | | } |
| | 87 | |
|
| 10 | 88 | | scenesWatcher[scene.sceneData.id] = new WatchSceneHandler(scene, new SceneEntitiesTracker()); |
| 10 | 89 | | } |
| | 90 | |
|
| | 91 | | private void IsBoundingBoxEnabledVariableOnOnRemoved(string sceneId, bool enabled) |
| | 92 | | { |
| 0 | 93 | | KillWatchScene(sceneId); |
| 0 | 94 | | } |
| | 95 | |
|
| | 96 | | private void IsBoundingBoxEnabledVariableOnOnAdded(string sceneId, bool enabled) |
| | 97 | | { |
| 12 | 98 | | if (enabled) |
| | 99 | | { |
| 10 | 100 | | WatchScene(sceneId); |
| 10 | 101 | | } |
| | 102 | | else |
| | 103 | | { |
| 2 | 104 | | KillWatchScene(sceneId); |
| | 105 | | } |
| 2 | 106 | | } |
| | 107 | |
|
| | 108 | | private void SceneControllerOnOnNewSceneAdded(IParcelScene scene) |
| | 109 | | { |
| 1 | 110 | | if (pendingScenesId.Remove(scene.sceneData.id)) |
| | 111 | | { |
| 1 | 112 | | WatchScene(scene); |
| | 113 | | } |
| 1 | 114 | | } |
| | 115 | | } |