< Summary

Class:BIWActionController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWActionController.cs
Covered lines:101
Uncovered lines:13
Coverable lines:114
Total lines:260
Line coverage:88.5% (101 of 114)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWActionController()0%110100%
Initialize(...)0%2.012087.5%
Dispose()0%220100%
EnterEditMode(...)0%110100%
Clear()0%110100%
TryToRedoAction()0%6.976070%
TryToUndoAction()0%15.6910061.54%
CreateActionEntityDeleted(...)0%110100%
CreateActionEntityDeleted(...)0%220100%
CreateActionEntityCreated(...)0%110100%
AddAction(...)0%5.395075%
ApplyAction(...)0%990100%
RedoCurrentAction()0%330100%
UndoCurrentAction()0%330100%
CheckButtonsInteractability()0%8.198085.71%

File(s)

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

#LineLine coverage
 1using System;
 2using DCL.Models;
 3using Newtonsoft.Json;
 4using System.Collections;
 5using System.Collections.Generic;
 6using System.Diagnostics.CodeAnalysis;
 7using DCL.Builder;
 8using DCL.Controllers;
 9using UnityEngine;
 10using static BIWCompleteAction;
 11
 12public class BIWActionController : BIWController, IBIWActionController
 13{
 14    private static bool VERBOSE = false;
 15
 16    public event System.Action OnRedo;
 17    public event System.Action OnUndo;
 18
 19    private IBIWEntityHandler entityHandler;
 20    private IBIWFloorHandler floorHandler;
 21
 522    private readonly List<IBIWCompleteAction> actionsMade = new List<IBIWCompleteAction>();
 23
 24    private int currentUndoStepIndex = 0;
 25    private int currentRedoStepIndex = 0;
 26
 27    public override void Initialize(IContext context)
 28    {
 829        base.Initialize(context);
 30
 831        entityHandler  = context.editorContext.entityHandler;
 832        floorHandler = context.editorContext.floorHandler;
 33
 834        if ( context.editorContext.editorHUD == null)
 035            return;
 836        context.editorContext.editorHUD.OnUndoAction += TryToUndoAction;
 837        context.editorContext.editorHUD.OnRedoAction += TryToRedoAction;
 838    }
 39
 40    public override void Dispose()
 41    {
 842        if ( context.editorContext.editorHUD != null)
 43        {
 844            context.editorContext.editorHUD.OnUndoAction -= TryToUndoAction;
 845            context.editorContext.editorHUD.OnRedoAction -= TryToRedoAction;
 46        }
 47
 848        Clear();
 849    }
 50
 51    public override void EnterEditMode(IParcelScene scene)
 52    {
 853        base.EnterEditMode(scene);
 854        actionsMade.Clear();
 55
 856        CheckButtonsInteractability();
 857    }
 58
 59    public void Clear()
 60    {
 861        actionsMade.Clear();
 862        currentUndoStepIndex = 0;
 863        currentRedoStepIndex = 0;
 864    }
 65
 66    [ExcludeFromCodeCoverage]
 67    public void GoToAction(BIWCompleteAction action)
 68    {
 69        int index = actionsMade.IndexOf(action);
 70        int stepsAmount = currentUndoStepIndex - index;
 71
 72        for (int i = 0; i <= Mathf.Abs(stepsAmount); i++)
 73        {
 74            if (stepsAmount > 0)
 75            {
 76                UndoCurrentAction();
 77                if (currentUndoStepIndex > 0)
 78                    currentUndoStepIndex--;
 79            }
 80            else
 81            {
 82                RedoCurrentAction();
 83                if (currentUndoStepIndex + 1 < actionsMade.Count)
 84                    currentUndoStepIndex++;
 85            }
 86        }
 87    }
 88
 89    public void TryToRedoAction()
 90    {
 691        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 092            return;
 93
 694        RedoCurrentAction();
 95
 696        if (currentRedoStepIndex + 1 < actionsMade.Count)
 097            currentRedoStepIndex++;
 98
 699        if (currentUndoStepIndex < actionsMade.Count - 1)
 2100            currentUndoStepIndex++;
 101
 6102        if (VERBOSE)
 0103            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6104    }
 105
 106    public void TryToUndoAction()
 107    {
 6108        if (currentUndoStepIndex < 0 ||
 109            actionsMade.Count <= 0 ||
 110            !actionsMade[0].IsDone())
 0111            return;
 112
 6113        UndoCurrentAction();
 114
 6115        if (currentUndoStepIndex > 0)
 116        {
 2117            currentUndoStepIndex--;
 2118            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0119                currentRedoStepIndex--;
 0120        }
 4121        else if (!actionsMade[currentUndoStepIndex].IsDone() && currentRedoStepIndex > 0)
 122        {
 0123            currentRedoStepIndex--;
 124        }
 125
 6126        if (VERBOSE)
 0127            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6128    }
 129
 2130    public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity });
 131
 132    public void CreateActionEntityDeleted(List<BIWEntity> entityList)
 133    {
 1134        BIWCompleteAction buildAction = new BIWCompleteAction();
 1135        List<BIWEntityAction> entityActionList = new List<BIWEntityAction>();
 136
 4137        foreach (BIWEntity entity in entityList)
 138        {
 1139            BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS
 1140            entityActionList.Add(biwEntityAction);
 141        }
 142
 1143        buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.DELETE);
 144
 1145        AddAction(buildAction);
 1146    }
 147
 148    public void CreateActionEntityCreated(IDCLEntity entity)
 149    {
 1150        BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti
 151
 1152        BIWCompleteAction buildAction = new BIWCompleteAction();
 1153        buildAction.CreateActionType(biwEntityAction, IBIWCompleteAction.ActionType.CREATE);
 154
 1155        AddAction(buildAction);
 1156    }
 157
 158    public void AddAction(IBIWCompleteAction action)
 159    {
 7160        if (currentRedoStepIndex < actionsMade.Count - 1)
 0161            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 7162        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].IsDone())
 0163            actionsMade.RemoveAt(actionsMade.Count - 1);
 164
 7165        actionsMade.Add(action);
 166
 7167        currentUndoStepIndex = actionsMade.Count - 1;
 7168        currentRedoStepIndex = actionsMade.Count - 1;
 169
 170
 7171        if (VERBOSE)
 0172            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 7173        action.OnApplyValue += ApplyAction;
 7174        CheckButtonsInteractability();
 7175    }
 176
 177    void ApplyAction(string entityIdToApply, object value, IBIWCompleteAction.ActionType actionType, bool isUndo)
 178    {
 179        switch (actionType)
 180        {
 181            case IBIWCompleteAction.ActionType.MOVE:
 2182                Vector3 convertedPosition = (Vector3) value;
 2183                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo
 2184                break;
 185
 186            case IBIWCompleteAction.ActionType.ROTATE:
 2187                Vector3 convertedAngles = (Vector3) value;
 2188                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte
 2189                break;
 190
 191            case IBIWCompleteAction.ActionType.SCALE:
 2192                Vector3 convertedScale = (Vector3) value;
 2193                IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 2194                Transform parent = entityToApply.gameObject.transform.parent;
 195
 2196                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 2197                break;
 198
 199            case IBIWCompleteAction.ActionType.CREATE:
 2200                string entityString = (string) value;
 2201                if (isUndo)
 1202                    entityHandler.DeleteEntity(entityString);
 203                else
 1204                    entityHandler.CreateEntityFromJSON(entityString);
 205
 1206                break;
 207
 208            case IBIWCompleteAction.ActionType.DELETE:
 2209                string deletedEntityString = (string) value;
 210
 2211                if (isUndo)
 1212                    entityHandler.CreateEntityFromJSON(deletedEntityString);
 213                else
 1214                    entityHandler.DeleteEntity(deletedEntityString);
 215
 1216                break;
 217            case IBIWCompleteAction.ActionType.CHANGE_FLOOR:
 2218                string catalogItemToApply = (string) value;
 219
 2220                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 2221                entityHandler.DeleteFloorEntities();
 2222                floorHandler.CreateFloor(floorObject);
 223                break;
 224        }
 2225    }
 226
 227    void RedoCurrentAction()
 228    {
 6229        if (!actionsMade[currentRedoStepIndex].IsDone())
 230        {
 6231            actionsMade[currentRedoStepIndex].Redo();
 6232            OnRedo?.Invoke();
 233
 6234            CheckButtonsInteractability();
 235        }
 6236    }
 237
 238    void UndoCurrentAction()
 239    {
 6240        if (actionsMade[currentUndoStepIndex].IsDone())
 241        {
 6242            actionsMade[currentUndoStepIndex].Undo();
 6243            OnUndo?.Invoke();
 244
 6245            CheckButtonsInteractability();
 246        }
 6247    }
 248
 249    void CheckButtonsInteractability()
 250    {
 27251        if ( context.editorContext.editorHUD == null)
 0252            return;
 253
 27254        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 27255        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].IsDone());
 256
 27257        context.editorContext.editorHUD.SetRedoButtonInteractable(canRedoAction);
 27258        context.editorContext.editorHUD.SetUndoButtonInteractable(canUndoAction);
 27259    }
 260}