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