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