1

Hi all I need your help using open TK works on dot net framework 4.5 using Xaml I figured that out. using the following code. I want to replicate it on a windows form but open TK doesn't like forms. I did a lot of googling and found 2 options SlimDX and SharpDX directinput. My goal here is to use a joystick to move a control along the form using a joystick input. I cant seem to replicate axis 1 and 2 the way I can with open TK. I am looking for example 0 or 1 and -1 as shown in the c3 sharp code for the XAML.

using OpenTK.Input;

private DispatcherTimer clock;
private GamePadState oldgstate;
private JoystickState oldjstate;
private int activeGamepad = 0;
private int activeJoystick = 0;


 public MainWindow()
    {
        InitializeComponent();
        createNewTimer();
        oldgstate = GamePad.GetState(activeGamepad);
        oldjstate = Joystick.GetState(activeJoystick);
    }

    //public Direction TestOpenTK.MainWindow.MyProperty { get; set; }

    private void createNewTimer() {
        clock = new DispatcherTimer();
        clock.Tick += new EventHandler(checkGamePads);
        clock.Interval = new TimeSpan(0,0,0,0,25);
        clock.Start();
    }

    private void checkGamePads(Object sender, EventArgs e)
    {
        var gState = GamePad.GetState(activeGamepad);
        var jState = Joystick.GetState(activeJoystick);


        if (!gState.Equals(oldgstate))
        {
            txtGamePadDebug.Text = gState.ToString();
        }

        if (!gState.Equals(oldjstate))
        {
            //txtJoystickDebug.Text = jState.ToString() + Environment.NewLine + " Axis 1 = " + jState.GetAxis(JoystickAxis.Axis1).ToString()
            //     + Environment.NewLine + " Throttle Axis 2 = " + jState.GetAxis(JoystickAxis.Axis2).ToString() + Environment.NewLine + " Axis 3 Rotation = " +
            //     jState.GetAxis(JoystickAxis.Axis3).ToString() + Environment.NewLine + " Axis 4 = " + jState.GetAxis(JoystickAxis.Axis4).ToString();
            for (int i = 0; i < 4; i++)
            {
                var state = Joystick.GetState(i);

                if (state.IsConnected)
                {

Need to get similar Axis using SharpDX or SlimDX or figure out how to use open TK on a windows form

float x = state.GetAxis(JoystickAxis.Axis0); float y = state.GetAxis(JoystickAxis.Axis1);

                    //Convert and round x and y
                    int X = (int)Math.Round(x);
                    int Y = (int)Math.Round(y);

                    //Set axis direction using integers X and Y
                    if (X == 0 && Y==1)
                    {
                        // N
                        txtJoystickDebug.Text = "North";
                    }

                    if (X == 1 && Y == 1)
                    {
                        // NE Up and right
                        txtJoystickDebug.Text = "North East";
                    }

                    if (X == 1 && Y == 0)
                    {
                        //E
                        txtJoystickDebug.Text = "East";
                    }

                    if (X == 1 && Y == -1)
                    {
                        //SE Down Right
                        txtJoystickDebug.Text = "South East";
                    }

                    if (X == 0 && Y == -1)
                    {
                        //S
                        txtJoystickDebug.Text = "South";
                    }

                    if (X == -1 && Y == -1)
                    {
                        // SW Down Left
                        txtJoystickDebug.Text = "South West";
                    }

                    if (X == -1 && Y == 0)
                    {
                        //W 
                        txtJoystickDebug.Text = "West";
                    }

                    if (X == -1 && Y == 1)
                    {
                        //NW Up or West
                        txtJoystickDebug.Text = "North West";

                    }

                    //Slider aka throttle axis
                    //float T = state.GetAxis(JoystickAxis.Axis2);

                    // Print the current state of the joystick
                    //Console.WriteLine(T);
                    //txtJoystickDebug.Text = "X = " + Math.Round(x).ToString() + Environment.NewLine + "Y = "  + Math.Round(y).ToString()
                    //    + Environment.NewLine + " Throttle = " + T.ToString();
                }
            }
        }
user2180706
  • 307
  • 3
  • 8

1 Answers1

0

If you use SharpDX, to access gamepad, you need to use SharpDX.XInput package.

Then to initialize a controller :

var controller = new SharpDX.XInput.Controller(SharpDX.XInput.UserIndex.One);

to poll controller :

if (controller.IsConnected)
{
    SharpDX.XInput.Gamepad gamepad = controller.GetState().Gamepad;

    //Axis 
    //LeftThumbX, LeftThumbY, RightThumbX, RightThumbY

    //these are short so to get a float from 0 to 1 instead :
    float tx = gamepad.LeftThumbX / (float)short.MaxValue;
    
    //Buttons
    //GamepadButtonFlags Buttons

}
mrvux
  • 8,523
  • 1
  • 27
  • 61