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