| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | | using System; |
| | 7 | |
|
| | 8 | | public interface IBIWInputHandler { } |
| | 9 | |
|
| | 10 | | public class BIWInputHandler : BIWController, IBIWInputHandler |
| | 11 | | { |
| | 12 | | private const float MS_BETWEEN_INPUT_INTERACTION = 200; |
| | 13 | |
|
| | 14 | | private IBIWActionController actionController; |
| | 15 | | private IBIWModeController modeController; |
| | 16 | | private IBIWInputWrapper inputWrapper; |
| | 17 | | private IBIWOutlinerController outlinerController; |
| | 18 | | private IBIWEntityHandler entityHandler; |
| | 19 | |
|
| | 20 | | private InputAction_Trigger toggleRedoActionInputAction; |
| | 21 | | private InputAction_Trigger toggleUndoActionInputAction; |
| | 22 | | private InputAction_Hold multiSelectionInputAction; |
| | 23 | |
|
| | 24 | | private InputAction_Hold.Started multiSelectionStartDelegate; |
| | 25 | | private InputAction_Hold.Finished multiSelectionFinishedDelegate; |
| | 26 | |
|
| | 27 | | private InputAction_Trigger.Triggered redoDelegate; |
| | 28 | | private InputAction_Trigger.Triggered undoDelegate; |
| | 29 | |
|
| | 30 | | private bool isMultiSelectionActive = false; |
| | 31 | |
|
| | 32 | | private float nexTimeToReceiveInput; |
| | 33 | |
|
| | 34 | | public override void Init(BIWContext biwContext) |
| | 35 | | { |
| 26 | 36 | | base.Init(biwContext); |
| | 37 | |
|
| 26 | 38 | | actionController = biwContext.actionController; |
| 26 | 39 | | modeController = biwContext.modeController; |
| 26 | 40 | | inputWrapper = biwContext.inputWrapper; |
| 26 | 41 | | outlinerController = biwContext.outlinerController; |
| 26 | 42 | | entityHandler = biwContext.entityHandler; |
| | 43 | |
|
| 26 | 44 | | toggleRedoActionInputAction = biwContext.inputsReferencesAsset.toggleRedoActionInputAction; |
| 26 | 45 | | toggleUndoActionInputAction = biwContext.inputsReferencesAsset.toggleUndoActionInputAction; |
| 26 | 46 | | multiSelectionInputAction = biwContext.inputsReferencesAsset.multiSelectionInputAction; |
| | 47 | |
|
| 26 | 48 | | if (HUDController.i.builderInWorldMainHud != null) |
| | 49 | | { |
| 26 | 50 | | HUDController.i.builderInWorldMainHud.OnStopInput += StopInput; |
| 26 | 51 | | HUDController.i.builderInWorldMainHud.OnResumeInput += ResumeInput; |
| | 52 | | } |
| | 53 | |
|
| 26 | 54 | | redoDelegate = (action) => RedoAction(); |
| 26 | 55 | | undoDelegate = (action) => UndoAction(); |
| | 56 | |
|
| 26 | 57 | | toggleRedoActionInputAction.OnTriggered += redoDelegate; |
| 26 | 58 | | toggleUndoActionInputAction.OnTriggered += undoDelegate; |
| | 59 | |
|
| 26 | 60 | | multiSelectionStartDelegate = (action) => StartMultiSelection(); |
| 26 | 61 | | multiSelectionFinishedDelegate = (action) => EndMultiSelection(); |
| | 62 | |
|
| 26 | 63 | | BIWInputWrapper.OnMouseClick += MouseClick; |
| 26 | 64 | | BIWInputWrapper.OnMouseClickOnUI += MouseClickOnUI; |
| 26 | 65 | | modeController.OnInputDone += InputDone; |
| | 66 | |
|
| 26 | 67 | | multiSelectionInputAction.OnStarted += multiSelectionStartDelegate; |
| 26 | 68 | | multiSelectionInputAction.OnFinished += multiSelectionFinishedDelegate; |
| 26 | 69 | | } |
| | 70 | |
|
| | 71 | | public override void Dispose() |
| | 72 | | { |
| 27 | 73 | | base.Dispose(); |
| | 74 | |
|
| 27 | 75 | | toggleRedoActionInputAction.OnTriggered -= redoDelegate; |
| 27 | 76 | | toggleUndoActionInputAction.OnTriggered -= undoDelegate; |
| | 77 | |
|
| 27 | 78 | | multiSelectionInputAction.OnStarted -= multiSelectionStartDelegate; |
| 27 | 79 | | multiSelectionInputAction.OnFinished -= multiSelectionFinishedDelegate; |
| | 80 | |
|
| 27 | 81 | | BIWInputWrapper.OnMouseClick -= MouseClick; |
| 27 | 82 | | BIWInputWrapper.OnMouseClickOnUI -= MouseClickOnUI; |
| 27 | 83 | | modeController.OnInputDone -= InputDone; |
| 27 | 84 | | if (HUDController.i.builderInWorldMainHud != null) |
| | 85 | | { |
| 27 | 86 | | HUDController.i.builderInWorldMainHud.OnStopInput -= StopInput; |
| 27 | 87 | | HUDController.i.builderInWorldMainHud.OnResumeInput -= ResumeInput; |
| | 88 | | } |
| 27 | 89 | | } |
| | 90 | |
|
| | 91 | | public override void Update() |
| | 92 | | { |
| 2 | 93 | | base.Update(); |
| | 94 | |
|
| 2 | 95 | | if (Time.timeSinceLevelLoad < nexTimeToReceiveInput) |
| 0 | 96 | | return; |
| | 97 | |
|
| 2 | 98 | | if (Utils.isCursorLocked || modeController.IsGodModeActive()) |
| 1 | 99 | | CheckEditModeInput(); |
| 2 | 100 | | modeController.CheckInput(); |
| 2 | 101 | | } |
| | 102 | |
|
| | 103 | | private void CheckEditModeInput() |
| | 104 | | { |
| 1 | 105 | | outlinerController.CheckOutline(); |
| | 106 | |
|
| 1 | 107 | | if (entityHandler.IsAnyEntitySelected()) |
| | 108 | | { |
| 0 | 109 | | modeController.CheckInputSelectedEntities(); |
| | 110 | | } |
| 1 | 111 | | } |
| | 112 | |
|
| | 113 | | private void StartMultiSelection() |
| | 114 | | { |
| 0 | 115 | | isMultiSelectionActive = true; |
| 0 | 116 | | entityHandler.SetMultiSelectionActive(isMultiSelectionActive); |
| 0 | 117 | | Cursor.SetCursor(null, Vector2.zero, CursorMode.Auto); |
| 0 | 118 | | modeController.StartMultiSelection(); |
| 0 | 119 | | } |
| | 120 | |
|
| | 121 | | private void EndMultiSelection() |
| | 122 | | { |
| 0 | 123 | | isMultiSelectionActive = false; |
| 0 | 124 | | entityHandler.SetMultiSelectionActive(isMultiSelectionActive); |
| 0 | 125 | | modeController.EndMultiSelection(); |
| 0 | 126 | | outlinerController.CancelUnselectedOutlines(); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | private void MouseClick(int buttonID, Vector3 position) |
| | 130 | | { |
| 0 | 131 | | if (!isEditModeActive) |
| 0 | 132 | | return; |
| | 133 | |
|
| 0 | 134 | | if (Time.timeSinceLevelLoad < nexTimeToReceiveInput) |
| 0 | 135 | | return; |
| | 136 | |
|
| 0 | 137 | | if (!BIWUtils.IsPointerOverUIElement()) |
| 0 | 138 | | HUDController.i.builderInWorldMainHud.HideExtraBtns(); |
| | 139 | |
|
| 0 | 140 | | if (Utils.isCursorLocked || modeController.IsGodModeActive()) |
| | 141 | | { |
| 0 | 142 | | if (buttonID == 0) |
| | 143 | | { |
| 0 | 144 | | MouseClickDetected(); |
| 0 | 145 | | InputDone(); |
| 0 | 146 | | return; |
| | 147 | | } |
| 0 | 148 | | outlinerController.CheckOutline(); |
| | 149 | | } |
| 0 | 150 | | } |
| | 151 | |
|
| 0 | 152 | | private void MouseClickOnUI(int buttonID, Vector3 position) { HUDController.i.builderInWorldMainHud.HideExtraBtns(); |
| | 153 | |
|
| 0 | 154 | | public bool IsMultiSelectionActive() => isMultiSelectionActive; |
| | 155 | |
|
| | 156 | | private void RedoAction() |
| | 157 | | { |
| 0 | 158 | | actionController.TryToRedoAction(); |
| 0 | 159 | | InputDone(); |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | private void UndoAction() |
| | 163 | | { |
| 0 | 164 | | InputDone(); |
| | 165 | |
|
| 0 | 166 | | if (modeController.ShouldCancelUndoAction()) |
| 0 | 167 | | return; |
| | 168 | |
|
| 0 | 169 | | actionController.TryToUndoAction(); |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | private void MouseClickDetected() { modeController.MouseClickDetected(); } |
| | 173 | |
|
| 0 | 174 | | private void InputDone() { nexTimeToReceiveInput = Time.timeSinceLevelLoad + MS_BETWEEN_INPUT_INTERACTION / 1000; } |
| | 175 | |
|
| 0 | 176 | | private void StopInput() { inputWrapper.StopInput(); } |
| | 177 | |
|
| 0 | 178 | | private void ResumeInput() { inputWrapper.ResumeInput(); } |
| | 179 | | } |