104 lines
3.6 KiB
C#
104 lines
3.6 KiB
C#
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 class DefaultInputModule : UiInputModule
|
|
{
|
|
public SelectionSource CurrentSelectionSource { get; private set; }
|
|
|
|
public DefaultInputModule()
|
|
{
|
|
var defaultActions = new DefaultInputActions();
|
|
InputActionReference.Create(defaultActions.UI.Navigate).action.performed += (ctx) => OnSelectionChangeStarted();
|
|
defaultActions.Enable();
|
|
|
|
if (Gamepad.current != null)
|
|
SetDirectionInput();
|
|
else
|
|
SetPointerInput();
|
|
|
|
//var keyboardAction = new InputAction(binding: "/<Keyboard>/*");
|
|
//keyboardAction.performed += (context) => CurrentInputSource = EInputSource.Keyboard;
|
|
//keyboardAction.Enable();
|
|
//var gamepadAction = new InputAction(binding: "/<Gamepad>/*");
|
|
//gamepadAction.performed += (context) => OnSelectionChangeStarted();
|
|
//gamepadAction.Enable();
|
|
|
|
var mouseAction = new InputAction(binding: "/<Mouse>/*");
|
|
mouseAction.performed += (context) =>
|
|
{
|
|
if(CurrentSelectionSource == SelectionSource.Pointer)
|
|
return;
|
|
SetPointerInput();
|
|
};
|
|
mouseAction.Enable();
|
|
}
|
|
|
|
private void OnSelectionChangeStarted()
|
|
{
|
|
if(CurrentSelectionSource == SelectionSource.Direction)
|
|
return;
|
|
SetDirectionInput();
|
|
}
|
|
|
|
private void SetDirectionInput()
|
|
{
|
|
CurrentSelectionSource = SelectionSource.Direction;
|
|
Cursor.visible = false;
|
|
if (EventSystem.current.currentSelectedGameObject == null)
|
|
{
|
|
EventSystem.current.SetSelectedGameObject(((MonoWindow)MonoUiManager.Instance.CurrentMainWindow).DefaultSelectedItem);
|
|
return;
|
|
}
|
|
var data = new PointerEventData(EventSystem.current);
|
|
var currentSelected = EventSystem.current.currentSelectedGameObject;
|
|
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;
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |