< Summary

Class:BIWMode
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/States/BIWMode.cs
Covered lines:112
Uncovered lines:16
Coverable lines:128
Total lines:271
Line coverage:87.5% (112 of 128)
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%110100%
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%440100%
TransformActionEnd(...)0%10.2910085.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 DCL.Builder;
 4using DCL.Configuration;
 5using DCL.Controllers;
 6using DCL.Models;
 7using UnityEngine;
 8
 9public class BIWMode : IBIWMode
 10{
 11    public event Action OnInputDone;
 12    public event Action<BIWCompleteAction> OnActionGenerated;
 13
 214    public bool isSnapActive => isSnapActiveValue;
 115    public float maxDistanceToSelectEntities => maxDistanceToSelectEntitiesValue;
 16
 7917    protected float maxDistanceToSelectEntitiesValue = 50;
 18
 19    //Note: Snap variables are set in each mode
 7920    protected float snapFactor = 1f;
 7921    protected float snapRotationDegresFactor = 15f;
 7922    protected float snapScaleFactor = 0.5f;
 7923    protected float snapDistanceToActivateMovement = 10f;
 24
 25    protected IBIWEntityHandler entityHandler;
 26    protected IBIWSaveController saveController;
 27    protected IBIWActionController actionController;
 28    internal IBIWRaycastController raycastController;
 29
 30    protected GameObject editionGO, undoGO, snapGO, freeMovementGO;
 31
 32    internal bool isSnapActiveValue = false;
 33    internal bool isModeActive = false;
 34    internal bool isMultiSelectionActive = false;
 7935    internal List<BIWEntity> selectedEntities = new List<BIWEntity>();
 36
 37    internal bool isNewObjectPlaced = false;
 38
 7939    internal List<BIWEntityAction> actionList = new List<BIWEntityAction>();
 40
 41    internal IContext context;
 42
 43    public virtual void Init(IContext context)
 44    {
 7945        this.context = context;
 7946        entityHandler = context.editorContext.entityHandler;
 7947        saveController = context.editorContext.saveController;
 7948        actionController = context.editorContext.actionController;
 7949        raycastController = context.editorContext.raycastController;
 7950        entityHandler.OnEntityDeleted += OnDeleteEntity;
 7951    }
 52
 53    public virtual void SetEditorReferences(GameObject goToEdit, GameObject undoGO, GameObject snapGO, GameObject freeMo
 54    {
 11155        editionGO = goToEdit;
 11156        this.undoGO = undoGO;
 11157        this.snapGO = snapGO;
 11158        this.freeMovementGO = freeMovementGO;
 59
 11160        this.selectedEntities = selectedEntities;
 11161    }
 62
 15863    public virtual void Dispose() { entityHandler.OnEntityDeleted -= OnDeleteEntity; }
 64
 365    public bool IsActive() { return isModeActive; }
 8266    public virtual void Activate(IParcelScene scene) { isModeActive = true; }
 67
 68    public virtual void Deactivate()
 69    {
 570        isModeActive = false;
 571        entityHandler.DeselectEntities();
 572    }
 73
 74    public virtual void SetSnapActive(bool isActive)
 75    {
 4476        if (isActive && !isSnapActiveValue)
 277            AudioScriptableObjects.enable.Play();
 4278        else if (!isActive && isSnapActiveValue)
 279            AudioScriptableObjects.disable.Play();
 80
 4481        isSnapActiveValue = isActive;
 4482        context.editorContext.editorHUD?.SetSnapModeActive(isSnapActiveValue);
 4483    }
 84
 485    public virtual void StartMultiSelection() { isMultiSelectionActive = true; }
 86
 187    public virtual Vector3 GetPointerPosition() { return Input.mousePosition; }
 88
 289    public virtual void EndMultiSelection() { isMultiSelectionActive = false; }
 90
 091    public virtual bool ShouldCancelUndoAction() { return false; }
 92
 093    public virtual void SetDuplicationOffset(float offset) { }
 94
 095    public virtual void EntityDoubleClick(BIWEntity entity) { }
 96
 97    public virtual void SelectedEntity(BIWEntity selectedEntity)
 98    {
 299        CenterGameObjectToEdit();
 100
 2101        BIWUtils.CopyGameObjectStatus(editionGO, undoGO, false, false);
 2102    }
 103
 104    public virtual void CenterGameObjectToEdit()
 105    {
 4106        if (selectedEntities.Count > 0)
 107        {
 8108            foreach (BIWEntity entity in selectedEntities)
 109            {
 2110                entity.rootEntity.gameObject.transform.SetParent(null);
 111            }
 112
 2113            editionGO.transform.position = GetCenterPointOfSelectedObjects();
 2114            editionGO.transform.rotation = Quaternion.Euler(0, 0, 0);
 2115            editionGO.transform.localScale = Vector3.one;
 8116            foreach (BIWEntity entity in selectedEntities)
 117            {
 2118                entity.rootEntity.gameObject.transform.SetParent(editionGO.transform);
 119            }
 120        }
 4121    }
 122
 123    public virtual void MouseClickDetected()
 124    {
 2125        BIWEntity entityToSelect = raycastController.GetEntityOnPointer();
 2126        if (entityToSelect != null)
 127        {
 1128            entityHandler.EntityClicked(entityToSelect);
 129        }
 1130        else if (!isMultiSelectionActive)
 131        {
 1132            entityHandler.DeselectEntities();
 133        }
 1134    }
 135
 4136    public virtual void CreatedEntity(BIWEntity createdEntity) { isNewObjectPlaced = true; }
 137
 138    public virtual void EntityDeselected(BIWEntity entityDeselected)
 139    {
 1140        CenterGameObjectToEdit();
 141
 1142        if (isNewObjectPlaced)
 143        {
 0144            actionController.CreateActionEntityCreated(entityDeselected.rootEntity);
 145        }
 146
 1147        isNewObjectPlaced = false;
 1148    }
 149
 0150    public virtual void OnDeleteEntity(BIWEntity entity) { }
 151
 2152    public virtual void OnDeselectedEntities() { entityHandler.ReportTransform(true); }
 153
 0154    public virtual void CheckInput() { }
 155
 0156    public virtual void CheckInputSelectedEntities() { }
 157
 0158    public virtual void InputDone() { OnInputDone?.Invoke(); }
 159
 160    public virtual void ResetScaleAndRotation()
 161    {
 1162        editionGO.transform.localScale = Vector3.one;
 1163        snapGO.transform.localScale = Vector3.one;
 1164        freeMovementGO.transform.localScale = Vector3.one;
 165
 1166        Quaternion zeroAnglesQuaternion = Quaternion.Euler(Vector3.zero);
 167
 1168        snapGO.transform.rotation = zeroAnglesQuaternion;
 1169        freeMovementGO.transform.rotation = zeroAnglesQuaternion;
 1170        editionGO.transform.rotation = zeroAnglesQuaternion;
 171
 2172        foreach (BIWEntity decentralandEntityToEdit in selectedEntities)
 173        {
 0174            decentralandEntityToEdit.ResetTransfrom();
 175        }
 176
 1177        CenterGameObjectToEdit();
 1178    }
 179
 0180    public virtual Vector3 GetCreatedEntityPoint() { return Vector3.zero; }
 181
 182    protected Vector3 GetCenterPointOfSelectedObjects()
 183    {
 2184        float totalX = 0f;
 2185        float totalY = 0f;
 2186        float totalZ = 0f;
 8187        foreach (BIWEntity entity in selectedEntities)
 188        {
 2189            totalX += entity.rootEntity.gameObject.transform.position.x;
 2190            totalY += entity.rootEntity.gameObject.transform.position.y;
 2191            totalZ += entity.rootEntity.gameObject.transform.position.z;
 192        }
 193
 2194        float centerX = totalX / selectedEntities.Count;
 2195        float centerY = totalY / selectedEntities.Count;
 2196        float centerZ = totalZ / selectedEntities.Count;
 2197        return new Vector3(centerX, centerY, centerZ);
 198    }
 199
 200    protected void TransformActionStarted(IDCLEntity entity, string type)
 201    {
 5202        BIWEntityAction buildModeEntityAction = new BIWEntityAction(entity);
 203        switch (type)
 204        {
 205            case BIWSettings.TRANSLATE_GIZMO_NAME:
 3206                buildModeEntityAction.oldValue = entity.gameObject.transform.position;
 3207                break;
 208            case BIWSettings.ROTATE_GIZMO_NAME:
 1209                buildModeEntityAction.oldValue = entity.gameObject.transform.rotation.eulerAngles;
 1210                break;
 211            case BIWSettings.SCALE_GIZMO_NAME:
 1212                buildModeEntityAction.oldValue = entity.gameObject.transform.lossyScale;
 213                break;
 214        }
 215
 5216        actionList.Add(buildModeEntityAction);
 5217    }
 218
 219    protected void TransformActionEnd(IDCLEntity entity, string type)
 220    {
 4221        List<BIWEntityAction> removeList = new List<BIWEntityAction>();
 16222        foreach (BIWEntityAction entityAction in actionList)
 223        {
 4224            if (entityAction.entityId != entity.entityId)
 225                continue;
 226
 227            switch (type)
 228            {
 229                case "MOVE":
 230
 2231                    entityAction.newValue = entity.gameObject.transform.position;
 2232                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 1233                        removeList.Add(entityAction);
 1234                    break;
 235                case "ROTATE":
 236
 1237                    entityAction.newValue = entity.gameObject.transform.rotation.eulerAngles;
 1238                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0239                        removeList.Add(entityAction);
 0240                    break;
 241                case "SCALE":
 1242                    entityAction.newValue = entity.gameObject.transform.lossyScale;
 1243                    if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f)
 0244                        removeList.Add(entityAction);
 245                    break;
 246            }
 247        }
 248
 10249        foreach (BIWEntityAction entityAction in removeList)
 250        {
 1251            actionList.Remove(entityAction);
 252        }
 4253    }
 254
 255    protected void ActionFinish(IBIWCompleteAction.ActionType type)
 256    {
 4257        if (actionList.Count > 0 && selectedEntities.Count > 0)
 258        {
 3259            BIWCompleteAction buildModeAction = new BIWCompleteAction();
 260
 3261            buildModeAction.actionType = type;
 3262            buildModeAction.CreateActionType(actionList, type);
 3263            OnActionGenerated?.Invoke(buildModeAction);
 264
 3265            actionList = new List<BIWEntityAction>();
 266        }
 4267    }
 0268    public virtual void Update() { }
 0269    public virtual void OnGUI() { }
 0270    public virtual void LateUpdate() { }
 271}