< Summary

Class:BIWMode
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/States/BIWMode.cs
Covered lines:110
Uncovered lines:17
Coverable lines:127
Total lines:267
Line coverage:86.6% (110 of 127)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWMode()0%110100%
Init(...)0%110100%
SetEditorReferences(...)0%110100%
Dispose()0%110100%
IsActive()0%2100%
Activate(...)0%110100%
Deactivate()0%110100%
SetSnapActive(...)0%660100%
StartMultiSelection()0%110100%
GetPointerPosition()0%110100%
EndMultiSelection()0%110100%
ShouldCancelUndoAction()0%2100%
SetDuplicationOffset(...)0%2100%
EntityDoubleClick(...)0%2100%
SelectedEntity(...)0%110100%
CenterGameObjectToEdit()0%440100%
MouseClickDetected()0%330100%
CreatedEntity(...)0%110100%
EntityDeselected(...)0%2.032080%
OnDeleteEntity(...)0%2100%
OnDeselectedEntities()0%110100%
CheckInput()0%110100%
CheckInputSelectedEntities()0%2100%
InputDone()0%6200%
ResetScaleAndRotation()0%2.012084.62%
GetCreatedEntityPoint()0%2100%
GetCenterPointOfSelectedObjects()0%220100%
TransformActionStarted(...)0%550100%
TransformActionEnd(...)0%11.3511085.71%
ActionFinish(...)0%440100%
Update()0%110100%
OnGUI()0%2100%
LateUpdate()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/States/BIWMode.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Models;
 3using System;
 4using System.Collections.Generic;
 5using DCL.Configuration;
 6using UnityEngine;
 7
 8public class BIWMode
 9{
 10    public event Action OnInputDone;
 11    public event Action<BIWCompleteAction> OnActionGenerated;
 12
 013    public bool isSnapActive => isSnapActiveValue;
 014    public float maxDistanceToSelectEntities => maxDistanceToSelectEntitiesValue;
 15
 13516    protected float maxDistanceToSelectEntitiesValue = 50;
 17
 18    //Note: Snap variables are set in each mode
 13519    protected float snapFactor = 1f;
 13520    protected float snapRotationDegresFactor = 15f;
 13521    protected float snapScaleFactor = 0.5f;
 13522    protected float snapDistanceToActivateMovement = 10f;
 23
 24    protected IBIWEntityHandler entityHandler;
 25    protected IBIWSaveController saveController;
 26    protected IBIWActionController actionController;
 27    protected IBIWRaycastController raycastController;
 28
 29    protected GameObject editionGO, undoGO, snapGO, freeMovementGO;
 30
 31    internal bool isSnapActiveValue = false;
 32    internal bool isModeActive = false;
 33    internal bool isMultiSelectionActive = false;
 34    internal List<BIWEntity> selectedEntities;
 35
 36    internal bool isNewObjectPlaced = false;
 37
 13538    internal List<BIWEntityAction> actionList = new List<BIWEntityAction>();
 39
 40    public virtual void Init(BIWContext context)
 41    {
 13542        entityHandler = context.entityHandler;
 13543        saveController = context.saveController;
 13544        actionController = context.actionController;
 13545        raycastController = context.raycastController;
 13546        entityHandler.OnEntityDeleted += OnDeleteEntity;
 13547    }
 48
 49    public virtual void SetEditorReferences(GameObject goToEdit, GameObject undoGO, GameObject snapGO, GameObject freeMo
 50    {
 17051        editionGO = goToEdit;
 17052        this.undoGO = undoGO;
 17053        this.snapGO = snapGO;
 17054        this.freeMovementGO = freeMovementGO;
 55
 17056        this.selectedEntities = selectedEntities;
 17057    }
 58
 27059    public virtual void Dispose() { entityHandler.OnEntityDeleted -= OnDeleteEntity; }
 60
 061    public bool IsActive() { return isModeActive; }
 262    public virtual void Activate(IParcelScene scene) { isModeActive = true; }
 63
 64    public virtual void Deactivate()
 65    {
 766        isModeActive = false;
 767        entityHandler.DeselectEntities();
 768    }
 69
 70    public virtual void SetSnapActive(bool isActive)
 71    {
 5772        if (isActive && !isSnapActiveValue)
 373            AudioScriptableObjects.enable.Play();
 5474        else if (!isActive && isSnapActiveValue)
 375            AudioScriptableObjects.disable.Play();
 76
 5777        isSnapActiveValue = isActive;
 5778        HUDController.i.builderInWorldMainHud?.SetSnapModeActive(isSnapActiveValue);
 879    }
 80
 281    public virtual void StartMultiSelection() { isMultiSelectionActive = true; }
 82
 183    public virtual Vector3 GetPointerPosition() { return Input.mousePosition; }
 84
 285    public virtual void EndMultiSelection() { isMultiSelectionActive = false; }
 86
 087    public virtual bool ShouldCancelUndoAction() { return false; }
 88
 089    public virtual void SetDuplicationOffset(float offset) { }
 90
 091    public virtual void EntityDoubleClick(BIWEntity entity) { }
 92
 93    public virtual void SelectedEntity(BIWEntity selectedEntity)
 94    {
 295        CenterGameObjectToEdit();
 96
 297        BIWUtils.CopyGameObjectStatus(editionGO, undoGO, false, false);
 298    }
 99
 100    public virtual void CenterGameObjectToEdit()
 101    {
 4102        if (selectedEntities.Count > 0)
 103        {
 8104            foreach (BIWEntity entity in selectedEntities)
 105            {
 2106                entity.rootEntity.gameObject.transform.SetParent(null);
 107            }
 108
 2109            editionGO.transform.position = GetCenterPointOfSelectedObjects();
 2110            editionGO.transform.rotation = Quaternion.Euler(0, 0, 0);
 2111            editionGO.transform.localScale = Vector3.one;
 8112            foreach (BIWEntity entity in selectedEntities)
 113            {
 2114                entity.rootEntity.gameObject.transform.SetParent(editionGO.transform);
 115            }
 116        }
 4117    }
 118
 119    public virtual void MouseClickDetected()
 120    {
 2121        BIWEntity entityToSelect = raycastController.GetEntityOnPointer();
 2122        if (entityToSelect != null)
 123        {
 1124            entityHandler.EntityClicked(entityToSelect);
 1125        }
 1126        else if (!isMultiSelectionActive)
 127        {
 1128            entityHandler.DeselectEntities();
 129        }
 1130    }
 131
 2132    public virtual void CreatedEntity(BIWEntity createdEntity) { isNewObjectPlaced = true; }
 133
 134    public virtual void EntityDeselected(BIWEntity entityDeselected)
 135    {
 1136        CenterGameObjectToEdit();
 137
 1138        if (isNewObjectPlaced)
 139        {
 0140            actionController.CreateActionEntityCreated(entityDeselected.rootEntity);
 141        }
 142
 1143        isNewObjectPlaced = false;
 1144    }
 145
 0146    public virtual void OnDeleteEntity(BIWEntity entity) { }
 147
 2148    public virtual void OnDeselectedEntities() { entityHandler.ReportTransform(true); }
 149
 1150    public virtual void CheckInput() { }
 151
 0152    public virtual void CheckInputSelectedEntities() { }
 153
 0154    public virtual void InputDone() { OnInputDone?.Invoke(); }
 155
 156    public virtual void ResetScaleAndRotation()
 157    {
 1158        editionGO.transform.localScale = Vector3.one;
 1159        snapGO.transform.localScale = Vector3.one;
 1160        freeMovementGO.transform.localScale = Vector3.one;
 161
 1162        Quaternion zeroAnglesQuaternion = Quaternion.Euler(Vector3.zero);
 163
 1164        snapGO.transform.rotation = zeroAnglesQuaternion;
 1165        freeMovementGO.transform.rotation = zeroAnglesQuaternion;
 1166        editionGO.transform.rotation = zeroAnglesQuaternion;
 167
 2168        foreach (BIWEntity decentralandEntityToEdit in selectedEntities)
 169        {
 0170            decentralandEntityToEdit.ResetTransfrom();
 171        }
 172
 1173        CenterGameObjectToEdit();
 1174    }
 175
 0176    public virtual Vector3 GetCreatedEntityPoint() { return Vector3.zero; }
 177
 178    protected Vector3 GetCenterPointOfSelectedObjects()
 179    {
 2180        float totalX = 0f;
 2181        float totalY = 0f;
 2182        float totalZ = 0f;
 8183        foreach (BIWEntity entity in selectedEntities)
 184        {
 2185            totalX += entity.rootEntity.gameObject.transform.position.x;
 2186            totalY += entity.rootEntity.gameObject.transform.position.y;
 2187            totalZ += entity.rootEntity.gameObject.transform.position.z;
 188        }
 189
 2190        float centerX = totalX / selectedEntities.Count;
 2191        float centerY = totalY / selectedEntities.Count;
 2192        float centerZ = totalZ / selectedEntities.Count;
 2193        return new Vector3(centerX, centerY, centerZ);
 194    }
 195
 196    protected void TransformActionStarted(IDCLEntity entity, string type)
 197    {
 5198        BIWEntityAction buildModeEntityAction = new BIWEntityAction(entity);
 199        switch (type)
 200        {
 201            case BIWSettings.TRANSLATE_GIZMO_NAME:
 3202                buildModeEntityAction.oldValue = entity.gameObject.transform.position;
 3203                break;
 204            case BIWSettings.ROTATE_GIZMO_NAME:
 1205                buildModeEntityAction.oldValue = entity.gameObject.transform.rotation.eulerAngles;
 1206                break;
 207            case BIWSettings.SCALE_GIZMO_NAME:
 1208                buildModeEntityAction.oldValue = entity.gameObject.transform.lossyScale;
 209                break;
 210        }
 211
 5212        actionList.Add(buildModeEntityAction);
 5213    }
 214
 215    protected void TransformActionEnd(IDCLEntity entity, string type)
 216    {
 4217        List<BIWEntityAction> removeList = new List<BIWEntityAction>();
 16218        foreach (BIWEntityAction entityAction in actionList)
 219        {
 4220            if (entityAction.entityId != entity.entityId)
 221                continue;
 222
 223            switch (type)
 224            {
 225                case "MOVE":
 226
 2227                    entityAction.newValue = entity.gameObject.transform.position;
 2228                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 1229                        removeList.Add(entityAction);
 1230                    break;
 231                case "ROTATE":
 232
 1233                    entityAction.newValue = entity.gameObject.transform.rotation.eulerAngles;
 1234                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0235                        removeList.Add(entityAction);
 0236                    break;
 237                case "SCALE":
 1238                    entityAction.newValue = entity.gameObject.transform.lossyScale;
 1239                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0240                        removeList.Add(entityAction);
 241                    break;
 242            }
 243        }
 244
 10245        foreach (BIWEntityAction entityAction in removeList)
 246        {
 1247            actionList.Remove(entityAction);
 248        }
 4249    }
 250
 251    protected void ActionFinish(BIWCompleteAction.ActionType type)
 252    {
 4253        if (actionList.Count > 0 && selectedEntities.Count > 0)
 254        {
 3255            BIWCompleteAction buildModeAction = new BIWCompleteAction();
 256
 3257            buildModeAction.actionType = type;
 3258            buildModeAction.CreateActionType(actionList, type);
 3259            OnActionGenerated?.Invoke(buildModeAction);
 260
 3261            actionList = new List<BIWEntityAction>();
 262        }
 4263    }
 2264    public virtual void Update() { }
 0265    public virtual void OnGUI() { }
 0266    public virtual void LateUpdate() { }
 267}