< Summary

Class:BIWFloorHandler
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWFloorHandler.cs
Covered lines:68
Uncovered lines:11
Coverable lines:79
Total lines:179
Line coverage:86% (68 of 79)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWFloorHandler()0%110100%
Initialize(...)0%110100%
Dispose()0%110100%
OnFloorEntityDeleted(...)0%220100%
CleanUp()0%110100%
ExistsFloorPlaceHolderForEntity(...)0%110100%
ChangeFloor(...)0%22090.91%
FindCurrentFloorCatalogItem()0%3.033085.71%
IsCatalogItemFloor(...)0%2100%
CreateDefaultFloor()0%2100%
CreateFloor(...)0%44094.74%
RemovePlaceHolder(...)0%110100%
OnFloorLoaded(...)0%4.054085.71%
RemovePlaceHolder(...)0%220100%
RemoveAllPlaceHolders()0%220100%
ExitEditMode()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWFloorHandler.cs

#LineLine coverage
 1using System;
 2using DCL;
 3using DCL.Configuration;
 4using DCL.Controllers;
 5using DCL.Models;
 6using System.Collections;
 7using System.Collections.Generic;
 8using System.Linq;
 9using DCL.Builder;
 10using UnityEngine;
 11
 12public class BIWFloorHandler : BIWController, IBIWFloorHandler
 13{
 14    public event Action OnAllParcelsFloorLoaded;
 15
 16    private IBIWActionController actionController;
 17
 18    private IBIWEntityHandler entityHandler;
 19    private IBIWCreatorController creatorController;
 20    private IBIWSaveController saveController;
 21
 22    private GameObject floorPrefab;
 23
 24    private int numberOfParcelsLoaded;
 25
 26    private CatalogItem lastFloorCalalogItemUsed;
 727    public Dictionary<string, GameObject> floorPlaceHolderDict { get; set; } = new Dictionary<string, GameObject>();
 728    private readonly List<string> loadedFloorEntities = new List<string>();
 29    private Camera mainCamera;
 30
 31    public override void Initialize(IContext context)
 32    {
 733        base.Initialize(context);
 34
 735        actionController = context.editorContext.actionController;
 736        entityHandler = context.editorContext.entityHandler;
 737        creatorController = context.editorContext.creatorController;
 738        saveController = context.editorContext.saveController;
 739        mainCamera = context.sceneReferences.mainCamera;
 740        floorPrefab = context.projectReferencesAsset.floorPlaceHolderPrefab;
 41
 742        entityHandler.OnEntityDeleted += OnFloorEntityDeleted;
 743    }
 44
 45    public override void Dispose()
 46    {
 847        entityHandler.OnEntityDeleted -= OnFloorEntityDeleted;
 848        CleanUp();
 849    }
 50
 51    private void OnFloorEntityDeleted(BIWEntity entity)
 52    {
 653        if (entity.isFloor)
 454            RemovePlaceHolder(entity);
 655    }
 56
 1657    public void CleanUp() { RemoveAllPlaceHolders(); }
 58
 259    public bool ExistsFloorPlaceHolderForEntity(string entityId) { return floorPlaceHolderDict.ContainsKey(entityId); }
 60
 61    public void ChangeFloor(CatalogItem newFloorObject)
 62    {
 263        saveController.SetSaveActivation(false);
 264        CatalogItem lastFloor = lastFloorCalalogItemUsed;
 265        if (lastFloor == null)
 066            lastFloor = FindCurrentFloorCatalogItem();
 67
 268        entityHandler.DeleteFloorEntities();
 69
 270        CreateFloor(newFloorObject);
 71
 272        BIWCompleteAction buildAction = new BIWCompleteAction();
 73
 274        buildAction.CreateChangeFloorAction(lastFloor, newFloorObject);
 275        actionController.AddAction(buildAction);
 76
 277        saveController.SetSaveActivation(true, true);
 278    }
 79
 80    public CatalogItem FindCurrentFloorCatalogItem()
 81    {
 382        foreach (BIWEntity entity in entityHandler.GetAllEntitiesFromCurrentScene())
 83        {
 184            if (entity.isFloor)
 85            {
 186                return entity.GetCatalogItemAssociated();
 87            }
 88        }
 89
 090        return null;
 191    }
 92
 093    public bool IsCatalogItemFloor(CatalogItem floorSceneObject) { return string.Equals(floorSceneObject.category, BIWSe
 94
 95    public void CreateDefaultFloor()
 96    {
 097        CatalogItem floorSceneObject = BIWUtils.CreateFloorSceneObject();
 098        CreateFloor(floorSceneObject);
 099    }
 100
 101    public void CreateFloor(CatalogItem floorSceneObject)
 102    {
 7103        if (sceneToEdit == null)
 0104            return;
 105
 7106        Vector3 initialPosition = new Vector3(ParcelSettings.PARCEL_SIZE / 2, 0, ParcelSettings.PARCEL_SIZE / 2);
 7107        Vector2Int[] parcelsPoints = sceneToEdit.sceneData.parcels;
 7108        numberOfParcelsLoaded = 0;
 7109        loadedFloorEntities.Clear();
 110
 28111        foreach (Vector2Int parcel in parcelsPoints)
 112        {
 7113            BIWEntity entity = creatorController.CreateCatalogItem(
 114                floorSceneObject,
 115                WorldStateUtils.ConvertPointInSceneToUnityPosition(initialPosition, parcel),
 116                false,
 117                true,
 118                OnFloorLoaded);
 119
 7120            string rootEntityId = entity.rootEntity.entityId;
 121
 122            // It may happen that when you get here, the floor entity is already loaded and it wouldn't be necessary to 
 7123            if (!loadedFloorEntities.Contains(rootEntityId))
 124            {
 7125                GameObject floorPlaceHolder = UnityEngine.Object.Instantiate(floorPrefab, entity.rootEntity.gameObject.t
 7126                floorPlaceHolder.GetComponentInChildren<BIWFloorLoading>().Initialize(mainCamera);
 7127                floorPlaceHolderDict.Add(entity.rootEntity.entityId, floorPlaceHolder);
 7128                entity.OnShapeFinishLoading += RemovePlaceHolder;
 129            }
 130        }
 131
 7132        entityHandler.DeselectEntities();
 7133        lastFloorCalalogItemUsed = floorSceneObject;
 7134    }
 135
 136    private void RemovePlaceHolder(BIWEntity entity)
 137    {
 4138        entity.OnShapeFinishLoading -= RemovePlaceHolder;
 4139        RemovePlaceHolder(entity.rootEntity.entityId);
 4140    }
 141
 142    private void OnFloorLoaded(IDCLEntity entity)
 143    {
 5144        entity.OnShapeUpdated -= OnFloorLoaded;
 5145        loadedFloorEntities.Add(entity.entityId);
 5146        RemovePlaceHolder(entity.entityId);
 147
 5148        numberOfParcelsLoaded++;
 5149        if (sceneToEdit != null && numberOfParcelsLoaded >= sceneToEdit.sceneData.parcels.Count())
 5150            OnAllParcelsFloorLoaded?.Invoke();
 0151    }
 152
 153    private void RemovePlaceHolder(string entityId)
 154    {
 9155        if (!floorPlaceHolderDict.ContainsKey(entityId))
 4156            return;
 157
 5158        GameObject floorPlaceHolder = floorPlaceHolderDict[entityId];
 5159        floorPlaceHolderDict.Remove(entityId);
 5160        UnityEngine.Object.Destroy(floorPlaceHolder);
 5161    }
 162
 163    private void RemoveAllPlaceHolders()
 164    {
 20165        foreach (GameObject gameObject in floorPlaceHolderDict.Values)
 166        {
 2167            UnityEngine.Object.Destroy(gameObject);
 168        }
 169
 8170        floorPlaceHolderDict.Clear();
 8171    }
 172
 173    public override void ExitEditMode()
 174    {
 0175        base.ExitEditMode();
 176
 0177        RemoveAllPlaceHolders();
 0178    }
 179}