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