| | 1 | | using System; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using DCL.Builder; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Rendering.Universal; |
| | 8 | |
|
| | 9 | | public class BIWOutlinerController : BIWController, IBIWOutlinerController |
| | 10 | | { |
| | 11 | | internal const int OUTLINER_OPTIMIZATION_TIMES = 10; |
| | 12 | | private const int BUILDER_RENDERER_INDEX = 1; |
| | 13 | |
|
| | 14 | | private Material cameraOutlinerMaterial; |
| | 15 | |
|
| | 16 | | private IBIWRaycastController raycastController; |
| | 17 | |
|
| 6 | 18 | | private readonly List<BIWEntity> entitiesOutlined = new List<BIWEntity>(); |
| | 19 | | private int outlinerOptimizationCounter = 0; |
| 6 | 20 | | private bool isOutlineCheckActive = true; |
| | 21 | |
|
| | 22 | | public override void Initialize(IContext context) |
| | 23 | | { |
| 6 | 24 | | base.Initialize(context); |
| 6 | 25 | | cameraOutlinerMaterial = context.projectReferencesAsset.cameraOutlinerMaterial; |
| | 26 | |
|
| 6 | 27 | | raycastController = context.editorContext.raycastController; |
| 6 | 28 | | } |
| | 29 | |
|
| | 30 | | public override void EnterEditMode(IParcelScene scene) |
| | 31 | | { |
| 7 | 32 | | base.EnterEditMode(scene); |
| 7 | 33 | | ActivateBuilderInWorldCamera(); |
| 7 | 34 | | } |
| | 35 | |
|
| | 36 | | public override void ExitEditMode() |
| | 37 | | { |
| 1 | 38 | | base.ExitEditMode(); |
| 1 | 39 | | DeactivateBuilderInWorldCamera(); |
| 1 | 40 | | } |
| | 41 | |
|
| | 42 | | public override void Dispose() |
| | 43 | | { |
| 6 | 44 | | base.Dispose(); |
| 6 | 45 | | RemoveBuilderInWorldCamera(); |
| 6 | 46 | | } |
| | 47 | |
|
| 0 | 48 | | public void SetOutlineCheckActive(bool isActive) { isOutlineCheckActive = isActive; } |
| | 49 | |
|
| | 50 | | public void CheckOutline() |
| | 51 | | { |
| 11 | 52 | | if (outlinerOptimizationCounter >= OUTLINER_OPTIMIZATION_TIMES && isOutlineCheckActive) |
| | 53 | | { |
| 1 | 54 | | if (!BIWUtils.IsPointerOverUIElement() && !BIWUtils.IsPointerOverMaskElement(BIWSettings.GIZMOS_LAYER)) |
| | 55 | | { |
| 1 | 56 | | BIWEntity entity = raycastController.GetEntityOnPointer(); |
| 1 | 57 | | RemoveEntitiesOutlineOutsidePointerOrUnselected(); |
| | 58 | |
|
| 1 | 59 | | if (entity != null && !entity.isSelected) |
| 0 | 60 | | OutlineEntity(entity); |
| 0 | 61 | | } |
| | 62 | | else |
| | 63 | | { |
| 0 | 64 | | CancelUnselectedOutlines(); |
| | 65 | | } |
| | 66 | |
|
| 1 | 67 | | outlinerOptimizationCounter = 0; |
| 1 | 68 | | } |
| | 69 | | else |
| 10 | 70 | | outlinerOptimizationCounter++; |
| 10 | 71 | | } |
| | 72 | |
|
| 4 | 73 | | public bool IsEntityOutlined(BIWEntity entity) { return entitiesOutlined.Contains(entity); } |
| | 74 | |
|
| | 75 | | public void OutlineEntities(List<BIWEntity> entitiesToEdit) |
| | 76 | | { |
| 0 | 77 | | foreach (BIWEntity entityToEdit in entitiesToEdit) |
| | 78 | | { |
| 0 | 79 | | OutlineEntity(entityToEdit); |
| | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public void OutlineEntity(BIWEntity entity) |
| | 84 | | { |
| 4 | 85 | | if (entity.rootEntity.meshRootGameObject == null) |
| 0 | 86 | | return; |
| | 87 | |
|
| 4 | 88 | | if (!entity.rootEntity.meshRootGameObject && entity.rootEntity.renderers.Length <= 0) |
| 0 | 89 | | return; |
| | 90 | |
|
| 4 | 91 | | if (entitiesOutlined.Contains(entity)) |
| 0 | 92 | | return; |
| | 93 | |
|
| 4 | 94 | | if (entity.isLocked) |
| 1 | 95 | | return; |
| | 96 | |
|
| 3 | 97 | | entitiesOutlined.Add(entity); |
| | 98 | |
|
| 12 | 99 | | for (int i = 0; i < entity.rootEntity.meshesInfo.renderers.Length; i++) |
| | 100 | | { |
| 3 | 101 | | if ( entity.rootEntity.meshesInfo.renderers[i] == null) |
| | 102 | | continue; |
| 3 | 103 | | entity.rootEntity.meshesInfo.renderers[i].gameObject.layer = BIWSettings.SELECTION_LAYER_INDEX; |
| | 104 | | } |
| 3 | 105 | | } |
| | 106 | |
|
| | 107 | | public void CancelUnselectedOutlines() |
| | 108 | | { |
| 0 | 109 | | for (int i = 0; i < entitiesOutlined.Count; i++) |
| | 110 | | { |
| 0 | 111 | | if (!entitiesOutlined[i].isSelected) |
| | 112 | | { |
| 0 | 113 | | CancelEntityOutline(entitiesOutlined[i]); |
| | 114 | | } |
| | 115 | | } |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public void RemoveEntitiesOutlineOutsidePointerOrUnselected() |
| | 119 | | { |
| 1 | 120 | | var entity = raycastController.GetEntityOnPointer(); |
| 4 | 121 | | for (int i = 0; i < entitiesOutlined.Count; i++) |
| | 122 | | { |
| 1 | 123 | | if (!entitiesOutlined[i].isSelected || entity != entitiesOutlined[i]) |
| 1 | 124 | | CancelEntityOutline(entitiesOutlined[i]); |
| | 125 | | } |
| 1 | 126 | | } |
| | 127 | |
|
| | 128 | | public void CancelAllOutlines() |
| | 129 | | { |
| 0 | 130 | | for (int i = 0; i < entitiesOutlined.Count; i++) |
| | 131 | | { |
| 0 | 132 | | CancelEntityOutline(entitiesOutlined[i]); |
| | 133 | | } |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public void CancelEntityOutline(BIWEntity entityToQuitOutline) |
| | 137 | | { |
| 3 | 138 | | if (!entitiesOutlined.Contains(entityToQuitOutline)) |
| 0 | 139 | | return; |
| | 140 | |
|
| 3 | 141 | | if (entityToQuitOutline.rootEntity.meshRootGameObject && entityToQuitOutline.rootEntity.meshesInfo.renderers.Len |
| | 142 | | { |
| 12 | 143 | | for (int x = 0; x < entityToQuitOutline.rootEntity.meshesInfo.renderers.Length; x++) |
| | 144 | | { |
| 3 | 145 | | if ( entityToQuitOutline.rootEntity.meshesInfo.renderers[x] == null) |
| | 146 | | continue; |
| 3 | 147 | | entityToQuitOutline.rootEntity.meshesInfo.renderers[x].gameObject.layer = BIWSettings.DEFAULT_LAYER_INDE |
| | 148 | | } |
| | 149 | | } |
| | 150 | |
|
| 3 | 151 | | entitiesOutlined.Remove(entityToQuitOutline); |
| 3 | 152 | | } |
| | 153 | |
|
| | 154 | | private void ActivateBuilderInWorldCamera() |
| | 155 | | { |
| 7 | 156 | | Camera camera = Camera.main; |
| 7 | 157 | | BIWOutline outliner = camera.GetComponent<BIWOutline>(); |
| 7 | 158 | | if (outliner == null) |
| | 159 | | { |
| 6 | 160 | | outliner = camera.gameObject.AddComponent(typeof(BIWOutline)) as BIWOutline; |
| 6 | 161 | | outliner.SetOutlineMaterial(cameraOutlinerMaterial); |
| 6 | 162 | | } |
| | 163 | | else |
| | 164 | | { |
| 1 | 165 | | outliner.enabled = true; |
| | 166 | | } |
| | 167 | |
|
| 7 | 168 | | outliner.Activate(); |
| | 169 | |
|
| 7 | 170 | | UniversalAdditionalCameraData additionalCameraData = camera.transform.GetComponent<UniversalAdditionalCameraData |
| 7 | 171 | | additionalCameraData.SetRenderer(BUILDER_RENDERER_INDEX); |
| 7 | 172 | | } |
| | 173 | |
|
| | 174 | | private void DeactivateBuilderInWorldCamera() |
| | 175 | | { |
| 1 | 176 | | Camera camera = Camera.main; |
| | 177 | |
|
| 1 | 178 | | if (camera == null) |
| 0 | 179 | | return; |
| | 180 | |
|
| 1 | 181 | | BIWOutline outliner = camera.GetComponent<BIWOutline>(); |
| 1 | 182 | | if (outliner != null) |
| | 183 | | { |
| 1 | 184 | | outliner.enabled = false; |
| 1 | 185 | | outliner.Deactivate(); |
| | 186 | | } |
| | 187 | |
|
| 1 | 188 | | UniversalAdditionalCameraData additionalCameraData = camera.transform.GetComponent<UniversalAdditionalCameraData |
| 1 | 189 | | additionalCameraData.SetRenderer(0); |
| 1 | 190 | | } |
| | 191 | |
|
| | 192 | | private void RemoveBuilderInWorldCamera() |
| | 193 | | { |
| 6 | 194 | | Camera camera = Camera.main; |
| | 195 | |
|
| 6 | 196 | | if (camera == null) |
| 0 | 197 | | return; |
| | 198 | |
|
| 6 | 199 | | BIWOutline outliner = camera.GetComponent<BIWOutline>(); |
| 6 | 200 | | if (outliner == null) |
| 0 | 201 | | return; |
| | 202 | |
|
| 6 | 203 | | outliner.Dispose(); |
| 6 | 204 | | GameObject.Destroy(outliner); |
| 6 | 205 | | } |
| | 206 | | } |