Ejemplo n.º 1
0
        int getButtonInt(XInputDotNetPure.ButtonState last, XInputDotNetPure.ButtonState current)
        {
            XInputDotNetPure.ButtonState onState  = XInputDotNetPure.ButtonState.Pressed;
            XInputDotNetPure.ButtonState offState = XInputDotNetPure.ButtonState.Pressed;

            bool lastDown    = last.Equals(onState);
            bool currentDown = current.Equals(onState);

            if (lastDown && currentDown)
            {
                return(1);
            }
            if (lastDown && !currentDown)
            {
                return(-1);
            }
            if (!lastDown && currentDown)
            {
                return(2);
            }
            if (!lastDown && !currentDown)
            {
                return(0);
            }

            return(0);
        }
Ejemplo n.º 2
0
 public static float GetValueByButtonState(XInputDotNetPure.ButtonState state)
 {
     if (state == XInputDotNetPure.ButtonState.Pressed)
     {
         return(1);
     }
     return(0);
 }
Ejemplo n.º 3
0
        protected virtual void ParseRawState(object pParsedProperty)
        {
            var parsedProperty = (GamePadButtons)pParsedProperty;

            var propertyInfo = typeof(GamePadButtons).GetProperty(index.ToString().Replace("Thumb", "Stick"));

            if (propertyInfo == null)
            {
                return;
            }

            var methodInfo = propertyInfo.GetMethod;

            if (methodInfo == null)
            {
                return;
            }

            prevRawState = rawState;
            rawState     = (ButtonState)methodInfo.Invoke(parsedProperty, null);
        }
