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