< Summary

Class:BIWActionController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/Controllers/BIWActionController.cs
Covered lines:112
Uncovered lines:17
Coverable lines:129
Total lines:291
Line coverage:86.8% (112 of 129)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWActionController()0%110100%
Initialize(...)0%2.012088.89%
Dispose()0%220100%
EnterEditMode(...)0%110100%
Clear()0%110100%
HasApplyAnyActionThisSession()0%6200%
GetLastActionTimestamp()0%2100%
TryToRedoAction()0%6.976070%
TryToUndoAction()0%15.6910061.54%
CreateActionEntityDeleted(...)0%110100%
CreateActionEntityDeleted(...)0%220100%
CreateActionEntityCreated(...)0%110100%
AddAction(...)0%5.315076.92%
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    private IBIWSaveController saveController;
 22
 523    private readonly List<IBIWCompleteAction> actionsMade = new List<IBIWCompleteAction>();
 24
 25    private int currentUndoStepIndex = 0;
 26    private int currentRedoStepIndex = 0;
 27    internal float lastActionTimeStamp;
 28
 29    public override void Initialize(IContext context)
 30    {
 831        base.Initialize(context);
 32
 833        entityHandler  = context.editorContext.entityHandler;
 834        floorHandler = context.editorContext.floorHandler;
 835        saveController = context.editorContext.saveController;
 36
 837        if ( context.editorContext.editorHUD == null)
 038            return;
 839        context.editorContext.editorHUD.OnUndoAction += TryToUndoAction;
 840        context.editorContext.editorHUD.OnRedoAction += TryToRedoAction;
 841    }
 42
 43    public override void Dispose()
 44    {
 845        if ( context.editorContext.editorHUD != null)
 46        {
 847            context.editorContext.editorHUD.OnUndoAction -= TryToUndoAction;
 848            context.editorContext.editorHUD.OnRedoAction -= TryToRedoAction;
 49        }
 50
 851        Clear();
 852    }
 53
 54    public override void EnterEditMode(IBuilderScene scene)
 55    {
 856        base.EnterEditMode(scene);
 857        actionsMade.Clear();
 58
 859        CheckButtonsInteractability();
 860        lastActionTimeStamp = 0;
 861    }
 62
 63    public void Clear()
 64    {
 865        actionsMade.Clear();
 866        currentUndoStepIndex = 0;
 867        currentRedoStepIndex = 0;
 868    }
 69
 70    public bool HasApplyAnyActionThisSession()
 71    {
 072        if (actionsMade.Count != 0)
 73        {
 074            return actionsMade[0].IsDone();
 75        }
 76
 077        return false;
 78    }
 79
 080    public float GetLastActionTimestamp() => lastActionTimeStamp;
 81
 82    [ExcludeFromCodeCoverage]
 83    public void GoToAction(BIWCompleteAction action)
 84    {
 85        int index = actionsMade.IndexOf(action);
 86        int stepsAmount = currentUndoStepIndex - index;
 87
 88        for (int i = 0; i <= Mathf.Abs(stepsAmount); i++)
 89        {
 90            if (stepsAmount > 0)
 91            {
 92                UndoCurrentAction();
 93                if (currentUndoStepIndex > 0)
 94                    currentUndoStepIndex--;
 95            }
 96            else
 97            {
 98                RedoCurrentAction();
 99                if (currentUndoStepIndex + 1 < actionsMade.Count)
 100                    currentUndoStepIndex++;
 101            }
 102        }
 103    }
 104
 105    public void TryToRedoAction()
 106    {
 6107        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 0108            return;
 109
 6110        RedoCurrentAction();
 111
 6112        if (currentRedoStepIndex + 1 < actionsMade.Count)
 0113            currentRedoStepIndex++;
 114
 6115        if (currentUndoStepIndex < actionsMade.Count - 1)
 2116            currentUndoStepIndex++;
 117
 6118        if (VERBOSE)
 0119            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6120    }
 121
 122    public void TryToUndoAction()
 123    {
 6124        if (currentUndoStepIndex < 0 ||
 125            actionsMade.Count <= 0 ||
 126            !actionsMade[0].IsDone())
 0127            return;
 128
 6129        UndoCurrentAction();
 130
 6131        if (currentUndoStepIndex > 0)
 132        {
 2133            currentUndoStepIndex--;
 2134            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0135                currentRedoStepIndex--;
 0136        }
 4137        else if (!actionsMade[currentUndoStepIndex].IsDone() && currentRedoStepIndex > 0)
 138        {
 0139            currentRedoStepIndex--;
 140        }
 141
 6142        if (VERBOSE)
 0143            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6144    }
 145
 2146    public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity });
 147
 148    public void CreateActionEntityDeleted(List<BIWEntity> entityList)
 149    {
 1150        BIWCompleteAction buildAction = new BIWCompleteAction();
 1151        List<BIWEntityAction> entityActionList = new List<BIWEntityAction>();
 152
 4153        foreach (BIWEntity entity in entityList)
 154        {
 1155            BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS
 1156            entityActionList.Add(biwEntityAction);
 157        }
 158
 1159        buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.DELETE);
 160
 1161        AddAction(buildAction);
 1162    }
 163
 164    public void CreateActionEntityCreated(IDCLEntity entity)
 165    {
 1166        BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti
 167
 1168        BIWCompleteAction buildAction = new BIWCompleteAction();
 1169        buildAction.CreateActionType(biwEntityAction, IBIWCompleteAction.ActionType.CREATE);
 170
 1171        AddAction(buildAction);
 1172    }
 173
 174    public void AddAction(IBIWCompleteAction action)
 175    {
 7176        if (currentRedoStepIndex < actionsMade.Count - 1)
 0177            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 7178        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].IsDone())
 0179            actionsMade.RemoveAt(actionsMade.Count - 1);
 180
 7181        actionsMade.Add(action);
 182
 7183        currentUndoStepIndex = actionsMade.Count - 1;
 7184        currentRedoStepIndex = actionsMade.Count - 1;
 185
 186
 7187        if (VERBOSE)
 0188            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 7189        action.OnApplyValue += ApplyAction;
 7190        CheckButtonsInteractability();
 191
 7192        lastActionTimeStamp = Time.unscaledTime;
 7193    }
 194
 195    void ApplyAction(long entityIdToApply, object value, IBIWCompleteAction.ActionType actionType, bool isUndo)
 196    {
 197        switch (actionType)
 198        {
 199            case IBIWCompleteAction.ActionType.MOVE:
 2200                Vector3 convertedPosition = (Vector3) value;
 2201                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo
 2202                break;
 203
 204            case IBIWCompleteAction.ActionType.ROTATE:
 2205                Vector3 convertedAngles = (Vector3) value;
 2206                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte
 2207                break;
 208
 209            case IBIWCompleteAction.ActionType.SCALE:
 2210                Vector3 convertedScale = (Vector3) value;
 2211                IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 2212                Transform parent = entityToApply.gameObject.transform.parent;
 213
 2214                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 2215                break;
 216
 217            case IBIWCompleteAction.ActionType.CREATE:
 2218                if (isUndo)
 219                {
 1220                    long entityString = (long) value;
 1221                    entityHandler.DeleteEntity(entityString);
 1222                }
 223                else
 224                {
 1225                    string entityString = (string) value;
 1226                    entityHandler.CreateEntityFromJSON(entityString);
 227                }
 228
 1229                break;
 230
 231            case IBIWCompleteAction.ActionType.DELETE:
 2232                if (isUndo)
 233                {
 1234                    string deletedEntityString = (string) value;
 1235                    entityHandler.CreateEntityFromJSON(deletedEntityString);
 1236                }
 237                else
 238                {
 1239                    long deletedEntityString = (long) value;
 1240                    entityHandler.DeleteEntity(deletedEntityString);
 241                }
 242
 1243                break;
 244            case IBIWCompleteAction.ActionType.CHANGE_FLOOR:
 2245                string catalogItemToApply = (string) value;
 246
 2247                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 2248                entityHandler.DeleteFloorEntities();
 2249                floorHandler.CreateFloor(floorObject);
 250                break;
 251        }
 2252    }
 253
 254    void RedoCurrentAction()
 255    {
 6256        if (!actionsMade[currentRedoStepIndex].IsDone())
 257        {
 6258            actionsMade[currentRedoStepIndex].Redo();
 6259            OnRedo?.Invoke();
 260
 6261            CheckButtonsInteractability();
 6262            saveController.TryToSave();
 6263            lastActionTimeStamp = Time.unscaledTime;
 264        }
 6265    }
 266
 267    void UndoCurrentAction()
 268    {
 6269        if (actionsMade[currentUndoStepIndex].IsDone())
 270        {
 6271            actionsMade[currentUndoStepIndex].Undo();
 6272            OnUndo?.Invoke();
 273
 6274            CheckButtonsInteractability();
 6275            saveController.TryToSave();
 6276            lastActionTimeStamp = Time.unscaledTime;
 277        }
 6278    }
 279
 280    void CheckButtonsInteractability()
 281    {
 27282        if ( context.editorContext.editorHUD == null)
 0283            return;
 284
 27285        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 27286        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].IsDone());
 287
 27288        context.editorContext.editorHUD.SetRedoButtonInteractable(canRedoAction);
 27289        context.editorContext.editorHUD.SetUndoButtonInteractable(canUndoAction);
 27290    }
 291}