示例#1
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length == PACKET_SIZE)
            {
                ControllerStateBuilder state = new ControllerStateBuilder();

                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }

                state.SetAnalog("lstick_x", ReadStick(packet[15]), packet[15]);
                state.SetAnalog("lstick_y", ReadStick(packet[16]), packet[16]);

                return(state.Build());
            }

            return(null);
        }
示例#2
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float x = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length));
            float y = ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            state.SetAnalog("stick_x", x, SignalTool.ReadByte(packet, BUTTONS.Length));
            state.SetAnalog("stick_y", y, SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            SignalTool.SetMouseProperties(x, y, SignalTool.ReadByte(packet, BUTTONS.Length), SignalTool.ReadByte(packet, BUTTONS.Length + 8), state);

            return(state.Build());
        }
示例#3
0
        static public ControllerState ReadFromPacket_SNES(byte[] packet)
        {
            if (packet.Length < BUTTONS_SNES.Length)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS_SNES.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_SNES[i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS_SNES[i], packet[i] != 0x00);
            }

            if (state != null && packet.Length == 32 && packet[15] != 0x00)
            {
                float y = (float)(SignalTool.readByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1)) / 127;
                float x = (float)(SignalTool.readByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1)) / 127;
                SignalTool.SetMouseProperties(x, y, state);
            }

            return(state.Build());
        }
示例#4
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            for (int i = 0; i < 24; ++i)
            {
                polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0);
            }

            for (int i = 0; i < 16; ++i)
            {
                polishedPacket[24 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[24 + i] |= (byte)((packet[24 + (i * 8) + j] == 0x30 ? 0 : 1) << j);
                }
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            outState.SetAnalog("rstick_x", ReadStick(polishedPacket[26]), polishedPacket[26]);
            outState.SetAnalog("rstick_y", ReadStick(polishedPacket[27]), polishedPacket[27]);
            outState.SetAnalog("lstick_x", ReadStick(polishedPacket[24]), polishedPacket[24]);
            outState.SetAnalog("lstick_y", ReadStick(polishedPacket[25]), polishedPacket[25]);

            outState.SetAnalog("analog_up", ReadAnalogButton(polishedPacket[28]), polishedPacket[28]);
            outState.SetAnalog("analog_right", ReadAnalogButton(polishedPacket[29]), polishedPacket[29]);
            outState.SetAnalog("analog_down", ReadAnalogButton(polishedPacket[30]), polishedPacket[30]);
            outState.SetAnalog("analog_left", ReadAnalogButton(polishedPacket[31]), polishedPacket[31]);
            outState.SetAnalog("l_trig", ReadAnalogButton(polishedPacket[32]), polishedPacket[32]);
            outState.SetAnalog("r_trig", ReadAnalogButton(polishedPacket[33]), polishedPacket[33]);
            outState.SetAnalog("analog_l1", ReadAnalogButton(polishedPacket[34]), polishedPacket[34]);
            outState.SetAnalog("analog_r1", ReadAnalogButton(polishedPacket[35]), polishedPacket[35]);
            outState.SetAnalog("analog_triangle", ReadAnalogButton(polishedPacket[36]), polishedPacket[36]);
            outState.SetAnalog("analog_circle", ReadAnalogButton(polishedPacket[37]), polishedPacket[37]);
            outState.SetAnalog("analog_x", ReadAnalogButton(polishedPacket[38]), polishedPacket[38]);
            outState.SetAnalog("analog_square", ReadAnalogButton(polishedPacket[39]), polishedPacket[39]);

            return(outState.Build());
        }
示例#5
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS [i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            state.SetAnalog("lstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length)));
            state.SetAnalog("lstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8)));
            state.SetAnalog("cstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length + 16)));
            state.SetAnalog("cstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 24)));
            state.SetAnalog("trig_l", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 32)));
            state.SetAnalog("trig_r", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 40)));

            return(state.Build());
        }
