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