2026-03-20 13:11:41 +08:00
|
|
|
#if INPUTSYSTEM_SUPPORT
|
2026-04-28 20:52:06 +08:00
|
|
|
using AlicizaX.UI.Runtime;
|
2026-03-19 19:50:32 +08:00
|
|
|
using UnityEngine.EventSystems;
|
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
|
|
namespace UnityEngine.UI
|
|
|
|
|
{
|
|
|
|
|
[DisallowMultipleComponent]
|
|
|
|
|
public sealed class HotkeyComponent : MonoBehaviour, IHotkeyTrigger
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private Component _component;
|
2026-04-28 20:52:06 +08:00
|
|
|
[SerializeField] private UIHolderObjectBase _holder;
|
2026-03-19 19:50:32 +08:00
|
|
|
[SerializeField] private InputActionReference _hotkeyAction;
|
2026-03-20 13:11:41 +08:00
|
|
|
[SerializeField] private EHotkeyPressType _hotkeyPressType = EHotkeyPressType.Performed;
|
2026-03-19 19:50:32 +08:00
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
private ISubmitHandler _submitHandler;
|
|
|
|
|
private BaseEventData _eventData;
|
|
|
|
|
private EventSystem _eventSystem;
|
|
|
|
|
|
2026-03-19 19:50:32 +08:00
|
|
|
public InputActionReference HotkeyAction
|
|
|
|
|
{
|
|
|
|
|
get => _hotkeyAction;
|
|
|
|
|
set => _hotkeyAction = value;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
public EHotkeyPressType HotkeyPressType => _hotkeyPressType;
|
|
|
|
|
public UIHolderObjectBase HotkeyHolder => _holder;
|
|
|
|
|
|
|
|
|
|
private void Reset()
|
2026-03-19 19:50:32 +08:00
|
|
|
{
|
2026-04-28 20:52:06 +08:00
|
|
|
AutoAssignTarget();
|
|
|
|
|
AutoAssignHolder();
|
2026-03-19 19:50:32 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
private void Awake()
|
2026-03-20 13:11:41 +08:00
|
|
|
{
|
|
|
|
|
AutoAssignTarget();
|
2026-04-28 20:52:06 +08:00
|
|
|
AutoAssignHolder();
|
|
|
|
|
CacheTarget();
|
|
|
|
|
CacheEventData();
|
2026-03-20 13:11:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEnable()
|
|
|
|
|
{
|
|
|
|
|
AutoAssignTarget();
|
2026-04-28 20:52:06 +08:00
|
|
|
AutoAssignHolder();
|
|
|
|
|
CacheTarget();
|
2026-03-20 13:11:41 +08:00
|
|
|
((IHotkeyTrigger)this).BindHotKey();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
((IHotkeyTrigger)this).UnBindHotKey();
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
private void OnApplicationFocus(bool hasFocus)
|
|
|
|
|
{
|
|
|
|
|
if (hasFocus)
|
|
|
|
|
{
|
|
|
|
|
CacheEventData();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 13:11:41 +08:00
|
|
|
private void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
((IHotkeyTrigger)this).UnBindHotKey();
|
2026-04-28 20:52:06 +08:00
|
|
|
_submitHandler = null;
|
|
|
|
|
_eventData = null;
|
|
|
|
|
_eventSystem = null;
|
2026-03-20 13:11:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
private void OnValidate()
|
|
|
|
|
{
|
|
|
|
|
AutoAssignTarget();
|
2026-04-28 20:52:06 +08:00
|
|
|
AutoAssignHolder();
|
|
|
|
|
CacheTarget();
|
|
|
|
|
|
|
|
|
|
if (_component != null && _submitHandler == null)
|
|
|
|
|
{
|
|
|
|
|
_component = null;
|
|
|
|
|
}
|
2026-03-20 13:11:41 +08:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
public void HotkeyActionTrigger()
|
2026-03-19 19:50:32 +08:00
|
|
|
{
|
2026-04-28 20:52:06 +08:00
|
|
|
if (!isActiveAndEnabled || _submitHandler == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_eventData == null)
|
2026-03-20 13:11:41 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
if (!ReferenceEquals(_eventSystem, EventSystem.current))
|
2026-03-20 13:11:41 +08:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-28 20:52:06 +08:00
|
|
|
_submitHandler.OnSubmit(_eventData);
|
2026-03-20 13:11:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AutoAssignTarget()
|
|
|
|
|
{
|
|
|
|
|
if (_component != null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TryGetComponent(typeof(ISubmitHandler), out Component submitHandler))
|
2026-03-19 20:16:58 +08:00
|
|
|
{
|
2026-03-20 13:11:41 +08:00
|
|
|
_component = submitHandler;
|
2026-03-19 20:16:58 +08:00
|
|
|
}
|
2026-03-19 19:50:32 +08:00
|
|
|
}
|
2026-04-28 20:52:06 +08:00
|
|
|
|
|
|
|
|
private void AutoAssignHolder()
|
|
|
|
|
{
|
|
|
|
|
if (_holder != null && _holder.IsValid())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_holder = GetComponentInParent<UIHolderObjectBase>(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CacheTarget()
|
|
|
|
|
{
|
|
|
|
|
_submitHandler = _component as ISubmitHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CacheEventData()
|
|
|
|
|
{
|
|
|
|
|
_eventSystem = EventSystem.current;
|
|
|
|
|
_eventData = new BaseEventData(_eventSystem);
|
|
|
|
|
}
|
2026-03-19 19:50:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-20 13:11:41 +08:00
|
|
|
#endif
|