示例#6
0
文件: NeoGeo.cs 项目: edru/RetroSpy
        public static ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float lstick_x = 0;
            float lstick_y = 0;

            if (packet[3] != 0x00)
            {
                lstick_x = 1;
            }
            else if (packet[8] != 0x00)
            {
                lstick_x = -1;
            }

            if (packet[4] != 0x00)
            {
                lstick_y = 1;
            }
            else if (packet[9] != 0x00)
            {
                lstick_y = -1;
            }

            if (lstick_y != 0 || lstick_x != 0)
            {
                // point on the unit circle at the same angle
                double radian = Math.Atan2(lstick_y, lstick_x);
                float  x1     = (float)Math.Cos(radian);
                float  y1     = (float)Math.Sin(radian);

                // Don't let magnitude exceed the unit circle
                if (Math.Sqrt(Math.Pow(lstick_x, 2) + Math.Pow(lstick_y, 2)) > 1.0)
                {
                    lstick_x = x1;
                    lstick_y = y1;
                }
            }

            state.SetAnalog("lstick_x", lstick_x);
            state.SetAnalog("lstick_y", lstick_y);

            return(state.Build());
        }
示例#7
0
        private void Tick(object sender, EventArgs e)
        {
            try
            {
                _keyboard.Poll();
            }
            catch (Exception)
            {
                Finish();
                ControllerDisconnected?.Invoke(this, EventArgs.Empty);
                return;
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();
            KeyboardState          state    = _keyboard.GetCurrentState();

            foreach (string key in Enum.GetNames(typeof(Key)))
            {
                outState.SetButton(key, false);
            }

            for (int i = 0; i < state.PressedKeys.Count; i++)
            {
                outState.SetButton(state.PressedKeys[i].ToString(), true);
            }

            ControllerStateChanged?.Invoke(this, outState.Build());
        }
示例#8
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] == 0x31);
            }

            return(state.Build());
        }
示例#9
0
        void tick (object sender, EventArgs e)
        {
            var state = new XInputState ();
            if (XInputDLL.XInputGetState (_id, ref state) > 0) {
                if (ControllerDisconnected != null) ControllerDisconnected (this, EventArgs.Empty);
                Finish ();
                return;
            }

            var outState = new ControllerStateBuilder ();
            
            outState.SetButton ("a", (state.wButtons & 0x1000) != 0);
            outState.SetButton ("b", (state.wButtons & 0x2000) != 0);
            outState.SetButton ("x", (state.wButtons & 0x4000) != 0);
            outState.SetButton ("y", (state.wButtons & 0x8000) != 0);
            outState.SetButton ("up", (state.wButtons & 0x0001) != 0);
            outState.SetButton ("down", (state.wButtons & 0x0002) != 0);
            outState.SetButton ("left", (state.wButtons & 0x0004) != 0);
            outState.SetButton ("right", (state.wButtons & 0x0008) != 0);
            outState.SetButton ("start", (state.wButtons & 0x0010) != 0);
            outState.SetButton ("back", (state.wButtons & 0x0020) != 0);
            outState.SetButton ("l3", (state.wButtons & 0x0040) != 0);
            outState.SetButton ("r3", (state.wButtons & 0x0080) != 0);
            outState.SetButton ("l", (state.wButtons & 0x0100) != 0);
            outState.SetButton ("r", (state.wButtons & 0x0200) != 0);

            outState.SetAnalog ("lstick_x", (float)state.sThumbLX / 32768);
            outState.SetAnalog ("lstick_y", (float)state.sThumbLY / 32768);
            outState.SetAnalog ("rstick_x", (float)state.sThumbRX / 32768);
            outState.SetAnalog ("rstick_y", (float)state.sThumbRY / 32768);
            outState.SetAnalog ("trig_l", (float)state.bLeftTrigger / 255);
            outState.SetAnalog ("trig_r", (float)state.bRightTrigger / 255);

            if (ControllerStateChanged != null) ControllerStateChanged (this, outState.Build ());
        }
示例#10
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS [i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            float x = readStick(SignalTool.readByte(packet, BUTTONS.Length));
            float y = readStick(SignalTool.readByte(packet, BUTTONS.Length + 8));

            state.SetAnalog("stick_x", x);
            state.SetAnalog("stick_y", y);

            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
示例#11
0
        public static ControllerStateEventArgs ReadFromPacketAtari52001(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != BUTTONS_ATARI5200.Length)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS_ATARI5200.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_ATARI5200[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS_ATARI5200[i], packet[i] == 0x00);
            }

            state.SetAnalog("x", (((packet[17] >> 4) | (packet[18])) - 128.0f) / -128.0f, (packet[17] >> 4) | (packet[18]));
            state.SetAnalog("y", atari5200_y, atari5200_yRaw);

            return(state.Build());
        }
