public virtual bool GetInputDigital(int code, ButtonState buttonState)
        {
            Update();

            // if (!this.isReady) return false;

            //UnityEngine.Debug.Log("GetKeyDown Joy"+this.ID);

            JoystickAxis axis = InputCode.toAxis(code);
            int          data = InputCode.toData(code);

            if (axis == JoystickAxis.None)                                         //MO data for axis => buttons data
            //UnityEngine.Debug.Log("Button state>" + button_collection[data].buttonState);

            //previous mapping might be to device with less or more buttons
            //at same device index
            {
                if (button_collection.Count > data)
                {
                    return(button_collection [data].buttonState == buttonState);
                }
                else
                {
                    return(false);
                }
            }



            //previous mapping might be to device with less or more axess
            //at same device index
            if (axis_collection.Count <= (int)axis)
            {
                return(false);
            }

            IAxisDetails axisDetails = axis_collection [axis];

            if (axisDetails == null)
            {
                return(false);
            }

            bool isEqualToButtonState = axisDetails.buttonState == buttonState || ButtonState.Up == buttonState;

            bool isNegative = ButtonState.Up == buttonState ? axisDetails.buttonState == ButtonState.NegToUp : axisDetails.value < 0;
            bool isPositive = ButtonState.Up == buttonState ? axisDetails.buttonState == ButtonState.PosToUp : axisDetails.value > 0;



            if (axis == JoystickAxis.AxisPovX)
            {
                if (data == (int)JoystickPovPosition.Left && isEqualToButtonState && isNegative)
                {
                    return(true);
                }
                if (data == (int)JoystickPovPosition.Right && isEqualToButtonState && isPositive)
                {
                    return(true);
                }

                return(false);
            }


            if (axis == JoystickAxis.AxisPovY)
            {
                if (data == (int)JoystickPovPosition.Backward && isEqualToButtonState && isNegative)
                {
                    return(true);
                }
                if (data == (int)JoystickPovPosition.Forward && isEqualToButtonState && isPositive)
                {
                    return(true);
                }

                return(false);
            }



            if (data == (int)JoystickPosition.Negative && isEqualToButtonState && isNegative)
            {
                return(true);
            }


            if (data == (int)JoystickPosition.Positive && isEqualToButtonState && isPositive)
            {
                //	UnityEngine.Debug.Log("data:" + data+"buttotnState:"+buttonState+" equal"+isEqualToButtonState+" value:"+axisDetails.value);
                return(true);
            }

//						if (data == (int)JoystickPosition.Full && buttonState == ButtonState.Up) {
//						if (isPositive && axisDetails.value >= 0)
//								return true;
//						if (isNegative && axisDetails.value <= 0)
//								return true;
//				}else
            if (data == (int)JoystickPosition.Full && isEqualToButtonState && (isPositive || isNegative))
            {
                return(true);
            }



            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resolves the device.
        /// </summary>
        /// <returns>returns JoystickDevice if driver is for this device or null</returns>
        /// <param name="info">Info.</param>
        /// <param name="hidDevice">Hid device.</param>
        public IDevice ResolveDevice(IHIDDevice hidDevice)
        {
            this._hidInterface = hidDevice.hidInterface;

            IntPtr deviceRef = hidDevice.deviceHandle;

            JoystickDevice device;
            int            axisIndex   = 0;
            int            buttonIndex = 0;

            Native.CFArray   elements = new Native.CFArray();
            IOHIDElementRef  element;
            IOHIDElementType type;

            //copy all matched
            elements.typeRef = Native.IOHIDDeviceCopyMatchingElements(deviceRef, IntPtr.Zero, (int)Native.IOHIDOptionsType.kIOHIDOptionsTypeNone);

            int numButtons = 0;

            int numPov = 0;

            int numElements    = elements.Length;
            int HIDElementType = Native.IOHIDElementGetTypeID();

            //check for profile
            DeviceProfile profile = hidDevice.loadProfile();



            IAxisDetails axisDetailsPovX = null;
            IAxisDetails axisDetailsPovY = null;

            List <IAxisDetails> axisDetailsList = new List <IAxisDetails>();


            IAxisDetails axisDetails;

            for (int elementIndex = 0; elementIndex < numElements; elementIndex++)
            {
                element = elements[elementIndex].typeRef;


                if (element != IntPtr.Zero && Native.CFGetTypeID(element) == HIDElementType)
                {
                    type = Native.IOHIDElementGetType(element);


                    // All of the axis elements I've ever detected have been kIOHIDElementTypeInput_Misc. kIOHIDElementTypeInput_Axis is only included for good faith...
                    if (type == IOHIDElementType.kIOHIDElementTypeInput_Misc ||
                        type == IOHIDElementType.kIOHIDElementTypeInput_Axis)
                    {
                        axisDetails = new AxisDetails();

                        axisDetails.uid        = Native.IOHIDElementGetCookie(element);
                        axisDetails.min        = (int)Native.IOHIDElementGetLogicalMin(element);
                        axisDetails.max        = (int)Native.IOHIDElementGetLogicalMax(element);
                        axisDetails.isNullable = Native.IOHIDElementHasNullState(element);



                        if (Native.IOHIDElementGetUsage(element) == (uint)Native.HIDUsageGD.Hatswitch)
                        {
                            axisDetails.isHat = true;
                            axisDetailsPovX   = axisDetails;

                            axisDetailsPovY = new AxisDetails();

                            axisDetailsPovY.uid        = Native.IOHIDElementGetCookie(element);
                            axisDetailsPovY.min        = (int)Native.IOHIDElementGetLogicalMin(element);
                            axisDetailsPovY.max        = (int)Native.IOHIDElementGetLogicalMax(element);
                            axisDetailsPovY.isNullable = Native.IOHIDElementHasNullState(element);
                            axisDetailsPovY.isHat      = true;

                            numPov++;
                        }
                        else
                        {
                            if (axisDetails.min == 0)
                            {
                                axisDetails.isTrigger = true;
                            }
                            axisDetailsList.Add(axisDetails);
                        }
                    }
                    else if (type == IOHIDElementType.kIOHIDElementTypeInput_Button)
                    {
                        numButtons++;
                    }
                }
            }


            if (axisDetailsPovX != null)
            {
                int diff;

                diff = axisDetailsList.Count - 8;
                //need 2 slots for Pov X,Y
                if (diff >= 0)
                {
                    //just insert them
                    axisDetailsList.Insert((int)JoystickAxis.AxisPovX, axisDetailsPovX);
                    axisDetailsList.Insert((int)JoystickAxis.AxisPovY, axisDetailsPovY);
                }
                else if (diff < -1)
                {
                    diff = diff + 2;
                    while (diff < 0)
                    {
                        axisDetailsList.Add(null);
                        diff += 1;
                    }

                    axisDetailsList.Add(axisDetailsPovX);
                    axisDetailsList.Add(axisDetailsPovY);
                }
                else                  //=-1
                {
                    axisDetailsList.Resize(9);
                    axisDetailsList[(int)JoystickAxis.AxisPovX] = axisDetailsPovX;
                    axisDetailsList[(int)JoystickAxis.AxisPovY] = axisDetailsPovY;
                }
            }


            device         = new JoystickDevice(hidDevice.index, hidDevice.PID, hidDevice.VID, hidDevice.ID, axisDetailsList.Count, numButtons, this);
            device.Name    = hidDevice.Name;
            device.numPOV  = numPov;
            device.profile = profile;

            for (; axisIndex < device.Axis.Count; axisIndex++)
            {
                device.Axis[(JoystickAxis)axisIndex] = axisDetailsList[axisIndex];
                if (profile != null && profile.axisNaming.Length > axisIndex && device.Axis[axisIndex] != null)
                {
                    device.Axis[axisIndex].name = profile.axisNaming [axisIndex];
                }
            }



            for (int elementIndex = 0; elementIndex < numElements; elementIndex++)
            {
                element = elements[elementIndex].typeRef;

                if (element != IntPtr.Zero && Native.CFGetTypeID(element) == HIDElementType)
                {
                    type = Native.IOHIDElementGetType(element);
                    if (type == IOHIDElementType.kIOHIDElementTypeInput_Button)
                    {
                        //
                        device.Buttons[buttonIndex] = new ButtonDetails(Native.IOHIDElementGetCookie(element));


                        if (profile != null && profile.buttonNaming.Length > buttonIndex)
                        {
                            device.Buttons[buttonIndex].name = profile.buttonNaming [buttonIndex];
                        }
                        buttonIndex++;
                    }
                }
            }



            //joystick.isReady = false;

            device.Extension = new OSXDefaultExtension();



            return(device);
        }