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