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