< Summary

Class:ActionController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/ActionController/ActionController.cs
Covered lines:84
Uncovered lines:40
Coverable lines:124
Total lines:250
Line coverage:67.7% (84 of 124)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ActionController()0%110100%
Init()0%2.52050%
OnDestroy()0%6200%
EnterEditMode(...)0%2100%
Clear()0%110100%
GoToAction(...)0%30500%
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%38.529028.57%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/ActionController/ActionController.cs

#LineLine coverage
 1using System;
 2using DCL.Models;
 3using Newtonsoft.Json;
 4using System.Collections;
 5using System.Collections.Generic;
 6using DCL.Controllers;
 7using UnityEngine;
 8using static BuildInWorldCompleteAction;
 9
 10public class ActionController : BIWController
 11{
 12    public static bool VERBOSE = false;
 13
 14    public BuilderInWorldEntityHandler builderInWorldEntityHandler;
 15    public BIWFloorHandler biwFloorHandler;
 16
 17    public System.Action OnUndo, OnRedo;
 18
 19819    readonly List<BuildInWorldCompleteAction> actionsMade = new List<BuildInWorldCompleteAction>();
 20
 21    int currentUndoStepIndex = 0;
 22    int currentRedoStepIndex = 0;
 23
 24    public override void Init()
 25    {
 926        base.Init();
 27
 928        if (HUDController.i.builderInWorldMainHud == null)
 929            return;
 030        HUDController.i.builderInWorldMainHud.OnUndoAction += TryToUndoAction;
 031        HUDController.i.builderInWorldMainHud.OnRedoAction += TryToRedoAction;
 032    }
 33
 34    private void OnDestroy()
 35    {
 036        if (HUDController.i.builderInWorldMainHud == null)
 037            return;
 038        HUDController.i.builderInWorldMainHud.OnUndoAction -= TryToUndoAction;
 039        HUDController.i.builderInWorldMainHud.OnRedoAction -= TryToRedoAction;
 040    }
 41
 42    public override void EnterEditMode(ParcelScene scene)
 43    {
 044        base.EnterEditMode(scene);
 045        actionsMade.Clear();
 46
 047        CheckButtonsInteractability();
 048    }
 49
 50    public void Clear()
 51    {
 552        actionsMade.Clear();
 553        currentUndoStepIndex = 0;
 554        currentRedoStepIndex = 0;
 555    }
 56
 57    public void GoToAction(BuildInWorldCompleteAction action)
 58    {
 059        int index = actionsMade.IndexOf(action);
 060        int stepsAmount = currentUndoStepIndex - index;
 61
 062        for (int i = 0; i <= Mathf.Abs(stepsAmount); i++)
 63        {
 064            if (stepsAmount > 0)
 65            {
 066                UndoCurrentAction();
 067                if (currentUndoStepIndex > 0)
 068                    currentUndoStepIndex--;
 069            }
 70            else
 71            {
 072                RedoCurrentAction();
 073                if (currentUndoStepIndex + 1 < actionsMade.Count)
 074                    currentUndoStepIndex++;
 75            }
 76        }
 077    }
 78
 79    public void TryToRedoAction()
 80    {
 681        if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0)
 082            return;
 83
 684        RedoCurrentAction();
 85
 686        if (currentRedoStepIndex + 1 < actionsMade.Count)
 087            currentRedoStepIndex++;
 88
 689        if (currentUndoStepIndex < actionsMade.Count - 1)
 290            currentUndoStepIndex++;
 91
 692        if (VERBOSE)
 093            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 694    }
 95
 96    public void TryToUndoAction()
 97    {
 698        if (currentUndoStepIndex < 0 ||
 99            actionsMade.Count <= 0 ||
 100            !actionsMade[0].isDone)
 0101            return;
 102
 6103        UndoCurrentAction();
 104
 6105        if (currentUndoStepIndex > 0)
 106        {
 2107            currentUndoStepIndex--;
 2108            if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1)
 0109                currentRedoStepIndex--;
 0110        }
 4111        else if (!actionsMade[currentUndoStepIndex].isDone && currentRedoStepIndex > 0)
 112        {
 0113            currentRedoStepIndex--;
 114        }
 115
 6116        if (VERBOSE)
 0117            Debug.Log("Undo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 6118    }
 119
 2120    public void CreateActionEntityDeleted(DCLBuilderInWorldEntity entity) { CreateActionEntityDeleted(new List<DCLBuilde
 121
 122    public void CreateActionEntityDeleted(List<DCLBuilderInWorldEntity> entityList)
 123    {
 2124        BuildInWorldCompleteAction buildAction = new BuildInWorldCompleteAction();
 2125        List<BuilderInWorldEntityAction> entityActionList = new List<BuilderInWorldEntityAction>();
 126
 8127        foreach (DCLBuilderInWorldEntity entity in entityList)
 128        {
 2129            BuilderInWorldEntityAction builderInWorldEntityAction = new BuilderInWorldEntityAction(entity.rootEntity.ent
 2130            entityActionList.Add(builderInWorldEntityAction);
 131        }
 132
 2133        buildAction.CreateActionType(entityActionList, BuildInWorldCompleteAction.ActionType.DELETE);
 134
 2135        AddAction(buildAction);
 2136    }
 137
 138    public void CreateActionEntityCreated(IDCLEntity entity)
 139    {
 1140        BuilderInWorldEntityAction builderInWorldEntityAction = new BuilderInWorldEntityAction(entity, entity.entityId, 
 141
 1142        BuildInWorldCompleteAction buildAction = new BuildInWorldCompleteAction();
 1143        buildAction.CreateActionType(builderInWorldEntityAction, ActionType.CREATE);
 144
 1145        AddAction(buildAction);
 1146    }
 147
 148    public void AddAction(BuildInWorldCompleteAction action)
 149    {
 10150        if (currentRedoStepIndex < actionsMade.Count - 1)
 0151            actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex);
 10152        else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].isDone)
 0153            actionsMade.RemoveAt(actionsMade.Count - 1);
 154
 10155        actionsMade.Add(action);
 156
 10157        currentUndoStepIndex = actionsMade.Count - 1;
 10158        currentRedoStepIndex = actionsMade.Count - 1;
 159
 160
 10161        if (VERBOSE)
 0162            Debug.Log("Redo:  Current actions " + actionsMade.Count + "   Current undo index " + currentUndoStepIndex + 
 10163        action.OnApplyValue += ApplyAction;
 10164        CheckButtonsInteractability();
 10165    }
 166
 167    void ApplyAction(string entityIdToApply, object value, ActionType actionType, bool isUndo)
 168    {
 169        switch (actionType)
 170        {
 171            case ActionType.MOVE:
 2172                Vector3 convertedPosition = (Vector3) value;
 2173                builderInWorldEntityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position
 2174                break;
 175
 176            case ActionType.ROTATE:
 2177                Vector3 convertedAngles = (Vector3) value;
 2178                builderInWorldEntityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAng
 2179                break;
 180
 181            case ActionType.SCALE:
 2182                Vector3 convertedScale = (Vector3) value;
 2183                IDCLEntity entityToApply = builderInWorldEntityHandler.GetConvertedEntity(entityIdToApply).rootEntity;
 2184                Transform parent = entityToApply.gameObject.transform.parent;
 185
 2186                entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv
 2187                break;
 188
 189            case ActionType.CREATE:
 2190                string entityString = (string) value;
 2191                if (isUndo)
 1192                    builderInWorldEntityHandler.DeleteEntity(entityString);
 193                else
 1194                    builderInWorldEntityHandler.CreateEntityFromJSON(entityString);
 195
 1196                break;
 197
 198            case ActionType.DELETE:
 2199                string deletedEntityString = (string) value;
 200
 2201                if (isUndo)
 1202                    builderInWorldEntityHandler.CreateEntityFromJSON(deletedEntityString);
 203                else
 1204                    builderInWorldEntityHandler.DeleteEntity(deletedEntityString);
 205
 1206                break;
 207            case ActionType.CHANGE_FLOOR:
 2208                string catalogItemToApply = (string) value;
 209
 2210                CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply);
 2211                builderInWorldEntityHandler.DeleteFloorEntities();
 2212                biwFloorHandler.CreateFloor(floorObject);
 213                break;
 214        }
 2215    }
 216
 217    void RedoCurrentAction()
 218    {
 6219        if (!actionsMade[currentRedoStepIndex].isDone)
 220        {
 6221            actionsMade[currentRedoStepIndex].Redo();
 6222            OnRedo?.Invoke();
 223
 6224            CheckButtonsInteractability();
 225        }
 6226    }
 227
 228    void UndoCurrentAction()
 229    {
 6230        if (actionsMade[currentUndoStepIndex].isDone)
 231        {
 6232            actionsMade[currentUndoStepIndex].Undo();
 6233            OnUndo?.Invoke();
 234
 6235            CheckButtonsInteractability();
 236        }
 6237    }
 238
 239    void CheckButtonsInteractability()
 240    {
 22241        if (HUDController.i.builderInWorldMainHud == null)
 22242            return;
 243
 0244        bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act
 0245        bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].isDone);
 246
 0247        HUDController.i.builderInWorldMainHud.SetRedoButtonInteractable(canRedoAction);
 0248        HUDController.i.builderInWorldMainHud.SetUndoButtonInteractable(canUndoAction);
 0249    }
 250}