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