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