示例#12
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            if (packet[4] != 5)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            state.SetAnalog("paddle", ReadPaddle(packet[2]), packet[2]);
            state.SetAnalog("paddle2", SecondPaddle, SecondPaddleRaw);
            state.SetButton("fire2", SecondButton);
            state.SetButton("fire", packet[1] != 0x00);

            return(state.Build());
        }
示例#13
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < 1)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet[0] == 0)
            {
                if (packet.Length < PACKET_SIZE)
                {
                    return(null);
                }

                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
            }
            else
            {
                if (packet.Length < MOUSE_PACKET_SIZE)
                {
                    return(null);
                }

                for (int i = 0; i < MOUSE_BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(MOUSE_BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00);
                }

                bool ySign = packet[11] != 0;
                byte yVal  = SignalTool.ReadByte(packet, 14, 7);
                bool xSign = packet[21] != 0;
                byte xVal  = SignalTool.ReadByte(packet, 24, 7);

                float x = ReadMouse(xSign, xVal);
                float y = ReadMouse(ySign, yVal);

                SignalTool.SetMouseProperties(x, y, xVal, yVal, state);
            }
            return(state.Build());
        }
示例#14
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            ControllerStateBuilder state = new ControllerStateBuilder();

            state.SetPrinterData(Encoding.UTF8.GetString(packet, 0, packet.Length));

            return(state.Build());
        }
示例#15
0
文件: EVS.cs 项目: retrospy/RetroSpy
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            string str = System.Text.Encoding.Default.GetString(packet, 0, 40);

            byte[] binaryPacket = new byte[20];

            try
            {
                for (int i = 0; i < str.Length; i += 2)
                {
                    binaryPacket[i / 2] = Convert.ToByte(str.Substring(i, 2), 16);
                }
            }
            catch (ArgumentException)
            {
                return(null);
            }
            catch (FormatException)
            {
                return(null);
            }
            catch (OverflowException)
            {
                return(null);
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS_2.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_2[i]))
                {
                    continue;
                }
                outState.SetButton(BUTTONS_2[i], (binaryPacket[3] & (1 << i)) != 0);
            }

            for (int i = 0; i < BUTTONS_1.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS_1[i]))
                {
                    continue;
                }
                outState.SetButton(BUTTONS_1[i], (binaryPacket[2] & (1 << i)) != 0);
            }

            return(outState.Build());
        }
示例#16
0
        public static void SetMouseProperties(float x, float y, int xRaw, int yRaw, ControllerStateBuilder state, float maxCircleSize = 1.0f)
        {
            if (!windows.ContainsKey(""))
            {
                windows[""] = new SlidingWindow();
            }

            SetMouseProperties(x, y, xRaw, yRaw, state, maxCircleSize, windows[""], "");
        }
示例#17
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE && packet.Length != MOUSE_PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet.Length == PACKET_SIZE)
            {
                for (int i = 0; i < BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS[i], packet[i] != 0x00);
                }
                state.SetButton("1", packet[5] != 0);
                state.SetButton("2", packet[6] != 0);
            }
            else if (packet.Length == MOUSE_PACKET_SIZE)
            {
                for (int i = 0; i < MOUSE_BUTTONS.Length; ++i)
                {
                    if (string.IsNullOrEmpty(MOUSE_BUTTONS[i]))
                    {
                        continue;
                    }

                    state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00);
                }

                bool xSign = packet[4] != 0;
                bool ySign = packet[5] != 0;
                bool xOver = packet[6] != 0;
                bool yOver = packet[7] != 0;

                byte xVal = SignalTool.ReadByteBackwards(packet, 8);
                byte yVal = SignalTool.ReadByteBackwards(packet, 16);

                float x = ReadMouse(xSign, xOver, xVal);
                float y = ReadMouse(ySign, yOver, yVal);

                SignalTool.SetMouseProperties(x, y, xVal, yVal, state);
            }

            return(state.Build());
        }
