< Summary

Class:BIWActionController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/ActionController/BIWActionController.cs
Covered lines:103
Uncovered lines:11
Coverable lines:114
Total lines:271
Line coverage:90.3% (103 of 114)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWActionController()0%110100%
Init(...)0%220100%
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%990100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/ActionController/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.Controllers;
 8using UnityEngine;
 9using static BIWCompleteAction;
 10
 11public interface IBIWActionController
 12{
 13    event System.Action OnRedo;
 14    event System.Action OnUndo;
 15    void AddAction(BIWCompleteAction action);
 16    void TryToRedoAction();
 17    void TryToUndoAction();
 18    void CreateActionEntityDeleted(List<BIWEntity> entityList);
 19    void CreateActionEntityDeleted(BIWEntity entity);
 20    void CreateActionEntityCreated(IDCLEntity entity);
 21}
 22
 23public class BIWActionController : BIWController, IBIWActionController
 24{
 25    private static bool VERBOSE = false;
 26
 27    public event System.Action OnRedo;
 28    public event System.Action OnUndo;
 29
 30    private IBIWEntityHandler entityHandler;
 31    private IBIWFloorHandler floorHandler;
 32
 533    private readonly List<BIWCompleteAction> actionsMade = new List<BIWCompleteAction>();
 34
 35    private int currentUndoStepIndex = 0;
 36    private int currentRedoStepIndex = 0;
 37
 38    public override void Init(BIWContext context)
 39    {
 3440        base.Init(context);
 41
 3442        entityHandler  = context.entityHandler;
 3443        floorHandler = context.floorHandler;
 44
 3445        if (HUDController.i.builderInWorldMainHud == null)
 846            return;
 2647        HUDController.i.builderInWorldMainHud.OnUndoAction += TryToUndoAction;
 2648        HUDController.i.builderInWorldMainHud.OnRedoAction += TryToRedoAction;
 2649    }
 50
 51    public override void Dispose()
 52    {
 3253        if (HUDController.i.builderInWorldMainHud != null)
 54        {
 2755            HUDController.i.builderInWorldMainHud.OnUndoAction -= TryToUndoAction;
 2756            HUDController.i.builderInWorldMainHud.OnRedoAction -= TryToRedoAction;
 57        }
 58
 3259        Clear();
 3260    }
 61
 62    public override void EnterEditMode(ParcelScene scene)
 63    {
 1564        base.EnterEditMode(scene);
 1565        actionsMade.Clear();
 66
 1567        CheckButtonsInteractability();
 1568    }
 69
 70    public void Clear()
 71    {
 3272        actionsMade.Clear();
 3273        currentUndoStepIndex = 0;
 3274        currentRedoStepIndex = 0;
 3275    }
 76
 77    [ExcludeFromCodeCoverage]
 78    public void GoToAction(BIWCompleteAction action)
 79    {
 80        int index = actionsMade.IndexOf(action);
 81        int stepsAmount = currentUndoStepIndex - index;
 82
 83        for (int i = 0; i <= Mathf.Abs(stepsAmount); i++)
 84        {
 85            if (stepsAmount > 0)
 86            {
 87                UndoCurrentAction();
 88                if (currentUndoStepIndex > 0)
 89                    currentUndoStepIndex--;
 90            }
 91            else
 92            {
 93                RedoCurrentAction();
 94                if (currentUndoStepIndex + 1 < actionsMade.Count)
 95                    currentUndoStepIndex++;
 96            }
 97        }
 98    }
 99
 100    public void TryToRedoAction()
 101    {
 6102        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 0103            return;
 104
 6105        RedoCurrentAction();
 106
 6107        if (currentRedoStepIndex + 1 < actionsMade.Count)
 0108            currentRedoStepIndex++;
 109
 6110        if (currentUndoStepIndex < actionsMade.Count - 1)
 2111            currentUndoStepIndex++;
 112
 6113        if (VERBOSE)
 0114            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6115    }
 116
 117    public void TryToUndoAction()
 118    {
 6119        if (currentUndoStepIndex < 0 ||
 120            actionsMade.Count <= 0 ||
 121            !actionsMade[0].isDone)
 0122            return;
 123
 6124        UndoCurrentAction();
 125
 6126        if (currentUndoStepIndex > 0)
 127        {
 2128            currentUndoStepIndex--;
 2129            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0130                currentRedoStepIndex--;
 0131        }
 4132        else if (!actionsMade[currentUndoStepIndex].isDone && currentRedoStepIndex > 0)
 133        {
 0134            currentRedoStepIndex--;
 135        }
 136
 6137        if (VERBOSE)
 0138            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6139    }
 140
 2141    public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity });
 142
 143    public void CreateActionEntityDeleted(List<BIWEntity> entityList)
 144    {
 1145        BIWCompleteAction buildAction = new BIWCompleteAction();
 1146        List<BIWEntityAction> entityActionList = new List<BIWEntityAction>();
 147
 4148        foreach (BIWEntity entity in entityList)
 149        {
 1150            BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS
 1151            entityActionList.Add(biwEntityAction);
 152        }
 153
 1154        buildAction.CreateActionType(entityActionList, BIWCompleteAction.ActionType.DELETE);
 155
 1156        AddAction(buildAction);
 1157    }
 158
 159    public void CreateActionEntityCreated(IDCLEntity entity)
 160    {
 1161        BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti
 162
 1163        BIWCompleteAction buildAction = new BIWCompleteAction();
 1164        buildAction.CreateActionType(biwEntityAction, ActionType.CREATE);
 165
 1166        AddAction(buildAction);
 1167    }
 168
 169    public void AddAction(BIWCompleteAction action)
 170    {
 7171        if (currentRedoStepIndex < actionsMade.Count - 1)
 0172            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 7173        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].isDone)
 0174            actionsMade.RemoveAt(actionsMade.Count - 1);
 175
 7176        actionsMade.Add(action);
 177
 7178        currentUndoStepIndex = actionsMade.Count - 1;
 7179        currentRedoStepIndex = actionsMade.Count - 1;
 180
 181
 7182        if (VERBOSE)
 0183            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 7184        action.OnApplyValue += ApplyAction;
 7185        CheckButtonsInteractability();
 7186    }
 187
 188    void ApplyAction(string entityIdToApply, object value, ActionType actionType, bool isUndo)
 189    {
 190        switch (actionType)
 191        {
 192            case ActionType.MOVE:
 2193                Vector3 convertedPosition = (Vector3) value;
 2194                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo
 2195                break;
 196
 197            case ActionType.ROTATE:
 2198                Vector3 convertedAngles = (Vector3) value;
 2199                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte
 2200                break;
 201
 202            case ActionType.SCALE:
 2203                Vector3 convertedScale = (Vector3) value;
 2204                IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 2205                Transform parent = entityToApply.gameObject.transform.parent;
 206
 2207                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 2208                break;
 209
 210            case ActionType.CREATE:
 2211                string entityString = (string) value;
 2212                if (isUndo)
 1213                    entityHandler.DeleteEntity(entityString);
 214                else
 1215                    entityHandler.CreateEntityFromJSON(entityString);
 216
 1217                break;
 218
 219            case ActionType.DELETE:
 2220                string deletedEntityString = (string) value;
 221
 2222                if (isUndo)
 1223                    entityHandler.CreateEntityFromJSON(deletedEntityString);
 224                else
 1225                    entityHandler.DeleteEntity(deletedEntityString);
 226
 1227                break;
 228            case ActionType.CHANGE_FLOOR:
 2229                string catalogItemToApply = (string) value;
 230
 2231                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 2232                entityHandler.DeleteFloorEntities();
 2233                floorHandler.CreateFloor(floorObject);
 234                break;
 235        }
 2236    }
 237
 238    void RedoCurrentAction()
 239    {
 6240        if (!actionsMade[currentRedoStepIndex].isDone)
 241        {
 6242            actionsMade[currentRedoStepIndex].Redo();
 6243            OnRedo?.Invoke();
 244
 6245            CheckButtonsInteractability();
 246        }
 6247    }
 248
 249    void UndoCurrentAction()
 250    {
 6251        if (actionsMade[currentUndoStepIndex].isDone)
 252        {
 6253            actionsMade[currentUndoStepIndex].Undo();
 6254            OnUndo?.Invoke();
 255
 6256            CheckButtonsInteractability();
 257        }
 6258    }
 259
 260    void CheckButtonsInteractability()
 261    {
 34262        if (HUDController.i.builderInWorldMainHud == null)
 27263            return;
 264
 7265        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 7266        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].isDone);
 267
 7268        HUDController.i.builderInWorldMainHud.SetRedoButtonInteractable(canRedoAction);
 7269        HUDController.i.builderInWorldMainHud.SetUndoButtonInteractable(canUndoAction);
 7270    }
 271}