< Summary

Class:BuilderInWorldEditor
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/BuilderInWorldEditor.cs
Covered lines:161
Uncovered lines:31
Coverable lines:192
Total lines:385
Line coverage:83.8% (161 of 192)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldEditor()0%110100%
Initialize(...)0%22093.75%
InitReferences(...)0%3.433063.64%
InitHUD(...)0%110100%
Dispose()0%330100%
OnGUI()0%3.033085.71%
Update()0%330100%
LateUpdate()0%3.033085.71%
OnNFTUsageChange()0%2100%
InitControllers()0%110100%
InitController(...)0%110100%
StartTutorial()0%2100%
CleanItems()0%550100%
ChangeProjectNameAndDescription(...)0%2100%
EnterEditMode(...)0%8.048091.18%
ExitEditMode()0%4.014093.1%
ActivateAvatars()0%2.152066.67%
ActivatePlayerAvatar(...)0%2100%
DeactivatePlayerAvatar(...)0%2100%
DeactivateAvatars()0%2.152066.67%
EnterBiwControllers()0%220100%
ExitBiwControllers()0%330100%
IsNewScene()0%110100%
SetupNewScene()0%110100%
NewSceneFloorLoaded()0%6200%

File(s)

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

#LineLine coverage
 1using DCL;
 2using DCL.Configuration;
 3using DCL.Controllers;
 4using DCL.Tutorial;
 5using Newtonsoft.Json;
 6using System.Collections;
 7using System.Collections.Generic;
 8using System.Linq;
 9using DCL.Builder;
 10using UnityEngine;
 11using Environment = DCL.Environment;
 12
 13public class BuilderInWorldEditor : IBIWEditor
 14{
 15    internal const string AVATAR_HIDE_CONSTRAINT = "BuilderInWorld_Avatar_Invisible";
 16
 17    private GameObject cursorGO;
 18    private GameObject[] groundVisualsGO;
 19
 920    internal IBIWOutlinerController outlinerController => context.editorContext.outlinerController;
 821    internal IBIWInputHandler inputHandler => context.editorContext.inputHandler;
 822    internal IBIWPublishController publishController => context.editorContext.publishController;
 1623    internal IBIWCreatorController creatorController => context.editorContext.creatorController;
 824    internal IBIWModeController modeController => context.editorContext.modeController;
 3825    internal IBIWFloorHandler floorHandler => context.editorContext.floorHandler;
 826    internal IBIWEntityHandler entityHandler => context.editorContext.entityHandler;
 827    internal IBIWActionController actionController => context.editorContext.actionController;
 828    internal IBIWSaveController saveController => context.editorContext.saveController;
 829    internal IBIWInputWrapper inputWrapper => context.editorContext.inputWrapper;
 830    internal IBIWRaycastController raycastController => context.editorContext.raycastController;
 831    internal IBIWGizmosController gizmosController => context.editorContext.gizmosController;
 32
 33    private BuilderInWorldBridge builderInWorldBridge;
 34    private BuilderInWorldAudioHandler biwAudioHandler;
 35    private DCL.Camera.CameraController mainCameraController;
 36
 37    internal IContext context;
 38
 839    private readonly List<IBIWController> controllers = new List<IBIWController>();
 40    private Material skyBoxMaterial;
 41
 042    public bool isBuilderInWorldActivated { get; internal set; } = false;
 43
 44    private bool isInit = false;
 45    private Material previousSkyBoxMaterial;
 46    private PlayerAvatarController avatarRenderer;
 47
 48    private float startEditorTimeStamp = 0;
 49    internal IBuilderScene sceneToEdit;
 50
 51    public void Initialize(IContext context)
 52    {
 853        if (isInit)
 054            return;
 55
 856        isInit = true;
 57
 858        this.context = context;
 59
 860        InitReferences(SceneReferences.i);
 61
 862        BIWNFTController.i.OnNFTUsageChange += OnNFTUsageChange;
 63
 864        InitHUD(context);
 65
 866        InitControllers();
 67
 868        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true);
 69
 870        mainCameraController = context.sceneReferences.cameraController.GetComponent<DCL.Camera.CameraController>();
 871        biwAudioHandler = UnityEngine.Object.Instantiate(context.projectReferencesAsset.audioPrefab, Vector3.zero, Quate
 872        biwAudioHandler.Initialize(context);
 873        biwAudioHandler.gameObject.SetActive(false);
 74
 875        floorHandler.OnAllParcelsFloorLoaded += NewSceneFloorLoaded;
 76
 877        avatarRenderer = context.sceneReferences.playerAvatarController.GetComponent<PlayerAvatarController>();
 878    }
 79
 80    public void InitReferences(SceneReferences sceneReferences)
 81    {
 882        builderInWorldBridge = sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>();
 883        cursorGO = sceneReferences.cursorCanvas;
 84
 885        List<GameObject> grounds = new List<GameObject>();
 86
 887        if (sceneReferences.groundVisual != null)
 88        {
 089            for (int i = 0; i < sceneReferences.groundVisual.transform.transform.childCount; i++)
 90            {
 091                grounds.Add(sceneReferences.groundVisual.transform.transform.GetChild(i).gameObject);
 92            }
 93        }
 94
 895        groundVisualsGO = grounds.ToArray();
 896        skyBoxMaterial = context.projectReferencesAsset.skyBoxMaterial;
 897    }
 98
 99    private void InitHUD(IContext context)
 100    {
 8101        context.editorContext.editorHUD.Initialize(context);
 8102        context.editorContext.editorHUD.OnTutorialAction += StartTutorial;
 8103        context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged += ChangeProjectNameAndDescription;
 8104    }
 105
 106    public void Dispose()
 107    {
 8108        if (context.editorContext.editorHUD != null)
 109        {
 8110            context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged -= ChangeProjectNameAndDescription;
 8111            context.editorContext.editorHUD.OnTutorialAction -= StartTutorial;
 112        }
 113
 8114        floorHandler.OnAllParcelsFloorLoaded -= NewSceneFloorLoaded;
 8115        BIWNFTController.i.OnNFTUsageChange -= OnNFTUsageChange;
 8116        BIWNFTController.i.Dispose();
 117
 8118        CleanItems();
 119
 8120        if (biwAudioHandler.gameObject != null)
 121        {
 8122            biwAudioHandler.Dispose();
 8123            UnityEngine.Object.Destroy(biwAudioHandler.gameObject);
 124        }
 8125    }
 126
 127    public void OnGUI()
 128    {
 1129        if (!isBuilderInWorldActivated)
 0130            return;
 131
 28132        foreach (var controller in controllers)
 133        {
 13134            controller.OnGUI();
 135        }
 1136    }
 137
 138    public void Update()
 139    {
 2140        if (!isBuilderInWorldActivated)
 1141            return;
 142
 28143        foreach (var controller in controllers)
 144        {
 13145            controller.Update();
 146        }
 1147    }
 148
 149    public void LateUpdate()
 150    {
 1151        if (!isBuilderInWorldActivated)
 0152            return;
 153
 28154        foreach (var controller in controllers)
 155        {
 13156            controller.LateUpdate();
 157        }
 1158    }
 159
 160    private void OnNFTUsageChange()
 161    {
 0162        context.editorContext.editorHUD.RefreshCatalogAssetPack();
 0163        context.editorContext.editorHUD.RefreshCatalogContent();
 0164    }
 165
 166    private void InitControllers()
 167    {
 8168        InitController(entityHandler);
 8169        InitController(modeController);
 8170        InitController(publishController);
 8171        InitController(creatorController);
 8172        InitController(outlinerController);
 8173        InitController(floorHandler);
 8174        InitController(inputHandler);
 8175        InitController(actionController);
 8176        InitController(inputWrapper);
 8177        InitController(raycastController);
 8178        InitController(gizmosController);
 8179        InitController(saveController);
 8180    }
 181
 182    public void InitController(IBIWController controller)
 183    {
 103184        controller.Initialize(context);
 103185        controllers.Add(controller);
 103186    }
 187
 0188    private void StartTutorial() { TutorialController.i.SetBuilderInWorldTutorialEnabled(); }
 189
 190    public void CleanItems()
 191    {
 8192        if ( context.editorContext.editorHUD != null)
 8193            context.editorContext.editorHUD.Dispose();
 194
 8195        Camera camera = Camera.main;
 196
 8197        if (camera != null)
 198        {
 8199            BIWOutline outliner = camera.GetComponent<BIWOutline>();
 8200            UnityEngine.Object.Destroy(outliner);
 201        }
 202
 8203        floorHandler?.CleanUp();
 8204        creatorController?.CleanUp();
 8205    }
 206
 207    private void ChangeProjectNameAndDescription(string name, string description)
 208    {
 0209        sceneToEdit.manifest.project.title = name;
 0210        sceneToEdit.manifest.project.description = description;
 211
 0212        saveController.ForceSave();
 0213    }
 214
 215    public void EnterEditMode(IBuilderScene builderScene)
 216    {
 5217        sceneToEdit = builderScene;
 218
 5219        BIWNFTController.i.StartEditMode();
 5220        ParcelSettings.VISUAL_LOADING_ENABLED = false;
 221
 5222        if (biwAudioHandler != null && biwAudioHandler.gameObject != null)
 5223            biwAudioHandler.gameObject.SetActive(true);
 224
 5225        cursorGO.SetActive(false);
 226
 5227        if ( context.editorContext.editorHUD != null)
 228        {
 5229            context.editorContext.editorHUD.SetParcelScene(sceneToEdit.scene);
 5230            context.editorContext.editorHUD.RefreshCatalogContent();
 5231            context.editorContext.editorHUD.RefreshCatalogAssetPack();
 5232            context.editorContext.editorHUD.SetVisibilityOfCatalog(true);
 5233            context.editorContext.editorHUD.SetVisibilityOfInspector(true);
 5234            if (sceneToEdit.HasBeenCreatedThisSession() && sceneToEdit.sceneType == IBuilderScene.SceneType.LAND)
 0235                context.editorContext.editorHUD.NewSceneForLand(sceneToEdit);
 236        }
 237
 5238        var culling = mainCameraController.GetCulling();
 5239        mainCameraController.SetCulling(  BIWUtils.GetBIWCulling(culling));
 240
 5241        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(false);
 5242        DataStore.i.builderInWorld.showTaskBar.Set(true);
 243
 5244        EnterBiwControllers();
 5245        Environment.i.world.sceneController.ActivateBuilderInWorldEditScene();
 246
 5247        if (IsNewScene())
 5248            SetupNewScene();
 249
 5250        isBuilderInWorldActivated = true;
 251
 5252        previousSkyBoxMaterial = RenderSettings.skybox;
 5253        RenderSettings.skybox = skyBoxMaterial;
 254
 5255        DeactivateAvatars();
 256
 10257        foreach (var groundVisual in groundVisualsGO)
 258        {
 0259            groundVisual.SetActive(false);
 260        }
 261
 5262        DataStore.i.player.otherPlayers.OnAdded += DeactivatePlayerAvatar;
 263
 5264        startEditorTimeStamp = Time.realtimeSinceStartup;
 265
 5266        BIWAnalytics.AddSceneInfo(sceneToEdit.scene.sceneData.basePosition, BIWUtils.GetLandOwnershipType(DataStore.i.bu
 5267    }
 268
 269    public void ExitEditMode()
 270    {
 1271        Environment.i.platform.cullingController.Start();
 1272        BIWNFTController.i.ExitEditMode();
 273
 1274        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true);
 1275        DataStore.i.builderInWorld.showTaskBar.Set(true);
 276
 1277        ParcelSettings.VISUAL_LOADING_ENABLED = true;
 278
 1279        var culling = mainCameraController.GetCulling();
 1280        culling -= BIWSettings.FX_LAYER;
 1281        mainCameraController.SetCulling(culling);
 282
 1283        outlinerController.CancelAllOutlines();
 284
 1285        cursorGO.SetActive(true);
 286
 1287        if ( context.editorContext.editorHUD != null)
 288        {
 1289            context.editorContext.editorHUD.ClearEntityList();
 1290            context.editorContext.editorHUD.SetVisibility(false);
 291        }
 292
 1293        Environment.i.world.sceneController.DeactivateBuilderInWorldEditScene();
 1294        Environment.i.world.blockersController.SetEnabled(true);
 295
 1296        ExitBiwControllers();
 297
 2298        foreach (var groundVisual in groundVisualsGO)
 299        {
 0300            groundVisual.SetActive(true);
 301        }
 302
 1303        isBuilderInWorldActivated = false;
 1304        RenderSettings.skybox = previousSkyBoxMaterial;
 305
 1306        if (biwAudioHandler.gameObject != null)
 1307            biwAudioHandler.gameObject.SetActive(false);
 1308        DataStore.i.common.appMode.Set(AppMode.DEFAULT);
 1309        DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f);
 310
 1311        ActivateAvatars();
 312
 1313        BIWAnalytics.ExitEditor(Time.realtimeSinceStartup - startEditorTimeStamp);
 1314    }
 315
 316    internal void ActivateAvatars()
 317    {
 1318        avatarRenderer.SetAvatarVisibility(true);
 319
 2320        foreach (Player player in DataStore.i.player.otherPlayers.GetValues())
 321        {
 0322            ActivatePlayerAvatar(null,player);
 323        }
 1324    }
 325
 326    internal void ActivatePlayerAvatar(string id,Player player)
 327    {
 0328        player.avatar.RemoveVisibilityConstrain(AVATAR_HIDE_CONSTRAINT);
 0329        player.playerName.RemoveVisibilityConstaint(AVATAR_HIDE_CONSTRAINT);
 0330    }
 331
 332    internal void DeactivatePlayerAvatar(string id,Player player)
 333    {
 0334        player.avatar.AddVisibilityConstrain(AVATAR_HIDE_CONSTRAINT);
 0335        player.playerName.AddVisibilityConstaint(AVATAR_HIDE_CONSTRAINT);
 0336    }
 337
 338    internal void DeactivateAvatars()
 339    {
 5340        avatarRenderer.SetAvatarVisibility(false);
 341
 10342        foreach (Player player in DataStore.i.player.otherPlayers.GetValues())
 343        {
 0344            DeactivatePlayerAvatar(null, player);
 345        }
 5346    }
 347
 348    public void EnterBiwControllers()
 349    {
 166350        foreach (var controller in controllers)
 351        {
 77352            controller.EnterEditMode(sceneToEdit);
 353        }
 354
 355        //Note: This audio should inside the controllers, it is here because it is still a monobehaviour
 6356        biwAudioHandler.EnterEditMode(sceneToEdit.scene);
 6357    }
 358
 359    public void ExitBiwControllers()
 360    {
 28361        foreach (var controller in controllers)
 362        {
 13363            controller.ExitEditMode();
 364        }
 365
 1366        if (biwAudioHandler.gameObject != null)
 1367            biwAudioHandler.ExitEditMode();
 1368    }
 369
 5370    public bool IsNewScene() { return sceneToEdit.scene.entities.Count <= 0; }
 371
 12372    public void SetupNewScene() { floorHandler.CreateDefaultFloor(); }
 373
 374    private void NewSceneFloorLoaded()
 375    {
 0376        if (!sceneToEdit.HasBeenCreatedThisSession())
 0377            return;
 378
 0379        context.cameraController.TakeSceneScreenshotFromResetPosition(snapshot =>
 380            {
 0381                context.builderAPIController.SetThumbnail(sceneToEdit.manifest.project.id, snapshot);
 0382            }
 383        );
 0384    }
 385}