示例#18
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            for (int i = 0; i < 16; ++i)
            {
                polishedPacket[i] = (byte)((packet[i] == 0x31) ? 1 : 0);
            }

            for (int i = 0; i < 2; ++i)
            {
                packet[16 + i] = 0;
                for (byte j = 0; j < 8; ++j)
                {
                    polishedPacket[16 + i] |= (byte)((packet[16 + (i * 8 + j)] == 0x30 ? 0 : 1) << j);
                }
            }

            short[] sticks = new short[4];

            for (int i = 0; i < 4; ++i)
            {
                sticks[i] = 0;
                for (byte j = 0; j < 16; ++j)
                {
                    sticks[i] |= (short)((packet[32 + (i * 16 + j)] == 0x30 ? 0 : 1) << j);
                }
            }

            var outState = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }
                outState.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }

            outState.SetAnalog("trig_l", readTrigger(polishedPacket[16]));
            outState.SetAnalog("trig_r", readTrigger(polishedPacket[17]));

            outState.SetAnalog("rstick_x", readStick(sticks[2]));
            outState.SetAnalog("rstick_y", readStick(sticks[3]));
            outState.SetAnalog("lstick_x", readStick(sticks[0]));
            outState.SetAnalog("lstick_y", readStick(sticks[1]));

            return(outState.Build());
        }
示例#19
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            byte[] reconstructedPacket = new byte[34];

            int j = 0;

            for (int i = 0; i < 34; ++i)
            {
                reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]);
                j += 2;
            }

            byte[] polishedPacket = new byte[256];

            int checksum = 0;

            for (int i = 0; i < 32; ++i)
            {
                checksum += reconstructedPacket[i];
                for (int k = 0; k < 8; ++k)
                {
                    polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0);
                }
            }

            var state = new ControllerStateBuilder();

            short sentChecksum = (short)((reconstructedPacket[33] << 8) | reconstructedPacket[32]);

            if (checksum != sentChecksum)
            {
                state.SetButton("ChecksumFail", true);
                return(state.Build());
            }


            for (int i = 0; i < KEYS.Length; ++i)
            {
                if (string.IsNullOrEmpty(KEYS[i]))
                {
                    continue;
                }
                state.SetButton(KEYS[i], polishedPacket[i] != 0x00);
                state.SetButton("ChecksumFail", false);
            }

            return(state.Build());
        }
