< Summary

Class:BIWModeController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWModeController.cs
Covered lines:80
Uncovered lines:50
Coverable lines:130
Total lines:282
Line coverage:61.5% (80 of 130)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Initialize(...)0%220100%
Dispose()0%220100%
SetEditorGameObjects()0%110100%
InitGameObjects()0%550100%
IsGodModeActive()0%2100%
GetCurrentEditionPosition()0%6200%
UndoEditionGOLastStep()0%12300%
OnGUI()0%2100%
Update()0%2100%
LateUpdate()0%2100%
EnterEditMode(...)0%220100%
ExitEditMode()0%2100%
GetCurrentMode()0%2100%
GetCurrentStateMode()0%2100%
InputDone()0%6200%
StartMultiSelection()0%2100%
EndMultiSelection()0%2100%
ResetScaleAndRotation()0%2100%
CheckInput()0%6200%
CheckInputSelectedEntities()0%2100%
ShouldCancelUndoAction()0%2100%
CreatedEntity(...)0%220100%
GetMaxDistanceToSelectEntities()0%110100%
EntityDoubleClick(...)0%2100%
GetMousePosition()0%110100%
GetModeCreationEntryPoint()0%6200%
MouseClickDetected()0%6200%
ChangeSnapMode()0%6200%
SetSnapActive(...)0%110100%
ChangeAdvanceMode()0%6200%
SetBuildMode(...)0%8.018094.44%

File(s)

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

