| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using DCLPlugins.DebugPlugins.Commons; |
| | 6 | | using UnityEngine; |
| | 7 | | using Object = UnityEngine.Object; |
| | 8 | |
|
| | 9 | | internal class SceneEntitiesTracker : ISceneListener |
| | 10 | | { |
| | 11 | | internal const string WIREFRAME_GAMEOBJECT_NAME = "ShapeBoundingBoxWireframe"; |
| | 12 | | private const string WIREFRAME_PREFAB_NAME = "Prefabs/WireframeCubeMesh"; |
| | 13 | |
|
| 10 | 14 | | private readonly Dictionary<IDCLEntity, WatchEntityShapeHandler> entityShapeHandler = new Dictionary<IDCLEntity, Wat |
| | 15 | | private GameObject wireframeOriginal; |
| | 16 | | private Material wireframeMaterial; |
| | 17 | |
|
| | 18 | | void IDisposable.Dispose() |
| | 19 | | { |
| 42 | 20 | | foreach (var handler in entityShapeHandler.Values) |
| | 21 | | { |
| 11 | 22 | | handler.Dispose(); |
| | 23 | | } |
| | 24 | |
|
| 10 | 25 | | entityShapeHandler.Clear(); |
| 10 | 26 | | DestroyWireframeOriginal(); |
| 10 | 27 | | } |
| | 28 | |
|
| | 29 | | void ISceneListener.OnEntityAdded(IDCLEntity entity) |
| | 30 | | { |
| 16 | 31 | | WatchEntityShape(entity); |
| 16 | 32 | | } |
| | 33 | |
|
| | 34 | | void ISceneListener.OnEntityRemoved(IDCLEntity entity) |
| | 35 | | { |
| 5 | 36 | | KillWatchEntityShape(entity); |
| 5 | 37 | | } |
| | 38 | |
|
| | 39 | | private void WatchEntityShape(IDCLEntity entity) |
| | 40 | | { |
| 16 | 41 | | if (entityShapeHandler.ContainsKey(entity)) |
| | 42 | | { |
| 0 | 43 | | return; |
| | 44 | | } |
| 16 | 45 | | entityShapeHandler.Add(entity, new WatchEntityShapeHandler(entity, new EntityWireframe(GetWireframeOriginal()))) |
| 16 | 46 | | } |
| | 47 | |
|
| | 48 | | private void KillWatchEntityShape(IDCLEntity entity) |
| | 49 | | { |
| 5 | 50 | | if (entityShapeHandler.TryGetValue(entity, out WatchEntityShapeHandler entityWatchHandler)) |
| | 51 | | { |
| 5 | 52 | | entityWatchHandler.Dispose(); |
| 5 | 53 | | entityShapeHandler.Remove(entity); |
| | 54 | | } |
| | 55 | |
|
| 5 | 56 | | if (entityShapeHandler.Count == 0) |
| | 57 | | { |
| 2 | 58 | | DestroyWireframeOriginal(); |
| | 59 | | } |
| 5 | 60 | | } |
| | 61 | |
|
| | 62 | | private GameObject GetWireframeOriginal() |
| | 63 | | { |
| 16 | 64 | | if (wireframeOriginal != null) |
| | 65 | | { |
| 9 | 66 | | return wireframeOriginal; |
| | 67 | | } |
| 7 | 68 | | wireframeOriginal = Object.Instantiate(Resources.Load<GameObject>(WIREFRAME_PREFAB_NAME)); |
| 7 | 69 | | wireframeOriginal.name = WIREFRAME_GAMEOBJECT_NAME; |
| 7 | 70 | | wireframeMaterial = wireframeOriginal.GetComponent<Renderer>().material; |
| 7 | 71 | | wireframeMaterial.SetColor(ShaderUtils.EmissionColor, Color.grey); |
| 7 | 72 | | wireframeOriginal.SetActive(false); |
| 7 | 73 | | return wireframeOriginal; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | private void DestroyWireframeOriginal() |
| | 77 | | { |
| 12 | 78 | | if (wireframeOriginal == null) |
| | 79 | | { |
| 4 | 80 | | return; |
| | 81 | | } |
| 8 | 82 | | Object.Destroy(wireframeOriginal); |
| 8 | 83 | | Object.Destroy(wireframeMaterial); |
| 8 | 84 | | } |
| | 85 | | } |