示例#20
0
#pragma warning restore CA1508 // Avoid dead conditional code

        private static void SetButtons(string[] buttons, byte[] polishedPacket, ControllerStateBuilder state)
        {
            for (int i = 0; i < buttons.Length; ++i)
            {
                if (string.IsNullOrEmpty(buttons[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00);
            }
        }
示例#21
0
        void tick(object sender, EventArgs e)
        {
            var outState = new ControllerStateBuilder();

            if (ticks % 18 == 0)
            {
                buttonsOn = buttonsOn ? false : true;
            }

            ticks++;

            if (!inCenter && (theta == 0 || theta == 90 || theta == 180 || theta == 270))
            {
                inCenter = true;
            }
            else if (inCenter)
            {
                inCenter = false;
            }

            float x = 0;
            float y = 0;

            if (!inCenter)
            {
                x      = (float)Math.Cos(theta * Math.PI / 180);
                y      = (float)Math.Sin(theta * Math.PI / 180);
                theta  = theta + 5;
                theta %= 360;
            }
            outState.SetButton("left", buttonsOn);
            outState.SetButton("middle", buttonsOn);
            outState.SetButton("right", buttonsOn);
            outState.SetButton("wired-1", buttonsOn);
            outState.SetButton("wired-2", buttonsOn);
            outState.SetButton("a", buttonsOn);
            outState.SetButton("b", buttonsOn);
            outState.SetButton("c", buttonsOn);
            outState.SetButton("start", buttonsOn);
            outState.SetButton("thumb", buttonsOn);
            outState.SetButton("scroll_up", buttonsOn);
            outState.SetButton("scroll_down", buttonsOn);

            outState.SetButton("mouse_center", true);

            outState.SetAnalog("stick_x", x);
            outState.SetAnalog("stick_y", y);

            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
示例#22
0
        private void Tick(object sender, EventArgs e)
        {
            try
            {
                _keyboard.Poll();
                _mouse.Poll();
            }
            catch (SharpDXException)
            {
                Finish();
                ControllerDisconnected?.Invoke(this, EventArgs.Empty);
                return;
            }

            ControllerStateBuilder outState   = new ControllerStateBuilder();
            KeyboardState          state      = _keyboard.GetCurrentState();
            MouseState             mouseState = _mouse.GetCurrentState();

            SignalTool.SetPCMouseProperties(mouseState.X / 255.0f, -mouseState.Y / 255.0f, mouseState.X, mouseState.Y, outState, 1.0f);

            if (mouseState.Z > 0)
            {
                outState.SetButton("MouseScrollUp", true);
                outState.SetButton("MouseScrollDown", false);
            }
            else if (mouseState.Z < 0)
            {
                outState.SetButton("MouseScrollDown", true);
                outState.SetButton("MouseScrollUp", false);
            }
            else
            {
                outState.SetButton("MouseScrollDown", false);
                outState.SetButton("MouseScrollUp", false);
            }

            for (int i = 0; i < MOUSE_BUTTONS.Length; ++i)
            {
                outState.SetButton(MOUSE_BUTTONS[i], mouseState.Buttons[i]);
            }

            foreach (string key in Enum.GetNames(typeof(Key)))
            {
                outState.SetButton(key, false);
            }

            for (int i = 0; i < state.PressedKeys.Count; i++)
            {
                outState.SetButton(state.PressedKeys[i].ToString(), true);
            }

            ControllerStateChanged?.Invoke(this, outState.Build());
        }
示例#23
0
        private void Tick(object sender, EventArgs e)
        {
            try
            {
                _joystick.Poll();
            }
            catch (SharpDXException)
            {
                Finish();
                ControllerDisconnected?.Invoke(this, EventArgs.Empty);
                return;
            }

            ControllerStateBuilder outState = new ControllerStateBuilder();
            JoystickState          state    = _joystick.GetCurrentState();

            for (int i = 0; i < _joystick.Capabilities.ButtonCount; ++i)
            {
                outState.SetButton("b" + i.ToString(CultureInfo.CurrentCulture), state.Buttons[i]);
            }

            int[] pov = state.PointOfViewControllers;

            outState.SetButton("up", false);
            outState.SetButton("right", false);
            outState.SetButton("down", false);
            outState.SetButton("left", false);

            if (pov != null && pov.Length > 0 && pov[0] >= 0)
            {
                outState.SetButton("up", pov[0] > OctantAngle(6) || pov[0] < OctantAngle(1));
                outState.SetButton("right", pov[0] > OctantAngle(0) && pov[0] < OctantAngle(3));
                outState.SetButton("down", pov[0] > OctantAngle(2) && pov[0] < OctantAngle(5));
                outState.SetButton("left", pov[0] > OctantAngle(4) && pov[0] < OctantAngle(7));
            }
            else if (_joystick.Information.ProductName == "8Bitdo SF30 Wireless Controller") // For SN30
            {
                outState.SetButton("up", (float)state.Y / RANGE == -1.0);
                outState.SetButton("down", (float)state.Y / RANGE == 1.0);
                outState.SetButton("left", (float)state.X / RANGE == -1.0);
                outState.SetButton("right", (float)state.X / RANGE == 1.0);
            }

            outState.SetAnalog("x", (float)state.X / RANGE, state.X);
            outState.SetAnalog("y", (float)state.Y / RANGE, state.Y);
            outState.SetAnalog("z", (float)state.Z / RANGE, state.Z);
            outState.SetAnalog("rx", (float)state.RotationX / RANGE, state.RotationX);
            outState.SetAnalog("ry", (float)state.RotationY / RANGE, state.RotationY);
            outState.SetAnalog("rz", (float)state.RotationZ / RANGE, state.RotationZ);

            ControllerStateChanged?.Invoke(this, outState.Build());
        }
示例#24
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE];

            polishedPacket[0] = (byte)((packet[0] >> 4) | packet[1]);
            polishedPacket[1] = (byte)(packet[9] == 0 ? 0 : 1);
            polishedPacket[2] = (byte)(packet[17] == 0 ? 0 : 1);

            for (int i = 18; i < 29; ++i)
            {
                polishedPacket[i - 15] = (byte)(packet[i] == 0 ? 0 : 1);
            }

            for (byte i = 0; i < 7; ++i)
            {
                polishedPacket[14] |= (byte)((packet[i + 2] == 0 ? 0 : 1) << i);
            }

            for (byte i = 0; i < 7; ++i)
            {
                polishedPacket[15] |= (byte)((packet[i + 10] == 0 ? 0 : 1) << i);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], polishedPacket[i] == 0x00);
            }


            float x = 0;
            float y = 0;

            if (packet[29] == 1) // This is the "Has Data" bit.  Reset the mouse on cached results.
            {
                y = readMouse(polishedPacket[14]);
                x = readMouse(polishedPacket[15]);
            }
            SignalTool.SetMouseProperties(x, y, state);

            return(state.Build());
        }
