| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using DCL.Configuration; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | public class BIWMode |
| | 9 | | { |
| | 10 | | public event Action OnInputDone; |
| | 11 | | public event Action<BIWCompleteAction> OnActionGenerated; |
| | 12 | |
|
| 0 | 13 | | public bool isSnapActive => isSnapActiveValue; |
| 0 | 14 | | public float maxDistanceToSelectEntities => maxDistanceToSelectEntitiesValue; |
| | 15 | |
|
| 135 | 16 | | protected float maxDistanceToSelectEntitiesValue = 50; |
| | 17 | |
|
| | 18 | | //Note: Snap variables are set in each mode |
| 135 | 19 | | protected float snapFactor = 1f; |
| 135 | 20 | | protected float snapRotationDegresFactor = 15f; |
| 135 | 21 | | protected float snapScaleFactor = 0.5f; |
| 135 | 22 | | protected float snapDistanceToActivateMovement = 10f; |
| | 23 | |
|
| | 24 | | protected IBIWEntityHandler entityHandler; |
| | 25 | | protected IBIWSaveController saveController; |
| | 26 | | protected IBIWActionController actionController; |
| | 27 | | protected IBIWRaycastController raycastController; |
| | 28 | |
|
| | 29 | | protected GameObject editionGO, undoGO, snapGO, freeMovementGO; |
| | 30 | |
|
| | 31 | | internal bool isSnapActiveValue = false; |
| | 32 | | internal bool isModeActive = false; |
| | 33 | | internal bool isMultiSelectionActive = false; |
| | 34 | | internal List<BIWEntity> selectedEntities; |
| | 35 | |
|
| | 36 | | internal bool isNewObjectPlaced = false; |
| | 37 | |
|
| 135 | 38 | | internal List<BIWEntityAction> actionList = new List<BIWEntityAction>(); |
| | 39 | |
|
| | 40 | | public virtual void Init(BIWContext context) |
| | 41 | | { |
| 135 | 42 | | entityHandler = context.entityHandler; |
| 135 | 43 | | saveController = context.saveController; |
| 135 | 44 | | actionController = context.actionController; |
| 135 | 45 | | raycastController = context.raycastController; |
| 135 | 46 | | entityHandler.OnEntityDeleted += OnDeleteEntity; |
| 135 | 47 | | } |
| | 48 | |
|
| | 49 | | public virtual void SetEditorReferences(GameObject goToEdit, GameObject undoGO, GameObject snapGO, GameObject freeMo |
| | 50 | | { |
| 170 | 51 | | editionGO = goToEdit; |
| 170 | 52 | | this.undoGO = undoGO; |
| 170 | 53 | | this.snapGO = snapGO; |
| 170 | 54 | | this.freeMovementGO = freeMovementGO; |
| | 55 | |
|
| 170 | 56 | | this.selectedEntities = selectedEntities; |
| 170 | 57 | | } |
| | 58 | |
|
| 270 | 59 | | public virtual void Dispose() { entityHandler.OnEntityDeleted -= OnDeleteEntity; } |
| | 60 | |
|
| 0 | 61 | | public bool IsActive() { return isModeActive; } |
| 2 | 62 | | public virtual void Activate(IParcelScene scene) { isModeActive = true; } |
| | 63 | |
|
| | 64 | | public virtual void Deactivate() |
| | 65 | | { |
| 7 | 66 | | isModeActive = false; |
| 7 | 67 | | entityHandler.DeselectEntities(); |
| 7 | 68 | | } |
| | 69 | |
|
| | 70 | | public virtual void SetSnapActive(bool isActive) |
| | 71 | | { |
| 57 | 72 | | if (isActive && !isSnapActiveValue) |
| 3 | 73 | | AudioScriptableObjects.enable.Play(); |
| 54 | 74 | | else if (!isActive && isSnapActiveValue) |
| 3 | 75 | | AudioScriptableObjects.disable.Play(); |
| | 76 | |
|
| 57 | 77 | | isSnapActiveValue = isActive; |
| 57 | 78 | | HUDController.i.builderInWorldMainHud?.SetSnapModeActive(isSnapActiveValue); |
| 8 | 79 | | } |
| | 80 | |
|
| 2 | 81 | | public virtual void StartMultiSelection() { isMultiSelectionActive = true; } |
| | 82 | |
|
| 1 | 83 | | public virtual Vector3 GetPointerPosition() { return Input.mousePosition; } |
| | 84 | |
|
| 2 | 85 | | public virtual void EndMultiSelection() { isMultiSelectionActive = false; } |
| | 86 | |
|
| 0 | 87 | | public virtual bool ShouldCancelUndoAction() { return false; } |
| | 88 | |
|
| 0 | 89 | | public virtual void SetDuplicationOffset(float offset) { } |
| | 90 | |
|
| 0 | 91 | | public virtual void EntityDoubleClick(BIWEntity entity) { } |
| | 92 | |
|
| | 93 | | public virtual void SelectedEntity(BIWEntity selectedEntity) |
| | 94 | | { |
| 2 | 95 | | CenterGameObjectToEdit(); |
| | 96 | |
|
| 2 | 97 | | BIWUtils.CopyGameObjectStatus(editionGO, undoGO, false, false); |
| 2 | 98 | | } |
| | 99 | |
|
| | 100 | | public virtual void CenterGameObjectToEdit() |
| | 101 | | { |
| 4 | 102 | | if (selectedEntities.Count > 0) |
| | 103 | | { |
| 8 | 104 | | foreach (BIWEntity entity in selectedEntities) |
| | 105 | | { |
| 2 | 106 | | entity.rootEntity.gameObject.transform.SetParent(null); |
| | 107 | | } |
| | 108 | |
|
| 2 | 109 | | editionGO.transform.position = GetCenterPointOfSelectedObjects(); |
| 2 | 110 | | editionGO.transform.rotation = Quaternion.Euler(0, 0, 0); |
| 2 | 111 | | editionGO.transform.localScale = Vector3.one; |
| 8 | 112 | | foreach (BIWEntity entity in selectedEntities) |
| | 113 | | { |
| 2 | 114 | | entity.rootEntity.gameObject.transform.SetParent(editionGO.transform); |
| | 115 | | } |
| | 116 | | } |
| 4 | 117 | | } |
| | 118 | |
|
| | 119 | | public virtual void MouseClickDetected() |
| | 120 | | { |
| 2 | 121 | | BIWEntity entityToSelect = raycastController.GetEntityOnPointer(); |
| 2 | 122 | | if (entityToSelect != null) |
| | 123 | | { |
| 1 | 124 | | entityHandler.EntityClicked(entityToSelect); |
| 1 | 125 | | } |
| 1 | 126 | | else if (!isMultiSelectionActive) |
| | 127 | | { |
| 1 | 128 | | entityHandler.DeselectEntities(); |
| | 129 | | } |
| 1 | 130 | | } |
| | 131 | |
|
| 2 | 132 | | public virtual void CreatedEntity(BIWEntity createdEntity) { isNewObjectPlaced = true; } |
| | 133 | |
|
| | 134 | | public virtual void EntityDeselected(BIWEntity entityDeselected) |
| | 135 | | { |
| 1 | 136 | | CenterGameObjectToEdit(); |
| | 137 | |
|
| 1 | 138 | | if (isNewObjectPlaced) |
| | 139 | | { |
| 0 | 140 | | actionController.CreateActionEntityCreated(entityDeselected.rootEntity); |
| | 141 | | } |
| | 142 | |
|
| 1 | 143 | | isNewObjectPlaced = false; |
| 1 | 144 | | } |
| | 145 | |
|
| 0 | 146 | | public virtual void OnDeleteEntity(BIWEntity entity) { } |
| | 147 | |
|
| 2 | 148 | | public virtual void OnDeselectedEntities() { entityHandler.ReportTransform(true); } |
| | 149 | |
|
| 1 | 150 | | public virtual void CheckInput() { } |
| | 151 | |
|
| 0 | 152 | | public virtual void CheckInputSelectedEntities() { } |
| | 153 | |
|
| 0 | 154 | | public virtual void InputDone() { OnInputDone?.Invoke(); } |
| | 155 | |
|
| | 156 | | public virtual void ResetScaleAndRotation() |
| | 157 | | { |
| 1 | 158 | | editionGO.transform.localScale = Vector3.one; |
| 1 | 159 | | snapGO.transform.localScale = Vector3.one; |
| 1 | 160 | | freeMovementGO.transform.localScale = Vector3.one; |
| | 161 | |
|
| 1 | 162 | | Quaternion zeroAnglesQuaternion = Quaternion.Euler(Vector3.zero); |
| | 163 | |
|
| 1 | 164 | | snapGO.transform.rotation = zeroAnglesQuaternion; |
| 1 | 165 | | freeMovementGO.transform.rotation = zeroAnglesQuaternion; |
| 1 | 166 | | editionGO.transform.rotation = zeroAnglesQuaternion; |
| | 167 | |
|
| 2 | 168 | | foreach (BIWEntity decentralandEntityToEdit in selectedEntities) |
| | 169 | | { |
| 0 | 170 | | decentralandEntityToEdit.ResetTransfrom(); |
| | 171 | | } |
| | 172 | |
|
| 1 | 173 | | CenterGameObjectToEdit(); |
| 1 | 174 | | } |
| | 175 | |
|
| 0 | 176 | | public virtual Vector3 GetCreatedEntityPoint() { return Vector3.zero; } |
| | 177 | |
|
| | 178 | | protected Vector3 GetCenterPointOfSelectedObjects() |
| | 179 | | { |
| 2 | 180 | | float totalX = 0f; |
| 2 | 181 | | float totalY = 0f; |
| 2 | 182 | | float totalZ = 0f; |
| 8 | 183 | | foreach (BIWEntity entity in selectedEntities) |
| | 184 | | { |
| 2 | 185 | | totalX += entity.rootEntity.gameObject.transform.position.x; |
| 2 | 186 | | totalY += entity.rootEntity.gameObject.transform.position.y; |
| 2 | 187 | | totalZ += entity.rootEntity.gameObject.transform.position.z; |
| | 188 | | } |
| | 189 | |
|
| 2 | 190 | | float centerX = totalX / selectedEntities.Count; |
| 2 | 191 | | float centerY = totalY / selectedEntities.Count; |
| 2 | 192 | | float centerZ = totalZ / selectedEntities.Count; |
| 2 | 193 | | return new Vector3(centerX, centerY, centerZ); |
| | 194 | | } |
| | 195 | |
|
| | 196 | | protected void TransformActionStarted(IDCLEntity entity, string type) |
| | 197 | | { |
| 5 | 198 | | BIWEntityAction buildModeEntityAction = new BIWEntityAction(entity); |
| | 199 | | switch (type) |
| | 200 | | { |
| | 201 | | case BIWSettings.TRANSLATE_GIZMO_NAME: |
| 3 | 202 | | buildModeEntityAction.oldValue = entity.gameObject.transform.position; |
| 3 | 203 | | break; |
| | 204 | | case BIWSettings.ROTATE_GIZMO_NAME: |
| 1 | 205 | | buildModeEntityAction.oldValue = entity.gameObject.transform.rotation.eulerAngles; |
| 1 | 206 | | break; |
| | 207 | | case BIWSettings.SCALE_GIZMO_NAME: |
| 1 | 208 | | buildModeEntityAction.oldValue = entity.gameObject.transform.lossyScale; |
| | 209 | | break; |
| | 210 | | } |
| | 211 | |
|
| 5 | 212 | | actionList.Add(buildModeEntityAction); |
| 5 | 213 | | } |
| | 214 | |
|
| | 215 | | protected void TransformActionEnd(IDCLEntity entity, string type) |
| | 216 | | { |
| 4 | 217 | | List<BIWEntityAction> removeList = new List<BIWEntityAction>(); |
| 16 | 218 | | foreach (BIWEntityAction entityAction in actionList) |
| | 219 | | { |
| 4 | 220 | | if (entityAction.entityId != entity.entityId) |
| | 221 | | continue; |
| | 222 | |
|
| | 223 | | switch (type) |
| | 224 | | { |
| | 225 | | case "MOVE": |
| | 226 | |
|
| 2 | 227 | | entityAction.newValue = entity.gameObject.transform.position; |
| 2 | 228 | | if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f) |
| 1 | 229 | | removeList.Add(entityAction); |
| 1 | 230 | | break; |
| | 231 | | case "ROTATE": |
| | 232 | |
|
| 1 | 233 | | entityAction.newValue = entity.gameObject.transform.rotation.eulerAngles; |
| 1 | 234 | | if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f) |
| 0 | 235 | | removeList.Add(entityAction); |
| 0 | 236 | | break; |
| | 237 | | case "SCALE": |
| 1 | 238 | | entityAction.newValue = entity.gameObject.transform.lossyScale; |
| 1 | 239 | | if (Vector3.Distance((Vector3) entityAction.oldValue, (Vector3) entityAction.newValue) <= 0.09f) |
| 0 | 240 | | removeList.Add(entityAction); |
| | 241 | | break; |
| | 242 | | } |
| | 243 | | } |
| | 244 | |
|
| 10 | 245 | | foreach (BIWEntityAction entityAction in removeList) |
| | 246 | | { |
| 1 | 247 | | actionList.Remove(entityAction); |
| | 248 | | } |
| 4 | 249 | | } |
| | 250 | |
|
| | 251 | | protected void ActionFinish(BIWCompleteAction.ActionType type) |
| | 252 | | { |
| 4 | 253 | | if (actionList.Count > 0 && selectedEntities.Count > 0) |
| | 254 | | { |
| 3 | 255 | | BIWCompleteAction buildModeAction = new BIWCompleteAction(); |
| | 256 | |
|
| 3 | 257 | | buildModeAction.actionType = type; |
| 3 | 258 | | buildModeAction.CreateActionType(actionList, type); |
| 3 | 259 | | OnActionGenerated?.Invoke(buildModeAction); |
| | 260 | |
|
| 3 | 261 | | actionList = new List<BIWEntityAction>(); |
| | 262 | | } |
| 4 | 263 | | } |
| 2 | 264 | | public virtual void Update() { } |
| 0 | 265 | | public virtual void OnGUI() { } |
| 0 | 266 | | public virtual void LateUpdate() { } |
| | 267 | | } |