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