Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI,
Actually I've made a winform application that read buttons and axis of my joystick.
My goal is to have a config file to set input list with functions list.
for example
I'm using Sharpdx but I can change if needed
Clear = buttons1
Left = AxisRight

Thanks for your help

What I have tried:

I allready use sharpdx with success
Posted
Comments
Pete O'Hanlon 24-Apr-24 5:08am    
It's a little bit unclear what you are asking for here. In your example, are Clear and Left methods that can be called? Are they classes? Please give us more context as to what it is that you are trying to accomplish.
OriginalGriff 24-Apr-24 5:22am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Assuming that you wish to change which function is called for different joystick inputs based on a configuration file then you could use something like the code shown below. You might want to try a more elegant solution if there are a lot of alternatives since otherwise the if then else if will become confusing to read and very repetitive.

JSON
{
    "button1": "FunctionA",
    "button2": "FunctionB"
}

And then :

C#
using Newtonsoft.Json;
using System.IO;

// Your functions
void FunctionA()
{
    Console.WriteLine("Function A is called.");
}

void FunctionB()
{
    Console.WriteLine("Function B is called.");
}

// Read configuration file
var config = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText("config.json"));

// Poll events from joystick
while (true)
{
    joystick.Poll();
    var datas = joystick.GetBufferedData();
    foreach (var state in datas)
    {
        // Call the function based on the configuration file
        if (state.Offset == JoystickOffset.Buttons0 && state.Value != 0)
        {
            if (config["button1"] == "FunctionA")
                FunctionA();
            else if (config["button1"] == "FunctionB")
                FunctionB();
        }
        else if (state.Offset == JoystickOffset.Buttons1 && state.Value != 0)
        {
            if (config["button2"] == "FunctionA")
                FunctionA();
            else if (config["button2"] == "FunctionB")
                FunctionB();
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900