| | 1 | | using DCL.Controllers; |
| | 2 | | using System; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Runtime.Remoting.Contexts; |
| | 6 | | using DCL; |
| | 7 | | using DCL.Builder; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | public class BIWModeController : BIWController, IBIWModeController |
| | 11 | | { |
| | 12 | |
|
| | 13 | | private GameObject cursorGO; |
| | 14 | | private GameObject cameraParentGO; |
| | 15 | |
|
| | 16 | | private IBIWActionController actionController; |
| | 17 | | private IBIWEntityHandler entityHandler; |
| | 18 | |
|
| | 19 | | private BIWFirstPersonMode firstPersonMode; |
| 0 | 20 | | public BIWGodMode godMode { get; set; } |
| | 21 | |
|
| | 22 | | private InputAction_Trigger toggleSnapModeInputAction; |
| | 23 | |
|
| | 24 | | public event Action OnInputDone; |
| | 25 | | public event Action<IBIWModeController.EditModeState, IBIWModeController.EditModeState> OnChangedEditModeState; |
| | 26 | |
|
| | 27 | | private IBIWModeController.EditModeState currentEditModeState = IBIWModeController.EditModeState.Inactive; |
| | 28 | |
|
| | 29 | | private BIWMode currentActiveMode; |
| | 30 | |
|
| | 31 | | private bool isSnapActive = false; |
| | 32 | |
|
| | 33 | | private InputAction_Trigger.Triggered snapModeDelegate; |
| | 34 | |
|
| | 35 | | private GameObject editionGO; |
| | 36 | | private GameObject undoGO; |
| | 37 | | private GameObject snapGO; |
| | 38 | | private GameObject freeMovementGO; |
| | 39 | |
|
| | 40 | | public override void Initialize(IContext context) |
| | 41 | | { |
| 34 | 42 | | base.Initialize(context); |
| | 43 | |
|
| 34 | 44 | | cursorGO = context.sceneReferences.cursorCanvas; |
| 34 | 45 | | cameraParentGO = context.sceneReferences.biwCameraParent; |
| 34 | 46 | | InitGameObjects(); |
| | 47 | |
|
| 34 | 48 | | firstPersonMode = new BIWFirstPersonMode(); |
| 34 | 49 | | godMode = new BIWGodMode(); |
| | 50 | |
|
| 34 | 51 | | firstPersonMode.Init(context); |
| 34 | 52 | | godMode.Init(context); |
| | 53 | |
|
| 34 | 54 | | firstPersonMode.OnInputDone += InputDone; |
| 34 | 55 | | godMode.OnInputDone += InputDone; |
| | 56 | |
|
| 34 | 57 | | if ( context.editorContext.editorHUD != null) |
| | 58 | | { |
| 34 | 59 | | context.editorContext.editorHUD.OnChangeModeAction += ChangeAdvanceMode; |
| 34 | 60 | | context.editorContext.editorHUD.OnResetAction += ResetScaleAndRotation; |
| 34 | 61 | | context.editorContext.editorHUD.OnChangeSnapModeAction += ChangeSnapMode; |
| | 62 | | } |
| | 63 | |
|
| 34 | 64 | | actionController = context.editorContext.actionController; |
| 34 | 65 | | entityHandler = context.editorContext.entityHandler; |
| 34 | 66 | | toggleSnapModeInputAction = context.inputsReferencesAsset.toggleSnapModeInputAction; |
| | 67 | |
|
| 34 | 68 | | snapModeDelegate = (action) => ChangeSnapMode(); |
| 34 | 69 | | toggleSnapModeInputAction.OnTriggered += snapModeDelegate; |
| | 70 | |
|
| 34 | 71 | | firstPersonMode.OnActionGenerated += actionController.AddAction; |
| 34 | 72 | | godMode.OnActionGenerated += actionController.AddAction; |
| | 73 | |
|
| 34 | 74 | | SetEditorGameObjects(); |
| 34 | 75 | | } |
| | 76 | |
|
| | 77 | | public override void Dispose() |
| | 78 | | { |
| 34 | 79 | | base.Dispose(); |
| | 80 | |
|
| 34 | 81 | | toggleSnapModeInputAction.OnTriggered -= snapModeDelegate; |
| | 82 | |
|
| 34 | 83 | | firstPersonMode.OnInputDone -= InputDone; |
| 34 | 84 | | godMode.OnInputDone -= InputDone; |
| | 85 | |
|
| 34 | 86 | | firstPersonMode.OnActionGenerated -= actionController.AddAction; |
| 34 | 87 | | godMode.OnActionGenerated -= actionController.AddAction; |
| | 88 | |
|
| 34 | 89 | | firstPersonMode.Dispose(); |
| 34 | 90 | | godMode.Dispose(); |
| | 91 | |
|
| 34 | 92 | | if ( context.editorContext.editorHUD != null) |
| | 93 | | { |
| 34 | 94 | | context.editorContext.editorHUD.OnChangeModeAction -= ChangeAdvanceMode; |
| 34 | 95 | | context.editorContext.editorHUD.OnResetAction -= ResetScaleAndRotation; |
| | 96 | | } |
| | 97 | |
|
| 34 | 98 | | GameObject.Destroy(undoGO); |
| 34 | 99 | | GameObject.Destroy(snapGO); |
| 34 | 100 | | GameObject.Destroy(editionGO); |
| 34 | 101 | | GameObject.Destroy(freeMovementGO); |
| 34 | 102 | | } |
| | 103 | |
|
| | 104 | | private void SetEditorGameObjects() |
| | 105 | | { |
| 34 | 106 | | godMode.SetEditorReferences(editionGO, undoGO, snapGO, freeMovementGO, entityHandler.GetSelectedEntityList()); |
| 34 | 107 | | firstPersonMode.SetEditorReferences(editionGO, undoGO, snapGO, freeMovementGO, entityHandler.GetSelectedEntityLi |
| 34 | 108 | | } |
| | 109 | |
|
| | 110 | | private void InitGameObjects() |
| | 111 | | { |
| 34 | 112 | | if (snapGO == null) |
| 34 | 113 | | snapGO = new GameObject("SnapGameObject"); |
| | 114 | |
|
| 34 | 115 | | if (freeMovementGO == null) |
| 34 | 116 | | freeMovementGO = new GameObject("FreeMovementGO"); |
| | 117 | |
|
| 34 | 118 | | freeMovementGO.transform.SetParent(cameraParentGO.transform); |
| | 119 | |
|
| 34 | 120 | | if (editionGO == null) |
| 34 | 121 | | editionGO = new GameObject("EditionGO"); |
| | 122 | |
|
| 34 | 123 | | editionGO.transform.SetParent(cameraParentGO.transform); |
| | 124 | |
|
| 34 | 125 | | if (undoGO == null) |
| 34 | 126 | | undoGO = new GameObject("UndoGameObject"); |
| 34 | 127 | | } |
| | 128 | |
|
| 0 | 129 | | public bool IsGodModeActive() { return currentEditModeState == IBIWModeController.EditModeState.GodMode; } |
| | 130 | |
|
| | 131 | | public Vector3 GetCurrentEditionPosition() |
| | 132 | | { |
| 0 | 133 | | if (editionGO != null) |
| 0 | 134 | | return editionGO.transform.position; |
| | 135 | | else |
| 0 | 136 | | return Vector3.zero; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | public void UndoEditionGOLastStep() |
| | 140 | | { |
| 0 | 141 | | if (undoGO == null || editionGO == null) |
| 0 | 142 | | return; |
| | 143 | |
|
| 0 | 144 | | BIWUtils.CopyGameObjectStatus(undoGO, editionGO, false, false); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | | public override void OnGUI() |
| | 148 | | { |
| 0 | 149 | | base.OnGUI(); |
| 0 | 150 | | godMode.OnGUI(); |
| 0 | 151 | | } |
| | 152 | |
|
| | 153 | | public override void Update() |
| | 154 | | { |
| 0 | 155 | | base.Update(); |
| 0 | 156 | | godMode.Update(); |
| 0 | 157 | | firstPersonMode.Update(); |
| 0 | 158 | | } |
| | 159 | |
|
| | 160 | | public override void LateUpdate() |
| | 161 | | { |
| 0 | 162 | | base.LateUpdate(); |
| 0 | 163 | | firstPersonMode.LateUpdate(); |
| 0 | 164 | | } |
| | 165 | |
|
| | 166 | | public override void EnterEditMode(IParcelScene scene) |
| | 167 | | { |
| 34 | 168 | | base.EnterEditMode(scene); |
| 34 | 169 | | if (currentActiveMode == null) |
| 34 | 170 | | SetBuildMode(IBIWModeController.EditModeState.GodMode); |
| 34 | 171 | | } |
| | 172 | |
|
| | 173 | | public override void ExitEditMode() |
| | 174 | | { |
| 0 | 175 | | base.ExitEditMode(); |
| 0 | 176 | | SetBuildMode(IBIWModeController.EditModeState.Inactive); |
| 0 | 177 | | snapGO.transform.SetParent(null); |
| 0 | 178 | | } |
| | 179 | |
|
| 0 | 180 | | public BIWMode GetCurrentMode() => currentActiveMode; |
| | 181 | |
|
| 0 | 182 | | public IBIWModeController.EditModeState GetCurrentStateMode() => currentEditModeState; |
| | 183 | |
|
| 0 | 184 | | private void InputDone() { OnInputDone?.Invoke(); } |
| | 185 | |
|
| 0 | 186 | | public void StartMultiSelection() { currentActiveMode.StartMultiSelection(); } |
| | 187 | |
|
| 0 | 188 | | public void EndMultiSelection() { currentActiveMode.EndMultiSelection(); } |
| | 189 | |
|
| 0 | 190 | | public void ResetScaleAndRotation() { currentActiveMode.ResetScaleAndRotation(); } |
| | 191 | |
|
| 0 | 192 | | public void CheckInput() { currentActiveMode?.CheckInput(); } |
| | 193 | |
|
| 0 | 194 | | public void CheckInputSelectedEntities() { currentActiveMode.CheckInputSelectedEntities(); } |
| | 195 | |
|
| 0 | 196 | | public bool ShouldCancelUndoAction() { return currentActiveMode.ShouldCancelUndoAction(); } |
| | 197 | |
|
| 2 | 198 | | public void CreatedEntity(BIWEntity entity) { currentActiveMode?.CreatedEntity(entity); } |
| | 199 | |
|
| 1 | 200 | | public float GetMaxDistanceToSelectEntities() { return currentActiveMode.maxDistanceToSelectEntities; } |
| | 201 | |
|
| 0 | 202 | | public void EntityDoubleClick(BIWEntity entity) { currentActiveMode.EntityDoubleClick(entity); } |
| | 203 | |
|
| 1 | 204 | | public Vector3 GetMousePosition() { return currentActiveMode.GetPointerPosition(); } |
| | 205 | |
|
| | 206 | | public Vector3 GetModeCreationEntryPoint() |
| | 207 | | { |
| 0 | 208 | | if (currentActiveMode != null) |
| 0 | 209 | | return currentActiveMode.GetCreatedEntityPoint(); |
| 0 | 210 | | return Vector3.zero; |
| | 211 | | } |
| | 212 | |
|
| 0 | 213 | | public void MouseClickDetected() { currentActiveMode?.MouseClickDetected(); } |
| | 214 | |
|
| | 215 | | private void ChangeSnapMode() |
| | 216 | | { |
| 0 | 217 | | SetSnapActive(!isSnapActive); |
| 0 | 218 | | InputDone(); |
| | 219 | |
|
| 0 | 220 | | if (isSnapActive) |
| 0 | 221 | | AudioScriptableObjects.enable.Play(); |
| | 222 | | else |
| 0 | 223 | | AudioScriptableObjects.disable.Play(); |
| 0 | 224 | | } |
| | 225 | |
|
| | 226 | | public void SetSnapActive(bool isActive) |
| | 227 | | { |
| 2 | 228 | | isSnapActive = isActive; |
| 2 | 229 | | currentActiveMode.SetSnapActive(isActive); |
| 2 | 230 | | } |
| | 231 | |
|
| | 232 | | public void ChangeAdvanceMode() |
| | 233 | | { |
| 0 | 234 | | if (currentEditModeState == IBIWModeController.EditModeState.GodMode) |
| | 235 | | { |
| 0 | 236 | | SetBuildMode(IBIWModeController.EditModeState.FirstPerson); |
| 0 | 237 | | } |
| | 238 | | else |
| | 239 | | { |
| 0 | 240 | | SetBuildMode(IBIWModeController.EditModeState.GodMode); |
| | 241 | | } |
| | 242 | |
|
| 0 | 243 | | InputDone(); |
| 0 | 244 | | } |
| | 245 | |
|
| | 246 | | public void SetBuildMode(IBIWModeController.EditModeState state) |
| | 247 | | { |
| 40 | 248 | | IBIWModeController.EditModeState previousState = currentEditModeState; |
| | 249 | |
|
| 40 | 250 | | if (currentActiveMode != null) |
| 4 | 251 | | currentActiveMode.Deactivate(); |
| | 252 | |
|
| 40 | 253 | | currentActiveMode = null; |
| | 254 | | switch (state) |
| | 255 | | { |
| | 256 | | case IBIWModeController.EditModeState.Inactive: |
| | 257 | | break; |
| | 258 | | case IBIWModeController.EditModeState.FirstPerson: |
| 1 | 259 | | currentActiveMode = firstPersonMode; |
| | 260 | |
|
| 1 | 261 | | if (cursorGO != null) |
| 1 | 262 | | cursorGO.SetActive(true); |
| 1 | 263 | | break; |
| | 264 | | case IBIWModeController.EditModeState.GodMode: |
| 36 | 265 | | if (cursorGO != null) |
| 36 | 266 | | cursorGO.SetActive(false); |
| 36 | 267 | | currentActiveMode = godMode; |
| | 268 | | break; |
| | 269 | | } |
| | 270 | |
|
| 40 | 271 | | currentEditModeState = state; |
| | 272 | |
|
| 40 | 273 | | if (currentActiveMode != null) |
| | 274 | | { |
| 37 | 275 | | currentActiveMode.Activate(sceneToEdit); |
| 37 | 276 | | currentActiveMode.SetSnapActive(isSnapActive); |
| 37 | 277 | | entityHandler.SetActiveMode(currentActiveMode); |
| | 278 | | } |
| | 279 | |
|
| 40 | 280 | | OnChangedEditModeState?.Invoke(previousState, state); |
| 0 | 281 | | } |
| | 282 | | } |