Example #1
0
        internal static OuyaGamePad GetGamePad(InputDevice device)
        {
            if (device == null || (device.Sources & InputSourceType.Gamepad) != InputSourceType.Gamepad)
            {
                return(null);
            }

            // The recommended way to map devices to players numbers is to use OuyaController.GetPlayerNumByDeviceId(),
            // however as of ODK 0.0.6 there is a bug where disconnected and reconnected controllers get mapped
            // to new player numbers. Also, the player number returned does not match the LED on the controller.
            // Once this is fixed, we could consider using OuyaController.GetPlayerNumByDeviceId()
            // http://forums.ouya.tv/discussion/819/getplayernumbydeviceid-can-return-1-after-controllers-disconnect-and-reconnect

            int firstDisconnectedPadId = -1;

            for (int i = 0; i < GamePads.Length; i++)
            {
                var pad = GamePads[i];
                if (pad != null && pad._isConnected && pad._deviceId == device.Id)
                {
                    return(pad);
                }
                else if (pad != null && !pad._isConnected && pad._descriptor == device.Descriptor)
                {
                    Debug.WriteLine("Found previous controller [" + i + "] " + device.Name);
                    pad._deviceId    = device.Id;
                    pad._isConnected = true;
                    return(pad);
                }
                else if (pad == null)
                {
                    Debug.WriteLine("Found new controller [" + i + "] " + device.Name);
                    pad         = new OuyaGamePad(device);
                    GamePads[i] = pad;
                    return(pad);
                }
                else if (!pad._isConnected && firstDisconnectedPadId < 0)
                {
                    firstDisconnectedPadId = i;
                }
            }

            // If we get here, we failed to find a game pad or an empty slot to create one.
            // If we're holding onto a disconnected pad, overwrite it with this one
            if (firstDisconnectedPadId >= 0)
            {
                Debug.WriteLine("Found new controller in place of disconnected controller [" + firstDisconnectedPadId + "] " + device.Name);
                var pad = new OuyaGamePad(device);
                GamePads[firstDisconnectedPadId] = pad;
                return(pad);
            }

            // All pad slots are taken so ignore further devices.
            return(null);
        }
Example #2
0
        internal static OuyaGamePad GetGamePad(InputDevice device)
        {
            if (device == null || (device.Sources & InputSourceType.Gamepad) != InputSourceType.Gamepad)
                return null;

            // The recommended way to map devices to players numbers is to use OuyaController.GetPlayerNumByDeviceId(), 
            // however as of ODK 0.0.6 there is a bug where disconnected and reconnected controllers get mapped
            // to new player numbers. Also, the player number returned does not match the LED on the controller.
            // Once this is fixed, we could consider using OuyaController.GetPlayerNumByDeviceId()
            // http://forums.ouya.tv/discussion/819/getplayernumbydeviceid-can-return-1-after-controllers-disconnect-and-reconnect

            int firstDisconnectedPadId = -1;
            for (int i = 0; i < GamePads.Length; i++)
            {
                var pad = GamePads[i];
                if (pad != null && pad._isConnected && pad._deviceId == device.Id)
                {
                    return pad;
                }
                else if (pad != null && !pad._isConnected && pad._descriptor == device.Descriptor)
                {
                    Debug.WriteLine("Found previous controller [" + i + "] " + device.Name);
                    pad._deviceId = device.Id;
                    pad._isConnected = true;
                    return pad;
                }
                else if (pad == null)
                {
                    Debug.WriteLine("Found new controller [" + i + "] " + device.Name);
                    pad = new OuyaGamePad(device);
                    GamePads[i] = pad;
                    return pad;
                }
                else if (!pad._isConnected && firstDisconnectedPadId < 0)
                {
                    firstDisconnectedPadId = i;
                }
            }

            // If we get here, we failed to find a game pad or an empty slot to create one.
            // If we're holding onto a disconnected pad, overwrite it with this one
            if (firstDisconnectedPadId >= 0)
            {
                Debug.WriteLine("Found new controller in place of disconnected controller [" + firstDisconnectedPadId + "] " + device.Name);
                var pad = new OuyaGamePad(device);
                GamePads[firstDisconnectedPadId] = pad;
                return pad;
            }

            // All pad slots are taken so ignore further devices.
            return null;
        }