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