| | 1 | | using UnityEngine; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using Builder.Gizmos; |
| | 4 | | using System.Collections.Generic; |
| | 5 | |
|
| | 6 | | namespace Builder |
| | 7 | | { |
| | 8 | | public class DCLBuilderObjectSelector : MonoBehaviour |
| | 9 | | { |
| | 10 | | const float DRAGGING_THRESHOLD_TIME = 0.25f; |
| | 11 | |
|
| | 12 | | public DCLBuilderRaycast builderRaycast; |
| | 13 | | public DCLBuilderGizmoManager gizmosManager; |
| | 14 | |
|
| | 15 | | public delegate void EntitySelectedDelegate(EditableEntity entity, string gizmoType); |
| | 16 | |
|
| | 17 | | public delegate void EntityDeselectedDelegate(EditableEntity entity); |
| | 18 | |
|
| | 19 | | public delegate void EntitySelectedListChangedDelegate(Transform selectionParent, List<EditableEntity> selectedE |
| | 20 | |
|
| | 21 | | public static event EntitySelectedDelegate OnMarkObjectSelected; |
| | 22 | | public static event EntitySelectedDelegate OnSelectedObject; |
| | 23 | | public static event EntityDeselectedDelegate OnDeselectedObject; |
| | 24 | | public static event System.Action OnNoObjectSelected; |
| | 25 | | public static event EntitySelectedListChangedDelegate OnSelectedObjectListChanged; |
| | 26 | | public static event System.Action<DCLBuilderEntity, Vector3> OnEntityPressed; |
| | 27 | | public static event System.Action<DCLBuilderGizmoAxis> OnGizmosAxisPressed; |
| | 28 | |
|
| 0 | 29 | | public Transform selectedEntitiesParent { private set; get; } |
| | 30 | |
|
| 199 | 31 | | private Dictionary<string, DCLBuilderEntity> entities = new Dictionary<string, DCLBuilderEntity>(); |
| 199 | 32 | | private List<EditableEntity> selectedEntities = new List<EditableEntity>(); |
| 199 | 33 | | private EntityPressedInfo lastPressedEntityInfo = new EntityPressedInfo(); |
| | 34 | | private bool isDirty = false; |
| | 35 | | private bool isSelectionTransformed = false; |
| | 36 | |
|
| | 37 | | private float groundClickTime = 0; |
| | 38 | |
|
| | 39 | | private bool isGameObjectActive = false; |
| | 40 | |
|
| | 41 | | private ParcelScene currentScene; |
| | 42 | |
|
| | 43 | | private void Awake() |
| | 44 | | { |
| 1 | 45 | | DCLBuilderBridge.OnPreviewModeChanged += OnPreviewModeChanged; |
| 1 | 46 | | SelectionParentCreate(); |
| 1 | 47 | | } |
| | 48 | |
|
| | 49 | | private void OnDestroy() |
| | 50 | | { |
| 1 | 51 | | if (selectedEntitiesParent != null) |
| 1 | 52 | | Destroy(selectedEntitiesParent.gameObject); |
| | 53 | |
|
| 1 | 54 | | DCLBuilderBridge.OnPreviewModeChanged -= OnPreviewModeChanged; |
| 1 | 55 | | } |
| | 56 | |
|
| | 57 | | private void OnEnable() |
| | 58 | | { |
| 1 | 59 | | if (!isGameObjectActive) |
| | 60 | | { |
| 1 | 61 | | DCLBuilderInput.OnMouseDown += OnMouseDown; |
| 1 | 62 | | DCLBuilderInput.OnMouseUp += OnMouseUp; |
| 1 | 63 | | DCLBuilderBridge.OnResetObject += OnResetObject; |
| 1 | 64 | | DCLBuilderBridge.OnEntityAdded += OnEntityAdded; |
| 1 | 65 | | DCLBuilderBridge.OnEntityRemoved += OnEntityRemoved; |
| 1 | 66 | | DCLBuilderBridge.OnSceneChanged += OnSceneChanged; |
| 1 | 67 | | DCLBuilderBridge.OnBuilderSelectEntity += OnBuilderSelectEntity; |
| 1 | 68 | | DCLBuilderGizmoManager.OnGizmoTransformObject += OnGizmoTransform; |
| 1 | 69 | | DCLBuilderGizmoManager.OnGizmoTransformObjectEnd += OnGizmoTransformEnded; |
| 1 | 70 | | DCLBuilderObjectDragger.OnDraggingObject += OnObjectsDrag; |
| 1 | 71 | | DCLBuilderObjectDragger.OnDraggingObjectEnd += OnObjectsDragEnd; |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | isGameObjectActive = true; |
| 1 | 75 | | } |
| | 76 | |
|
| | 77 | | private void OnDisable() |
| | 78 | | { |
| 1 | 79 | | isGameObjectActive = false; |
| 1 | 80 | | DCLBuilderInput.OnMouseDown -= OnMouseDown; |
| 1 | 81 | | DCLBuilderInput.OnMouseUp -= OnMouseUp; |
| 1 | 82 | | DCLBuilderBridge.OnResetObject -= OnResetObject; |
| 1 | 83 | | DCLBuilderBridge.OnEntityAdded -= OnEntityAdded; |
| 1 | 84 | | DCLBuilderBridge.OnEntityRemoved -= OnEntityRemoved; |
| 1 | 85 | | DCLBuilderBridge.OnSceneChanged -= OnSceneChanged; |
| 1 | 86 | | DCLBuilderBridge.OnBuilderSelectEntity -= OnBuilderSelectEntity; |
| 1 | 87 | | DCLBuilderGizmoManager.OnGizmoTransformObject -= OnGizmoTransform; |
| 1 | 88 | | DCLBuilderGizmoManager.OnGizmoTransformObjectEnd -= OnGizmoTransformEnded; |
| 1 | 89 | | DCLBuilderObjectDragger.OnDraggingObject -= OnObjectsDrag; |
| 1 | 90 | | DCLBuilderObjectDragger.OnDraggingObjectEnd -= OnObjectsDragEnd; |
| 1 | 91 | | } |
| | 92 | |
|
| | 93 | | private void Update() |
| | 94 | | { |
| 11 | 95 | | if (!isDirty) |
| | 96 | | { |
| 11 | 97 | | return; |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | isDirty = false; |
| 0 | 101 | | SelectionParentReset(); |
| 0 | 102 | | OnSelectedObjectListChanged?.Invoke(selectedEntitiesParent, selectedEntities); |
| 0 | 103 | | if (selectedEntities.Count == 0) |
| | 104 | | { |
| 0 | 105 | | OnNoObjectSelected?.Invoke(); |
| | 106 | | } |
| 0 | 107 | | } |
| | 108 | |
|
| | 109 | | private void OnMouseDown(int buttonId, Vector3 mousePosition) |
| | 110 | | { |
| 0 | 111 | | if (buttonId != 0) |
| | 112 | | { |
| 0 | 113 | | return; |
| | 114 | | } |
| | 115 | |
|
| 0 | 116 | | bool gizmoOrEntityPressed = false; |
| | 117 | |
|
| | 118 | | RaycastHit hit; |
| 0 | 119 | | if (builderRaycast.Raycast(mousePosition, builderRaycast.defaultMask | builderRaycast.gizmoMask, out hit, Co |
| | 120 | | { |
| 0 | 121 | | DCLBuilderGizmoAxis gizmosAxis = hit.collider.gameObject.GetComponent<DCLBuilderGizmoAxis>(); |
| 0 | 122 | | if (gizmosAxis != null) |
| | 123 | | { |
| 0 | 124 | | OnGizmosAxisPressed?.Invoke(gizmosAxis); |
| 0 | 125 | | gizmoOrEntityPressed = true; |
| 0 | 126 | | } |
| | 127 | | else |
| | 128 | | { |
| 0 | 129 | | var builderSelectionCollider = hit.collider.gameObject.GetComponent<DCLBuilderSelectionCollider>(); |
| 0 | 130 | | DCLBuilderEntity pressedEntity = null; |
| | 131 | |
|
| 0 | 132 | | if (builderSelectionCollider != null) |
| | 133 | | { |
| 0 | 134 | | pressedEntity = builderSelectionCollider.ownerEntity; |
| | 135 | | } |
| | 136 | |
|
| 0 | 137 | | if (pressedEntity != null && CanSelect(pressedEntity)) |
| | 138 | | { |
| 0 | 139 | | SetLastPressedEntity(pressedEntity, hit.point); |
| 0 | 140 | | OnEntityPressed?.Invoke(pressedEntity, hit.point); |
| 0 | 141 | | gizmoOrEntityPressed = true; |
| | 142 | | } |
| | 143 | | } |
| | 144 | | } |
| | 145 | |
|
| 0 | 146 | | if (gizmoOrEntityPressed) |
| | 147 | | { |
| 0 | 148 | | groundClickTime = 0; |
| 0 | 149 | | } |
| | 150 | | else |
| | 151 | | { |
| 0 | 152 | | groundClickTime = Time.unscaledTime; |
| | 153 | | } |
| 0 | 154 | | } |
| | 155 | |
|
| | 156 | | private void OnMouseUp(int buttonId, Vector3 mousePosition) |
| | 157 | | { |
| 0 | 158 | | if (buttonId != 0) |
| | 159 | | { |
| 0 | 160 | | return; |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | if (lastPressedEntityInfo.pressedEntity != null) |
| | 164 | | { |
| | 165 | | // NOTE: we only process entity as selected if we are not considering that user was holding mouse button |
| 0 | 166 | | if ((Time.unscaledTime - lastPressedEntityInfo.pressedTime) < DRAGGING_THRESHOLD_TIME) |
| | 167 | | { |
| 0 | 168 | | ProcessEntityPressed(lastPressedEntityInfo.pressedEntity, lastPressedEntityInfo.hitPoint); |
| | 169 | | } |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | lastPressedEntityInfo.pressedEntity = null; |
| | 173 | |
|
| | 174 | | // NOTE: deselect all entities if the user click on the ground and it wasn't holding the mouse left button |
| 0 | 175 | | if (groundClickTime != 0 && (Time.unscaledTime - groundClickTime) < DRAGGING_THRESHOLD_TIME) |
| | 176 | | { |
| 0 | 177 | | if (selectedEntities != null) |
| | 178 | | { |
| 0 | 179 | | OnNoObjectSelected?.Invoke(); |
| | 180 | | } |
| | 181 | | } |
| | 182 | |
|
| 0 | 183 | | groundClickTime = 0; |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private void OnResetObject() |
| | 187 | | { |
| 0 | 188 | | for (int i = 0; i < selectedEntities.Count; i++) |
| | 189 | | { |
| 0 | 190 | | selectedEntities[i].transform.localRotation = Quaternion.identity; |
| | 191 | | } |
| 0 | 192 | | } |
| | 193 | |
|
| | 194 | | private void OnEntityAdded(DCLBuilderEntity entity) |
| | 195 | | { |
| 1 | 196 | | if (!entities.ContainsKey(entity.rootEntity.entityId)) |
| | 197 | | { |
| 1 | 198 | | entities.Add(entity.rootEntity.entityId, entity); |
| | 199 | | } |
| 1 | 200 | | } |
| | 201 | |
|
| | 202 | | private void OnEntityRemoved(DCLBuilderEntity entity) |
| | 203 | | { |
| 0 | 204 | | if (selectedEntities.Contains(entity)) |
| | 205 | | { |
| 0 | 206 | | Deselect(entity); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | if (entities.ContainsKey(entity.rootEntity.entityId)) |
| | 210 | | { |
| 0 | 211 | | entities.Remove(entity.rootEntity.entityId); |
| | 212 | | } |
| 0 | 213 | | } |
| | 214 | |
|
| 4 | 215 | | private void OnSceneChanged(ParcelScene scene) { currentScene = scene; } |
| | 216 | |
|
| | 217 | | private void OnBuilderSelectEntity(string[] entitiesId) |
| | 218 | | { |
| 0 | 219 | | List<EditableEntity> entitiesToDeselect = new List<EditableEntity>(selectedEntities); |
| | 220 | |
|
| 0 | 221 | | for (int i = 0; i < entitiesId.Length; i++) |
| | 222 | | { |
| 0 | 223 | | if (entities.ContainsKey(entitiesId[i])) |
| | 224 | | { |
| 0 | 225 | | DCLBuilderEntity entity = entities[entitiesId[i]]; |
| 0 | 226 | | if (!SelectionParentHasChild(entity.transform)) |
| | 227 | | { |
| 0 | 228 | | Select(entity); |
| 0 | 229 | | } |
| | 230 | | else |
| | 231 | | { |
| 0 | 232 | | entitiesToDeselect.Remove(entity); |
| | 233 | | } |
| | 234 | | } |
| | 235 | | } |
| | 236 | |
|
| 0 | 237 | | for (int i = 0; i < entitiesToDeselect.Count; i++) |
| | 238 | | { |
| 0 | 239 | | Deselect(entitiesToDeselect[i]); |
| | 240 | | } |
| 0 | 241 | | } |
| | 242 | |
|
| 0 | 243 | | private void OnGizmoTransform(string gizmoType) { isSelectionTransformed = true; } |
| | 244 | |
|
| | 245 | | private void OnGizmoTransformEnded(string gizmoType) |
| | 246 | | { |
| 0 | 247 | | if (isSelectionTransformed) |
| | 248 | | { |
| 0 | 249 | | SelectionParentReset(); |
| | 250 | | } |
| | 251 | |
|
| 0 | 252 | | isSelectionTransformed = false; |
| 0 | 253 | | } |
| | 254 | |
|
| 0 | 255 | | private void OnObjectsDrag() { isSelectionTransformed = true; } |
| | 256 | |
|
| | 257 | | private void OnObjectsDragEnd() |
| | 258 | | { |
| 0 | 259 | | if (isSelectionTransformed) |
| | 260 | | { |
| 0 | 261 | | SelectionParentReset(); |
| | 262 | | } |
| | 263 | |
|
| 0 | 264 | | isSelectionTransformed = false; |
| 0 | 265 | | } |
| | 266 | |
|
| 0 | 267 | | private bool CanSelect(DCLBuilderEntity entity) { return entity.hasGizmoComponent; } |
| | 268 | |
|
| | 269 | | private void MarkSelected(DCLBuilderEntity entity) |
| | 270 | | { |
| 0 | 271 | | if (entity == null) |
| | 272 | | { |
| 0 | 273 | | return; |
| | 274 | | } |
| | 275 | |
|
| 0 | 276 | | OnMarkObjectSelected?.Invoke(entity, gizmosManager.GetSelectedGizmo()); |
| 0 | 277 | | } |
| | 278 | |
|
| | 279 | | private void Select(DCLBuilderEntity entity) |
| | 280 | | { |
| 0 | 281 | | if (entity == null) |
| | 282 | | { |
| 0 | 283 | | return; |
| | 284 | | } |
| | 285 | |
|
| 0 | 286 | | if (!selectedEntities.Contains(entity)) |
| | 287 | | { |
| 0 | 288 | | selectedEntities.Add(entity); |
| | 289 | | } |
| | 290 | |
|
| 0 | 291 | | SelectionParentAddEntity(entity); |
| 0 | 292 | | entity.SetSelectLayer(); |
| | 293 | |
|
| 0 | 294 | | OnSelectedObject?.Invoke(entity, gizmosManager.GetSelectedGizmo()); |
| 0 | 295 | | isDirty = true; |
| 0 | 296 | | } |
| | 297 | |
|
| | 298 | | private void Deselect(EditableEntity entity) |
| | 299 | | { |
| 0 | 300 | | if (entity != null) |
| | 301 | | { |
| 0 | 302 | | SelectionParentRemoveEntity(entity); |
| 0 | 303 | | OnDeselectedObject?.Invoke(entity); |
| 0 | 304 | | entity.SetDefaultLayer(); |
| | 305 | | } |
| | 306 | |
|
| 0 | 307 | | if (selectedEntities.Contains(entity)) |
| | 308 | | { |
| 0 | 309 | | selectedEntities.Remove(entity); |
| | 310 | | } |
| | 311 | |
|
| 0 | 312 | | isDirty = true; |
| 0 | 313 | | } |
| | 314 | |
|
| | 315 | | private void DeselectAll() |
| | 316 | | { |
| 0 | 317 | | for (int i = selectedEntities.Count - 1; i >= 0; i--) |
| | 318 | | { |
| 0 | 319 | | Deselect(selectedEntities[i]); |
| | 320 | | } |
| | 321 | |
|
| 0 | 322 | | SelectionParentRemoveAllEntities(); |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | private void OnPreviewModeChanged(bool isPreview) |
| | 326 | | { |
| 0 | 327 | | DeselectAll(); |
| 0 | 328 | | gameObject.SetActive(!isPreview); |
| 0 | 329 | | } |
| | 330 | |
|
| | 331 | | private void SetLastPressedEntity(DCLBuilderEntity pressedEntity, Vector3 hitPoint) |
| | 332 | | { |
| 0 | 333 | | lastPressedEntityInfo.pressedEntity = pressedEntity; |
| 0 | 334 | | lastPressedEntityInfo.pressedTime = Time.unscaledTime; |
| 0 | 335 | | lastPressedEntityInfo.hitPoint = hitPoint; |
| 0 | 336 | | } |
| | 337 | |
|
| | 338 | | private void ProcessEntityPressed(DCLBuilderEntity pressedEntity, Vector3 hitPoint) |
| | 339 | | { |
| 0 | 340 | | if (CanSelect(pressedEntity)) |
| | 341 | | { |
| 0 | 342 | | MarkSelected(pressedEntity); |
| | 343 | | } |
| 0 | 344 | | } |
| | 345 | |
|
| 2 | 346 | | private void SelectionParentCreate() { selectedEntitiesParent = new GameObject("BuilderSelectedEntitiesParent"). |
| | 347 | |
|
| | 348 | | private void SelectionParentReset() |
| | 349 | | { |
| 0 | 350 | | if (selectedEntitiesParent.childCount == 0) |
| | 351 | | { |
| 0 | 352 | | return; |
| | 353 | | } |
| | 354 | |
|
| 0 | 355 | | Transform entitiyTransform = selectedEntitiesParent.GetChild(0); |
| 0 | 356 | | Vector3 min = entitiyTransform.position; |
| 0 | 357 | | Vector3 max = entitiyTransform.position; |
| | 358 | |
|
| 0 | 359 | | List<Transform> children = new List<Transform>(selectedEntitiesParent.childCount); |
| | 360 | |
|
| 0 | 361 | | children.Add(entitiyTransform); |
| 0 | 362 | | SelectionParentRemoveEntity(entitiyTransform); |
| | 363 | |
|
| 0 | 364 | | for (int i = selectedEntitiesParent.childCount - 1; i >= 0; i--) |
| | 365 | | { |
| 0 | 366 | | entitiyTransform = selectedEntitiesParent.GetChild(i); |
| 0 | 367 | | if (entitiyTransform.position.x < min.x) |
| 0 | 368 | | min.x = entitiyTransform.position.x; |
| 0 | 369 | | if (entitiyTransform.position.y < min.y) |
| 0 | 370 | | min.y = entitiyTransform.position.y; |
| 0 | 371 | | if (entitiyTransform.position.z < min.z) |
| 0 | 372 | | min.z = entitiyTransform.position.z; |
| 0 | 373 | | if (entitiyTransform.position.x > max.x) |
| 0 | 374 | | max.x = entitiyTransform.position.x; |
| 0 | 375 | | if (entitiyTransform.position.y > max.y) |
| 0 | 376 | | max.y = entitiyTransform.position.y; |
| 0 | 377 | | if (entitiyTransform.position.z > max.z) |
| 0 | 378 | | max.z = entitiyTransform.position.z; |
| | 379 | |
|
| 0 | 380 | | children.Add(entitiyTransform); |
| 0 | 381 | | SelectionParentRemoveEntity(entitiyTransform); |
| | 382 | | } |
| | 383 | |
|
| 0 | 384 | | selectedEntitiesParent.position = min + (max - min) * 0.5f; |
| 0 | 385 | | selectedEntitiesParent.localScale = Vector3.one; |
| 0 | 386 | | selectedEntitiesParent.rotation = Quaternion.identity; |
| | 387 | |
|
| 0 | 388 | | for (int i = 0; i < children.Count; i++) |
| | 389 | | { |
| 0 | 390 | | SelectionParentAddEntity(children[i]); |
| | 391 | | } |
| 0 | 392 | | } |
| | 393 | |
|
| 0 | 394 | | private void SelectionParentAddEntity(DCLBuilderEntity entity) { SelectionParentAddEntity(entity.transform); } |
| | 395 | |
|
| 0 | 396 | | private void SelectionParentAddEntity(Transform entityTransform) { entityTransform.SetParent(selectedEntitiesPar |
| | 397 | |
|
| 0 | 398 | | private void SelectionParentRemoveEntity(EditableEntity entity) { SelectionParentRemoveEntity(entity.transform); |
| | 399 | |
|
| 0 | 400 | | private void SelectionParentRemoveEntity(Transform entityTransform) { entityTransform.SetParent(currentScene.tra |
| | 401 | |
|
| | 402 | | private void SelectionParentRemoveAllEntities() |
| | 403 | | { |
| 0 | 404 | | for (int i = selectedEntitiesParent.childCount - 1; i >= 0; i--) |
| | 405 | | { |
| 0 | 406 | | SelectionParentRemoveEntity(selectedEntitiesParent.GetChild(i)); |
| | 407 | | } |
| 0 | 408 | | } |
| | 409 | |
|
| 0 | 410 | | private bool SelectionParentHasChild(Transform transform) { return transform.parent == selectedEntitiesParent; } |
| | 411 | |
|
| | 412 | | private RaycastHit CompareSelectionHit(RaycastHit[] hits) |
| | 413 | | { |
| 0 | 414 | | RaycastHit closestHit = hits[0]; |
| 0 | 415 | | bool isHitASelectedObject = IsEntityHitAndSelected(closestHit); |
| | 416 | |
|
| 0 | 417 | | if (IsGizmoHit(closestHit)) // Gizmos has always priority |
| | 418 | | { |
| 0 | 419 | | return closestHit; |
| | 420 | | } |
| | 421 | |
|
| | 422 | | RaycastHit hit; |
| 0 | 423 | | for (int i = 1; i < hits.Length; i++) |
| | 424 | | { |
| 0 | 425 | | hit = hits[i]; |
| 0 | 426 | | if (IsGizmoHit(hit)) // Gizmos has always priority |
| | 427 | | { |
| 0 | 428 | | return hit; |
| | 429 | | } |
| | 430 | |
|
| 0 | 431 | | if (hit.distance < closestHit.distance) |
| | 432 | | { |
| 0 | 433 | | isHitASelectedObject = IsEntityHitAndSelected(hit); |
| 0 | 434 | | closestHit = hit; |
| 0 | 435 | | } |
| 0 | 436 | | else if (hit.distance == closestHit.distance && !isHitASelectedObject) |
| | 437 | | { |
| 0 | 438 | | isHitASelectedObject = IsEntityHitAndSelected(hit); |
| 0 | 439 | | closestHit = hit; |
| | 440 | | } |
| | 441 | | } |
| | 442 | |
|
| 0 | 443 | | return closestHit; |
| | 444 | | } |
| | 445 | |
|
| 0 | 446 | | private bool IsGizmoHit(RaycastHit hit) { return hit.collider.gameObject.GetComponent<DCLBuilderGizmoAxis>() != |
| | 447 | |
|
| | 448 | | private bool IsEntityHitAndSelected(RaycastHit hit) |
| | 449 | | { |
| 0 | 450 | | var collider = hit.collider.gameObject.GetComponent<DCLBuilderSelectionCollider>(); |
| 0 | 451 | | if (collider != null) |
| | 452 | | { |
| 0 | 453 | | return SelectionParentHasChild(collider.ownerEntity.transform); |
| | 454 | | } |
| | 455 | |
|
| 0 | 456 | | return false; |
| | 457 | | } |
| | 458 | |
|
| | 459 | | private class EntityPressedInfo |
| | 460 | | { |
| | 461 | | public DCLBuilderEntity pressedEntity; |
| | 462 | | public float pressedTime; |
| | 463 | | public Vector3 hitPoint; |
| | 464 | | } |
| | 465 | | } |
| | 466 | | } |