using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace DanroJump.Hardware.Com.Input
{
///
/// Кастомное Input System устройство для входов основной COM-платы автомата.
///
[InputControlLayout(
stateType = typeof(ComControllerState),
displayName = "COM Controller")]
public sealed class ComControllerDevice : InputDevice
{
public const string LayoutName = "ComController";
public const string InterfaceName = "COM";
public const string ProductName = "Danro Jump COM Controller";
public static readonly FourCC StateFormat = new FourCC('C', 'O', 'M', 'C');
public static ComControllerDevice current { get; private set; }
public ButtonControl button00 { get; private set; }
public ButtonControl button01 { get; private set; }
public ButtonControl button02 { get; private set; }
public ButtonControl button03 { get; private set; }
public ButtonControl button04 { get; private set; }
public ButtonControl button05 { get; private set; }
public ButtonControl button06 { get; private set; }
public ButtonControl button07 { get; private set; }
public ButtonControl button08 { get; private set; }
public ButtonControl button09 { get; private set; }
public ButtonControl button10 { get; private set; }
public ButtonControl button11 { get; private set; }
public ButtonControl button12 { get; private set; }
public ButtonControl button13 { get; private set; }
public ButtonControl button14 { get; private set; }
public ButtonControl button15 { get; private set; }
public ButtonControl button16 { get; private set; }
public ButtonControl button17 { get; private set; }
public ButtonControl button18 { get; private set; }
public ButtonControl button19 { get; private set; }
public ButtonControl button20 { get; private set; }
public ButtonControl button21 { get; private set; }
public ButtonControl button22 { get; private set; }
public ButtonControl button23 { get; private set; }
public AxisControl adc00 { get; private set; }
public AxisControl adc01 { get; private set; }
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
public static void RegisterLayout()
{
current = null;
InputSystem.RegisterLayout(
LayoutName,
matches: new InputDeviceMatcher()
.WithInterface(InterfaceName)
.WithDeviceClass(LayoutName));
}
public static ComControllerDevice AddDevice()
{
RegisterLayout();
foreach (InputDevice device in InputSystem.devices)
{
if (device is ComControllerDevice comDevice)
{
comDevice.MakeCurrent();
return comDevice;
}
}
var description = new InputDeviceDescription
{
interfaceName = InterfaceName,
deviceClass = LayoutName,
product = ProductName,
manufacturer = "Robotic Retailers"
};
return (ComControllerDevice)InputSystem.AddDevice(description);
}
protected override void FinishSetup()
{
base.FinishSetup();
button00 = GetChildControl(nameof(button00));
button01 = GetChildControl(nameof(button01));
button02 = GetChildControl(nameof(button02));
button03 = GetChildControl(nameof(button03));
button04 = GetChildControl(nameof(button04));
button05 = GetChildControl(nameof(button05));
button06 = GetChildControl(nameof(button06));
button07 = GetChildControl(nameof(button07));
button08 = GetChildControl(nameof(button08));
button09 = GetChildControl(nameof(button09));
button10 = GetChildControl(nameof(button10));
button11 = GetChildControl(nameof(button11));
button12 = GetChildControl(nameof(button12));
button13 = GetChildControl(nameof(button13));
button14 = GetChildControl(nameof(button14));
button15 = GetChildControl(nameof(button15));
button16 = GetChildControl(nameof(button16));
button17 = GetChildControl(nameof(button17));
button18 = GetChildControl(nameof(button18));
button19 = GetChildControl(nameof(button19));
button20 = GetChildControl(nameof(button20));
button21 = GetChildControl(nameof(button21));
button22 = GetChildControl(nameof(button22));
button23 = GetChildControl(nameof(button23));
adc00 = GetChildControl(nameof(adc00));
adc01 = GetChildControl(nameof(adc01));
}
public override void MakeCurrent()
{
base.MakeCurrent();
current = this;
}
protected override void OnRemoved()
{
if (current == this)
{
current = null;
}
base.OnRemoved();
}
}
#if UNITY_EDITOR
[InitializeOnLoad]
internal static class ComControllerDeviceEditorRegistration
{
static ComControllerDeviceEditorRegistration()
{
ComControllerDevice.RegisterLayout();
}
}
#endif
}