< Summary

Class:BIWMode
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/States/BIWMode.cs
Covered lines:110
Uncovered lines:19
Coverable lines:129
Total lines:272
Line coverage:85.2% (110 of 129)
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%2100%
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%2100%
OnGUI()0%2100%
LateUpdate()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Runtime.Remoting.Contexts;
 4using DCL.Builder;
 5using DCL.Configuration;
 6using DCL.Controllers;
 7using DCL.Models;
 8using UnityEngine;
 9
 10public class BIWMode : IBIWMode
 11{
 12    public event Action OnInputDone;
 13    public event Action<BIWCompleteAction> OnActionGenerated;
 14
 015    public bool isSnapActive => isSnapActiveValue;
 016    public float maxDistanceToSelectEntities => maxDistanceToSelectEntitiesValue;
 17
 7718    protected float maxDistanceToSelectEntitiesValue = 50;
 19
 20    //Note: Snap variables are set in each mode
 7721    protected float snapFactor = 1f;
 7722    protected float snapRotationDegresFactor = 15f;
 7723    protected float snapScaleFactor = 0.5f;
 7724    protected float snapDistanceToActivateMovement = 10f;
 25
 26    protected IBIWEntityHandler entityHandler;
 27    protected IBIWSaveController saveController;
 28    protected IBIWActionController actionController;
 29    protected IBIWRaycastController raycastController;
 30
 31    protected GameObject editionGO, undoGO, snapGO, freeMovementGO;
 32
 33    internal bool isSnapActiveValue = false;
 34    internal bool isModeActive = false;
 35    internal bool isMultiSelectionActive = false;
 7736    internal List<BIWEntity> selectedEntities = new List<BIWEntity>();
 37
 38    internal bool isNewObjectPlaced = false;
 39
 7740    internal List<BIWEntityAction> actionList = new List<BIWEntityAction>();
 41
 42    internal IContext context;
 43
 44    public virtual void Init(IContext context)
 45    {
 7746        this.context = context;
 7747        entityHandler = context.editorContext.entityHandler;
 7748        saveController = context.editorContext.saveController;
 7749        actionController = context.editorContext.actionController;
 7750        raycastController = context.editorContext.raycastController;
 7751        entityHandler.OnEntityDeleted += OnDeleteEntity;
 7752    }
 53
 54    public virtual void SetEditorReferences(GameObject goToEdit, GameObject undoGO, GameObject snapGO, GameObject freeMo
 55    {
 10856        editionGO = goToEdit;
 10857        this.undoGO = undoGO;
 10858        this.snapGO = snapGO;
 10859        this.freeMovementGO = freeMovementGO;
 60
 10861        this.selectedEntities = selectedEntities;
 10862    }
 63
 15464    public virtual void Dispose() { entityHandler.OnEntityDeleted -= OnDeleteEntity; }
 65
 066    public bool IsActive() { return isModeActive; }
 267    public virtual void Activate(IParcelScene scene) { isModeActive = true; }
 68
 69    public virtual void Deactivate()
 70    {
 571        isModeActive = false;
 572        entityHandler.DeselectEntities();
 573    }
 74
 75    public virtual void SetSnapActive(bool isActive)
 76    {
 4377        if (isActive && !isSnapActiveValue)
 278            AudioScriptableObjects.enable.Play();
 4179        else if (!isActive && isSnapActiveValue)
 280            AudioScriptableObjects.disable.Play();
 81
 4382        isSnapActiveValue = isActive;
 4383        context.editorContext.editorHUD?.SetSnapModeActive(isSnapActiveValue);
 4384    }
 85
 286    public virtual void StartMultiSelection() { isMultiSelectionActive = true; }
 87
 188    public virtual Vector3 GetPointerPosition() { return Input.mousePosition; }
 89
 290    public virtual void EndMultiSelection() { isMultiSelectionActive = false; }
 91
 092    public virtual bool ShouldCancelUndoAction() { return false; }
 93
 094    public virtual void SetDuplicationOffset(float offset) { }
 95
 096    public virtual void EntityDoubleClick(BIWEntity entity) { }
 97
 98    public virtual void SelectedEntity(BIWEntity selectedEntity)
 99    {
 2100        CenterGameObjectToEdit();
 101
 2102        BIWUtils.CopyGameObjectStatus(editionGO, undoGO, false, false);
 2103    }
 104
 105    public virtual void CenterGameObjectToEdit()
 106    {
 4107        if (selectedEntities.Count > 0)
 108        {
 8109            foreach (BIWEntity entity in selectedEntities)
 110            {
 2111                entity.rootEntity.gameObject.transform.SetParent(null);
 112            }
 113
 2114            editionGO.transform.position = GetCenterPointOfSelectedObjects();
 2115            editionGO.transform.rotation = Quaternion.Euler(0, 0, 0);
 2116            editionGO.transform.localScale = Vector3.one;
 8117            foreach (BIWEntity entity in selectedEntities)
 118            {
 2119                entity.rootEntity.gameObject.transform.SetParent(editionGO.transform);
 120            }
 121        }
 4122    }
 123
 124    public virtual void MouseClickDetected()
 125    {
 2126        BIWEntity entityToSelect = raycastController.GetEntityOnPointer();
 2127        if (entityToSelect != null)
 128        {
 1129            entityHandler.EntityClicked(entityToSelect);
 1130        }
 1131        else if (!isMultiSelectionActive)
 132        {
 1133            entityHandler.DeselectEntities();
 134        }
 1135    }
 136
 2137    public virtual void CreatedEntity(BIWEntity createdEntity) { isNewObjectPlaced = true; }
 138
 139    public virtual void EntityDeselected(BIWEntity entityDeselected)
 140    {
 1141        CenterGameObjectToEdit();
 142
 1143        if (isNewObjectPlaced)
 144        {
 0145            actionController.CreateActionEntityCreated(entityDeselected.rootEntity);
 146        }
 147
 1148        isNewObjectPlaced = false;
 1149    }
 150
 0151    public virtual void OnDeleteEntity(BIWEntity entity) { }
 152
 2153    public virtual void OnDeselectedEntities() { entityHandler.ReportTransform(true); }
 154
 0155    public virtual void CheckInput() { }
 156
 0157    public virtual void CheckInputSelectedEntities() { }
 158
 0159    public virtual void InputDone() { OnInputDone?.Invoke(); }
 160
 161    public virtual void ResetScaleAndRotation()
 162    {
 1163        editionGO.transform.localScale = Vector3.one;
 1164        snapGO.transform.localScale = Vector3.one;
 1165        freeMovementGO.transform.localScale = Vector3.one;
 166
 1167        Quaternion zeroAnglesQuaternion = Quaternion.Euler(Vector3.zero);
 168
 1169        snapGO.transform.rotation = zeroAnglesQuaternion;
 1170        freeMovementGO.transform.rotation = zeroAnglesQuaternion;
 1171        editionGO.transform.rotation = zeroAnglesQuaternion;
 172
 2173        foreach (BIWEntity decentralandEntityToEdit in selectedEntities)
 174        {
 0175            decentralandEntityToEdit.ResetTransfrom();
 176        }
 177
 1178        CenterGameObjectToEdit();
 1179    }
 180
 0181    public virtual Vector3 GetCreatedEntityPoint() { return Vector3.zero; }
 182
 183    protected Vector3 GetCenterPointOfSelectedObjects()
 184    {
 2185        float totalX = 0f;
 2186        float totalY = 0f;
 2187        float totalZ = 0f;
 8188        foreach (BIWEntity entity in selectedEntities)
 189        {
 2190            totalX += entity.rootEntity.gameObject.transform.position.x;
 2191            totalY += entity.rootEntity.gameObject.transform.position.y;
 2192            totalZ += entity.rootEntity.gameObject.transform.position.z;
 193        }
 194
 2195        float centerX = totalX / selectedEntities.Count;
 2196        float centerY = totalY / selectedEntities.Count;
 2197        float centerZ = totalZ / selectedEntities.Count;
 2198        return new Vector3(centerX, centerY, centerZ);
 199    }
 200
 201    protected void TransformActionStarted(IDCLEntity entity, string type)
 202    {
 5203        BIWEntityAction buildModeEntityAction = new BIWEntityAction(entity);
 204        switch (type)
 205        {
 206            case BIWSettings.TRANSLATE_GIZMO_NAME:
 3207                buildModeEntityAction.oldValue = entity.gameObject.transform.position;
 3208                break;
 209            case BIWSettings.ROTATE_GIZMO_NAME:
 1210                buildModeEntityAction.oldValue = entity.gameObject.transform.rotation.eulerAngles;
 1211                break;
 212            case BIWSettings.SCALE_GIZMO_NAME:
 1213                buildModeEntityAction.oldValue = entity.gameObject.transform.lossyScale;
 214                break;
 215        }
 216
 5217        actionList.Add(buildModeEntityAction);
 5218    }
 219
 220    protected void TransformActionEnd(IDCLEntity entity, string type)
 221    {
 4222        List<BIWEntityAction> removeList = new List<BIWEntityAction>();
 16223        foreach (BIWEntityAction entityAction in actionList)
 224        {
 4225            if (entityAction.entityId != entity.entityId)
 226                continue;
 227
 228            switch (type)
 229            {
 230                case "MOVE":
 231
 2232                    entityAction.newValue = entity.gameObject.transform.position;
 2233                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 1234                        removeList.Add(entityAction);
 1235                    break;
 236                case "ROTATE":
 237
 1238                    entityAction.newValue = entity.gameObject.transform.rotation.eulerAngles;
 1239                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0240                        removeList.Add(entityAction);
 0241                    break;
 242                case "SCALE":
 1243                    entityAction.newValue = entity.gameObject.transform.lossyScale;
 1244                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0245                        removeList.Add(entityAction);
 246                    break;
 247            }
 248        }
 249
 10250        foreach (BIWEntityAction entityAction in removeList)
 251        {
 1252            actionList.Remove(entityAction);
 253        }
 4254    }
 255
 256    protected void ActionFinish(IBIWCompleteAction.ActionType type)
 257    {
 4258        if (actionList.Count > 0 && selectedEntities.Count > 0)
 259        {
 3260            BIWCompleteAction buildModeAction = new BIWCompleteAction();
 261
 3262            buildModeAction.actionType = type;
 3263            buildModeAction.CreateActionType(actionList, type);
 3264            OnActionGenerated?.Invoke(buildModeAction);
 265
 3266            actionList = new List<BIWEntityAction>();
 267        }
 4268    }
 0269    public virtual void Update() { }
 0270    public virtual void OnGUI() { }
 0271    public virtual void LateUpdate() { }
 272}