< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWActionController()0%2100%
Initialize(...)0%6200%
Dispose()0%6200%
EnterEditMode(...)0%2100%
Clear()0%2100%
HasApplyAnyActionThisSession()0%6200%
GetLastActionTimestamp()0%2100%
TryToRedoAction()0%42600%
TryToUndoAction()0%1101000%
CreateActionEntityDeleted(...)0%2100%
CreateActionEntityDeleted(...)0%6200%
CreateActionEntityCreated(...)0%2100%
AddAction(...)0%30500%
ApplyAction(...)0%90900%
RedoCurrentAction()0%12300%
UndoCurrentAction()0%12300%
CheckButtonsInteractability()0%72800%

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
 023    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    {
 031        base.Initialize(context);
 32
 033        entityHandler  = context.editorContext.entityHandler;
 034        floorHandler = context.editorContext.floorHandler;
 035        saveController = context.editorContext.saveController;
 36
 037        if ( context.editorContext.editorHUD == null)
 038            return;
 039        context.editorContext.editorHUD.OnUndoAction += TryToUndoAction;
 040        context.editorContext.editorHUD.OnRedoAction += TryToRedoAction;
 041    }
 42
 43    public override void Dispose()
 44    {
 045        if ( context.editorContext.editorHUD != null)
 46        {
 047            context.editorContext.editorHUD.OnUndoAction -= TryToUndoAction;
 048            context.editorContext.editorHUD.OnRedoAction -= TryToRedoAction;
 49        }
 50
 051        Clear();
 052    }
 53
 54    public override void EnterEditMode(IBuilderScene scene)
 55    {
 056        base.EnterEditMode(scene);
 057        actionsMade.Clear();
 58
 059        CheckButtonsInteractability();
 060        lastActionTimeStamp = 0;
 061    }
 62
 63    public void Clear()
 64    {
 065        actionsMade.Clear();
 066        currentUndoStepIndex = 0;
 067        currentRedoStepIndex = 0;
 068    }
 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    {
 0107        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 0108            return;
 109
 0110        RedoCurrentAction();
 111
 0112        if (currentRedoStepIndex + 1 < actionsMade.Count)
 0113            currentRedoStepIndex++;
 114
 0115        if (currentUndoStepIndex < actionsMade.Count - 1)
 0116            currentUndoStepIndex++;
 117
 0118        if (VERBOSE)
 0119            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 0120    }
 121
 122    public void TryToUndoAction()
 123    {
 0124        if (currentUndoStepIndex < 0 ||
 125            actionsMade.Count <= 0 ||
 126            !actionsMade[0].IsDone())
 0127            return;
 128
 0129        UndoCurrentAction();
 130
 0131        if (currentUndoStepIndex > 0)
 132        {
 0133            currentUndoStepIndex--;
 0134            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0135                currentRedoStepIndex--;
 136        }
 0137        else if (!actionsMade[currentUndoStepIndex].IsDone() && currentRedoStepIndex > 0)
 138        {
 0139            currentRedoStepIndex--;
 140        }
 141
 0142        if (VERBOSE)
 0143            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 0144    }
 145
 0146    public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity });
 147
 148    public void CreateActionEntityDeleted(List<BIWEntity> entityList)
 149    {
 0150        BIWCompleteAction buildAction = new BIWCompleteAction();
 0151        List<BIWEntityAction> entityActionList = new List<BIWEntityAction>();
 152
 0153        foreach (BIWEntity entity in entityList)
 154        {
 0155            BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS
 0156            entityActionList.Add(biwEntityAction);
 157        }
 158
 0159        buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.DELETE);
 160
 0161        AddAction(buildAction);
 0162    }
 163
 164    public void CreateActionEntityCreated(IDCLEntity entity)
 165    {
 0166        BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti
 167
 0168        BIWCompleteAction buildAction = new BIWCompleteAction();
 0169        buildAction.CreateActionType(biwEntityAction, IBIWCompleteAction.ActionType.CREATE);
 170
 0171        AddAction(buildAction);
 0172    }
 173
 174    public void AddAction(IBIWCompleteAction action)
 175    {
 0176        if (currentRedoStepIndex < actionsMade.Count - 1)
 0177            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 0178        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].IsDone())
 0179            actionsMade.RemoveAt(actionsMade.Count - 1);
 180
 0181        actionsMade.Add(action);
 182
 0183        currentUndoStepIndex = actionsMade.Count - 1;
 0184        currentRedoStepIndex = actionsMade.Count - 1;
 185
 186
 0187        if (VERBOSE)
 0188            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 0189        action.OnApplyValue += ApplyAction;
 0190        CheckButtonsInteractability();
 191
 0192        lastActionTimeStamp = Time.unscaledTime;
 0193    }
 194
 195    void ApplyAction(long entityIdToApply, object value, IBIWCompleteAction.ActionType actionType, bool isUndo)
 196    {
 197        switch (actionType)
 198        {
 199            case IBIWCompleteAction.ActionType.MOVE:
 0200                Vector3 convertedPosition = (Vector3) value;
 0201                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo
 0202                break;
 203
 204            case IBIWCompleteAction.ActionType.ROTATE:
 0205                Vector3 convertedAngles = (Vector3) value;
 0206                entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte
 0207                break;
 208
 209            case IBIWCompleteAction.ActionType.SCALE:
 0210                Vector3 convertedScale = (Vector3) value;
 0211                IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 0212                Transform parent = entityToApply.gameObject.transform.parent;
 213
 0214                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 0215                break;
 216
 217            case IBIWCompleteAction.ActionType.CREATE:
 0218                if (isUndo)
 219                {
 0220                    long entityString = (long) value;
 0221                    entityHandler.DeleteEntity(entityString);
 222                }
 223                else
 224                {
 0225                    string entityString = (string) value;
 0226                    entityHandler.CreateEntityFromJSON(entityString);
 227                }
 228
 0229                break;
 230
 231            case IBIWCompleteAction.ActionType.DELETE:
 0232                if (isUndo)
 233                {
 0234                    string deletedEntityString = (string) value;
 0235                    entityHandler.CreateEntityFromJSON(deletedEntityString);
 236                }
 237                else
 238                {
 0239                    long deletedEntityString = (long) value;
 0240                    entityHandler.DeleteEntity(deletedEntityString);
 241                }
 242
 0243                break;
 244            case IBIWCompleteAction.ActionType.CHANGE_FLOOR:
 0245                string catalogItemToApply = (string) value;
 246
 0247                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 0248                entityHandler.DeleteFloorEntities();
 0249                floorHandler.CreateFloor(floorObject);
 250                break;
 251        }
 0252    }
 253
 254    void RedoCurrentAction()
 255    {
 0256        if (!actionsMade[currentRedoStepIndex].IsDone())
 257        {
 0258            actionsMade[currentRedoStepIndex].Redo();
 0259            OnRedo?.Invoke();
 260
 0261            CheckButtonsInteractability();
 0262            saveController.TryToSave();
 0263            lastActionTimeStamp = Time.unscaledTime;
 264        }
 0265    }
 266
 267    void UndoCurrentAction()
 268    {
 0269        if (actionsMade[currentUndoStepIndex].IsDone())
 270        {
 0271            actionsMade[currentUndoStepIndex].Undo();
 0272            OnUndo?.Invoke();
 273
 0274            CheckButtonsInteractability();
 0275            saveController.TryToSave();
 0276            lastActionTimeStamp = Time.unscaledTime;
 277        }
 0278    }
 279
 280    void CheckButtonsInteractability()
 281    {
 0282        if ( context.editorContext.editorHUD == null)
 0283            return;
 284
 0285        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 0286        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].IsDone());
 287
 0288        context.editorContext.editorHUD.SetRedoButtonInteractable(canRedoAction);
 0289        context.editorContext.editorHUD.SetUndoButtonInteractable(canUndoAction);
 0290    }
 291}