#LineLine coverage
 1using DCL.Controllers;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.Runtime.Remoting.Contexts;
 6using DCL;
 7using DCL.Builder;
 8using UnityEngine;
 9
 10public class BIWModeController : BIWController, IBIWModeController
 11{
 12
 13    private GameObject cursorGO;
 14    private GameObject cameraParentGO;
 15
 16    private IBIWActionController actionController;
 17    private IBIWEntityHandler entityHandler;
 18
 19    private BIWFirstPersonMode firstPersonMode;
 020    public BIWGodMode godMode { get; set; }
 21
 22    private InputAction_Trigger toggleSnapModeInputAction;
 23
 24    public event Action OnInputDone;
 25    public event Action<IBIWModeController.EditModeState, IBIWModeController.EditModeState> OnChangedEditModeState;
 26
 27    private IBIWModeController.EditModeState currentEditModeState = IBIWModeController.EditModeState.Inactive;
 28
 29    private BIWMode currentActiveMode;
 30
 31    private bool isSnapActive = false;
 32
 33    private InputAction_Trigger.Triggered snapModeDelegate;
 34
 35    private GameObject editionGO;
 36    private GameObject undoGO;
 37    private GameObject snapGO;
 38    private GameObject freeMovementGO;
 39
 40    public override void Initialize(IContext context)
 41    {
 3442        base.Initialize(context);
 43
 3444        cursorGO = context.sceneReferences.cursorCanvas;
 3445        cameraParentGO = context.sceneReferences.biwCameraParent;
 3446        InitGameObjects();
 47
 3448        firstPersonMode = new BIWFirstPersonMode();
 3449        godMode = new BIWGodMode();
 50
 3451        firstPersonMode.Init(context);
 3452        godMode.Init(context);
 53
 3454        firstPersonMode.OnInputDone += InputDone;
 3455        godMode.OnInputDone += InputDone;
 56
 3457        if ( context.editorContext.editorHUD != null)
 58        {
 3459            context.editorContext.editorHUD.OnChangeModeAction += ChangeAdvanceMode;
 3460            context.editorContext.editorHUD.OnResetAction += ResetScaleAndRotation;
 3461            context.editorContext.editorHUD.OnChangeSnapModeAction += ChangeSnapMode;
 62        }
 63
 3464        actionController = context.editorContext.actionController;
 3465        entityHandler = context.editorContext.entityHandler;
 3466        toggleSnapModeInputAction = context.inputsReferencesAsset.toggleSnapModeInputAction;
 67
 3468        snapModeDelegate = (action) => ChangeSnapMode();
 3469        toggleSnapModeInputAction.OnTriggered += snapModeDelegate;
 70
 3471        firstPersonMode.OnActionGenerated += actionController.AddAction;
 3472        godMode.OnActionGenerated += actionController.AddAction;
 73
 3474        SetEditorGameObjects();
 3475    }
 76
 77    public override void Dispose()
 78    {
 3479        base.Dispose();
 80
 3481        toggleSnapModeInputAction.OnTriggered -= snapModeDelegate;
 82
 3483        firstPersonMode.OnInputDone -= InputDone;
 3484        godMode.OnInputDone -= InputDone;
 85
 3486        firstPersonMode.OnActionGenerated -= actionController.AddAction;
 3487        godMode.OnActionGenerated -= actionController.AddAction;
 88
 3489        firstPersonMode.Dispose();
 3490        godMode.Dispose();
 91
 3492        if ( context.editorContext.editorHUD != null)
 93        {
 3494            context.editorContext.editorHUD.OnChangeModeAction -= ChangeAdvanceMode;
 3495            context.editorContext.editorHUD.OnResetAction -= ResetScaleAndRotation;
 96        }
 97
 3498        GameObject.Destroy(undoGO);
 3499        GameObject.Destroy(snapGO);
 34100        GameObject.Destroy(editionGO);
 34101        GameObject.Destroy(freeMovementGO);
 34102    }
 103
 104    private void SetEditorGameObjects()
 105    {
 34106        godMode.SetEditorReferences(editionGO, undoGO, snapGO, freeMovementGO, entityHandler.GetSelectedEntityList());
 34107        firstPersonMode.SetEditorReferences(editionGO, undoGO, snapGO, freeMovementGO, entityHandler.GetSelectedEntityLi
 34108    }
 109
 110    private void InitGameObjects()
 111    {
 34112        if (snapGO == null)
 34113            snapGO = new GameObject("SnapGameObject");
 114
 34115        if (freeMovementGO == null)
 34116            freeMovementGO = new GameObject("FreeMovementGO");
 117
 34118        freeMovementGO.transform.SetParent(cameraParentGO.transform);
 119
 34120        if (editionGO == null)
 34121            editionGO = new GameObject("EditionGO");
 122
 34123        editionGO.transform.SetParent(cameraParentGO.transform);
 124
 34125        if (undoGO == null)
 34126            undoGO = new GameObject("UndoGameObject");
 34127    }
 128
 0129    public bool IsGodModeActive() { return currentEditModeState == IBIWModeController.EditModeState.GodMode; }
 130
 131    public Vector3 GetCurrentEditionPosition()
 132    {
 0133        if (editionGO != null)
 0134            return editionGO.transform.position;
 135        else
 0136            return Vector3.zero;
 137    }
 138
 139    public void UndoEditionGOLastStep()
 140    {
 0141        if (undoGO == null || editionGO == null)
 0142            return;
 143
 0144        BIWUtils.CopyGameObjectStatus(undoGO, editionGO, false, false);
 0145    }
 146
 147    public override void OnGUI()
 148    {
 0149        base.OnGUI();
 0150        godMode.OnGUI();
 0151    }
 152
 153    public override void Update()
 154    {
 0155        base.Update();
 0156        godMode.Update();
 0157        firstPersonMode.Update();
 0158    }
 159
 160    public override void LateUpdate()
 161    {
 0162        base.LateUpdate();
 0163        firstPersonMode.LateUpdate();
 0164    }
 165
 166    public override void EnterEditMode(IParcelScene scene)
 167    {
 34168        base.EnterEditMode(scene);
 34169        if (currentActiveMode == null)
 34170            SetBuildMode(IBIWModeController.EditModeState.GodMode);
 34171    }
 172
 173    public override void ExitEditMode()
 174    {
 0175        base.ExitEditMode();
 0176        SetBuildMode(IBIWModeController.EditModeState.Inactive);
 0177        snapGO.transform.SetParent(null);
 0178    }
 179
 0180    public BIWMode GetCurrentMode() => currentActiveMode;
 181
 0182    public IBIWModeController.EditModeState GetCurrentStateMode() => currentEditModeState;
 183
 0184    private void InputDone() { OnInputDone?.Invoke(); }
 185
 0186    public void StartMultiSelection() { currentActiveMode.StartMultiSelection(); }
 187
 0188    public void EndMultiSelection() { currentActiveMode.EndMultiSelection(); }
 189
 0190    public void ResetScaleAndRotation() { currentActiveMode.ResetScaleAndRotation(); }
 191
 0192    public void CheckInput() { currentActiveMode?.CheckInput(); }
 193
 0194    public void CheckInputSelectedEntities() { currentActiveMode.CheckInputSelectedEntities(); }
 195
 0196    public bool ShouldCancelUndoAction() { return currentActiveMode.ShouldCancelUndoAction(); }
 197
 2198    public void CreatedEntity(BIWEntity entity) { currentActiveMode?.CreatedEntity(entity); }
 199
 1200    public float GetMaxDistanceToSelectEntities() { return currentActiveMode.maxDistanceToSelectEntities; }
 201
 0202    public void EntityDoubleClick(BIWEntity entity) {  currentActiveMode.EntityDoubleClick(entity); }
 203
 1204    public Vector3 GetMousePosition() { return currentActiveMode.GetPointerPosition(); }
 205
 206    public Vector3 GetModeCreationEntryPoint()
 207    {
 0208        if (currentActiveMode != null)
 0209            return currentActiveMode.GetCreatedEntityPoint();
 0210        return Vector3.zero;
 211    }
 212
 0213    public void MouseClickDetected() { currentActiveMode?.MouseClickDetected(); }
 214
 215    private void ChangeSnapMode()
 216    {
 0217        SetSnapActive(!isSnapActive);
 0218        InputDone();
 219
 0220        if (isSnapActive)
 0221            AudioScriptableObjects.enable.Play();
 222        else
 0223            AudioScriptableObjects.disable.Play();
 0224    }
 225
 226    public void SetSnapActive(bool isActive)
 227    {
 2228        isSnapActive = isActive;
 2229        currentActiveMode.SetSnapActive(isActive);
 2230    }
 231
 232    public void ChangeAdvanceMode()
 233    {
 0234        if (currentEditModeState == IBIWModeController.EditModeState.GodMode)
 235        {
 0236            SetBuildMode(IBIWModeController.EditModeState.FirstPerson);
 0237        }
 238        else
 239        {
 0240            SetBuildMode(IBIWModeController.EditModeState.GodMode);
 241        }
 242
 0243        InputDone();
 0244    }
 245
 246    public void SetBuildMode(IBIWModeController.EditModeState state)
 247    {
 40248        IBIWModeController.EditModeState previousState = currentEditModeState;
 249
 40250        if (currentActiveMode != null)
 4251            currentActiveMode.Deactivate();
 252
 40253        currentActiveMode = null;
 254        switch (state)
 255        {
 256            case IBIWModeController.EditModeState.Inactive:
 257                break;
 258            case IBIWModeController.EditModeState.FirstPerson:
 1259                currentActiveMode = firstPersonMode;
 260
 1261                if (cursorGO != null)
 1262                    cursorGO.SetActive(true);
 1263                break;
 264            case IBIWModeController.EditModeState.GodMode:
 36265                if (cursorGO != null)
 36266                    cursorGO.SetActive(false);
 36267                currentActiveMode = godMode;
 268                break;
 269        }
 270
 40271        currentEditModeState = state;
 272
 40273        if (currentActiveMode != null)
 274        {
 37275            currentActiveMode.Activate(sceneToEdit);
 37276            currentActiveMode.SetSnapActive(isSnapActive);
 37277            entityHandler.SetActiveMode(currentActiveMode);
 278        }
 279
 40280        OnChangedEditModeState?.Invoke(previousState, state);
 0281    }
 282}