< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BuilderInWorldEditor()0%2100%
Initialize(...)0%6200%
InitReferences(...)0%12300%
InitHUD(...)0%2100%
Dispose()0%12300%
OnGUI()0%12300%
Update()0%12300%
LateUpdate()0%12300%
OnNFTUsageChange()0%2100%
InitControllers()0%2100%
InitController(...)0%2100%
StartTutorial()0%2100%
CleanItems()0%30500%
ChangeProjectNameAndDescription(...)0%2100%
EnterEditMode(...)0%72800%
ExitEditMode()0%20400%
ActivateAvatars()0%6200%
ActivatePlayerAvatar(...)0%2100%
DeactivatePlayerAvatar(...)0%2100%
DeactivateAvatars()0%6200%
EnterBiwControllers()0%6200%
ExitBiwControllers()0%12300%
IsNewScene()0%2100%
SetupNewScene()0%2100%
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
 020    internal IBIWOutlinerController outlinerController => context.editorContext.outlinerController;
 021    internal IBIWInputHandler inputHandler => context.editorContext.inputHandler;
 022    internal IBIWPublishController publishController => context.editorContext.publishController;
 023    internal IBIWCreatorController creatorController => context.editorContext.creatorController;
 024    internal IBIWModeController modeController => context.editorContext.modeController;
 025    internal IBIWFloorHandler floorHandler => context.editorContext.floorHandler;
 026    internal IBIWEntityHandler entityHandler => context.editorContext.entityHandler;
 027    internal IBIWActionController actionController => context.editorContext.actionController;
 028    internal IBIWSaveController saveController => context.editorContext.saveController;
 029    internal IBIWInputWrapper inputWrapper => context.editorContext.inputWrapper;
 030    internal IBIWRaycastController raycastController => context.editorContext.raycastController;
 031    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
 039    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    {
 053        if (isInit)
 054            return;
 55
 056        isInit = true;
 57
 058        this.context = context;
 59
 060        InitReferences(SceneReferences.i);
 61
 062        BIWNFTController.i.OnNFTUsageChange += OnNFTUsageChange;
 63
 064        InitHUD(context);
 65
 066        InitControllers();
 67
 068        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true);
 69
 070        mainCameraController = context.sceneReferences.cameraController.GetComponent<DCL.Camera.CameraController>();
 071        biwAudioHandler = UnityEngine.Object.Instantiate(context.projectReferencesAsset.audioPrefab, Vector3.zero, Quate
 072        biwAudioHandler.Initialize(context);
 073        biwAudioHandler.gameObject.SetActive(false);
 74
 075        floorHandler.OnAllParcelsFloorLoaded += NewSceneFloorLoaded;
 76
 077        avatarRenderer = context.sceneReferences.playerAvatarController.GetComponent<PlayerAvatarController>();
 078    }
 79
 80    public void InitReferences(SceneReferences sceneReferences)
 81    {
 082        builderInWorldBridge = sceneReferences.biwBridgeGameObject.GetComponent<BuilderInWorldBridge>();
 083        cursorGO = sceneReferences.cursorCanvas;
 84
 085        List<GameObject> grounds = new List<GameObject>();
 86
 087        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
 095        groundVisualsGO = grounds.ToArray();
 096        skyBoxMaterial = context.projectReferencesAsset.skyBoxMaterial;
 097    }
 98
 99    private void InitHUD(IContext context)
 100    {
 0101        context.editorContext.editorHUD.Initialize(context);
 0102        context.editorContext.editorHUD.OnTutorialAction += StartTutorial;
 0103        context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged += ChangeProjectNameAndDescription;
 0104    }
 105
 106    public void Dispose()
 107    {
 0108        if (context.editorContext.editorHUD != null)
 109        {
 0110            context.editorContext.editorHUD.OnProjectNameAndDescriptionChanged -= ChangeProjectNameAndDescription;
 0111            context.editorContext.editorHUD.OnTutorialAction -= StartTutorial;
 112        }
 113
 0114        floorHandler.OnAllParcelsFloorLoaded -= NewSceneFloorLoaded;
 0115        BIWNFTController.i.OnNFTUsageChange -= OnNFTUsageChange;
 0116        BIWNFTController.i.Dispose();
 117
 0118        CleanItems();
 119
 0120        if (biwAudioHandler.gameObject != null)
 121        {
 0122            biwAudioHandler.Dispose();
 0123            UnityEngine.Object.Destroy(biwAudioHandler.gameObject);
 124        }
 0125    }
 126
 127    public void OnGUI()
 128    {
 0129        if (!isBuilderInWorldActivated)
 0130            return;
 131
 0132        foreach (var controller in controllers)
 133        {
 0134            controller.OnGUI();
 135        }
 0136    }
 137
 138    public void Update()
 139    {
 0140        if (!isBuilderInWorldActivated)
 0141            return;
 142
 0143        foreach (var controller in controllers)
 144        {
 0145            controller.Update();
 146        }
 0147    }
 148
 149    public void LateUpdate()
 150    {
 0151        if (!isBuilderInWorldActivated)
 0152            return;
 153
 0154        foreach (var controller in controllers)
 155        {
 0156            controller.LateUpdate();
 157        }
 0158    }
 159
 160    private void OnNFTUsageChange()
 161    {
 0162        context.editorContext.editorHUD.RefreshCatalogAssetPack();
 0163        context.editorContext.editorHUD.RefreshCatalogContent();
 0164    }
 165
 166    private void InitControllers()
 167    {
 0168        InitController(entityHandler);
 0169        InitController(modeController);
 0170        InitController(publishController);
 0171        InitController(creatorController);
 0172        InitController(outlinerController);
 0173        InitController(floorHandler);
 0174        InitController(inputHandler);
 0175        InitController(actionController);
 0176        InitController(inputWrapper);
 0177        InitController(raycastController);
 0178        InitController(gizmosController);
 0179        InitController(saveController);
 0180    }
 181
 182    public void InitController(IBIWController controller)
 183    {
 0184        controller.Initialize(context);
 0185        controllers.Add(controller);
 0186    }
 187
 0188    private void StartTutorial() { TutorialController.i.SetBuilderInWorldTutorialEnabled(); }
 189
 190    public void CleanItems()
 191    {
 0192        if ( context.editorContext.editorHUD != null)
 0193            context.editorContext.editorHUD.Dispose();
 194
 0195        Camera camera = Camera.main;
 196
 0197        if (camera != null)
 198        {
 0199            BIWOutline outliner = camera.GetComponent<BIWOutline>();
 0200            UnityEngine.Object.Destroy(outliner);
 201        }
 202
 0203        floorHandler?.CleanUp();
 0204        creatorController?.CleanUp();
 0205    }
 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    {
 0217        sceneToEdit = builderScene;
 218
 0219        BIWNFTController.i.StartEditMode();
 0220        ParcelSettings.VISUAL_LOADING_ENABLED = false;
 221
 0222        if (biwAudioHandler != null && biwAudioHandler.gameObject != null)
 0223            biwAudioHandler.gameObject.SetActive(true);
 224
 0225        cursorGO.SetActive(false);
 226
 0227        if ( context.editorContext.editorHUD != null)
 228        {
 0229            context.editorContext.editorHUD.SetParcelScene(sceneToEdit.scene);
 0230            context.editorContext.editorHUD.RefreshCatalogContent();
 0231            context.editorContext.editorHUD.RefreshCatalogAssetPack();
 0232            context.editorContext.editorHUD.SetVisibilityOfCatalog(true);
 0233            context.editorContext.editorHUD.SetVisibilityOfInspector(true);
 0234            if (sceneToEdit.HasBeenCreatedThisSession() && sceneToEdit.sceneType == IBuilderScene.SceneType.LAND)
 0235                context.editorContext.editorHUD.NewSceneForLand(sceneToEdit);
 236        }
 237
 0238        var culling = mainCameraController.GetCulling();
 0239        mainCameraController.SetCulling(  BIWUtils.GetBIWCulling(culling));
 240
 0241        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(false);
 0242        DataStore.i.builderInWorld.showTaskBar.Set(true);
 243
 0244        EnterBiwControllers();
 0245        Environment.i.world.sceneController.ActivateBuilderInWorldEditScene();
 246
 0247        if (IsNewScene())
 0248            SetupNewScene();
 249
 0250        isBuilderInWorldActivated = true;
 251
 0252        previousSkyBoxMaterial = RenderSettings.skybox;
 0253        RenderSettings.skybox = skyBoxMaterial;
 254
 0255        DeactivateAvatars();
 256
 0257        foreach (var groundVisual in groundVisualsGO)
 258        {
 0259            groundVisual.SetActive(false);
 260        }
 261
 0262        DataStore.i.player.otherPlayers.OnAdded += DeactivatePlayerAvatar;
 263
 0264        startEditorTimeStamp = Time.realtimeSinceStartup;
 265
 0266        BIWAnalytics.AddSceneInfo(sceneToEdit.scene.sceneData.basePosition, BIWUtils.GetLandOwnershipType(DataStore.i.bu
 0267    }
 268
 269    public void ExitEditMode()
 270    {
 0271        Environment.i.platform.cullingController.Start();
 0272        BIWNFTController.i.ExitEditMode();
 273
 0274        CommonScriptableObjects.builderInWorldNotNecessaryUIVisibilityStatus.Set(true);
 0275        DataStore.i.builderInWorld.showTaskBar.Set(true);
 276
 0277        ParcelSettings.VISUAL_LOADING_ENABLED = true;
 278
 0279        var culling = mainCameraController.GetCulling();
 0280        culling -= BIWSettings.FX_LAYER;
 0281        mainCameraController.SetCulling(culling);
 282
 0283        outlinerController.CancelAllOutlines();
 284
 0285        cursorGO.SetActive(true);
 286
 0287        if ( context.editorContext.editorHUD != null)
 288        {
 0289            context.editorContext.editorHUD.ClearEntityList();
 0290            context.editorContext.editorHUD.SetVisibility(false);
 291        }
 292
 0293        Environment.i.world.sceneController.DeactivateBuilderInWorldEditScene();
 0294        Environment.i.world.blockersController.SetEnabled(true);
 295
 0296        ExitBiwControllers();
 297
 0298        foreach (var groundVisual in groundVisualsGO)
 299        {
 0300            groundVisual.SetActive(true);
 301        }
 302
 0303        isBuilderInWorldActivated = false;
 0304        RenderSettings.skybox = previousSkyBoxMaterial;
 305
 0306        if (biwAudioHandler.gameObject != null)
 0307            biwAudioHandler.gameObject.SetActive(false);
 0308        DataStore.i.common.appMode.Set(AppMode.DEFAULT);
 0309        DataStore.i.virtualAudioMixer.sceneSFXVolume.Set(1f);
 310
 0311        ActivateAvatars();
 312
 0313        BIWAnalytics.ExitEditor(Time.realtimeSinceStartup - startEditorTimeStamp);
 0314    }
 315
 316    internal void ActivateAvatars()
 317    {
 0318        avatarRenderer.SetAvatarVisibility(true);
 319
 0320        foreach (Player player in DataStore.i.player.otherPlayers.GetValues())
 321        {
 0322            ActivatePlayerAvatar(null,player);
 323        }
 0324    }
 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.AddVisibilityConstraint(AVATAR_HIDE_CONSTRAINT);
 0335        player.playerName.AddVisibilityConstaint(AVATAR_HIDE_CONSTRAINT);
 0336    }
 337
 338    internal void DeactivateAvatars()
 339    {
 0340        avatarRenderer.SetAvatarVisibility(false);
 341
 0342        foreach (Player player in DataStore.i.player.otherPlayers.GetValues())
 343        {
 0344            DeactivatePlayerAvatar(null, player);
 345        }
 0346    }
 347
 348    public void EnterBiwControllers()
 349    {
 0350        foreach (var controller in controllers)
 351        {
 0352            controller.EnterEditMode(sceneToEdit);
 353        }
 354
 355        //Note: This audio should inside the controllers, it is here because it is still a monobehaviour
 0356        biwAudioHandler.EnterEditMode(sceneToEdit.scene);
 0357    }
 358
 359    public void ExitBiwControllers()
 360    {
 0361        foreach (var controller in controllers)
 362        {
 0363            controller.ExitEditMode();
 364        }
 365
 0366        if (biwAudioHandler.gameObject != null)
 0367            biwAudioHandler.ExitEditMode();
 0368    }
 369
 0370    public bool IsNewScene() { return sceneToEdit.scene.entities.Count <= 0; }
 371
 0372    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}