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