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