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