I'm working on a multi-joystick setup where I need to build custom button mappings. And even map across devices. For example, if there is a separate joystick and throttle quadrant I need to map 'Z' or whatever, over to the throttle input. I also need to be able to check and see if there is a slider(s) available on each stick as I build the mapping profiles, etc. I cannot figure out how to just say something like:
if (joystick.HasSliders)
So far I've tried different variations of
PropertyInfo rxprop = typeof(SharpDX.DirectInput.JoystickState).GetProperty("Sliders");
However, if the joystick does not have sliders, this of course will throw an exception. If it only has a single slider, I will always get back an Int array of Sliders[] with two values, no matter if the stick only has a single slider or not. So it is not a guaranteed way to know if sliders exist, or even how many exist. If there is no second actual slider, the value Sliders[1] is always 0, but if there is a second slider it could still be in the 0 position.
Currently this is what I've been messing with:
usbjoysticks[k].Poll();
JoystickState currentState = null;
try
{
currentState = usbjoysticks[k].GetCurrentState();
//joystickmap.axisRangeMax = usbjoysticks[k].GetObjectInfoById(1);
PropertyInfo rxprop = typeof(SharpDX.DirectInput.JoystickState).GetProperty("Sliders");
var rzvalue = (int[])rxprop.GetValue(currentState, null);
var mySlider1 = rzvalue[0];
var datas = usbjoysticks[k].GetBufferedData();
foreach (var state in datas)
{
Console.WriteLine(state);
if (state.Offset == JoystickOffset.X)
{
Console.WriteLine(state.Value);
}
}
//DeviceObjectInstance my = usbjoysticks[k].GetObjectInfoByName("Sliders1");
var stuff = usbjoysticks[k].GetObjectPropertiesByName("Sliders0");
//=================
joystickmap.jX = "X";
joystickmap.axisRangeMax = usbjoysticks[k].GetObjectPropertiesByName("Y").Range.Maximum; //65535
joystickmap.jY = "Y";
if (currentState.Z != 0)
{
joystickmap.jZ = "Z";
var maxZrange = usbjoysticks[k].GetObjectPropertiesByName("Z").Range.Maximum;
joystickmap.axisRangeMax = usbjoysticks[k].GetObjectPropertiesByName("Z").Range.Maximum;
}
if (currentState.Sliders[0] != 0 || currentState.Sliders[1] != 0) //TODO possible change to Joystick State value in class object instead of text.
{
joystickmap.SLD1 = "Slider1";
//joystickmap.jZ = "Sliders";
joystickmap.jsSliders = currentState.Sliders;
var maxSld1range = usbjoysticks[k].GetObjectPropertiesByName("Sliders0").Range.Maximum;
joystickmap.sld1 = joystickmap.jsSliders[0];
joystickmap.sld2 = joystickmap.jsSliders[1];
}
if (currentState.Sliders[1] != 0)
{
joystickmap.SLD2 = "Slider2";
}
if (currentState.RotationX != 0)
{
joystickmap.rX = "RotationX";
}
if (currentState.RotationY != 0)
{
joystickmap.rY = "RotationY";
}
if (currentState.RotationZ != 0)
{
joystickmap.rZ = "RotationZ";
//joystickmap.jZ = "RotationZ";
}
joystickmap.axisdivider = 2;
for (var b = 0; b < 20; b++)
{
//joystickmap.joybuttons.Add(currentState.Buttons[b]);
joystickmap.joybuttons[b] = (b + 1);
}
joystickmappings.Add(joystickmap);
if (!File.Exists(filename))
{
File.WriteAllText(filename, JsonConvert.SerializeObject(joystickmap));
// serialize JSON directly to a file
using (StreamWriter file = File.CreateText(filename))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, joystickmap);
}
}
}
catch (Exception e)
{
MessageBox.Show("USB joystick map creation failure!\n" + e.Message);
break;
}