| | 1 | | using System; |
| | 2 | | using DCL.Models; |
| | 3 | | using Newtonsoft.Json; |
| | 4 | | using System.Collections; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics.CodeAnalysis; |
| | 7 | | using DCL.Builder; |
| | 8 | | using DCL.Controllers; |
| | 9 | | using UnityEngine; |
| | 10 | | using static BIWCompleteAction; |
| | 11 | |
|
| | 12 | | public 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 | |
|
| 5 | 22 | | private readonly List<IBIWCompleteAction> actionsMade = new List<IBIWCompleteAction>(); |
| | 23 | |
|
| | 24 | | private int currentUndoStepIndex = 0; |
| | 25 | | private int currentRedoStepIndex = 0; |
| | 26 | |
|
| | 27 | | public override void Initialize(IContext context) |
| | 28 | | { |
| 8 | 29 | | base.Initialize(context); |
| | 30 | |
|
| 8 | 31 | | entityHandler = context.editorContext.entityHandler; |
| 8 | 32 | | floorHandler = context.editorContext.floorHandler; |
| | 33 | |
|
| 8 | 34 | | if ( context.editorContext.editorHUD == null) |
| 0 | 35 | | return; |
| 8 | 36 | | context.editorContext.editorHUD.OnUndoAction += TryToUndoAction; |
| 8 | 37 | | context.editorContext.editorHUD.OnRedoAction += TryToRedoAction; |
| 8 | 38 | | } |
| | 39 | |
|
| | 40 | | public override void Dispose() |
| | 41 | | { |
| 8 | 42 | | if ( context.editorContext.editorHUD != null) |
| | 43 | | { |
| 8 | 44 | | context.editorContext.editorHUD.OnUndoAction -= TryToUndoAction; |
| 8 | 45 | | context.editorContext.editorHUD.OnRedoAction -= TryToRedoAction; |
| | 46 | | } |
| | 47 | |
|
| 8 | 48 | | Clear(); |
| 8 | 49 | | } |
| | 50 | |
|
| | 51 | | public override void EnterEditMode(IParcelScene scene) |
| | 52 | | { |
| 8 | 53 | | base.EnterEditMode(scene); |
| 8 | 54 | | actionsMade.Clear(); |
| | 55 | |
|
| 8 | 56 | | CheckButtonsInteractability(); |
| 8 | 57 | | } |
| | 58 | |
|
| | 59 | | public void Clear() |
| | 60 | | { |
| 8 | 61 | | actionsMade.Clear(); |
| 8 | 62 | | currentUndoStepIndex = 0; |
| 8 | 63 | | currentRedoStepIndex = 0; |
| 8 | 64 | | } |
| | 65 | |
|
| | 66 | | [ExcludeFromCodeCoverage] |
| | 67 | | public void GoToAction(BIWCompleteAction action) |
| | 68 | | { |
| | 69 | | int index = actionsMade.IndexOf(action); |
| | 70 | | int stepsAmount = currentUndoStepIndex - index; |
| | 71 | |
|
| | 72 | | for (int i = 0; i <= Mathf.Abs(stepsAmount); i++) |
| | 73 | | { |
| | 74 | | if (stepsAmount > 0) |
| | 75 | | { |
| | 76 | | UndoCurrentAction(); |
| | 77 | | if (currentUndoStepIndex > 0) |
| | 78 | | currentUndoStepIndex--; |
| | 79 | | } |
| | 80 | | else |
| | 81 | | { |
| | 82 | | RedoCurrentAction(); |
| | 83 | | if (currentUndoStepIndex + 1 < actionsMade.Count) |
| | 84 | | currentUndoStepIndex++; |
| | 85 | | } |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public void TryToRedoAction() |
| | 90 | | { |
| 6 | 91 | | if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0) |
| 0 | 92 | | return; |
| | 93 | |
|
| 6 | 94 | | RedoCurrentAction(); |
| | 95 | |
|
| 6 | 96 | | if (currentRedoStepIndex + 1 < actionsMade.Count) |
| 0 | 97 | | currentRedoStepIndex++; |
| | 98 | |
|
| 6 | 99 | | if (currentUndoStepIndex < actionsMade.Count - 1) |
| 2 | 100 | | currentUndoStepIndex++; |
| | 101 | |
|
| 6 | 102 | | if (VERBOSE) |
| 0 | 103 | | Debug.Log("Redo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 6 | 104 | | } |
| | 105 | |
|
| | 106 | | public void TryToUndoAction() |
| | 107 | | { |
| 6 | 108 | | if (currentUndoStepIndex < 0 || |
| | 109 | | actionsMade.Count <= 0 || |
| | 110 | | !actionsMade[0].IsDone()) |
| 0 | 111 | | return; |
| | 112 | |
|
| 6 | 113 | | UndoCurrentAction(); |
| | 114 | |
|
| 6 | 115 | | if (currentUndoStepIndex > 0) |
| | 116 | | { |
| 2 | 117 | | currentUndoStepIndex--; |
| 2 | 118 | | if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1) |
| 0 | 119 | | currentRedoStepIndex--; |
| 0 | 120 | | } |
| 4 | 121 | | else if (!actionsMade[currentUndoStepIndex].IsDone() && currentRedoStepIndex > 0) |
| | 122 | | { |
| 0 | 123 | | currentRedoStepIndex--; |
| | 124 | | } |
| | 125 | |
|
| 6 | 126 | | if (VERBOSE) |
| 0 | 127 | | Debug.Log("Undo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 6 | 128 | | } |
| | 129 | |
|
| 2 | 130 | | public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity }); |
| | 131 | |
|
| | 132 | | public void CreateActionEntityDeleted(List<BIWEntity> entityList) |
| | 133 | | { |
| 1 | 134 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| 1 | 135 | | List<BIWEntityAction> entityActionList = new List<BIWEntityAction>(); |
| | 136 | |
|
| 4 | 137 | | foreach (BIWEntity entity in entityList) |
| | 138 | | { |
| 1 | 139 | | BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS |
| 1 | 140 | | entityActionList.Add(biwEntityAction); |
| | 141 | | } |
| | 142 | |
|
| 1 | 143 | | buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.DELETE); |
| | 144 | |
|
| 1 | 145 | | AddAction(buildAction); |
| 1 | 146 | | } |
| | 147 | |
|
| | 148 | | public void CreateActionEntityCreated(IDCLEntity entity) |
| | 149 | | { |
| 1 | 150 | | BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti |
| | 151 | |
|
| 1 | 152 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| 1 | 153 | | buildAction.CreateActionType(biwEntityAction, IBIWCompleteAction.ActionType.CREATE); |
| | 154 | |
|
| 1 | 155 | | AddAction(buildAction); |
| 1 | 156 | | } |
| | 157 | |
|
| | 158 | | public void AddAction(IBIWCompleteAction action) |
| | 159 | | { |
| 7 | 160 | | if (currentRedoStepIndex < actionsMade.Count - 1) |
| 0 | 161 | | actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex); |
| 7 | 162 | | else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].IsDone()) |
| 0 | 163 | | actionsMade.RemoveAt(actionsMade.Count - 1); |
| | 164 | |
|
| 7 | 165 | | actionsMade.Add(action); |
| | 166 | |
|
| 7 | 167 | | currentUndoStepIndex = actionsMade.Count - 1; |
| 7 | 168 | | currentRedoStepIndex = actionsMade.Count - 1; |
| | 169 | |
|
| | 170 | |
|
| 7 | 171 | | if (VERBOSE) |
| 0 | 172 | | Debug.Log("Redo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 7 | 173 | | action.OnApplyValue += ApplyAction; |
| 7 | 174 | | CheckButtonsInteractability(); |
| 7 | 175 | | } |
| | 176 | |
|
| | 177 | | void ApplyAction(string entityIdToApply, object value, IBIWCompleteAction.ActionType actionType, bool isUndo) |
| | 178 | | { |
| | 179 | | switch (actionType) |
| | 180 | | { |
| | 181 | | case IBIWCompleteAction.ActionType.MOVE: |
| 2 | 182 | | Vector3 convertedPosition = (Vector3) value; |
| 2 | 183 | | entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo |
| 2 | 184 | | break; |
| | 185 | |
|
| | 186 | | case IBIWCompleteAction.ActionType.ROTATE: |
| 2 | 187 | | Vector3 convertedAngles = (Vector3) value; |
| 2 | 188 | | entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte |
| 2 | 189 | | break; |
| | 190 | |
|
| | 191 | | case IBIWCompleteAction.ActionType.SCALE: |
| 2 | 192 | | Vector3 convertedScale = (Vector3) value; |
| 2 | 193 | | IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity; |
| 2 | 194 | | Transform parent = entityToApply.gameObject.transform.parent; |
| | 195 | |
|
| 2 | 196 | | entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv |
| 2 | 197 | | break; |
| | 198 | |
|
| | 199 | | case IBIWCompleteAction.ActionType.CREATE: |
| 2 | 200 | | string entityString = (string) value; |
| 2 | 201 | | if (isUndo) |
| 1 | 202 | | entityHandler.DeleteEntity(entityString); |
| | 203 | | else |
| 1 | 204 | | entityHandler.CreateEntityFromJSON(entityString); |
| | 205 | |
|
| 1 | 206 | | break; |
| | 207 | |
|
| | 208 | | case IBIWCompleteAction.ActionType.DELETE: |
| 2 | 209 | | string deletedEntityString = (string) value; |
| | 210 | |
|
| 2 | 211 | | if (isUndo) |
| 1 | 212 | | entityHandler.CreateEntityFromJSON(deletedEntityString); |
| | 213 | | else |
| 1 | 214 | | entityHandler.DeleteEntity(deletedEntityString); |
| | 215 | |
|
| 1 | 216 | | break; |
| | 217 | | case IBIWCompleteAction.ActionType.CHANGE_FLOOR: |
| 2 | 218 | | string catalogItemToApply = (string) value; |
| | 219 | |
|
| 2 | 220 | | CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply); |
| 2 | 221 | | entityHandler.DeleteFloorEntities(); |
| 2 | 222 | | floorHandler.CreateFloor(floorObject); |
| | 223 | | break; |
| | 224 | | } |
| 2 | 225 | | } |
| | 226 | |
|
| | 227 | | void RedoCurrentAction() |
| | 228 | | { |
| 6 | 229 | | if (!actionsMade[currentRedoStepIndex].IsDone()) |
| | 230 | | { |
| 6 | 231 | | actionsMade[currentRedoStepIndex].Redo(); |
| 6 | 232 | | OnRedo?.Invoke(); |
| | 233 | |
|
| 6 | 234 | | CheckButtonsInteractability(); |
| | 235 | | } |
| 6 | 236 | | } |
| | 237 | |
|
| | 238 | | void UndoCurrentAction() |
| | 239 | | { |
| 6 | 240 | | if (actionsMade[currentUndoStepIndex].IsDone()) |
| | 241 | | { |
| 6 | 242 | | actionsMade[currentUndoStepIndex].Undo(); |
| 6 | 243 | | OnUndo?.Invoke(); |
| | 244 | |
|
| 6 | 245 | | CheckButtonsInteractability(); |
| | 246 | | } |
| 6 | 247 | | } |
| | 248 | |
|
| | 249 | | void CheckButtonsInteractability() |
| | 250 | | { |
| 27 | 251 | | if ( context.editorContext.editorHUD == null) |
| 0 | 252 | | return; |
| | 253 | |
|
| 27 | 254 | | bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act |
| 27 | 255 | | bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].IsDone()); |
| | 256 | |
|
| 27 | 257 | | context.editorContext.editorHUD.SetRedoButtonInteractable(canRedoAction); |
| 27 | 258 | | context.editorContext.editorHUD.SetUndoButtonInteractable(canUndoAction); |
| 27 | 259 | | } |
| | 260 | | } |