| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Configuration; |
| | 4 | | using DCL.Controllers; |
| | 5 | | using DCL.Helpers; |
| | 6 | | using DCL.Interface; |
| | 7 | | using DCL.Models; |
| | 8 | | using Newtonsoft.Json; |
| | 9 | | using System; |
| | 10 | | using System.Collections; |
| | 11 | | using System.Collections.Generic; |
| | 12 | | using System.Diagnostics.CodeAnalysis; |
| | 13 | | using DCL.Builder; |
| | 14 | | using UnityEngine; |
| | 15 | | using Environment = DCL.Environment; |
| | 16 | |
|
| | 17 | | public class BIWEntityHandler : BIWController, IBIWEntityHandler |
| | 18 | | { |
| | 19 | | private const float DUPLICATE_OFFSET = 2f; |
| | 20 | |
|
| | 21 | | private IBIWOutlinerController outlinerController; |
| | 22 | |
|
| | 23 | | private IBIWModeController modeController; |
| | 24 | | private IBIWActionController actionController; |
| | 25 | | private IBIWCreatorController creatorController; |
| | 26 | | private IBIWRaycastController raycastController; |
| | 27 | | private IBIWSaveController saveController; |
| | 28 | |
|
| | 29 | | private BuilderInWorldBridge bridge; |
| | 30 | | private Material editMaterial; |
| | 31 | |
|
| | 32 | | private InputAction_Trigger hideSelectedEntitiesAction; |
| | 33 | |
|
| | 34 | | private InputAction_Trigger showAllEntitiesAction; |
| | 35 | |
|
| 70 | 36 | | private readonly Dictionary<string, BIWEntity> convertedEntities = new Dictionary<string, BIWEntity>(); |
| 70 | 37 | | private readonly List<BIWEntity> selectedEntities = new List<BIWEntity>(); |
| | 38 | |
|
| | 39 | | private IBIWMode currentActiveMode; |
| | 40 | | internal bool isMultiSelectionActive = false; |
| | 41 | | internal bool isSecondayClickPressed = false; |
| | 42 | |
|
| | 43 | | private float lastTransformReportTime; |
| | 44 | |
|
| 70 | 45 | | private List<string> entityNameList = new List<string>(); |
| | 46 | |
|
| | 47 | | private InputAction_Trigger.Triggered hideSelectedEntitiesDelegate; |
| | 48 | | private InputAction_Trigger.Triggered showAllEntitiesDelegate; |
| | 49 | |
|
| | 50 | | private IBuilderEditorHUDController hudController; |
| | 51 | |
|
| | 52 | | public event Action<BIWEntity> OnEntityDeselected; |
| | 53 | | public event Action OnEntitySelected; |
| | 54 | | public event Action<List<BIWEntity>> OnDeleteSelectedEntities; |
| | 55 | | public event Action<BIWEntity> OnEntityDeleted; |
| | 56 | |
|
| | 57 | | private BIWEntity lastClickedEntity; |
| | 58 | | private float lastTimeEntityClicked; |
| | 59 | |
|
| | 60 | | public override void Initialize(IContext context) |
| | 61 | | { |
| 70 | 62 | | base.Initialize(context); |
| 70 | 63 | | if ( context.editorContext.editorHUD != null) |
| | 64 | | { |
| 70 | 65 | | hudController = context.editorContext.editorHUD; |
| 70 | 66 | | hudController.OnEntityDelete += DeleteSingleEntity; |
| 70 | 67 | | hudController.OnDuplicateSelectedAction += DuplicateSelectedEntitiesInput; |
| 70 | 68 | | hudController.OnDeleteSelectedAction += DeleteSelectedEntitiesInput; |
| 70 | 69 | | hudController.OnEntityClick += ChangeEntitySelectionFromList; |
| 70 | 70 | | hudController.OnEntityLock += ChangeEntityLockStatus; |
| 70 | 71 | | hudController.OnEntityChangeVisibility += ChangeEntityVisibilityStatus; |
| 70 | 72 | | hudController.OnEntityRename += SetEntityName; |
| 70 | 73 | | hudController.OnEntitySmartItemComponentUpdate += UpdateSmartItemComponentInKernel; |
| | 74 | | } |
| | 75 | |
|
| | 76 | |
|
| 70 | 77 | | BIWInputWrapper.OnMouseDown += OnInputMouseDown; |
| 70 | 78 | | BIWInputWrapper.OnMouseUp += OnInputMouseUp; |
| | 79 | |
|
| 70 | 80 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged += ChangeEntityBoundsCheckerStatus |
| | 81 | |
|
| 70 | 82 | | if ( context.sceneReferences.biwBridgeGameObject != null ) |
| 4 | 83 | | bridge = context.sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>(); |
| | 84 | |
|
| 70 | 85 | | outlinerController = context.editorContext.outlinerController; |
| | 86 | |
|
| 70 | 87 | | modeController = context.editorContext.modeController; |
| 70 | 88 | | actionController = context.editorContext.actionController; |
| 70 | 89 | | creatorController = context.editorContext.creatorController; |
| 70 | 90 | | raycastController = context.editorContext.raycastController; |
| 70 | 91 | | saveController = context.editorContext.saveController; |
| | 92 | |
|
| 70 | 93 | | editMaterial = context.projectReferencesAsset.editMaterial; |
| | 94 | |
|
| 70 | 95 | | hideSelectedEntitiesAction = context.inputsReferencesAsset.hideSelectedEntitiesAction; |
| 70 | 96 | | showAllEntitiesAction = context.inputsReferencesAsset.showAllEntitiesAction; |
| | 97 | |
|
| 70 | 98 | | hideSelectedEntitiesDelegate = (action) => ChangeShowStateSelectedEntities(); |
| 70 | 99 | | showAllEntitiesDelegate = (action) => ShowAllEntities(); |
| | 100 | |
|
| 70 | 101 | | hideSelectedEntitiesAction.OnTriggered += hideSelectedEntitiesDelegate; |
| 70 | 102 | | showAllEntitiesAction.OnTriggered += showAllEntitiesDelegate; |
| | 103 | |
|
| 70 | 104 | | actionController.OnRedo += ReSelectEntities; |
| 70 | 105 | | actionController.OnUndo += ReSelectEntities; |
| 70 | 106 | | } |
| | 107 | |
|
| | 108 | | internal void OnInputMouseDown(int buttonId, Vector3 mousePosition) |
| | 109 | | { |
| 0 | 110 | | if (buttonId == 1) |
| 0 | 111 | | isSecondayClickPressed = true; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | internal void OnInputMouseUp(int buttonId, Vector3 mousePosition) |
| | 115 | | { |
| 5 | 116 | | if (buttonId == 1) |
| 5 | 117 | | isSecondayClickPressed = false; |
| 5 | 118 | | } |
| | 119 | |
|
| | 120 | | public override void Dispose() |
| | 121 | | { |
| 56 | 122 | | DestroyCollidersForAllEntities(); |
| | 123 | |
|
| 56 | 124 | | actionController.OnRedo -= ReSelectEntities; |
| 56 | 125 | | actionController.OnUndo -= ReSelectEntities; |
| | 126 | |
|
| 56 | 127 | | hideSelectedEntitiesAction.OnTriggered -= hideSelectedEntitiesDelegate; |
| 56 | 128 | | showAllEntitiesAction.OnTriggered -= showAllEntitiesDelegate; |
| | 129 | |
|
| 56 | 130 | | DCL.Environment.i.world.sceneBoundsChecker.OnEntityBoundsCheckerStatusChanged -= ChangeEntityBoundsCheckerStatus |
| | 131 | |
|
| 56 | 132 | | BIWInputWrapper.OnMouseDown -= OnInputMouseDown; |
| 56 | 133 | | BIWInputWrapper.OnMouseUp -= OnInputMouseUp; |
| | 134 | |
|
| 56 | 135 | | if (hudController != null) |
| | 136 | | { |
| 56 | 137 | | hudController.OnEntityDelete -= DeleteSingleEntity; |
| 56 | 138 | | hudController.OnDuplicateSelectedAction -= DuplicateSelectedEntitiesInput; |
| 56 | 139 | | hudController.OnDeleteSelectedAction -= DeleteSelectedEntitiesInput; |
| 56 | 140 | | hudController.OnEntityClick -= ChangeEntitySelectionFromList; |
| 56 | 141 | | hudController.OnEntityLock -= ChangeEntityLockStatus; |
| 56 | 142 | | hudController.OnEntityChangeVisibility -= ChangeEntityVisibilityStatus; |
| 56 | 143 | | hudController.OnEntityChangeVisibility -= ChangeEntityVisibilityStatus; |
| 56 | 144 | | hudController.OnEntityRename -= SetEntityName; |
| 56 | 145 | | hudController.OnEntitySmartItemComponentUpdate -= UpdateSmartItemComponentInKernel; |
| | 146 | | } |
| 56 | 147 | | } |
| | 148 | |
|
| | 149 | | public override void Update() |
| | 150 | | { |
| 0 | 151 | | base.Update(); |
| | 152 | |
|
| 0 | 153 | | if (selectedEntities.Count == 0) |
| 0 | 154 | | return; |
| 0 | 155 | | if ((DCLTime.realtimeSinceStartup - lastTransformReportTime) <= BIWSettings.ENTITY_POSITION_REPORTING_DELAY) |
| 0 | 156 | | return; |
| | 157 | |
|
| 0 | 158 | | ReportTransform(); |
| 0 | 159 | | } |
| | 160 | |
|
| | 161 | | public void ReportTransform(bool forceReport = false) |
| | 162 | | { |
| 4 | 163 | | foreach (BIWEntity entity in selectedEntities) |
| | 164 | | { |
| 0 | 165 | | if (!entity.HasMovedSinceLastReport() && |
| | 166 | | !entity.HasScaledSinceLastReport() && |
| | 167 | | !entity.HasRotatedSinceLastReport() && |
| | 168 | | !forceReport) |
| 0 | 169 | | return; |
| | 170 | |
|
| 0 | 171 | | if ( bridge != null ) |
| 0 | 172 | | bridge.EntityTransformReport(entity.rootEntity, sceneToEdit); |
| 0 | 173 | | entity.PositionReported(); |
| 0 | 174 | | entity.ScaleReported(); |
| 0 | 175 | | entity.RotationReported(); |
| | 176 | | } |
| | 177 | |
|
| 2 | 178 | | lastTransformReportTime = DCLTime.realtimeSinceStartup; |
| 2 | 179 | | } |
| | 180 | |
|
| 0 | 181 | | public float GetLastTimeReport() { return lastTransformReportTime; } |
| | 182 | |
|
| 0 | 183 | | public List<BIWEntity> GetSelectedEntityList() { return selectedEntities; } |
| | 184 | |
|
| 1 | 185 | | public bool IsAnyEntitySelected() { return selectedEntities.Count > 0; } |
| | 186 | |
|
| | 187 | | public void SetActiveMode(IBIWMode buildMode) |
| | 188 | | { |
| 0 | 189 | | currentActiveMode = buildMode; |
| 0 | 190 | | DeselectEntities(); |
| 0 | 191 | | } |
| | 192 | |
|
| 0 | 193 | | public void SetMultiSelectionActive(bool isActive) { isMultiSelectionActive = isActive; } |
| | 194 | |
|
| | 195 | | public override void EnterEditMode(IBuilderScene scene) |
| | 196 | | { |
| 72 | 197 | | base.EnterEditMode(scene); |
| | 198 | |
|
| 72 | 199 | | SetupAllEntities(); |
| 72 | 200 | | EntityListChanged(); |
| 72 | 201 | | CheckErrorOnEntities(); |
| 72 | 202 | | } |
| | 203 | |
|
| | 204 | | private void CheckErrorOnEntities() |
| | 205 | | { |
| 240 | 206 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 207 | | { |
| 48 | 208 | | entity.CheckErrors(); |
| 48 | 209 | | if (entity.hasMissingCatalogItemError) |
| 48 | 210 | | creatorController.CreateErrorOnEntity(entity); |
| | 211 | | } |
| 72 | 212 | | } |
| | 213 | |
|
| | 214 | | public bool IsPointerInSelectedEntity() |
| | 215 | | { |
| 2 | 216 | | BIWEntity entityInPointer = raycastController.GetEntityOnPointer(); |
| 2 | 217 | | if (entityInPointer == null) |
| 1 | 218 | | return false; |
| | 219 | |
|
| 3 | 220 | | foreach (BIWEntity entity in selectedEntities) |
| | 221 | | { |
| 1 | 222 | | if (entityInPointer == entity) |
| 1 | 223 | | return true; |
| | 224 | | } |
| | 225 | |
|
| 0 | 226 | | return false; |
| 1 | 227 | | } |
| | 228 | |
|
| | 229 | | private void DeleteSelectedEntitiesInput() |
| | 230 | | { |
| 0 | 231 | | if (selectedEntities.Count > 0) |
| 0 | 232 | | DeleteSelectedEntities(); |
| 0 | 233 | | } |
| | 234 | |
|
| | 235 | | private void DuplicateSelectedEntitiesInput() |
| | 236 | | { |
| 0 | 237 | | if (selectedEntities.Count <= 0 || isSecondayClickPressed) |
| 0 | 238 | | return; |
| | 239 | |
|
| 0 | 240 | | DuplicateSelectedEntities(); |
| 0 | 241 | | } |
| | 242 | |
|
| | 243 | | public override void ExitEditMode() |
| | 244 | | { |
| 0 | 245 | | base.ExitEditMode(); |
| 0 | 246 | | DeselectEntities(); |
| | 247 | |
|
| 0 | 248 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 249 | | { |
| 0 | 250 | | entity.Dispose(); |
| | 251 | | } |
| | 252 | |
|
| 0 | 253 | | convertedEntities.Clear(); |
| 0 | 254 | | } |
| | 255 | |
|
| | 256 | | internal void ChangeEntitySelectionFromList(BIWEntity entityToEdit) |
| | 257 | | { |
| 1 | 258 | | if (!selectedEntities.Contains(entityToEdit)) |
| 1 | 259 | | SelectFromList(entityToEdit); |
| | 260 | | else |
| 0 | 261 | | DeselectEntity(entityToEdit); |
| 0 | 262 | | } |
| | 263 | |
|
| | 264 | | private void SelectFromList(BIWEntity entityToEdit) |
| | 265 | | { |
| 1 | 266 | | if (!isMultiSelectionActive) |
| 1 | 267 | | DeselectEntities(); |
| 1 | 268 | | if (SelectEntity(entityToEdit)) |
| | 269 | | { |
| 1 | 270 | | if (!isMultiSelectionActive) |
| 1 | 271 | | outlinerController.OutlineEntity(entityToEdit); |
| | 272 | | else |
| 0 | 273 | | outlinerController.OutlineEntities(selectedEntities); |
| | 274 | | } |
| 0 | 275 | | } |
| | 276 | |
|
| | 277 | | public void DeselectEntity(BIWEntity entity) |
| | 278 | | { |
| 12 | 279 | | if (!selectedEntities.Contains(entity)) |
| 0 | 280 | | return; |
| | 281 | |
|
| 12 | 282 | | entity.Deselect(); |
| | 283 | |
|
| 12 | 284 | | outlinerController.CancelEntityOutline(entity); |
| 12 | 285 | | selectedEntities.Remove(entity); |
| 12 | 286 | | hudController?.UpdateEntitiesSelection(selectedEntities.Count); |
| 12 | 287 | | currentActiveMode?.EntityDeselected(entity); |
| 12 | 288 | | if (selectedEntities.Count <= 0 && |
| | 289 | | hudController != null) |
| 12 | 290 | | hudController.HideEntityInformation(); |
| | 291 | |
|
| 12 | 292 | | OnEntityDeselected?.Invoke(entity); |
| 0 | 293 | | } |
| | 294 | |
|
| | 295 | | public void DeselectEntities() |
| | 296 | | { |
| 40 | 297 | | if (selectedEntities.Count <= 0) |
| 31 | 298 | | return; |
| | 299 | |
|
| 9 | 300 | | int amountToDeselect = selectedEntities.Count; |
| 36 | 301 | | for (int i = 0; i < amountToDeselect; i++) |
| | 302 | | { |
| 9 | 303 | | DeselectEntity(selectedEntities[0]); |
| | 304 | | } |
| | 305 | |
|
| 9 | 306 | | currentActiveMode?.OnDeselectedEntities(); |
| | 307 | |
|
| 9 | 308 | | Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); |
| 9 | 309 | | } |
| | 310 | |
|
| | 311 | | public void EntityClicked(BIWEntity entityToSelect) |
| | 312 | | { |
| 3 | 313 | | if (entityToSelect != null) |
| | 314 | | { |
| 3 | 315 | | if (!isMultiSelectionActive) |
| 3 | 316 | | DeselectEntities(); |
| | 317 | |
|
| 3 | 318 | | if (!entityToSelect.isLocked) |
| 3 | 319 | | ChangeEntitySelectStatus(entityToSelect); |
| | 320 | |
|
| 3 | 321 | | if (entityToSelect == lastClickedEntity && (lastTimeEntityClicked + BIWSettings.MOUSE_MS_DOUBLE_CLICK_THRESH |
| 1 | 322 | | modeController.EntityDoubleClick(entityToSelect); |
| | 323 | |
|
| 3 | 324 | | lastClickedEntity = entityToSelect; |
| 3 | 325 | | lastTimeEntityClicked = Time.realtimeSinceStartup; |
| 3 | 326 | | } |
| 0 | 327 | | else if (!isMultiSelectionActive) |
| | 328 | | { |
| 0 | 329 | | DeselectEntities(); |
| | 330 | | } |
| 0 | 331 | | } |
| | 332 | |
|
| | 333 | | internal void ReSelectEntities() |
| | 334 | | { |
| 13 | 335 | | List<BIWEntity> entitiesToReselect = new List<BIWEntity>(); |
| 28 | 336 | | foreach (BIWEntity entity in selectedEntities) |
| | 337 | | { |
| 1 | 338 | | entitiesToReselect.Add(entity); |
| | 339 | | } |
| | 340 | |
|
| 13 | 341 | | DeselectEntities(); |
| | 342 | |
|
| 28 | 343 | | foreach (BIWEntity entity in entitiesToReselect) |
| | 344 | | { |
| 1 | 345 | | SelectEntity(entity); |
| | 346 | | } |
| 13 | 347 | | } |
| | 348 | |
|
| | 349 | | internal void ChangeEntitySelectStatus(BIWEntity entityCliked) |
| | 350 | | { |
| 3 | 351 | | if (entityCliked.isSelected) |
| 0 | 352 | | DeselectEntity(entityCliked); |
| | 353 | | else |
| 3 | 354 | | SelectEntity(entityCliked); |
| 3 | 355 | | } |
| | 356 | |
|
| | 357 | | public void CancelSelection() |
| | 358 | | { |
| 1 | 359 | | if (selectedEntities.Count == 0) |
| 0 | 360 | | return; |
| | 361 | |
|
| 1 | 362 | | DestroyLastCreatedEntities(); |
| 1 | 363 | | DeselectEntities(); |
| 1 | 364 | | } |
| | 365 | |
|
| | 366 | | public void ChangeLockStateSelectedEntities() |
| | 367 | | { |
| 4 | 368 | | foreach (BIWEntity entity in selectedEntities) |
| | 369 | | { |
| 1 | 370 | | entity.ToggleLockStatus(); |
| | 371 | | } |
| | 372 | |
|
| 1 | 373 | | DeselectEntities(); |
| 1 | 374 | | } |
| | 375 | |
|
| | 376 | | public void ShowAllEntities() |
| | 377 | | { |
| 4 | 378 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 379 | | { |
| 1 | 380 | | if (!entity.isVisible) |
| 1 | 381 | | entity.ToggleShowStatus(); |
| | 382 | | } |
| 1 | 383 | | } |
| | 384 | |
|
| | 385 | | public void ChangeShowStateSelectedEntities() |
| | 386 | | { |
| 1 | 387 | | List<BIWEntity> entitiesToHide = new List<BIWEntity>(selectedEntities); |
| | 388 | |
|
| 4 | 389 | | foreach (BIWEntity entity in entitiesToHide) |
| | 390 | | { |
| 1 | 391 | | if (entity.isVisible && entity.isSelected) |
| 1 | 392 | | DeselectEntity(entity); |
| 1 | 393 | | entity.ToggleShowStatus(); |
| | 394 | | } |
| 1 | 395 | | } |
| | 396 | |
|
| | 397 | | public void Select(IDCLEntity entity) |
| | 398 | | { |
| 8 | 399 | | BIWEntity entityEditable = GetConvertedEntity(entity); |
| 8 | 400 | | if (entityEditable == null) |
| 0 | 401 | | return; |
| | 402 | |
|
| 8 | 403 | | SelectEntity(entityEditable, true); |
| 8 | 404 | | } |
| | 405 | |
|
| | 406 | | public bool SelectEntity(BIWEntity entityEditable, bool selectedFromCatalog = false) |
| | 407 | | { |
| 27 | 408 | | if (entityEditable.isLocked) |
| 1 | 409 | | return false; |
| | 410 | |
|
| 26 | 411 | | if (entityEditable.isSelected) |
| 0 | 412 | | return false; |
| | 413 | |
|
| 26 | 414 | | entityEditable.Select(); |
| | 415 | |
|
| 26 | 416 | | selectedEntities.Add(entityEditable); |
| | 417 | |
|
| 26 | 418 | | currentActiveMode?.SelectedEntity(entityEditable); |
| | 419 | |
|
| 26 | 420 | | if ( context.editorContext.editorHUD != null) |
| | 421 | | { |
| 26 | 422 | | hudController.UpdateEntitiesSelection(selectedEntities.Count); |
| 26 | 423 | | hudController.ShowEntityInformation(selectedFromCatalog); |
| 26 | 424 | | hudController.EntityInformationSetEntity(entityEditable, sceneToEdit); |
| | 425 | | } |
| | 426 | |
|
| 26 | 427 | | outlinerController.CancelAllOutlines(); |
| | 428 | |
|
| 26 | 429 | | OnEntitySelected?.Invoke(); |
| | 430 | |
|
| 26 | 431 | | return true; |
| | 432 | | } |
| | 433 | |
|
| | 434 | | public List<BIWEntity> GetAllVoxelsEntities() |
| | 435 | | { |
| 1 | 436 | | List<BIWEntity> voxelEntities = new List<BIWEntity>(); |
| 4 | 437 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 438 | | { |
| 1 | 439 | | if (entity.rootEntity.scene == sceneToEdit && entity.isVoxel) |
| 1 | 440 | | voxelEntities.Add(entity); |
| | 441 | | } |
| | 442 | |
|
| 1 | 443 | | return voxelEntities; |
| | 444 | | } |
| | 445 | |
|
| | 446 | | public List<BIWEntity> GetAllEntitiesFromCurrentScene() |
| | 447 | | { |
| 65 | 448 | | List<BIWEntity> entities = new List<BIWEntity>(); |
| 274 | 449 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 450 | | { |
| 72 | 451 | | if (entity.rootEntity.scene == sceneToEdit) |
| 72 | 452 | | entities.Add(entity); |
| | 453 | | } |
| | 454 | |
|
| 65 | 455 | | return entities; |
| | 456 | | } |
| | 457 | |
|
| | 458 | | public BIWEntity GetConvertedEntity(long entityId) |
| | 459 | | { |
| 6 | 460 | | if (convertedEntities.ContainsKey(GetConvertedUniqueKeyForEntity(entityId))) |
| 6 | 461 | | return convertedEntities[GetConvertedUniqueKeyForEntity(entityId)]; |
| | 462 | |
|
| 0 | 463 | | return null; |
| | 464 | | } |
| | 465 | |
|
| | 466 | | public BIWEntity GetConvertedEntity(IDCLEntity entity) |
| | 467 | | { |
| 19 | 468 | | if (convertedEntities.ContainsKey(GetConvertedUniqueKeyForEntity(entity))) |
| 16 | 469 | | return convertedEntities[GetConvertedUniqueKeyForEntity(entity)]; |
| | 470 | |
|
| 3 | 471 | | return null; |
| | 472 | | } |
| | 473 | |
|
| | 474 | | public void DuplicateSelectedEntities() |
| | 475 | | { |
| 2 | 476 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| 2 | 477 | | buildAction.actionType = IBIWCompleteAction.ActionType.CREATE; |
| | 478 | |
|
| 2 | 479 | | List<BIWEntityAction> entityActionList = new List<BIWEntityAction>(); |
| 2 | 480 | | List<BIWEntity> entitiesToDuplicate = new List<BIWEntity>(selectedEntities); |
| 2 | 481 | | DeselectEntities(); |
| | 482 | |
|
| 8 | 483 | | foreach (BIWEntity entityToDuplicate in entitiesToDuplicate) |
| | 484 | | { |
| 2 | 485 | | if (entityToDuplicate.isNFT) |
| | 486 | | continue; |
| | 487 | |
|
| 2 | 488 | | var entityDuplicated = DuplicateEntity(entityToDuplicate); |
| | 489 | |
|
| | 490 | | // We move the entity before completing the action to save its position too |
| 2 | 491 | | entityDuplicated.rootEntity.gameObject.transform.position += Vector3.right * DUPLICATE_OFFSET; |
| | 492 | |
|
| 2 | 493 | | BIWEntityAction biwEntityAction = new BIWEntityAction(entityDuplicated.rootEntity, entityDuplicated.rootEnti |
| 2 | 494 | | entityActionList.Add(biwEntityAction); |
| 2 | 495 | | SelectEntity(entityDuplicated); |
| | 496 | | } |
| | 497 | |
|
| 2 | 498 | | buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.CREATE); |
| 2 | 499 | | actionController.AddAction(buildAction); |
| 2 | 500 | | } |
| | 501 | |
|
| | 502 | | public BIWEntity DuplicateEntity(BIWEntity entityToDuplicate) |
| | 503 | | { |
| 3 | 504 | | IDCLEntity entity = BIWUtils.DuplicateEntity(sceneToEdit, entityToDuplicate.rootEntity); |
| | 505 | | //Note: If the entity contains the name component or DCLLockedOnEdit, we don't want to copy them |
| 3 | 506 | | sceneToEdit.componentsManagerLegacy.RemoveSharedComponent(entity, typeof(DCLName), false); |
| 3 | 507 | | sceneToEdit.componentsManagerLegacy.RemoveSharedComponent(entity, typeof(DCLLockedOnEdit), false); |
| | 508 | |
|
| 3 | 509 | | BIWUtils.CopyGameObjectStatus(entityToDuplicate.rootEntity.gameObject, entity.gameObject, false, false); |
| 3 | 510 | | BIWEntity convertedEntity = SetupEntityToEdit(entity); |
| | 511 | |
|
| 3 | 512 | | NotifyEntityIsCreated(entity); |
| 3 | 513 | | EntityListChanged(); |
| 3 | 514 | | return convertedEntity; |
| | 515 | | } |
| | 516 | |
|
| | 517 | | public IDCLEntity CreateEntityFromJSON(string entityJson) |
| | 518 | | { |
| 3 | 519 | | EntityData data = BIWUtils.ConvertJSONToEntityData(entityJson); |
| | 520 | |
|
| 3 | 521 | | IDCLEntity newEntity = sceneToEdit.CreateEntity(data.entityId); |
| | 522 | |
|
| 3 | 523 | | if (data.transformComponent != null) |
| | 524 | | { |
| 1 | 525 | | DCLTransform.Model model = new DCLTransform.Model(); |
| 1 | 526 | | model.position = data.transformComponent.position; |
| 1 | 527 | | model.rotation = Quaternion.Euler(data.transformComponent.rotation); |
| 1 | 528 | | model.scale = data.transformComponent.scale; |
| | 529 | |
|
| 1 | 530 | | EntityComponentsUtils.AddTransformComponent(sceneToEdit, newEntity, model); |
| | 531 | | } |
| | 532 | |
|
| 6 | 533 | | foreach (ProtocolV2.GenericComponent component in data.components) |
| | 534 | | { |
| 0 | 535 | | sceneToEdit.componentsManagerLegacy.EntityComponentCreateOrUpdate(newEntity.entityId, (CLASS_ID_COMPONENT) c |
| | 536 | | } |
| | 537 | |
|
| 10 | 538 | | foreach (ProtocolV2.GenericComponent component in data.sharedComponents) |
| | 539 | | { |
| 2 | 540 | | sceneToEdit.componentsManagerLegacy.SceneSharedComponentAttach(newEntity.entityId, component.classId); |
| | 541 | | } |
| | 542 | |
|
| 3 | 543 | | if (data.nftComponent != null) |
| | 544 | | { |
| 1 | 545 | | NFTShape.Model model = new NFTShape.Model(); |
| 1 | 546 | | model.color = data.nftComponent.color.ToColor(); |
| 1 | 547 | | model.src = data.nftComponent.src; |
| 1 | 548 | | model.assetId = data.nftComponent.assetId; |
| | 549 | |
|
| 1 | 550 | | EntityComponentsUtils.AddNFTShapeComponent(sceneToEdit, newEntity, model, data.nftComponent.id); |
| | 551 | | } |
| | 552 | |
|
| 3 | 553 | | var convertedEntity = SetupEntityToEdit(newEntity, true); |
| | 554 | |
|
| 3 | 555 | | if(!convertedEntity.isLoaded) |
| 2 | 556 | | creatorController.CreateLoadingObject(convertedEntity); |
| | 557 | |
|
| 3 | 558 | | var rootEntity = convertedEntity.rootEntity; |
| 3 | 559 | | if (sceneToEdit.componentsManagerLegacy.TryGetSharedComponent(rootEntity, CLASS_ID.GLTF_SHAPE, out var gltfCompo |
| 0 | 560 | | gltfComponent.CallWhenReady(convertedEntity.ShapeLoadFinish); |
| | 561 | |
|
| 3 | 562 | | if (sceneToEdit.componentsManagerLegacy.TryGetSharedComponent(rootEntity, CLASS_ID.NFT_SHAPE, out var nftCompone |
| 1 | 563 | | nftComponent.CallWhenReady(convertedEntity.ShapeLoadFinish); |
| | 564 | |
|
| 3 | 565 | | EntityListChanged(); |
| 3 | 566 | | return newEntity; |
| | 567 | | } |
| | 568 | |
|
| | 569 | | public BIWEntity CreateEmptyEntity(IParcelScene parcelScene, Vector3 entryPoint, Vector3 editionGOPosition, bool not |
| | 570 | | { |
| 30 | 571 | | var sceneController = Environment.i.world.sceneController; |
| 30 | 572 | | IDCLEntity newEntity = |
| | 573 | | parcelScene.CreateEntity( |
| | 574 | | sceneController.entityIdHelper.EntityFromLegacyEntityString(Guid.NewGuid().ToString())); |
| 30 | 575 | | DCLTransform.Model transformModel = new DCLTransform.Model(); |
| 30 | 576 | | transformModel.position = WorldStateUtils.ConvertUnityToScenePosition(entryPoint, parcelScene); |
| | 577 | |
|
| 30 | 578 | | Camera camera = context.sceneReferences.mainCamera; |
| 30 | 579 | | Vector3 pointToLookAt = camera != null ? camera.transform.position : Vector3.zero; |
| 30 | 580 | | pointToLookAt.y = editionGOPosition.y; |
| 30 | 581 | | Quaternion lookOnLook = Quaternion.LookRotation(editionGOPosition - pointToLookAt); |
| | 582 | |
|
| 30 | 583 | | transformModel.rotation = lookOnLook; |
| 30 | 584 | | transformModel.scale = newEntity.gameObject.transform.lossyScale; |
| | 585 | |
|
| 30 | 586 | | EntityComponentsUtils.AddTransformComponent(parcelScene, newEntity, transformModel); |
| | 587 | |
|
| 30 | 588 | | BIWEntity convertedEntity = SetupEntityToEdit(newEntity, true); |
| 30 | 589 | | hudController?.UpdateSceneLimitInfo(); |
| | 590 | |
|
| 30 | 591 | | if (notifyEntityList) |
| 11 | 592 | | EntityListChanged(); |
| 30 | 593 | | return convertedEntity; |
| | 594 | | } |
| | 595 | |
|
| | 596 | | private void SetupAllEntities() |
| | 597 | | { |
| 240 | 598 | | foreach (IDCLEntity entity in sceneToEdit.entities.Values) |
| | 599 | | { |
| 48 | 600 | | SetupEntityToEdit(entity); |
| | 601 | | } |
| 72 | 602 | | } |
| | 603 | |
|
| | 604 | | public void DestroyLastCreatedEntities() |
| | 605 | | { |
| 2 | 606 | | List<BIWEntity> entitiesToRemove = new List<BIWEntity>(); |
| 8 | 607 | | foreach (BIWEntity entity in selectedEntities) |
| | 608 | | { |
| 2 | 609 | | if (entity.isSelected && entity.isNew) |
| 1 | 610 | | entitiesToRemove.Add(entity); |
| | 611 | | } |
| | 612 | |
|
| 2 | 613 | | if (entitiesToRemove.Count == 0) |
| 1 | 614 | | return; |
| | 615 | |
|
| 1 | 616 | | modeController.UndoEditionGOLastStep(); |
| | 617 | |
|
| 4 | 618 | | foreach (BIWEntity entity in entitiesToRemove) |
| | 619 | | { |
| 1 | 620 | | DeleteEntity(entity, false); |
| | 621 | | } |
| | 622 | |
|
| 1 | 623 | | saveController.TryToSave(); |
| 1 | 624 | | hudController?.HideEntityInformation(); |
| 1 | 625 | | } |
| | 626 | |
|
| | 627 | | public void EntityListChanged() |
| | 628 | | { |
| 117 | 629 | | if ( context.editorContext.editorHUD == null) |
| 0 | 630 | | return; |
| 117 | 631 | | hudController.SetEntityList(GetEntitiesInCurrentScene()); |
| 117 | 632 | | } |
| | 633 | |
|
| 2 | 634 | | public int GetCurrentSceneEntityCount() { return GetEntitiesInCurrentScene().Count; } |
| | 635 | |
|
| | 636 | | List<BIWEntity> GetEntitiesInCurrentScene() |
| | 637 | | { |
| 119 | 638 | | List<BIWEntity> currentEntitiesInScene = new List<BIWEntity>(); |
| 446 | 639 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 640 | | { |
| 104 | 641 | | if (entity.rootEntity.scene == sceneToEdit) |
| 104 | 642 | | currentEntitiesInScene.Add(entity); |
| | 643 | | } |
| | 644 | |
|
| 119 | 645 | | return currentEntitiesInScene; |
| | 646 | | } |
| | 647 | |
|
| | 648 | | BIWEntity SetupEntityToEdit(IDCLEntity entity, bool hasBeenCreated = false) |
| | 649 | | { |
| 84 | 650 | | string biwEntityId = GetConvertedUniqueKeyForEntity(entity); |
| | 651 | |
|
| 84 | 652 | | if (convertedEntities.ContainsKey(biwEntityId)) |
| 2 | 653 | | return convertedEntities[biwEntityId]; |
| | 654 | |
|
| 82 | 655 | | BIWEntity entityToEdit = new BIWEntity(); |
| 82 | 656 | | entityToEdit.Initialize(entity, editMaterial); |
| 82 | 657 | | convertedEntities.Add(entityToEdit.entityUniqueId, entityToEdit); |
| 82 | 658 | | entity.OnRemoved += RemoveConvertedEntity; |
| 82 | 659 | | entityToEdit.isNew = hasBeenCreated; |
| | 660 | |
|
| 82 | 661 | | string entityName = entityToEdit.GetDescriptiveName(); |
| 82 | 662 | | var catalogItem = entityToEdit.GetCatalogItemAssociated(); |
| | 663 | |
|
| 82 | 664 | | if ((string.IsNullOrEmpty(entityName) || entityNameList.Contains(entityName)) && catalogItem != null) |
| | 665 | | { |
| 0 | 666 | | entityName = GetNewNameForEntity(catalogItem); |
| 0 | 667 | | SetEntityName(entityToEdit, entityName); |
| 0 | 668 | | } |
| 82 | 669 | | else if (!string.IsNullOrEmpty(entityName) && !entityNameList.Contains(entityName)) |
| | 670 | | { |
| 0 | 671 | | entityNameList.Add(entityName); |
| | 672 | | } |
| | 673 | |
|
| 82 | 674 | | return entityToEdit; |
| | 675 | | } |
| | 676 | |
|
| | 677 | | internal void ChangeEntityBoundsCheckerStatus(IDCLEntity entity, bool isInsideBoundaries) |
| | 678 | | { |
| 4 | 679 | | var convertedEntity = GetConvertedEntity(entity); |
| 4 | 680 | | if (convertedEntity == null) |
| 3 | 681 | | return; |
| | 682 | |
|
| 1 | 683 | | convertedEntity.SetEntityBoundariesError(isInsideBoundaries); |
| 1 | 684 | | } |
| | 685 | |
|
| 1 | 686 | | public string GetNewNameForEntity(CatalogItem sceneObject) { return GetNewNameForEntity(sceneObject.name); } |
| | 687 | |
|
| | 688 | | public string GetNewNameForEntity(string name) |
| | 689 | | { |
| 3 | 690 | | int i = 1; |
| 3 | 691 | | if (!entityNameList.Contains(name)) |
| 1 | 692 | | return name; |
| | 693 | |
|
| 2 | 694 | | string newName = name + " " + i; |
| 2 | 695 | | while (entityNameList.Contains(newName)) |
| | 696 | | { |
| 0 | 697 | | i++; |
| 0 | 698 | | newName = name + " " + i; |
| | 699 | | } |
| | 700 | |
|
| 2 | 701 | | return newName; |
| | 702 | | } |
| | 703 | |
|
| | 704 | | public void DeleteFloorEntities() |
| | 705 | | { |
| 4 | 706 | | List<BIWEntity> entitiesToDelete = new List<BIWEntity>(); |
| | 707 | |
|
| 22 | 708 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 709 | | { |
| 7 | 710 | | if (entity.isFloor) |
| | 711 | | { |
| 4 | 712 | | entitiesToDelete.Add(entity); |
| | 713 | | } |
| | 714 | | } |
| | 715 | |
|
| 16 | 716 | | foreach (BIWEntity entity in entitiesToDelete) |
| 4 | 717 | | DeleteEntity(entity, false); |
| | 718 | |
|
| 4 | 719 | | saveController.TryToSave(); |
| 4 | 720 | | } |
| | 721 | |
|
| | 722 | | public void DeleteEntity(long entityId) |
| | 723 | | { |
| 3 | 724 | | BIWEntity entity = convertedEntities[GetConvertedUniqueKeyForEntity(entityId)]; |
| 3 | 725 | | DeleteEntity(entity, true); |
| 3 | 726 | | } |
| | 727 | |
|
| 8 | 728 | | public void DeleteEntity(BIWEntity entityToDelete) { DeleteEntity(entityToDelete, true); } |
| | 729 | |
|
| | 730 | | public void DeleteEntity(BIWEntity entityToDelete, bool checkSelection) |
| | 731 | | { |
| 13 | 732 | | if (entityToDelete.isSelected && checkSelection) |
| 0 | 733 | | DeselectEntity(entityToDelete); |
| | 734 | |
|
| 13 | 735 | | if (selectedEntities.Contains(entityToDelete)) |
| | 736 | | { |
| 1 | 737 | | selectedEntities.Remove(entityToDelete); |
| 1 | 738 | | hudController?.UpdateEntitiesSelection(selectedEntities.Count); |
| | 739 | | } |
| | 740 | |
|
| 13 | 741 | | string entityName = entityToDelete.GetDescriptiveName(); |
| | 742 | |
|
| 13 | 743 | | if (entityNameList.Contains(entityName)) |
| 4 | 744 | | entityNameList.Remove(entityName); |
| | 745 | |
|
| 13 | 746 | | RemoveConvertedEntity(entityToDelete.rootEntity); |
| 13 | 747 | | entityToDelete.rootEntity.OnRemoved -= RemoveConvertedEntity; |
| 13 | 748 | | entityToDelete.Delete(); |
| 13 | 749 | | long idToRemove = entityToDelete.rootEntity.entityId; |
| 13 | 750 | | OnEntityDeleted?.Invoke(entityToDelete); |
| 13 | 751 | | creatorController.RemoveLoadingObjectInmediate(entityToDelete.rootEntity.entityId); |
| 13 | 752 | | if (sceneToEdit.entities.ContainsKey(idToRemove)) |
| 13 | 753 | | sceneToEdit.RemoveEntity(idToRemove, true); |
| | 754 | |
|
| 13 | 755 | | hudController?.RefreshCatalogAssetPack(); |
| 13 | 756 | | EntityListChanged(); |
| | 757 | |
|
| 13 | 758 | | if ( bridge != null ) |
| 0 | 759 | | bridge.RemoveEntityOnKernel(idToRemove, sceneToEdit); |
| 13 | 760 | | } |
| | 761 | |
|
| | 762 | | public void DeleteSingleEntity(BIWEntity entityToDelete) |
| | 763 | | { |
| 1 | 764 | | actionController.CreateActionEntityDeleted(entityToDelete); |
| 1 | 765 | | DeleteEntity(entityToDelete, true); |
| 1 | 766 | | saveController.TryToSave(); |
| 1 | 767 | | } |
| | 768 | |
|
| | 769 | | public void DeleteSelectedEntities() |
| | 770 | | { |
| 2 | 771 | | List<BIWEntity> entitiesToRemove = new List<BIWEntity>(); |
| | 772 | |
|
| 8 | 773 | | for (int i = 0; i < selectedEntities.Count; i++) |
| | 774 | | { |
| 2 | 775 | | entitiesToRemove.Add(selectedEntities[i]); |
| | 776 | | } |
| | 777 | |
|
| 2 | 778 | | actionController.CreateActionEntityDeleted(entitiesToRemove); |
| | 779 | |
|
| 2 | 780 | | DeselectEntities(); |
| | 781 | |
|
| 8 | 782 | | foreach (BIWEntity entity in entitiesToRemove) |
| | 783 | | { |
| 2 | 784 | | DeleteEntity(entity); |
| | 785 | | } |
| | 786 | |
|
| 2 | 787 | | OnDeleteSelectedEntities?.Invoke(entitiesToRemove); |
| 2 | 788 | | saveController.TryToSave(); |
| 2 | 789 | | } |
| | 790 | |
|
| | 791 | | public void DeleteEntitiesOutsideSceneBoundaries() |
| | 792 | | { |
| 1 | 793 | | List<BIWEntity> entitiesToRemove = new List<BIWEntity>(); |
| 6 | 794 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 795 | | { |
| 2 | 796 | | if (entity.rootEntity.scene == sceneToEdit) |
| | 797 | | { |
| 2 | 798 | | if (!DCL.Environment.i.world.sceneBoundsChecker.IsEntityMeshInsideSceneBoundaries(entity.rootEntity)) |
| | 799 | | { |
| 2 | 800 | | entitiesToRemove.Add(entity); |
| | 801 | | } |
| | 802 | | } |
| | 803 | | } |
| | 804 | |
|
| 6 | 805 | | foreach (BIWEntity entity in entitiesToRemove) |
| | 806 | | { |
| 2 | 807 | | DeleteEntity(entity); |
| | 808 | | } |
| | 809 | |
|
| 1 | 810 | | saveController.TryToSave(); |
| 1 | 811 | | } |
| | 812 | |
|
| | 813 | | private void DestroyCollidersForAllEntities() |
| | 814 | | { |
| 228 | 815 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 816 | | { |
| 58 | 817 | | entity.DestroyColliders(); |
| | 818 | | } |
| 56 | 819 | | } |
| | 820 | |
|
| 164 | 821 | | private void RemoveConvertedEntity(IDCLEntity entity) { convertedEntities.Remove(GetConvertedUniqueKeyForEntity(enti |
| | 822 | |
|
| | 823 | | public void NotifyEntityIsCreated(IDCLEntity entity) |
| | 824 | | { |
| 18 | 825 | | if (bridge != null) |
| 0 | 826 | | bridge.AddEntityOnKernel(entity, sceneToEdit); |
| 18 | 827 | | } |
| | 828 | |
|
| | 829 | | [ExcludeFromCodeCoverage] |
| | 830 | | public void UpdateSmartItemComponentInKernel(BIWEntity entityToUpdate) |
| | 831 | | { |
| | 832 | | if ( bridge != null ) |
| | 833 | | bridge.UpdateSmartItemComponent(entityToUpdate, sceneToEdit); |
| | 834 | | } |
| | 835 | |
|
| 4 | 836 | | public void SetEntityName(BIWEntity entityToApply, string newName) { SetEntityName(entityToApply, newName, true); } |
| | 837 | |
|
| | 838 | | public void SetEntityName(BIWEntity entityToApply, string newName, bool sendUpdateToKernel = true) |
| | 839 | | { |
| 17 | 840 | | string currentName = entityToApply.GetDescriptiveName(); |
| | 841 | |
|
| 17 | 842 | | if (currentName == newName) |
| 0 | 843 | | return; |
| | 844 | |
|
| 17 | 845 | | if (entityNameList.Contains(newName)) |
| 2 | 846 | | newName = GetNewNameForEntity(newName); |
| | 847 | |
|
| 17 | 848 | | if (entityNameList.Contains(currentName)) |
| 0 | 849 | | entityNameList.Remove(currentName); |
| | 850 | |
|
| 17 | 851 | | entityToApply.SetDescriptiveName(newName); |
| 17 | 852 | | entityNameList.Add(newName); |
| | 853 | |
|
| 17 | 854 | | if (sendUpdateToKernel && bridge != null) |
| 0 | 855 | | bridge.ChangedEntityName(entityToApply, sceneToEdit); |
| 17 | 856 | | } |
| | 857 | |
|
| | 858 | | internal void ChangeEntityVisibilityStatus(BIWEntity entityToApply) |
| | 859 | | { |
| 1 | 860 | | entityToApply.ToggleShowStatus(); |
| 1 | 861 | | if (!entityToApply.isVisible && selectedEntities.Contains(entityToApply)) |
| 1 | 862 | | DeselectEntity(entityToApply); |
| 1 | 863 | | } |
| | 864 | |
|
| | 865 | | internal void ChangeEntityLockStatus(BIWEntity entityToApply) |
| | 866 | | { |
| 1 | 867 | | entityToApply.ToggleLockStatus(); |
| 1 | 868 | | if (entityToApply.isLocked && selectedEntities.Contains(entityToApply)) |
| 0 | 869 | | DeselectEntity(entityToApply); |
| | 870 | |
|
| 1 | 871 | | if (bridge != null) |
| 0 | 872 | | bridge.ChangeEntityLockStatus(entityToApply, sceneToEdit); |
| 1 | 873 | | } |
| | 874 | |
|
| 15 | 875 | | private string GetConvertedUniqueKeyForEntity(long entityID) { return sceneToEdit.sceneData.id + entityID; } |
| | 876 | |
|
| 201 | 877 | | private string GetConvertedUniqueKeyForEntity(IDCLEntity entity) { return entity.scene.sceneData.id + entity.entityI |
| | 878 | |
|
| | 879 | | public bool AreAllEntitiesInsideBoundaries() |
| | 880 | | { |
| 5 | 881 | | bool areAllIn = true; |
| 14 | 882 | | foreach (BIWEntity entity in convertedEntities.Values) |
| | 883 | | { |
| 3 | 884 | | if (!DCL.Environment.i.world.sceneBoundsChecker.IsEntityMeshInsideSceneBoundaries(entity.rootEntity)) |
| | 885 | | { |
| 2 | 886 | | areAllIn = false; |
| 2 | 887 | | break; |
| | 888 | | } |
| | 889 | | } |
| | 890 | |
|
| 5 | 891 | | return areAllIn; |
| | 892 | | } |
| | 893 | | } |