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