| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using DCL.Helpers; |
| | 5 | | using DCL.Models; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using DCL.Builder; |
| | 9 | | using DCL.Camera; |
| | 10 | | using UnityEngine; |
| | 11 | | using CameraController = DCL.Camera.CameraController; |
| | 12 | | using Environment = DCL.Environment; |
| | 13 | |
|
| | 14 | | public class BIWGodMode : BIWMode |
| | 15 | | { |
| 35 | 16 | | private float snapDragFactor = 5f; |
| | 17 | | internal IFreeCameraMovement freeCameraController; |
| | 18 | |
|
| | 19 | | private Transform lookAtTransform; |
| | 20 | | private MouseCatcher mouseCatcher; |
| | 21 | | private IBIWGizmosController gizmoManager; |
| | 22 | | private IBIWOutlinerController outlinerController; |
| | 23 | |
|
| | 24 | | private InputAction_Trigger focusOnSelectedEntitiesInputAction; |
| | 25 | | private InputAction_Hold multiSelectionInputAction; |
| | 26 | |
|
| | 27 | | private IParcelScene sceneToEdit; |
| | 28 | |
|
| | 29 | | internal bool isPlacingNewObject = false; |
| | 30 | | internal bool mouseMainBtnPressed = false; |
| | 31 | | internal bool mouseSecondaryBtnPressed = false; |
| | 32 | | internal bool isSquareMultiSelectionInputActive = false; |
| | 33 | | internal bool isMouseDragging = false; |
| | 34 | | internal bool changeSnapTemporaryButtonPressed = false; |
| | 35 | |
|
| | 36 | | internal bool wasGizmosActive = false; |
| | 37 | | internal bool isDraggingStarted = false; |
| | 38 | | internal bool canDragSelectedEntities = false; |
| | 39 | |
|
| | 40 | | internal Vector3 lastMousePosition; |
| | 41 | | internal Vector3 dragStartedPoint; |
| | 42 | |
|
| | 43 | | public const float RAYCAST_MAX_DISTANCE = 10000f; |
| | 44 | |
|
| | 45 | | public override void Init(IContext context) |
| | 46 | | { |
| 35 | 47 | | base.Init(context); |
| | 48 | |
|
| 35 | 49 | | this.context = context; |
| 35 | 50 | | lookAtTransform = new GameObject("BIWGodModeTransform").transform; |
| 35 | 51 | | maxDistanceToSelectEntitiesValue = context.editorContext.godModeDynamicVariablesAsset.maxDistanceToSelectEntitie |
| | 52 | |
|
| 35 | 53 | | snapFactor = context.editorContext.godModeDynamicVariablesAsset.snapFactor; |
| 35 | 54 | | snapRotationDegresFactor = context.editorContext.godModeDynamicVariablesAsset.snapRotationDegresFactor; |
| 35 | 55 | | snapScaleFactor = context.editorContext.godModeDynamicVariablesAsset.snapScaleFactor; |
| 35 | 56 | | snapDistanceToActivateMovement = context.editorContext.godModeDynamicVariablesAsset.snapDistanceToActivateMovem |
| | 57 | |
|
| 35 | 58 | | snapDragFactor = context.editorContext.godModeDynamicVariablesAsset.snapDragFactor; |
| | 59 | |
|
| 35 | 60 | | outlinerController = context.editorContext.outlinerController; |
| 35 | 61 | | gizmoManager = context.editorContext.gizmosController; |
| | 62 | |
|
| 35 | 63 | | if ( context.editorContext.editorHUD != null) |
| | 64 | | { |
| 35 | 65 | | context.editorContext.editorHUD.OnTranslateSelectedAction += TranslateMode; |
| 35 | 66 | | context.editorContext.editorHUD.OnRotateSelectedAction += RotateMode; |
| 35 | 67 | | context.editorContext.editorHUD.OnScaleSelectedAction += ScaleMode; |
| 35 | 68 | | context.editorContext.editorHUD.OnSelectedObjectPositionChange += UpdateSelectionPosition; |
| 35 | 69 | | context.editorContext.editorHUD.OnSelectedObjectRotationChange += UpdateSelectionRotation; |
| 35 | 70 | | context.editorContext.editorHUD.OnSelectedObjectScaleChange += UpdateSelectionScale; |
| 35 | 71 | | context.editorContext.editorHUD.OnResetCameraAction += ResetCamera; |
| | 72 | | } |
| | 73 | |
|
| 35 | 74 | | if (context.sceneReferences.cameraController.GetComponent<CameraController>().TryGetCameraStateByType<FreeCamera |
| 35 | 75 | | freeCameraController = (FreeCameraMovement) cameraState; |
| 35 | 76 | | mouseCatcher = context.sceneReferences.mouseCatcher.GetComponent<MouseCatcher>(); |
| | 77 | |
|
| 35 | 78 | | BIWInputWrapper.OnMouseDown += OnInputMouseDown; |
| 35 | 79 | | BIWInputWrapper.OnMouseUp += OnInputMouseUp; |
| 35 | 80 | | BIWInputWrapper.OnMouseUpOnUI += OnInputMouseUpOnUi; |
| 35 | 81 | | BIWInputWrapper.OnMouseDrag += OnInputMouseDrag; |
| | 82 | |
|
| 35 | 83 | | focusOnSelectedEntitiesInputAction = context.inputsReferencesAsset.focusOnSelectedEntitiesInputAction; |
| 35 | 84 | | multiSelectionInputAction = context.inputsReferencesAsset.multiSelectionInputAction; |
| | 85 | |
|
| 35 | 86 | | focusOnSelectedEntitiesInputAction.OnTriggered += (o) => FocusOnSelectedEntitiesInput(); |
| | 87 | |
|
| 35 | 88 | | multiSelectionInputAction.OnStarted += MultiSelectionInputStart; |
| 35 | 89 | | multiSelectionInputAction.OnFinished += MultiSelectionInputEnd; |
| | 90 | |
|
| 35 | 91 | | gizmoManager.OnChangeTransformValue += EntitiesTransfromByGizmos; |
| 35 | 92 | | gizmoManager.OnGizmoTransformObjectEnd += OnGizmosTransformEnd; |
| 35 | 93 | | gizmoManager.OnGizmoTransformObjectStart += OnGizmosTransformStart; |
| 35 | 94 | | } |
| | 95 | |
|
| | 96 | | public override void Dispose() |
| | 97 | | { |
| 35 | 98 | | base.Dispose(); |
| | 99 | |
|
| 35 | 100 | | isPlacingNewObject = false; |
| | 101 | |
|
| 35 | 102 | | gizmoManager.OnGizmoTransformObjectEnd -= OnGizmosTransformEnd; |
| 35 | 103 | | gizmoManager.OnGizmoTransformObjectStart -= OnGizmosTransformStart; |
| | 104 | |
|
| 35 | 105 | | multiSelectionInputAction.OnStarted -= MultiSelectionInputStart; |
| 35 | 106 | | multiSelectionInputAction.OnFinished -= MultiSelectionInputEnd; |
| | 107 | |
|
| 35 | 108 | | BIWInputWrapper.OnMouseDown -= OnInputMouseDown; |
| 35 | 109 | | BIWInputWrapper.OnMouseUp -= OnInputMouseUp; |
| 35 | 110 | | BIWInputWrapper.OnMouseUpOnUI -= OnInputMouseUpOnUi; |
| 35 | 111 | | BIWInputWrapper.OnMouseDrag -= OnInputMouseDrag; |
| | 112 | |
|
| 35 | 113 | | gizmoManager.OnChangeTransformValue -= EntitiesTransfromByGizmos; |
| | 114 | |
|
| 35 | 115 | | if (lookAtTransform.gameObject != null) |
| 35 | 116 | | GameObject.Destroy(lookAtTransform.gameObject); |
| | 117 | |
|
| 35 | 118 | | if ( context.editorContext.editorHUD == null) |
| 0 | 119 | | return; |
| | 120 | |
|
| 35 | 121 | | context.editorContext.editorHUD.OnSelectedObjectPositionChange -= UpdateSelectionPosition; |
| 35 | 122 | | context.editorContext.editorHUD.OnSelectedObjectRotationChange -= UpdateSelectionRotation; |
| 35 | 123 | | context.editorContext.editorHUD.OnSelectedObjectScaleChange -= UpdateSelectionScale; |
| 35 | 124 | | context.editorContext.editorHUD.OnTranslateSelectedAction -= TranslateMode; |
| 35 | 125 | | context.editorContext.editorHUD.OnRotateSelectedAction -= RotateMode; |
| 35 | 126 | | context.editorContext.editorHUD.OnScaleSelectedAction -= ScaleMode; |
| 35 | 127 | | context.editorContext.editorHUD.OnResetCameraAction -= ResetCamera; |
| 35 | 128 | | } |
| | 129 | |
|
| | 130 | | public override void Update() |
| | 131 | | { |
| 0 | 132 | | base.Update(); |
| | 133 | |
|
| 0 | 134 | | if (isPlacingNewObject) |
| 0 | 135 | | SetEditObjectAtMouse(); |
| 0 | 136 | | else if (isSquareMultiSelectionInputActive && isMouseDragging) |
| 0 | 137 | | CheckOutlineEntitiesInSquareSelection(Input.mousePosition); |
| | 138 | |
|
| 0 | 139 | | } |
| | 140 | |
|
| | 141 | | public override void OnGUI() |
| | 142 | | { |
| 0 | 143 | | base.OnGUI(); |
| 0 | 144 | | if (mouseMainBtnPressed && isSquareMultiSelectionInputActive) |
| | 145 | | { |
| 0 | 146 | | var rect = BIWUtils.GetScreenRect(lastMousePosition, Input.mousePosition); |
| 0 | 147 | | BIWUtils.DrawScreenRect(rect, new Color(1f, 1f, 1f, 0.25f)); |
| 0 | 148 | | BIWUtils.DrawScreenRectBorder(rect, 1, Color.white); |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | |
|
| 4 | 152 | | private void MultiSelectionInputStart(DCLAction_Hold action) { ChangeSnapTemporaryActivated(); } |
| | 153 | |
|
| 66 | 154 | | private void MultiSelectionInputEnd(DCLAction_Hold action) { ChangeSnapTemporaryDeactivated(); } |
| | 155 | |
|
| | 156 | | internal void CheckOutlineEntitiesInSquareSelection(Vector3 mousePosition) |
| | 157 | | { |
| | 158 | | List<BIWEntity> allEntities = null; |
| | 159 | |
|
| 1 | 160 | | allEntities = entityHandler.GetAllEntitiesFromCurrentScene(); |
| | 161 | |
|
| 4 | 162 | | foreach (BIWEntity entity in allEntities) |
| | 163 | | { |
| 1 | 164 | | if (!entity.rootEntity.meshRootGameObject || entity.rootEntity.meshesInfo.renderers.Length <= 0) |
| | 165 | | continue; |
| | 166 | |
|
| 1 | 167 | | if (BIWUtils.IsWithinSelectionBounds(entity.rootEntity.meshesInfo.mergedBounds.center, lastMousePosition, mo |
| 1 | 168 | | outlinerController.OutlineEntity(entity); |
| | 169 | | else |
| 0 | 170 | | outlinerController.CancelEntityOutline(entity); |
| | 171 | | } |
| 1 | 172 | | } |
| | 173 | |
|
| | 174 | | private void ChangeSnapTemporaryDeactivated() |
| | 175 | | { |
| 33 | 176 | | if (changeSnapTemporaryButtonPressed) |
| 2 | 177 | | SetSnapActive(!isSnapActiveValue); |
| | 178 | |
|
| 33 | 179 | | changeSnapTemporaryButtonPressed = false; |
| 33 | 180 | | } |
| | 181 | |
|
| | 182 | | private void ChangeSnapTemporaryActivated() |
| | 183 | | { |
| 2 | 184 | | if (selectedEntities.Count == 0) |
| 0 | 185 | | return; |
| | 186 | |
|
| 2 | 187 | | changeSnapTemporaryButtonPressed = true; |
| | 188 | |
|
| 2 | 189 | | SetSnapActive(!isSnapActiveValue); |
| 2 | 190 | | } |
| | 191 | |
|
| | 192 | | internal void EntitiesTransfromByGizmos(Vector3 transformValue) |
| | 193 | | { |
| 1 | 194 | | if (gizmoManager.GetSelectedGizmo() != BIWSettings.ROTATE_GIZMO_NAME) |
| 0 | 195 | | return; |
| | 196 | |
|
| 4 | 197 | | foreach (BIWEntity entity in selectedEntities) |
| | 198 | | { |
| 1 | 199 | | entity.AddRotation(transformValue); |
| | 200 | | } |
| 1 | 201 | | } |
| | 202 | |
|
| | 203 | | public void UpdateSelectionPosition(Vector3 newPosition) |
| | 204 | | { |
| 1 | 205 | | if (selectedEntities.Count != 1) |
| 0 | 206 | | return; |
| | 207 | |
|
| 1 | 208 | | TransformActionStarted(selectedEntities[0].rootEntity, BIWSettings.TRANSLATE_GIZMO_NAME); |
| 1 | 209 | | editionGO.transform.position = WorldStateUtils.ConvertSceneToUnityPosition(newPosition, sceneToEdit); |
| 1 | 210 | | gizmoManager.SetSelectedEntities(editionGO.transform, selectedEntities); |
| 1 | 211 | | TransformActionEnd(selectedEntities[0].rootEntity, BIWSettings.TRANSLATE_GIZMO_NAME); |
| 1 | 212 | | ActionFinish(IBIWCompleteAction.ActionType.MOVE); |
| 1 | 213 | | entityHandler.ReportTransform(true); |
| 1 | 214 | | saveController.TryToSave(); |
| 1 | 215 | | } |
| | 216 | |
|
| | 217 | | public void UpdateSelectionRotation(Vector3 rotation) |
| | 218 | | { |
| 1 | 219 | | if (selectedEntities.Count != 1) |
| 0 | 220 | | return; |
| | 221 | |
|
| 1 | 222 | | TransformActionStarted(selectedEntities[0].rootEntity, BIWSettings.ROTATE_GIZMO_NAME); |
| 1 | 223 | | selectedEntities[0].rootEntity.gameObject.transform.rotation = Quaternion.Euler(rotation); |
| 1 | 224 | | TransformActionEnd(selectedEntities[0].rootEntity, BIWSettings.ROTATE_GIZMO_NAME); |
| 1 | 225 | | ActionFinish(IBIWCompleteAction.ActionType.ROTATE); |
| 1 | 226 | | entityHandler.ReportTransform(true); |
| 1 | 227 | | saveController.TryToSave(); |
| 1 | 228 | | } |
| | 229 | |
|
| | 230 | | public void UpdateSelectionScale(Vector3 scale) |
| | 231 | | { |
| 1 | 232 | | if (selectedEntities.Count != 1) |
| 0 | 233 | | return; |
| | 234 | |
|
| 1 | 235 | | var entityToUpdate = selectedEntities[0]; |
| | 236 | |
|
| 1 | 237 | | TransformActionStarted(entityToUpdate.rootEntity, BIWSettings.SCALE_GIZMO_NAME); |
| | 238 | | // Before change the scale, we unparent the entity to not to make it dependant on the editionGO and after that, |
| 1 | 239 | | entityToUpdate.rootEntity.gameObject.transform.SetParent(null); |
| 1 | 240 | | entityToUpdate.rootEntity.gameObject.transform.localScale = scale; |
| 1 | 241 | | editionGO.transform.localScale = Vector3.one; |
| 1 | 242 | | entityToUpdate.rootEntity.gameObject.transform.SetParent(editionGO.transform); |
| | 243 | |
|
| 1 | 244 | | TransformActionEnd(entityToUpdate.rootEntity, BIWSettings.SCALE_GIZMO_NAME); |
| 1 | 245 | | ActionFinish(IBIWCompleteAction.ActionType.SCALE); |
| 1 | 246 | | entityHandler.ReportTransform(true); |
| 1 | 247 | | saveController.TryToSave(); |
| 1 | 248 | | } |
| | 249 | |
|
| | 250 | | internal void DragEditionGameObject(Vector3 mousePosition) |
| | 251 | | { |
| 1 | 252 | | Vector3 currentPoint = raycastController.GetFloorPointAtMouse(mousePosition); |
| 1 | 253 | | Vector3 initialEntityPosition = editionGO.transform.position; |
| | 254 | |
|
| 1 | 255 | | if (isSnapActiveValue) |
| | 256 | | { |
| 0 | 257 | | currentPoint = GetPositionRoundedToSnapFactor(currentPoint); |
| 0 | 258 | | initialEntityPosition = GetPositionRoundedToSnapFactor(initialEntityPosition); |
| | 259 | | } |
| | 260 | |
|
| 1 | 261 | | Vector3 move = currentPoint - dragStartedPoint; |
| 1 | 262 | | Vector3 destination = initialEntityPosition + move; |
| | 263 | |
|
| 1 | 264 | | editionGO.transform.position = destination; |
| 1 | 265 | | dragStartedPoint = currentPoint; |
| 1 | 266 | | } |
| | 267 | |
|
| | 268 | | #region Mouse |
| | 269 | |
|
| | 270 | | public override void MouseClickDetected() |
| | 271 | | { |
| 1 | 272 | | if (isPlacingNewObject) |
| | 273 | | { |
| 1 | 274 | | entityHandler.DeselectEntities(); |
| 1 | 275 | | saveController.TryToSave(); |
| 1 | 276 | | return; |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | base.MouseClickDetected(); |
| 0 | 280 | | } |
| | 281 | |
|
| | 282 | | internal void OnInputMouseDrag(int buttonId, Vector3 mousePosition, float axisX, float axisY) |
| | 283 | | { |
| 1 | 284 | | if (Vector3.Distance(lastMousePosition, mousePosition) <= BIWSettings.MOUSE_THRESHOLD_FOR_DRAG && !isMouseDraggi |
| 0 | 285 | | return; |
| | 286 | |
|
| 1 | 287 | | isMouseDragging = true; |
| 1 | 288 | | if (buttonId != 0 || |
| | 289 | | selectedEntities.Count <= 0 || |
| | 290 | | BIWUtils.IsPointerOverMaskElement(BIWSettings.GIZMOS_LAYER) || |
| | 291 | | isSquareMultiSelectionInputActive) |
| 1 | 292 | | return; |
| | 293 | |
|
| 0 | 294 | | if (!isDraggingStarted) |
| 0 | 295 | | StarDraggingSelectedEntities(); |
| | 296 | |
|
| 0 | 297 | | if (canDragSelectedEntities) |
| 0 | 298 | | DragEditionGameObject(mousePosition); |
| 0 | 299 | | } |
| | 300 | |
|
| | 301 | | internal Vector3 GetPositionRoundedToSnapFactor(Vector3 position) |
| | 302 | | { |
| 0 | 303 | | position = new Vector3( |
| | 304 | | Mathf.Round(position.x / snapDragFactor) * snapDragFactor, |
| | 305 | | position.y, |
| | 306 | | Mathf.Round(position.z / snapDragFactor) * snapDragFactor); |
| | 307 | |
|
| 0 | 308 | | return position; |
| | 309 | | } |
| | 310 | |
|
| | 311 | | internal void OnInputMouseUpOnUi(int buttonID, Vector3 position) |
| | 312 | | { |
| 1 | 313 | | if (buttonID == 1) |
| | 314 | | { |
| 0 | 315 | | mouseSecondaryBtnPressed = false; |
| 0 | 316 | | freeCameraController.StopDetectingMovement(); |
| | 317 | | } |
| | 318 | |
|
| 1 | 319 | | if (buttonID != 0) |
| 0 | 320 | | return; |
| | 321 | |
|
| 1 | 322 | | CheckEndBoundMultiselection(position); |
| 1 | 323 | | isMouseDragging = false; |
| 1 | 324 | | } |
| | 325 | |
|
| | 326 | | internal void OnInputMouseUp(int buttonID, Vector3 position) |
| | 327 | | { |
| 1 | 328 | | if (buttonID == 1) |
| | 329 | | { |
| 0 | 330 | | mouseSecondaryBtnPressed = false; |
| 0 | 331 | | if (CanCancelAction(position)) |
| 0 | 332 | | entityHandler.CancelSelection(); |
| | 333 | |
|
| 0 | 334 | | freeCameraController.StopDetectingMovement(); |
| | 335 | | } |
| | 336 | |
|
| 1 | 337 | | if (buttonID != 0) |
| 0 | 338 | | return; |
| | 339 | |
|
| 1 | 340 | | EndDraggingSelectedEntities(); |
| | 341 | |
|
| 1 | 342 | | CheckEndBoundMultiselection(position); |
| | 343 | |
|
| 1 | 344 | | outlinerController.SetOutlineCheckActive(true); |
| | 345 | |
|
| 1 | 346 | | isMouseDragging = false; |
| 1 | 347 | | } |
| | 348 | |
|
| | 349 | | internal void OnInputMouseDown(int buttonID, Vector3 position) |
| | 350 | | { |
| 1 | 351 | | lastMousePosition = position; |
| | 352 | |
|
| 1 | 353 | | if (buttonID == 1) |
| | 354 | | { |
| 0 | 355 | | mouseSecondaryBtnPressed = true; |
| 0 | 356 | | freeCameraController.StartDetectingMovement(); |
| | 357 | | } |
| | 358 | |
|
| 1 | 359 | | if (buttonID != 0) |
| 0 | 360 | | return; |
| | 361 | |
|
| 1 | 362 | | dragStartedPoint = raycastController.GetFloorPointAtMouse(position); |
| | 363 | |
|
| 1 | 364 | | if (isSnapActiveValue) |
| | 365 | | { |
| 0 | 366 | | dragStartedPoint.x = Mathf.Round(dragStartedPoint.x); |
| 0 | 367 | | dragStartedPoint.z = Mathf.Round(dragStartedPoint.z); |
| | 368 | | } |
| | 369 | |
|
| 1 | 370 | | if (isPlacingNewObject) |
| 0 | 371 | | return; |
| | 372 | |
|
| 1 | 373 | | var entity = raycastController.GetEntityOnPointer(); |
| 1 | 374 | | if ((entity == null |
| | 375 | | || (entity != null && !entity.isSelected)) |
| | 376 | | && !BIWUtils.IsPointerOverMaskElement(BIWSettings.GIZMOS_LAYER)) |
| | 377 | | { |
| 1 | 378 | | isSquareMultiSelectionInputActive = true; |
| 1 | 379 | | outlinerController.SetOutlineCheckActive(false); |
| | 380 | | } |
| | 381 | |
|
| 1 | 382 | | mouseMainBtnPressed = true; |
| 1 | 383 | | freeCameraController.SetCameraCanMove(false); |
| 1 | 384 | | } |
| | 385 | |
|
| | 386 | | #endregion |
| | 387 | |
|
| | 388 | | private void CheckEndBoundMultiselection(Vector3 position) |
| | 389 | | { |
| 2 | 390 | | if (isSquareMultiSelectionInputActive && mouseMainBtnPressed ) |
| | 391 | | { |
| 0 | 392 | | if (Vector3.Distance(lastMousePosition, position) >= BIWSettings.MOUSE_THRESHOLD_FOR_DRAG) |
| 0 | 393 | | EndBoundMultiSelection(Input.mousePosition); |
| | 394 | |
|
| 0 | 395 | | isSquareMultiSelectionInputActive = false; |
| 0 | 396 | | mouseMainBtnPressed = false; |
| | 397 | | } |
| 2 | 398 | | } |
| | 399 | |
|
| 0 | 400 | | private bool CanCancelAction(Vector3 currentMousePosition) { return Vector3.Distance(lastMousePosition, currentMouse |
| | 401 | |
|
| | 402 | | internal void StarDraggingSelectedEntities() |
| | 403 | | { |
| 2 | 404 | | if (!entityHandler.IsPointerInSelectedEntity() || |
| | 405 | | gizmoManager.HasAxisHover()) |
| 1 | 406 | | return; |
| | 407 | |
|
| 1 | 408 | | if (gizmoManager.IsGizmoActive()) |
| | 409 | | { |
| 1 | 410 | | gizmoManager.HideGizmo(); |
| 1 | 411 | | wasGizmosActive = true; |
| | 412 | | } |
| | 413 | |
|
| 1 | 414 | | freeCameraController.SetCameraCanMove(false); |
| 1 | 415 | | isDraggingStarted = true; |
| | 416 | |
|
| 1 | 417 | | canDragSelectedEntities = true; |
| 1 | 418 | | } |
| | 419 | |
|
| | 420 | | internal void EndDraggingSelectedEntities() |
| | 421 | | { |
| 2 | 422 | | if (wasGizmosActive && !isPlacingNewObject) |
| | 423 | | { |
| 0 | 424 | | gizmoManager.ShowGizmo(); |
| 0 | 425 | | saveController.TryToSave(); |
| | 426 | | } |
| | 427 | |
|
| 2 | 428 | | wasGizmosActive = false; |
| | 429 | |
|
| 2 | 430 | | freeCameraController.SetCameraCanMove(true); |
| 2 | 431 | | isDraggingStarted = false; |
| | 432 | |
|
| 2 | 433 | | canDragSelectedEntities = false; |
| 2 | 434 | | } |
| | 435 | |
|
| | 436 | | internal void EndBoundMultiSelection(Vector3 mousePosition) |
| | 437 | | { |
| 1 | 438 | | freeCameraController.SetCameraCanMove(true); |
| | 439 | | List<BIWEntity> allEntities = null; |
| | 440 | |
|
| 1 | 441 | | allEntities = entityHandler.GetAllEntitiesFromCurrentScene(); |
| | 442 | |
|
| 1 | 443 | | List<BIWEntity> selectedInsideBoundsEntities = new List<BIWEntity>(); |
| 1 | 444 | | int alreadySelectedEntities = 0; |
| | 445 | |
|
| 1 | 446 | | if (!isMultiSelectionActive) |
| 1 | 447 | | entityHandler.DeselectEntities(); |
| | 448 | |
|
| 4 | 449 | | foreach (BIWEntity entity in allEntities) |
| | 450 | | { |
| 1 | 451 | | if (entity.rootEntity.meshRootGameObject && entity.rootEntity.meshesInfo.renderers.Length > 0) |
| | 452 | | { |
| 1 | 453 | | if (BIWUtils.IsWithinSelectionBounds(entity.rootEntity.meshesInfo.mergedBounds.center, lastMousePosition |
| | 454 | | && !entity.isLocked) |
| | 455 | | { |
| 1 | 456 | | if (entity.isSelected) |
| 0 | 457 | | alreadySelectedEntities++; |
| | 458 | |
|
| 1 | 459 | | entityHandler.SelectEntity(entity); |
| 1 | 460 | | selectedInsideBoundsEntities.Add(entity); |
| | 461 | | } |
| | 462 | | } |
| | 463 | | } |
| | 464 | |
|
| 1 | 465 | | if (selectedInsideBoundsEntities.Count == alreadySelectedEntities && alreadySelectedEntities > 0) |
| | 466 | | { |
| 0 | 467 | | foreach (BIWEntity entity in selectedInsideBoundsEntities) |
| | 468 | | { |
| 0 | 469 | | entityHandler.DeselectEntity(entity); |
| | 470 | | } |
| | 471 | | } |
| | 472 | |
|
| 1 | 473 | | outlinerController.CancelAllOutlines(); |
| 1 | 474 | | } |
| | 475 | |
|
| | 476 | | public override void Activate(IParcelScene scene) |
| | 477 | | { |
| 39 | 478 | | base.Activate(scene); |
| 39 | 479 | | sceneToEdit = scene; |
| | 480 | |
|
| 39 | 481 | | SetLookAtObject(scene); |
| | 482 | |
|
| 39 | 483 | | ConfigureCamera(scene); |
| | 484 | |
|
| 39 | 485 | | if (gizmoManager.GetSelectedGizmo() == DCL.Components.DCLGizmos.Gizmo.NONE) |
| 32 | 486 | | gizmoManager.SetGizmoType(BIWSettings.TRANSLATE_GIZMO_NAME); |
| 39 | 487 | | mouseCatcher.enabled = false; |
| 39 | 488 | | Environment.i.world.sceneController.IsolateScene(sceneToEdit); |
| 39 | 489 | | Utils.UnlockCursor(); |
| | 490 | |
|
| 39 | 491 | | gizmoManager.HideGizmo(); |
| 39 | 492 | | editionGO.transform.SetParent(null); |
| | 493 | |
|
| 39 | 494 | | context.editorContext.editorHUD?.ActivateGodModeUI(); |
| 39 | 495 | | } |
| | 496 | |
|
| | 497 | | public void ConfigureCamera(IParcelScene parcelScene) |
| | 498 | | { |
| 39 | 499 | | freeCameraController.gameObject.SetActive(true); |
| 39 | 500 | | SetLookAtObject(parcelScene); |
| | 501 | |
|
| 39 | 502 | | freeCameraController.LookAt(lookAtTransform); |
| 39 | 503 | | freeCameraController.SetResetConfiguration(Camera.main.transform.position, lookAtTransform); |
| 39 | 504 | | } |
| | 505 | |
|
| | 506 | | public override void OnDeleteEntity(BIWEntity entity) |
| | 507 | | { |
| 0 | 508 | | base.OnDeleteEntity(entity); |
| | 509 | |
|
| 0 | 510 | | if (selectedEntities.Count == 0) |
| 0 | 511 | | gizmoManager.HideGizmo(); |
| 0 | 512 | | } |
| | 513 | |
|
| | 514 | | public override void OnDeselectedEntities() |
| | 515 | | { |
| 0 | 516 | | base.OnDeselectedEntities(); |
| 0 | 517 | | gizmoManager.SetSelectedEntities(editionGO.transform, new List<BIWEntity>()); |
| 0 | 518 | | } |
| | 519 | |
|
| | 520 | | public override void Deactivate() |
| | 521 | | { |
| 5 | 522 | | base.Deactivate(); |
| 5 | 523 | | mouseCatcher.enabled = true; |
| 5 | 524 | | Utils.LockCursor(); |
| | 525 | |
|
| 5 | 526 | | Environment.i.world.sceneController.ReIntegrateIsolatedScene(); |
| | 527 | |
|
| 5 | 528 | | gizmoManager.HideGizmo(); |
| 5 | 529 | | RenderSettings.fog = true; |
| 5 | 530 | | } |
| | 531 | |
|
| | 532 | | public override void StartMultiSelection() |
| | 533 | | { |
| 1 | 534 | | base.StartMultiSelection(); |
| | 535 | |
|
| 1 | 536 | | snapGO.transform.SetParent(null); |
| 1 | 537 | | freeMovementGO.transform.SetParent(null); |
| 1 | 538 | | } |
| | 539 | |
|
| | 540 | | public override void SetDuplicationOffset(float offset) |
| | 541 | | { |
| 0 | 542 | | base.SetDuplicationOffset(offset); |
| 0 | 543 | | editionGO.transform.position += Vector3.right * offset; |
| 0 | 544 | | } |
| | 545 | |
|
| | 546 | | public override void CreatedEntity(BIWEntity createdEntity) |
| | 547 | | { |
| 1 | 548 | | base.CreatedEntity(createdEntity); |
| | 549 | |
|
| 1 | 550 | | if (!createdEntity.isFloor) |
| | 551 | | { |
| 1 | 552 | | isPlacingNewObject = true; |
| 1 | 553 | | outlinerController.SetOutlineCheckActive(false); |
| | 554 | |
|
| 1 | 555 | | SetEditObjectAtMouse(); |
| | 556 | | } |
| | 557 | |
|
| 1 | 558 | | gizmoManager.HideGizmo(); |
| 1 | 559 | | if (createdEntity.isVoxel) |
| 0 | 560 | | createdEntity.rootEntity.gameObject.tag = BIWSettings.VOXEL_TAG; |
| 1 | 561 | | } |
| | 562 | |
|
| 0 | 563 | | public override Vector3 GetCreatedEntityPoint() { return raycastController.GetFloorPointAtMouse(Input.mousePosition) |
| | 564 | |
|
| | 565 | | public override void SelectedEntity(BIWEntity selectedEntity) |
| | 566 | | { |
| 1 | 567 | | base.SelectedEntity(selectedEntity); |
| | 568 | |
|
| 1 | 569 | | gizmoManager.SetSelectedEntities(editionGO.transform, selectedEntities); |
| | 570 | |
|
| 1 | 571 | | if (!isMultiSelectionActive && !selectedEntity.isNew) |
| 1 | 572 | | TryLookAtEntity(selectedEntity.rootEntity); |
| | 573 | |
|
| 1 | 574 | | snapGO.transform.SetParent(null); |
| | 575 | |
|
| 1 | 576 | | UpdateActionsInteractable(); |
| 1 | 577 | | } |
| | 578 | |
|
| | 579 | | public override void EntityDeselected(BIWEntity entityDeselected) |
| | 580 | | { |
| 1 | 581 | | base.EntityDeselected(entityDeselected); |
| 1 | 582 | | if (selectedEntities.Count <= 0) |
| | 583 | | { |
| 0 | 584 | | gizmoManager.HideGizmo(); |
| 0 | 585 | | UpdateActionsInteractable(); |
| | 586 | | } |
| | 587 | |
|
| 1 | 588 | | if (isPlacingNewObject) |
| 1 | 589 | | outlinerController.SetOutlineCheckActive(true); |
| 1 | 590 | | isPlacingNewObject = false; |
| 1 | 591 | | } |
| | 592 | |
|
| | 593 | | public override void EntityDoubleClick(BIWEntity entity) |
| | 594 | | { |
| 0 | 595 | | base.EntityDoubleClick(entity); |
| 0 | 596 | | if (!entity.isLocked) |
| 0 | 597 | | LookAtEntity(entity.rootEntity); |
| 0 | 598 | | } |
| | 599 | |
|
| | 600 | | private void UpdateActionsInteractable() |
| | 601 | | { |
| 1 | 602 | | bool areInteratable = selectedEntities.Count > 0; |
| 1 | 603 | | if ( context.editorContext.editorHUD != null) |
| 1 | 604 | | context.editorContext.editorHUD.SetActionsButtonsInteractable(areInteratable); |
| 1 | 605 | | } |
| | 606 | |
|
| | 607 | | public override bool ShouldCancelUndoAction() |
| | 608 | | { |
| 1 | 609 | | if (isPlacingNewObject) |
| | 610 | | { |
| 1 | 611 | | entityHandler.DestroyLastCreatedEntities(); |
| 1 | 612 | | isPlacingNewObject = false; |
| 1 | 613 | | return true; |
| | 614 | | } |
| | 615 | |
|
| 0 | 616 | | return false; |
| | 617 | | } |
| | 618 | |
|
| | 619 | | public override void SetSnapActive(bool isActive) |
| | 620 | | { |
| 43 | 621 | | base.SetSnapActive(isActive); |
| | 622 | |
|
| 43 | 623 | | if (isSnapActiveValue) |
| | 624 | | { |
| 2 | 625 | | gizmoManager.SetSnapFactor(snapFactor, snapRotationDegresFactor, snapScaleFactor); |
| 2 | 626 | | } |
| | 627 | | else |
| | 628 | | { |
| 41 | 629 | | gizmoManager.SetSnapFactor(0, 0, 0); |
| | 630 | | } |
| 41 | 631 | | } |
| | 632 | |
|
| | 633 | | public void FocusOnSelectedEntitiesInput() |
| | 634 | | { |
| 0 | 635 | | if (isModeActive) |
| | 636 | | { |
| 0 | 637 | | FocusEntities(selectedEntities); |
| 0 | 638 | | InputDone(); |
| | 639 | | } |
| 0 | 640 | | } |
| | 641 | |
|
| | 642 | | public void TryLookAtEntity(IDCLEntity entity) |
| | 643 | | { |
| 1 | 644 | | if (entity.meshRootGameObject == null |
| | 645 | | || entity.meshesInfo == null |
| | 646 | | || BIWUtils.IsBoundInsideCamera(entity.meshesInfo.mergedBounds)) |
| 1 | 647 | | return; |
| | 648 | |
|
| 0 | 649 | | LookAtEntity(entity); |
| 0 | 650 | | } |
| | 651 | |
|
| | 652 | | public void LookAtEntity(IDCLEntity entity) |
| | 653 | | { |
| 1 | 654 | | Vector3 pointToLook = entity.gameObject.transform.position; |
| 1 | 655 | | if (entity.meshesInfo != null && entity.meshesInfo.renderers.Length > 0) |
| 0 | 656 | | pointToLook = CalculateEntityMidPoint(entity); |
| | 657 | |
|
| 1 | 658 | | freeCameraController.SmoothLookAt(pointToLook); |
| 1 | 659 | | } |
| | 660 | |
|
| | 661 | | internal Vector3 CalculateEntityMidPoint(IDCLEntity entity) |
| | 662 | | { |
| 1 | 663 | | Vector3 midPointFromEntityMesh = Vector3.zero; |
| 4 | 664 | | foreach (Renderer render in entity.renderers) |
| | 665 | | { |
| 1 | 666 | | if (render == null) |
| | 667 | | continue; |
| 1 | 668 | | midPointFromEntityMesh += render.bounds.center; |
| | 669 | | } |
| | 670 | |
|
| 1 | 671 | | midPointFromEntityMesh /= entity.renderers.Length; |
| 1 | 672 | | return midPointFromEntityMesh; |
| | 673 | | } |
| | 674 | |
|
| | 675 | | #region Gizmos |
| | 676 | |
|
| 12 | 677 | | public void TranslateMode() { GizmosMode( BIWSettings.TRANSLATE_GIZMO_NAME); } |
| | 678 | |
|
| 8 | 679 | | public void RotateMode() { GizmosMode( BIWSettings.ROTATE_GIZMO_NAME); } |
| | 680 | |
|
| 4 | 681 | | public void ScaleMode() { GizmosMode( BIWSettings.SCALE_GIZMO_NAME); } |
| | 682 | |
|
| | 683 | | private void GizmosMode(string gizmos) |
| | 684 | | { |
| 12 | 685 | | if ((!isModeActive && isPlacingNewObject) || mouseSecondaryBtnPressed) |
| 0 | 686 | | return; |
| 12 | 687 | | if (gizmoManager.GetSelectedGizmo() != gizmos) |
| | 688 | | { |
| 7 | 689 | | context.editorContext.editorHUD?.SetGizmosActive(gizmos); |
| 7 | 690 | | gizmoManager.SetGizmoType(gizmos); |
| 7 | 691 | | if (selectedEntities.Count > 0 ) |
| 0 | 692 | | gizmoManager.ShowGizmo(); |
| | 693 | | } |
| | 694 | | // Note: Free-Movement tool, This could be re-enabled in the future so let the code there |
| | 695 | | // else |
| | 696 | | // { |
| | 697 | | // gizmoManager.HideGizmo(true); |
| | 698 | | // context.editorContext.editorHUD?.SetGizmosActive(BIWSettings.EMPTY_GIZMO_NAME); |
| | 699 | | // } |
| 12 | 700 | | } |
| | 701 | |
|
| | 702 | | internal void OnGizmosTransformStart(string gizmoType) |
| | 703 | | { |
| 2 | 704 | | outlinerController.SetOutlineCheckActive(false); |
| 8 | 705 | | foreach (BIWEntity entity in selectedEntities) |
| | 706 | | { |
| 2 | 707 | | TransformActionStarted(entity.rootEntity, gizmoType); |
| | 708 | | } |
| 2 | 709 | | } |
| | 710 | |
|
| | 711 | | internal void OnGizmosTransformEnd(string gizmoType) |
| | 712 | | { |
| 1 | 713 | | outlinerController.SetOutlineCheckActive(true); |
| 4 | 714 | | foreach (BIWEntity entity in selectedEntities) |
| | 715 | | { |
| 1 | 716 | | TransformActionEnd(entity.rootEntity, gizmoType); |
| | 717 | | } |
| | 718 | |
|
| | 719 | | switch (gizmoType) |
| | 720 | | { |
| | 721 | | case BIWSettings.TRANSLATE_GIZMO_NAME: |
| | 722 | |
|
| 1 | 723 | | ActionFinish(IBIWCompleteAction.ActionType.MOVE); |
| 1 | 724 | | break; |
| | 725 | | case BIWSettings.ROTATE_GIZMO_NAME: |
| 0 | 726 | | ActionFinish(IBIWCompleteAction.ActionType.ROTATE); |
| 0 | 727 | | break; |
| | 728 | | case BIWSettings.SCALE_GIZMO_NAME: |
| 0 | 729 | | ActionFinish(IBIWCompleteAction.ActionType.SCALE); |
| | 730 | | break; |
| | 731 | | } |
| | 732 | |
|
| 1 | 733 | | saveController.TryToSave(); |
| 1 | 734 | | } |
| | 735 | |
|
| | 736 | | #endregion |
| | 737 | |
|
| 2 | 738 | | public void FocusEntities(List<BIWEntity> entitiesToFocus) { freeCameraController.FocusOnEntities(entitiesToFocus); |
| | 739 | |
|
| | 740 | | internal void SetLookAtObject(IParcelScene parcelScene) |
| | 741 | | { |
| 78 | 742 | | Vector3 middlePoint = BIWUtils.CalculateUnityMiddlePoint(parcelScene); |
| 78 | 743 | | lookAtTransform.position = middlePoint; |
| 78 | 744 | | } |
| | 745 | |
|
| | 746 | | internal void SetEditObjectAtMouse() |
| | 747 | | { |
| 1 | 748 | | if (raycastController.RayCastFloor(out Vector3 destination)) |
| | 749 | | { |
| 0 | 750 | | if (isSnapActiveValue) |
| | 751 | | { |
| 0 | 752 | | destination.x = Mathf.Round(destination.x / snapDragFactor) * snapDragFactor; |
| 0 | 753 | | destination.z = Mathf.Round(destination.z / snapDragFactor) * snapDragFactor; |
| | 754 | | } |
| | 755 | |
|
| 0 | 756 | | editionGO.transform.position = destination; |
| | 757 | |
|
| 0 | 758 | | if (selectedEntities.Count > 0 && selectedEntities[0].isNFT) |
| 0 | 759 | | editionGO.transform.position += Vector3.up * 2f; |
| | 760 | | } |
| 1 | 761 | | } |
| | 762 | |
|
| 2 | 763 | | internal void ResetCamera() { freeCameraController.ResetCameraPosition(); } |
| | 764 | | } |