示例#25
0
        void tick(object sender, EventArgs e)
        {
            Random rand     = new Random();
            var    outState = new ControllerStateBuilder();

            if (ticks % 18 == 0)
            {
                buttonsOn = buttonsOn ? false : true;
            }

            if (ticks % 36 == 0)
            {
                visable = visable ? false : true;
            }

            ticks++;

            double x1 = rand.NextDouble();
            double y1 = rand.NextDouble();
            double x2 = rand.NextDouble();
            double y2 = rand.NextDouble();

            double x3 = rand.NextDouble();
            double y3 = rand.NextDouble();
            double x4 = rand.NextDouble();
            double y4 = rand.NextDouble();


            if (visable)
            {
                if (buttonsOn)
                {
                    outState.SetAnalog("touchpad_x3", (float)x3);
                    outState.SetAnalog("touchpad_y3", (float)y3);
                    outState.SetAnalog("touchpad_x4", (float)x4);
                    outState.SetAnalog("touchpad_y4", (float)y4);
                }
                else
                {
                    outState.SetAnalog("touchpad_x1", (float)x1);
                    outState.SetAnalog("touchpad_y1", (float)y1);
                    outState.SetAnalog("touchpad_x2", (float)x2);
                    outState.SetAnalog("touchpad_y2", (float)y2);
                }
            }
            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
示例#26
0
        void tick(object sender, EventArgs e)
        {
            try
            { _joystick.Poll(); }
            catch (Exception)
            {
                Finish();
                if (ControllerDisconnected != null)
                {
                    ControllerDisconnected(this, EventArgs.Empty);
                }
                return;
            }

            var outState = new ControllerStateBuilder();
            var state    = _joystick.GetCurrentState();

            for (int i = 0; i < _joystick.Capabilities.ButtonCount; ++i)
            {
                outState.SetButton("b" + i.ToString(), state.Buttons[i]);
            }

            int[] pov = state.PointOfViewControllers;

            outState.SetButton("up", false);
            outState.SetButton("right", false);
            outState.SetButton("down", false);
            outState.SetButton("left", false);

            if (pov != null && pov.Length > 0 && pov[0] >= 0)
            {
                outState.SetButton("up", pov[0] > octantAngle(6) || pov[0] < octantAngle(1));
                outState.SetButton("right", pov[0] > octantAngle(0) && pov[0] < octantAngle(3));
                outState.SetButton("down", pov[0] > octantAngle(2) && pov[0] < octantAngle(5));
                outState.SetButton("left", pov[0] > octantAngle(4) && pov[0] < octantAngle(7));
            }

            outState.SetAnalog("x", (float)state.X / RANGE);
            outState.SetAnalog("y", (float)state.Y / RANGE);
            outState.SetAnalog("z", (float)state.Z / RANGE);
            outState.SetAnalog("rx", (float)state.RotationX / RANGE);
            outState.SetAnalog("ry", (float)state.RotationY / RANGE);
            outState.SetAnalog("rz", (float)state.RotationZ / RANGE);

            if (ControllerStateChanged != null)
            {
                ControllerStateChanged(this, outState.Build());
            }
        }
示例#27
0
        public static ControllerStateEventArgs ReadFromPacketSNES(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < BUTTONS_SNES.Length)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet.Length == 32 && packet[15] == 0x00 && packet[13] != 0x00)
            {
                for (int i = 0; i < BUTTONS_SNES_NTTDATA.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_SNES_NTTDATA[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_SNES_NTTDATA[i], packet[i] != 0x00);
                }
            }
            else
            {
                for (int i = 0; i < BUTTONS_SNES.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_SNES[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_SNES[i], packet[i] != 0x00);
                }

                if (packet.Length == 32 && packet[15] != 0x00)
                {
                    float y = (float)(SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1)) / 127;
                    float x = (float)(SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1)) / 127;
                    SignalTool.SetMouseProperties(x, y, SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1), SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1), state);
                }
            }

            return(state.Build());
        }
