| | 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 | | private IBIWSaveController saveController; |
| | 22 | |
|
| 5 | 23 | | 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 | | { |
| 8 | 31 | | base.Initialize(context); |
| | 32 | |
|
| 8 | 33 | | entityHandler = context.editorContext.entityHandler; |
| 8 | 34 | | floorHandler = context.editorContext.floorHandler; |
| 8 | 35 | | saveController = context.editorContext.saveController; |
| | 36 | |
|
| 8 | 37 | | if ( context.editorContext.editorHUD == null) |
| 0 | 38 | | return; |
| 8 | 39 | | context.editorContext.editorHUD.OnUndoAction += TryToUndoAction; |
| 8 | 40 | | context.editorContext.editorHUD.OnRedoAction += TryToRedoAction; |
| 8 | 41 | | } |
| | 42 | |
|
| | 43 | | public override void Dispose() |
| | 44 | | { |
| 8 | 45 | | if ( context.editorContext.editorHUD != null) |
| | 46 | | { |
| 8 | 47 | | context.editorContext.editorHUD.OnUndoAction -= TryToUndoAction; |
| 8 | 48 | | context.editorContext.editorHUD.OnRedoAction -= TryToRedoAction; |
| | 49 | | } |
| | 50 | |
|
| 8 | 51 | | Clear(); |
| 8 | 52 | | } |
| | 53 | |
|
| | 54 | | public override void EnterEditMode(IBuilderScene scene) |
| | 55 | | { |
| 8 | 56 | | base.EnterEditMode(scene); |
| 8 | 57 | | actionsMade.Clear(); |
| | 58 | |
|
| 8 | 59 | | CheckButtonsInteractability(); |
| 8 | 60 | | lastActionTimeStamp = 0; |
| 8 | 61 | | } |
| | 62 | |
|
| | 63 | | public void Clear() |
| | 64 | | { |
| 8 | 65 | | actionsMade.Clear(); |
| 8 | 66 | | currentUndoStepIndex = 0; |
| 8 | 67 | | currentRedoStepIndex = 0; |
| 8 | 68 | | } |
| | 69 | |
|
| | 70 | | public bool HasApplyAnyActionThisSession() |
| | 71 | | { |
| 0 | 72 | | if (actionsMade.Count != 0) |
| | 73 | | { |
| 0 | 74 | | return actionsMade[0].IsDone(); |
| | 75 | | } |
| | 76 | |
|
| 0 | 77 | | return false; |
| | 78 | | } |
| | 79 | |
|
| 0 | 80 | | 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 | | { |
| 6 | 107 | | if (currentRedoStepIndex >= actionsMade.Count || currentRedoStepIndex < 0) |
| 0 | 108 | | return; |
| | 109 | |
|
| 6 | 110 | | RedoCurrentAction(); |
| | 111 | |
|
| 6 | 112 | | if (currentRedoStepIndex + 1 < actionsMade.Count) |
| 0 | 113 | | currentRedoStepIndex++; |
| | 114 | |
|
| 6 | 115 | | if (currentUndoStepIndex < actionsMade.Count - 1) |
| 2 | 116 | | currentUndoStepIndex++; |
| | 117 | |
|
| 6 | 118 | | if (VERBOSE) |
| 0 | 119 | | Debug.Log("Redo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 6 | 120 | | } |
| | 121 | |
|
| | 122 | | public void TryToUndoAction() |
| | 123 | | { |
| 6 | 124 | | if (currentUndoStepIndex < 0 || |
| | 125 | | actionsMade.Count <= 0 || |
| | 126 | | !actionsMade[0].IsDone()) |
| 0 | 127 | | return; |
| | 128 | |
|
| 6 | 129 | | UndoCurrentAction(); |
| | 130 | |
|
| 6 | 131 | | if (currentUndoStepIndex > 0) |
| | 132 | | { |
| 2 | 133 | | currentUndoStepIndex--; |
| 2 | 134 | | if (currentRedoStepIndex < actionsMade.Count - 1 || currentRedoStepIndex - currentUndoStepIndex > 1) |
| 0 | 135 | | currentRedoStepIndex--; |
| 0 | 136 | | } |
| 4 | 137 | | else if (!actionsMade[currentUndoStepIndex].IsDone() && currentRedoStepIndex > 0) |
| | 138 | | { |
| 0 | 139 | | currentRedoStepIndex--; |
| | 140 | | } |
| | 141 | |
|
| 6 | 142 | | if (VERBOSE) |
| 0 | 143 | | Debug.Log("Undo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 6 | 144 | | } |
| | 145 | |
|
| 2 | 146 | | public void CreateActionEntityDeleted(BIWEntity entity) { CreateActionEntityDeleted(new List<BIWEntity> { entity }); |
| | 147 | |
|
| | 148 | | public void CreateActionEntityDeleted(List<BIWEntity> entityList) |
| | 149 | | { |
| 1 | 150 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| 1 | 151 | | List<BIWEntityAction> entityActionList = new List<BIWEntityAction>(); |
| | 152 | |
|
| 4 | 153 | | foreach (BIWEntity entity in entityList) |
| | 154 | | { |
| 1 | 155 | | BIWEntityAction biwEntityAction = new BIWEntityAction(entity.rootEntity.entityId, BIWUtils.ConvertEntityToJS |
| 1 | 156 | | entityActionList.Add(biwEntityAction); |
| | 157 | | } |
| | 158 | |
|
| 1 | 159 | | buildAction.CreateActionType(entityActionList, IBIWCompleteAction.ActionType.DELETE); |
| | 160 | |
|
| 1 | 161 | | AddAction(buildAction); |
| 1 | 162 | | } |
| | 163 | |
|
| | 164 | | public void CreateActionEntityCreated(IDCLEntity entity) |
| | 165 | | { |
| 1 | 166 | | BIWEntityAction biwEntityAction = new BIWEntityAction(entity, entity.entityId, BIWUtils.ConvertEntityToJSON(enti |
| | 167 | |
|
| 1 | 168 | | BIWCompleteAction buildAction = new BIWCompleteAction(); |
| 1 | 169 | | buildAction.CreateActionType(biwEntityAction, IBIWCompleteAction.ActionType.CREATE); |
| | 170 | |
|
| 1 | 171 | | AddAction(buildAction); |
| 1 | 172 | | } |
| | 173 | |
|
| | 174 | | public void AddAction(IBIWCompleteAction action) |
| | 175 | | { |
| 7 | 176 | | if (currentRedoStepIndex < actionsMade.Count - 1) |
| 0 | 177 | | actionsMade.RemoveRange(currentRedoStepIndex, actionsMade.Count - currentRedoStepIndex); |
| 7 | 178 | | else if (actionsMade.Count > 0 && !actionsMade[currentRedoStepIndex].IsDone()) |
| 0 | 179 | | actionsMade.RemoveAt(actionsMade.Count - 1); |
| | 180 | |
|
| 7 | 181 | | actionsMade.Add(action); |
| | 182 | |
|
| 7 | 183 | | currentUndoStepIndex = actionsMade.Count - 1; |
| 7 | 184 | | currentRedoStepIndex = actionsMade.Count - 1; |
| | 185 | |
|
| | 186 | |
|
| 7 | 187 | | if (VERBOSE) |
| 0 | 188 | | Debug.Log("Redo: Current actions " + actionsMade.Count + " Current undo index " + currentUndoStepIndex + |
| 7 | 189 | | action.OnApplyValue += ApplyAction; |
| 7 | 190 | | CheckButtonsInteractability(); |
| | 191 | |
|
| 7 | 192 | | lastActionTimeStamp = Time.unscaledTime; |
| 7 | 193 | | } |
| | 194 | |
|
| | 195 | | void ApplyAction(long entityIdToApply, object value, IBIWCompleteAction.ActionType actionType, bool isUndo) |
| | 196 | | { |
| | 197 | | switch (actionType) |
| | 198 | | { |
| | 199 | | case IBIWCompleteAction.ActionType.MOVE: |
| 2 | 200 | | Vector3 convertedPosition = (Vector3) value; |
| 2 | 201 | | entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.position = convertedPo |
| 2 | 202 | | break; |
| | 203 | |
|
| | 204 | | case IBIWCompleteAction.ActionType.ROTATE: |
| 2 | 205 | | Vector3 convertedAngles = (Vector3) value; |
| 2 | 206 | | entityHandler.GetConvertedEntity(entityIdToApply).rootEntity.gameObject.transform.eulerAngles = converte |
| 2 | 207 | | break; |
| | 208 | |
|
| | 209 | | case IBIWCompleteAction.ActionType.SCALE: |
| 2 | 210 | | Vector3 convertedScale = (Vector3) value; |
| 2 | 211 | | IDCLEntity entityToApply = entityHandler.GetConvertedEntity(entityIdToApply).rootEntity; |
| 2 | 212 | | Transform parent = entityToApply.gameObject.transform.parent; |
| | 213 | |
|
| 2 | 214 | | entityToApply.gameObject.transform.localScale = new Vector3(convertedScale.x / parent.localScale.x, conv |
| 2 | 215 | | break; |
| | 216 | |
|
| | 217 | | case IBIWCompleteAction.ActionType.CREATE: |
| 2 | 218 | | if (isUndo) |
| | 219 | | { |
| 1 | 220 | | long entityString = (long) value; |
| 1 | 221 | | entityHandler.DeleteEntity(entityString); |
| 1 | 222 | | } |
| | 223 | | else |
| | 224 | | { |
| 1 | 225 | | string entityString = (string) value; |
| 1 | 226 | | entityHandler.CreateEntityFromJSON(entityString); |
| | 227 | | } |
| | 228 | |
|
| 1 | 229 | | break; |
| | 230 | |
|
| | 231 | | case IBIWCompleteAction.ActionType.DELETE: |
| 2 | 232 | | if (isUndo) |
| | 233 | | { |
| 1 | 234 | | string deletedEntityString = (string) value; |
| 1 | 235 | | entityHandler.CreateEntityFromJSON(deletedEntityString); |
| 1 | 236 | | } |
| | 237 | | else |
| | 238 | | { |
| 1 | 239 | | long deletedEntityString = (long) value; |
| 1 | 240 | | entityHandler.DeleteEntity(deletedEntityString); |
| | 241 | | } |
| | 242 | |
|
| 1 | 243 | | break; |
| | 244 | | case IBIWCompleteAction.ActionType.CHANGE_FLOOR: |
| 2 | 245 | | string catalogItemToApply = (string) value; |
| | 246 | |
|
| 2 | 247 | | CatalogItem floorObject = JsonConvert.DeserializeObject<CatalogItem>(catalogItemToApply); |
| 2 | 248 | | entityHandler.DeleteFloorEntities(); |
| 2 | 249 | | floorHandler.CreateFloor(floorObject); |
| | 250 | | break; |
| | 251 | | } |
| 2 | 252 | | } |
| | 253 | |
|
| | 254 | | void RedoCurrentAction() |
| | 255 | | { |
| 6 | 256 | | if (!actionsMade[currentRedoStepIndex].IsDone()) |
| | 257 | | { |
| 6 | 258 | | actionsMade[currentRedoStepIndex].Redo(); |
| 6 | 259 | | OnRedo?.Invoke(); |
| | 260 | |
|
| 6 | 261 | | CheckButtonsInteractability(); |
| 6 | 262 | | saveController.TryToSave(); |
| 6 | 263 | | lastActionTimeStamp = Time.unscaledTime; |
| | 264 | | } |
| 6 | 265 | | } |
| | 266 | |
|
| | 267 | | void UndoCurrentAction() |
| | 268 | | { |
| 6 | 269 | | if (actionsMade[currentUndoStepIndex].IsDone()) |
| | 270 | | { |
| 6 | 271 | | actionsMade[currentUndoStepIndex].Undo(); |
| 6 | 272 | | OnUndo?.Invoke(); |
| | 273 | |
|
| 6 | 274 | | CheckButtonsInteractability(); |
| 6 | 275 | | saveController.TryToSave(); |
| 6 | 276 | | lastActionTimeStamp = Time.unscaledTime; |
| | 277 | | } |
| 6 | 278 | | } |
| | 279 | |
|
| | 280 | | void CheckButtonsInteractability() |
| | 281 | | { |
| 27 | 282 | | if ( context.editorContext.editorHUD == null) |
| 0 | 283 | | return; |
| | 284 | |
|
| 27 | 285 | | bool canRedoAction = actionsMade.Count > 0 && !(currentRedoStepIndex == actionsMade.Count - 1 && actionsMade[act |
| 27 | 286 | | bool canUndoAction = actionsMade.Count > 0 && !(currentUndoStepIndex == 0 && !actionsMade[0].IsDone()); |
| | 287 | |
|
| 27 | 288 | | context.editorContext.editorHUD.SetRedoButtonInteractable(canRedoAction); |
| 27 | 289 | | context.editorContext.editorHUD.SetUndoButtonInteractable(canUndoAction); |
| 27 | 290 | | } |
| | 291 | | } |