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