< 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:272
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%
Initialize(...)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 : IBIWController
 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    void Clear();
 22}
 23
 24public class BIWActionController : BIWController, IBIWActionController
 25{
 26    private static bool VERBOSE = false;
 27
 28    public event System.Action OnRedo;
 29    public event System.Action OnUndo;
 30
 31    private IBIWEntityHandler entityHandler;
 32    private IBIWFloorHandler floorHandler;
 33
 534    private readonly List<BIWCompleteAction> actionsMade = new List<BIWCompleteAction>();
 35
 36    private int currentUndoStepIndex = 0;
 37    private int currentRedoStepIndex = 0;
 38
 39    public override void Initialize(BIWContext context)
 40    {
 3341        base.Initialize(context);
 42
 3343        entityHandler  = context.entityHandler;
 3344        floorHandler = context.floorHandler;
 45
 3346        if (HUDController.i.builderInWorldMainHud == null)
 847            return;
 2548        HUDController.i.builderInWorldMainHud.OnUndoAction += TryToUndoAction;
 2549        HUDController.i.builderInWorldMainHud.OnRedoAction += TryToRedoAction;
 2550    }
 51
 52    public override void Dispose()
 53    {
 3354        if (HUDController.i.builderInWorldMainHud != null)
 55        {
 2556            HUDController.i.builderInWorldMainHud.OnUndoAction -= TryToUndoAction;
 2557            HUDController.i.builderInWorldMainHud.OnRedoAction -= TryToRedoAction;
 58        }
 59
 3360        Clear();
 3361    }
 62
 63    public override void EnterEditMode(IParcelScene scene)
 64    {
 1665        base.EnterEditMode(scene);
 1666        actionsMade.Clear();
 67
 1668        CheckButtonsInteractability();
 1669    }
 70
 71    public void Clear()
 72    {
 3373        actionsMade.Clear();
 3374        currentUndoStepIndex = 0;
 3375        currentRedoStepIndex = 0;
 3376    }
 77
 78    [ExcludeFromCodeCoverage]
 79    public void GoToAction(BIWCompleteAction action)
 80    {
 81        int index = actionsMade.IndexOf(action);
 82        int stepsAmount = currentUndoStepIndex - index;
 83
 84        for (int i = 0; i <= Mathf.Abs(stepsAmount); i++)
 85        {
 86            if (stepsAmount > 0)
 87            {
 88                UndoCurrentAction();
 89                if (currentUndoStepIndex > 0)
 90                    currentUndoStepIndex--;
 91            }
 92            else
 93            {
 94                RedoCurrentAction();
 95                if (currentUndoStepIndex + 1 < actionsMade.Count)
 96                    currentUndoStepIndex++;
 97            }
 98        }
 99    }
 100
 101    public void TryToRedoAction()
 102    {
 6103        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 0104            return;
 105
 6106        RedoCurrentAction();
 107
 6108        if (currentRedoStepIndex + 1 < actionsMade.Count)
 0109            currentRedoStepIndex++;
 110
 6111        if (currentUndoStepIndex < actionsMade.Count - 1)
 2112            currentUndoStepIndex++;
 113
 6114        if (VERBOSE)
 0115            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6116    }
 117
 118    public void TryToUndoAction()
 119    {
 6120        if (currentUndoStepIndex < 0 ||
 121            actionsMade.Count <= 0 ||
 122            !actionsMade[0].isDone)
 0123            return;
 124
 6125        UndoCurrentAction();
 126
 6127        if (currentUndoStepIndex > 0)
 128        {
 2129            currentUndoStepIndex--;
 2130            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0131                currentRedoStepIndex--;
 0132        }
 4133        else if (!actionsMade[currentUndoStepIndex].isDone && currentRedoStepIndex > 0)
 134        {
 0135            currentRedoStepIndex--;
 136        }
 137
 6138        if (VERBOSE)
 0139            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6140    }
 141
 2142    public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity });
 143
 144    public void CreateActionEntityDeleted(List<BIWEntity> entityList)
 145    {
 1146        BIWCompleteAction buildAction = new BIWCompleteAction();
 1147        List<BIWEntityAction> entityActionList = new List<BIWEntityAction>();
 148
 4149        foreach (BIWEntity entity in entityList)
 150        {
 1151            BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS
 1152            entityActionList.Add(biwEntityAction);
 153        }
 154
 1155        buildAction.CreateActionType(entityActionList, BIWCompleteAction.ActionType.DELETE);
 156
 1157        AddAction(buildAction);
 1158    }
 159
 160    public void CreateActionEntityCreated(IDCLEntity entity)
 161    {
 1162        BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti
 163
 1164        BIWCompleteAction buildAction = new BIWCompleteAction();
 1165        buildAction.CreateActionType(biwEntityAction, ActionType.CREATE);
 166
 1167        AddAction(buildAction);
 1168    }
 169
 170    public void AddAction(BIWCompleteAction action)
 171    {
 7172        if (currentRedoStepIndex < actionsMade.Count - 1)
 0173            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 7174        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].isDone)
 0175            actionsMade.RemoveAt(actionsMade.Count - 1);
 176
 7177        actionsMade.Add(action);
 178
 7179        currentUndoStepIndex = actionsMade.Count - 1;
 7180        currentRedoStepIndex = actionsMade.Count - 1;
 181
 182
 7183        if (VERBOSE)
 0184            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 7185        action.OnApplyValue += ApplyAction;
 7186        CheckButtonsInteractability();
 7187    }
 188
 189    void ApplyAction(string entityIdToApply, object value, ActionType actionType, bool isUndo)
 190    {
 191        switch (actionType)
 192        {
 193            case ActionType.MOVE:
 2194                Vector3 convertedPosition = (Vector3) value;
 2195                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo
 2196                break;
 197
 198            case ActionType.ROTATE:
 2199                Vector3 convertedAngles = (Vector3) value;
 2200                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte
 2201                break;
 202
 203            case ActionType.SCALE:
 2204                Vector3 convertedScale = (Vector3) value;
 2205                IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 2206                Transform parent = entityToApply.gameObject.transform.parent;
 207
 2208                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 2209                break;
 210
 211            case ActionType.CREATE:
 2212                string entityString = (string) value;
 2213                if (isUndo)
 1214                    entityHandler.DeleteEntity(entityString);
 215                else
 1216                    entityHandler.CreateEntityFromJSON(entityString);
 217
 1218                break;
 219
 220            case ActionType.DELETE:
 2221                string deletedEntityString = (string) value;
 222
 2223                if (isUndo)
 1224                    entityHandler.CreateEntityFromJSON(deletedEntityString);
 225                else
 1226                    entityHandler.DeleteEntity(deletedEntityString);
 227
 1228                break;
 229            case ActionType.CHANGE_FLOOR:
 2230                string catalogItemToApply = (string) value;
 231
 2232                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 2233                entityHandler.DeleteFloorEntities();
 2234                floorHandler.CreateFloor(floorObject);
 235                break;
 236        }
 2237    }
 238
 239    void RedoCurrentAction()
 240    {
 6241        if (!actionsMade[currentRedoStepIndex].isDone)
 242        {
 6243            actionsMade[currentRedoStepIndex].Redo();
 6244            OnRedo?.Invoke();
 245
 6246            CheckButtonsInteractability();
 247        }
 6248    }
 249
 250    void UndoCurrentAction()
 251    {
 6252        if (actionsMade[currentUndoStepIndex].isDone)
 253        {
 6254            actionsMade[currentUndoStepIndex].Undo();
 6255            OnUndo?.Invoke();
 256
 6257            CheckButtonsInteractability();
 258        }
 6259    }
 260
 261    void CheckButtonsInteractability()
 262    {
 35263        if (HUDController.i.builderInWorldMainHud == null)
 27264            return;
 265
 8266        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 8267        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].isDone);
 268
 8269        HUDController.i.builderInWorldMainHud.SetRedoButtonInteractable(canRedoAction);
 8270        HUDController.i.builderInWorldMainHud.SetUndoButtonInteractable(canUndoAction);
 8271    }
 272}