示例#28
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            var str = System.Text.Encoding.Default.GetString(packet, 0, 40);

            byte[] binaryPacket = new byte[20];

            try
            {
                for (int i = 0; i < str.Length; i += 2)
                {
                    binaryPacket[i / 2] = Convert.ToByte(str.Substring(i, 2), 16);
                }
            }
            catch (Exception)
            {
                return(null);
            }

            var outState = new ControllerStateBuilder();

            for (int i = 0; i < 8; ++i)
            {
                outState.SetButton(BUTTONS[i], (binaryPacket[2] & (1 << i)) != 0);
            }

            for (int i = 4; i < 12; ++i)
            {
                outState.SetButton(ANALOG_BUTTONS[i - 4], binaryPacket[i] > 0);
                outState.SetAnalog(ANALOG_BUTTONS[i - 4], ReadTrigger(binaryPacket[i]));
            }

            int j = 0;

            for (int i = 0; i < 4; ++i)
            {
                short val = binaryPacket[12 + j];
                val += (short)(binaryPacket[13 + j] << 8);
                outState.SetAnalog(STICKS[i], ReadStick(val));
                j += 2;
            }

            return(outState.Build());
        }
示例#29
0
        public static ControllerStateEventArgs ReadFromPacketJaguar(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < 4)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            state.SetButton("pause", (packet[0] & 0b00000100) == 0x00);
            state.SetButton("a", (packet[0] & 0b00001000) == 0x00);
            state.SetButton("right", (packet[0] & 0b00010000) == 0x00);
            state.SetButton("left", (packet[0] & 0b00100000) == 0x00);
            state.SetButton("down", (packet[0] & 0b01000000) == 0x00);
            state.SetButton("up", (packet[0] & 0b10000000) == 0x00);

            state.SetButton("b", (packet[1] & 0b00001000) == 0x00);
            state.SetButton("1", (packet[1] & 0b00010000) == 0x00);
            state.SetButton("4", (packet[1] & 0b00100000) == 0x00);
            state.SetButton("l", (packet[1] & 0b00100000) == 0x00);
            state.SetButton("7", (packet[1] & 0b01000000) == 0x00);
            state.SetButton("x", (packet[1] & 0b01000000) == 0x00);
            state.SetButton("star", (packet[1] & 0b10000000) == 0x00);

            state.SetButton("c", (packet[2] & 0b00001000) == 0x00);
            state.SetButton("2", (packet[2] & 0b00010000) == 0x00);
            state.SetButton("5", (packet[2] & 0b00100000) == 0x00);
            state.SetButton("8", (packet[2] & 0b01000000) == 0x00);
            state.SetButton("y", (packet[2] & 0b01000000) == 0x00);
            state.SetButton("0", (packet[2] & 0b10000000) == 0x00);

            state.SetButton("option", (packet[3] & 0b00001000) == 0x00);
            state.SetButton("3", (packet[3] & 0b00010000) == 0x00);
            state.SetButton("6", (packet[3] & 0b00100000) == 0x00);
            state.SetButton("r", (packet[3] & 0b00100000) == 0x00);
            state.SetButton("9", (packet[3] & 0b01000000) == 0x00);
            state.SetButton("z", (packet[3] & 0b01000000) == 0x00);
            state.SetButton("pound", (packet[3] & 0b10000000) == 0x00);

            return(state.Build());
        }
示例#30
0
        static public ControllerState ReadFromPacket(byte[] packet)
        {
            if (packet.Length < PACKET_SIZE)
            {
                return(null);
            }

            var state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS [i]))
                {
                    continue;
                }
                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            return(state.Build());
        }