74 lines
2.1 KiB
C#
74 lines
2.1 KiB
C#
using SepCore.InputModule;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
namespace SepCore.InputModule.Runtime.VirtualInput
|
|
{
|
|
public sealed class VirtualButtonBridge : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
|
{
|
|
[SerializeField] private InputActionId _actionId;
|
|
[SerializeField] private InputContextId _contextId = InputContextId.GameplayExplore;
|
|
[SerializeField] private bool _injectDeviceKind = true;
|
|
[SerializeField] private InputModuleComponent _inputModule;
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
Inject(InputCommandPhase.Started);
|
|
Inject(InputCommandPhase.Performed);
|
|
|
|
if (_injectDeviceKind)
|
|
{
|
|
TryForceDeviceKind();
|
|
}
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
Inject(InputCommandPhase.Canceled);
|
|
}
|
|
|
|
private void Inject(InputCommandPhase phase)
|
|
{
|
|
InputModuleComponent inputModule = GetInputModule();
|
|
if (inputModule == null || !inputModule.IsInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
float scalar = phase == InputCommandPhase.Performed ? 1f : 0f;
|
|
var command = new InputCommand(
|
|
_actionId,
|
|
_contextId,
|
|
phase,
|
|
InputDeviceKind.Touch,
|
|
Vector2.zero,
|
|
scalar,
|
|
Time.time);
|
|
|
|
inputModule.InjectCommand(command);
|
|
}
|
|
|
|
private void TryForceDeviceKind()
|
|
{
|
|
InputModuleComponent inputModule = GetInputModule();
|
|
if (inputModule == null || !inputModule.IsInitialized)
|
|
{
|
|
return;
|
|
}
|
|
|
|
inputModule.ForceDeviceKind(InputDeviceKind.Touch);
|
|
}
|
|
|
|
private InputModuleComponent GetInputModule()
|
|
{
|
|
if (_inputModule != null)
|
|
{
|
|
return _inputModule;
|
|
}
|
|
|
|
_inputModule = FindObjectOfType<InputModuleComponent>();
|
|
return _inputModule;
|
|
}
|
|
}
|
|
}
|