Ejemplo n.º 4
0
        private void btn_connect_Click(object sender, EventArgs e)
        {
            {
                if (run == 1)
                {
                    run = 0;
                    btn_connect.Text = "Connect to drone";
                }
                else
                {
                    run = 1;
                    btn_connect.Text = "Disconnect";
                }
                //create a new server
                var server = new UdpListener();

                //start listening for messages and copy the messages back to the client
                Task.Factory.StartNew(async() =>
                {
                    while (run == 1)
                    {
                        var received = await server.Receive();
                    }
                });

                //create a new client
                var client = UdpUser.ConnectTo(txt_ipaddr.Text, Int32.Parse(txt_ipport.Text));

                //wait for reply messages from server and send them to console
                Task.Factory.StartNew(async() =>
                {
                    while (true)
                    {
                        var received = await client.Receive();
                        rtxt_log.AppendText(Convert.ToString(received.Message));
                        if (received.Message[1] == 0xFF)
                        {
                            break;
                        }
                    }
                });
                if (run == 1)
                {
                    client.Send(new byte[2] {
                        0x00, 0x01
                    });
                }
                else
                {
                    client.Send(new byte[2] {
                        0x01, 0xFF
                    });
                }



                XInputDotNetPure.ButtonState prstart = 0, prback = 0, prleftStick = 0, prrightStick = 0, prleftShoulder = 0, prrightShoulder = 0, prguide = 0, pra = 0, prb = 0, prx = 0, pry = 0;
                float prrigthX = 0, prrigthY = 0, prleftX = 0, prleftY = 0;

                while (true)
                {
                    GamePadState state = GamePad.GetState(PlayerIndex.One);

                    if (state.Buttons.Start != prstart)
                    {
                        client.Send(new byte[2] {
                            0x01, (byte)state.Buttons.Start
                        });
                    }

                    if (state.Buttons.Back != prback)
                    {
                        client.Send(new byte[2] {
                            0x02, (byte)state.Buttons.Back
                        });
                    }

                    if (state.Buttons.LeftStick != prleftStick)
                    {
                        client.Send(new byte[2] {
                            0x03, (byte)state.Buttons.LeftStick
                        });
                    }

                    if (state.Buttons.RightStick != prrightStick)
                    {
                        client.Send(new byte[2] {
                            0x04, (byte)state.Buttons.RightStick
                        });
                    }

                    if (state.Buttons.LeftShoulder != prleftShoulder)
                    {
                        client.Send(new byte[2] {
                            0x05, (byte)state.Buttons.LeftShoulder
                        });
                    }

                    if (state.Buttons.RightShoulder != prrightShoulder)
                    {
                        client.Send(new byte[2] {
                            0x06, (byte)state.Buttons.RightShoulder
                        });
                    }

                    if (state.Buttons.Guide != prguide)
                    {
                        client.Send(new byte[2] {
                            0x01, 0xFF
                        });                                                                             //TODO: need async thread
                        client.Send(new byte[2] {
                            0x07, (byte)state.Buttons.Guide
                        });
                        break;
                    }

                    if (state.Buttons.X != prx)
                    {
                        client.Send(new byte[2] {
                            0x08, (byte)state.Buttons.X
                        });
                    }

                    if (state.Buttons.Y != pry)
                    {
                        client.Send(new byte[2] {
                            0x09, (byte)state.Buttons.Y
                        });
                    }

                    if (state.Buttons.A != pra)
                    {
                        client.Send(new byte[2] {
                            0x0A, (byte)state.Buttons.A
                        });
                    }

                    if (state.Buttons.B != prb)
                    {
                        client.Send(new byte[2] {
                            0x0B, (byte)state.Buttons.B
                        });
                    }


                    if (state.ThumbSticks.Left.X != prrigthX)
                    {
                        client.Send(new byte[2] {
                            0x0C, (byte)((((state.ThumbSticks.Left.X + 1.0f) / 2.0f) * 255.0f) - 128.0f)
                        });
                    }
                    if (state.ThumbSticks.Left.Y != prrigthY)
                    {
                        client.Send(new byte[2] {
                            0x0D, (byte)((((state.ThumbSticks.Left.Y + 1.0f) / 2.0f) * 255.0f) - 128.0f)
                        });
                    }
                    if (state.ThumbSticks.Right.X != prleftX)
                    {
                        client.Send(new byte[2] {
                            0x0E, (byte)((((state.ThumbSticks.Right.X + 1.0f) / 2.0f) * 255.0f) - 128.0f)
                        });
                    }
                    if (state.ThumbSticks.Right.Y != prleftY)
                    {
                        client.Send(new byte[2] {
                            0x0F, (byte)((((state.ThumbSticks.Right.Y + 1.0f) / 2.0f) * 255.0f) - 128.0f)
                        });
                    }
                    Thread.Sleep(16);
                }
            }
        }
        public void XInputGamePadInputRoll()
        {
            var State = XInputDotNetPure.GamePad.GetState(PlayerIndex.One);

            foreach (PropertyInfo buttonProperty in AvailableXButtonProperties)
            {
                XInputDotNetPure.ButtonState CurrentButtonState = (XInputDotNetPure.ButtonState)buttonProperty.GetValue(State.Buttons);
                XInputDotNetPure.ButtonState OldButtonState     = (XInputDotNetPure.ButtonState)buttonProperty.GetValue(State.Buttons);
                if (!Object.ReferenceEquals(XOldState, null))
                {
                    OldButtonState = (XInputDotNetPure.ButtonState)buttonProperty.GetValue(XOldState.Buttons);
                }

                if (CurrentButtonState == XInputDotNetPure.ButtonState.Pressed &&
                    (Object.ReferenceEquals(XOldState, null) || OldButtonState == XInputDotNetPure.ButtonState.Released))
                {
                    CallButtonMapping(buttonProperty.Name, true);
                    System.Diagnostics.Debug.WriteLine($"{buttonProperty.Name} Pressed");
                }
                if (CurrentButtonState == XInputDotNetPure.ButtonState.Released &&
                    (!Object.ReferenceEquals(XOldState, null) && OldButtonState == XInputDotNetPure.ButtonState.Pressed))
                {
                    CallButtonMapping(buttonProperty.Name, false);
                    System.Diagnostics.Debug.WriteLine($"{buttonProperty.Name} Released");
                }
            }

            PropertyInfo[] AvailableXTriggerProperties = typeof(XInputDotNetPure.GamePadTriggers).GetProperties().ToArray();

            foreach (PropertyInfo buttonProperty in AvailableXTriggerProperties)
            {
                float CurrentTriggerState = (float)buttonProperty.GetValue(State.Triggers);
                float OldTriggerState     = (float)buttonProperty.GetValue(State.Triggers);
                if (!Object.ReferenceEquals(XOldState, null))
                {
                    OldTriggerState = (float)buttonProperty.GetValue(XOldState.Triggers);
                }

                if (CurrentTriggerState == 1 &&
                    (Object.ReferenceEquals(XOldState, null) || OldTriggerState == 0))
                {
                    CallButtonMapping(buttonProperty.Name, true);
                    System.Diagnostics.Debug.WriteLine($"{buttonProperty.Name} Trigger Pressed");
                }
                if (CurrentTriggerState == 0 &&
                    (!Object.ReferenceEquals(XOldState, null) && OldTriggerState == 1))
                {
                    CallButtonMapping(buttonProperty.Name, false);
                    System.Diagnostics.Debug.WriteLine($"{buttonProperty.Name} Trigger Released");
                }
            }

            var oldUp    = Convert.ToBoolean(XOldState.DPad.Up);
            var oldDown  = Convert.ToBoolean(XOldState.DPad.Down);
            var oldLeft  = Convert.ToBoolean(XOldState.DPad.Left);
            var oldRight = Convert.ToBoolean(XOldState.DPad.Right);

            var newUp    = Convert.ToBoolean(State.DPad.Up);
            var newDown  = Convert.ToBoolean(State.DPad.Down);
            var newLeft  = Convert.ToBoolean(State.DPad.Left);
            var newRight = Convert.ToBoolean(State.DPad.Right);

            var oldLeftX = Math.Round(XOldState.ThumbSticks.Left.X);
            var oldLeftY = Math.Round(XOldState.ThumbSticks.Left.Y);

            var newLeftX = Math.Round(State.ThumbSticks.Left.X);
            var newLeftY = Math.Round(State.ThumbSticks.Left.Y);

            if (oldLeftX == 0 && newLeftX == 1 || oldRight == true && newRight == false)
            {
                System.Diagnostics.Debug.WriteLine("Right Pushed");
                CallButtonMapping("IsRight", true);
            }

            if (oldLeftX == 1 && newLeftX == 0 || oldRight == false && newRight == true)
            {
                System.Diagnostics.Debug.WriteLine("Right Released");
                CallButtonMapping("IsRight", false);
            }

            if (oldLeftX == 0 && newLeftX == -1 || oldLeft == true && newLeft == false)
            {
                System.Diagnostics.Debug.WriteLine("Left Pushed");
                CallButtonMapping("IsLeft", true);
            }

            if (oldLeftX == -1 && newLeftX == 0 || oldLeft == false && newLeft == true)
            {
                System.Diagnostics.Debug.WriteLine("Left Released");
                CallButtonMapping("IsLeft", false);
            }

            if (oldLeftY == 0 && newLeftY == 1 || oldUp == true && newUp == false)
            {
                System.Diagnostics.Debug.WriteLine("Up Pushed");
                CallButtonMapping("IsUp", true);
            }

            if (oldLeftY == 1 && newLeftY == 0 || oldUp == false && newUp == true)
            {
                System.Diagnostics.Debug.WriteLine("Up Released");
                CallButtonMapping("IsUp", false);
            }

            if (oldLeftY == 0 && newLeftY == -1 || oldDown == true && newDown == false)
            {
                System.Diagnostics.Debug.WriteLine("Down Pushed");
                CallButtonMapping("IsDown", true);
            }

            if (oldLeftY == -1 && newLeftY == 0 || oldDown == false && newDown == true)
            {
                System.Diagnostics.Debug.WriteLine("Down Released");
                CallButtonMapping("IsDown", false);
            }

            XOldState = State;
        }