using NEG.UI.UnityUi.Window; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.InputSystem; using UnityEngine.InputSystem.UI; namespace NEG.UI.UnityUi { public enum SelectionSource { Pointer, Direction } public class UiInputModule { public SelectionSource CurrentSelectionSource { get; protected set; } } public class DefaultInputModule : UiInputModule { public DefaultInputModule() { var defaultActions = new DefaultInputActions(); InputActionReference.Create(defaultActions.UI.Navigate).action.performed += ctx => OnSelectionChangeStarted(); InputActionReference.Create(defaultActions.UI.Cancel).action.performed += _ => UiManager.Instance.UseBack(); defaultActions.Enable(); if (Gamepad.current != null) SetDirectionInput(); else SetPointerInput(); //var keyboardAction = new InputAction(binding: "//*"); //keyboardAction.performed += (context) => CurrentInputSource = EInputSource.Keyboard; //keyboardAction.Enable(); //var gamepadAction = new InputAction(binding: "//*"); //gamepadAction.performed += (context) => OnSelectionChangeStarted(); //gamepadAction.Enable(); var mouseAction = new InputAction(binding: "//*"); mouseAction.performed += context => { if (CurrentSelectionSource == SelectionSource.Pointer) return; SetPointerInput(); }; mouseAction.Enable(); } private void OnSelectionChangeStarted() { if (CurrentSelectionSource == SelectionSource.Direction && EventSystem.current.currentSelectedGameObject != null) return; SetDirectionInput(); } private void SetDirectionInput() { if (EventSystem.current == null || MonoUiManager.Instance == null) return; CurrentSelectionSource = SelectionSource.Direction; Cursor.visible = false; if (EventSystem.current.currentSelectedGameObject == null && MonoUiManager.Instance.CurrentMainWindow != null) { EventSystem.current.SetSelectedGameObject(((MonoWindow)MonoUiManager.Instance.CurrentMainWindow) .DefaultSelectedItem); return; } var data = new PointerEventData(EventSystem.current); var currentSelected = EventSystem.current.currentSelectedGameObject; if (currentSelected != null) for (var current = EventSystem.current.currentSelectedGameObject.transform; current != null; current = current.parent) ExecuteEvents.Execute(current.gameObject, data, ExecuteEvents.pointerExitHandler); EventSystem.current.SetSelectedGameObject(currentSelected); } private void SetPointerInput() { CurrentSelectionSource = SelectionSource.Pointer; Cursor.visible = true; if (EventSystem.current == null) { SetDirectionInput(); return; } if (EventSystem.current.currentInputModule == null) return; EventSystem.current.SetSelectedGameObject(null); var module = (InputSystemUIInputModule)EventSystem.current.currentInputModule; var result = module.GetLastRaycastResult(0); if (result.gameObject == null) return; var data = new PointerEventData(EventSystem.current); for (var current = result.gameObject.transform; current != null; current = current.parent) ExecuteEvents.Execute(current.gameObject, data, ExecuteEvents.pointerEnterHandler); } } }