SCJMapper-V2/Joystick/DeviceTuningParameter.cs

330 lines
10 KiB
C#
Raw Normal View History

2014-10-26 22:11:57 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace SCJMapper_V2
{
/// <summary>
/// set of parameters to tune the Joystick
/// </summary>
public class DeviceTuningParameter
2014-10-26 22:11:57 +00:00
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType );
2014-11-19 22:58:05 +00:00
private String m_actionCommand = ""; // v_pitch - js1_x ..
private String m_cmdCtrl = ""; // x, y, rotz ...
private String m_type = ""; // joystick OR xboxpad
private int m_devInstanceNo = -1; // jsN - instance in XML
2014-10-26 22:11:57 +00:00
String m_option = ""; // the option name (level where it applies)
private String m_deviceName = "";
private bool m_senseEnabled = false; // default
private String m_sense = "1.00";
private bool m_invertEnabled = false; // default
private bool m_expEnabled = false; // default
private String m_exponent = "1.000";
private bool m_ptsEnabled = false; // default
private List<String> m_PtsIn = new List<String>( );
private List<String> m_PtsOut = new List<String>( );
private DeviceCls m_device = null;
2014-10-26 22:11:57 +00:00
private DeviceDeadzoneParameter m_deadzone = null;
public DeviceTuningParameter( )
2014-10-26 22:11:57 +00:00
{
}
#region Properties
2014-11-19 22:58:05 +00:00
public DeviceCls GameDevice
2014-10-26 22:11:57 +00:00
{
get { return m_device; }
set { m_device = value;
m_type = "";
m_devInstanceNo = -1;
if ( JoystickCls.IsDeviceClass( m_device.DevClass ) ) {
m_type = m_device.DevClass;
m_devInstanceNo = (m_device as JoystickCls).JSAssignment;
}
else if ( GamepadCls.IsDeviceClass( m_device.DevClass ) ) {
m_type = m_device.DevClass;
m_devInstanceNo = 1; // supports ONE gamepad
}
}
2014-10-26 22:11:57 +00:00
}
public int DevInstanceNo
2014-10-26 22:11:57 +00:00
{
get { return m_devInstanceNo; }
2014-10-26 22:11:57 +00:00
}
public String DeviceName
{
get { return m_deviceName; }
set { m_deviceName = value; }
}
2014-11-19 22:58:05 +00:00
public String ActionCommand
2014-10-26 22:11:57 +00:00
{
2014-11-19 22:58:05 +00:00
get { return m_actionCommand; }
set { m_actionCommand = value; DecomposeCommand( ); }
2014-10-26 22:11:57 +00:00
}
public String CommandCtrl
{
get { return m_cmdCtrl; }
set { m_cmdCtrl = value; }
}
public DeviceDeadzoneParameter Deadzone
2014-10-26 22:11:57 +00:00
{
get { return m_deadzone; }
set { m_deadzone = value; }
}
public bool SensitivityUsed
{
get { return m_senseEnabled; }
set { m_senseEnabled = value; }
}
public String Sensitivity
{
get { return m_sense; }
set { m_sense = value; }
}
public bool InvertUsed
{
get { return m_invertEnabled; }
set { m_invertEnabled = value; }
}
public bool ExponentUsed
{
get { return m_expEnabled; }
set { m_expEnabled = value; }
}
public String Exponent
{
get { return m_exponent; }
set { m_exponent = value; }
}
public bool NonLinCurveUsed
{
get { return m_ptsEnabled; }
set { m_ptsEnabled = value; }
}
public List<String> NonLinCurvePtsIn
{
get { return m_PtsIn; }
set { m_PtsIn = value; }
}
public List<String> NonLinCurvePtsOut
{
get { return m_PtsOut; }
set { m_PtsOut = value; }
}
#endregion
/// <summary>
/// Derive values from a command (e.g. v_pitch - js1_x)
/// </summary>
private void DecomposeCommand( )
{
// populate from input
// something like "v_pitch - js1_x" OR "v_pitch - xi_thumbl" OR "v_pitch - ximod+xi_thumbl+xi_mod"
2014-11-19 22:58:05 +00:00
String cmd = ActionTreeNode.CommandFromNodeText( ActionCommand );
String action = ActionTreeNode.ActionFromNodeText( ActionCommand );
m_cmdCtrl = "";
if ( !String.IsNullOrWhiteSpace( cmd ) ) {
// decomp gamepad entries - could have modifiers so check for contains...
if ( cmd.Contains( "xi_thumblx" ) ) {
// gamepad
m_cmdCtrl = "xi_thumblx";
m_deviceName = m_device.DevName;
if ( action.Contains( "pitch" ) ) m_option = String.Format( "flight_move_pitch" );
else m_option = String.Format( "flight_move_yaw" );
}
else if ( cmd.Contains( "xi_thumbly" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbly";
m_deviceName = m_device.DevName;
if ( action.Contains( "pitch" ) ) m_option = String.Format( "flight_move_pitch" );
else m_option = String.Format( "flight_move_yaw" );
}
else if ( cmd.Contains( "xi_thumbrx" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbrx";
m_deviceName = m_device.DevName;
if ( action.Contains( "pitch" ) ) m_option = String.Format( "flight_move_pitch" );
else m_option = String.Format( "flight_move_yaw" );
}
else if ( cmd.Contains( "xi_thumbry" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbry";
m_deviceName = m_device.DevName;
if ( action.Contains( "pitch" ) ) m_option = String.Format( "flight_move_pitch" );
else m_option = String.Format( "flight_move_yaw" );
}
// assume joystick
else {
// get parts
m_cmdCtrl = JoystickCls.ActionFromJsCommand( cmd ); //js1_x -> x; js2_rotz -> rotz
m_deviceName = m_device.DevName;
if ( action.Contains( "pitch" ) ) m_option = String.Format( "flight_move_pitch" );
else if ( action.Contains( "yaw" ) ) m_option = String.Format( "flight_move_yaw" );
else m_option = String.Format( "flight_move_roll" );
}
2014-10-26 22:11:57 +00:00
}
}
/// <summary>
/// Format an XML -options- node from the tuning contents
/// </summary>
/// <returns>The XML string or an empty string</returns>
public String Options_toXML( )
{
if ( ( m_senseEnabled || m_expEnabled || m_invertEnabled || m_ptsEnabled ) == false ) return ""; // not used
String tmp = "";
tmp += String.Format( "\t<options type=\"{0}\" instance=\"{1}\">\n", m_type, m_devInstanceNo.ToString( ) );
2014-10-26 22:11:57 +00:00
tmp += String.Format( "\t\t<{0} ", m_option );
if ( InvertUsed ) {
tmp += String.Format( "invert=\"1\" " );
}
if ( SensitivityUsed ) {
tmp += String.Format( "sensitivity=\"{0}\" ", m_sense );
}
if ( NonLinCurveUsed ) {
// add exp to avoid merge of things...
tmp += String.Format( "exponent=\"1.00\" > \n" ); // CIG get to default expo 2.something if not set to 1 here
tmp += String.Format( "\t\t\t<nonlinearity_curve>\n" );
tmp += String.Format( "\t\t\t\t<point in=\"{0}\" out=\"{1}\"/>\n", m_PtsIn[0], m_PtsOut[0] );
tmp += String.Format( "\t\t\t\t<point in=\"{0}\" out=\"{1}\"/>\n", m_PtsIn[1], m_PtsOut[1] );
tmp += String.Format( "\t\t\t\t<point in=\"{0}\" out=\"{1}\"/>\n", m_PtsIn[2], m_PtsOut[2] );
tmp += String.Format( "\t\t\t</nonlinearity_curve>\n" );
tmp += String.Format( "\t\t</{0}> \n", m_option );
}
else if ( ExponentUsed ) {
// only exp used
tmp += String.Format( "exponent=\"{0}\" /> \n", m_exponent );
}
else {
// neither exp or curve
tmp += String.Format( "exponent=\"1.00\" /> \n" );// CIG get to default expo 2.something if not set to 1 here
}
tmp += String.Format( "\t</options>\n \n" );
return tmp;
}
/// <summary>
/// Read the options from the XML
/// can get only the 3 ones for Move X,Y,RotZ, pitch and yaq right now
2014-10-26 22:11:57 +00:00
/// </summary>
/// <param name="reader">A prepared XML reader</param>
/// <param name="instance">the Josyticj instance number</param>
/// <returns></returns>
public Boolean Options_fromXML( XmlReader reader, String type, int instance )
2014-10-26 22:11:57 +00:00
{
m_type = type;
2014-10-26 22:11:57 +00:00
String invert = "";
String sensitivity = "";
String exponent = "";
String instance_inv = "";
m_option = reader.Name;
m_devInstanceNo = instance;
2014-10-26 22:11:57 +00:00
// derive from pilot_move_x || pilot_move_rotx || pilot_moveyaw (nothing bad should arrive here)
2014-10-26 22:11:57 +00:00
String[] e = m_option.ToLowerInvariant( ).Split( new char[] { '_' } );
if ( e.Length > 2 ) m_cmdCtrl = e[2]; // TODO - see if m_cmdCtrl is needed to be derived here
2014-10-26 22:11:57 +00:00
if ( reader.HasAttributes ) {
invert = reader["invert"];
if ( !String.IsNullOrWhiteSpace( invert ) ) {
m_invertEnabled = false;
if ( invert == "1" ) m_invertEnabled = true;
}
instance_inv = reader["instance"]; // CIG wrong attr name ?!
if ( !String.IsNullOrWhiteSpace( instance_inv ) ) {
m_invertEnabled = false;
if ( instance_inv == "1" ) m_invertEnabled = true;
}
sensitivity = reader["sensitivity"];
if ( !String.IsNullOrWhiteSpace( sensitivity ) ) {
m_sense = sensitivity;
m_senseEnabled = true;
}
exponent = reader["exponent"];
if ( !String.IsNullOrWhiteSpace( exponent ) ) {
m_exponent = exponent;
m_expEnabled = true;
}
}
// we may have a nonlin curve...
reader.Read( );
if ( !reader.EOF ) {
if ( reader.Name.ToLowerInvariant( ) == "nonlinearity_curve" ) {
m_PtsIn.Clear( ); m_PtsOut.Clear( ); // reset pts
reader.Read( );
while ( !reader.EOF ) {
String ptIn = "";
String ptOut = "";
if ( reader.Name.ToLowerInvariant( ) == "point" ) {
if ( reader.HasAttributes ) {
ptIn = reader["in"];
ptOut = reader["out"];
m_PtsIn.Add( ptIn ); m_PtsOut.Add( ptOut ); m_ptsEnabled = true;
}
}
reader.Read( );
}//while
// sanity check - we've have to have 3 pts here - else we subst
// add 2nd
if ( m_PtsIn.Count < 2 ) {
m_PtsIn.Add( "0.5" ); m_PtsOut.Add( "0.5" );
log.Info( "Options_fromXML: got only one nonlin point, added (0.5|0.5)" );
}
// add 3rd
if ( m_PtsIn.Count < 3 ) {
m_PtsIn.Add( "0.75" ); m_PtsOut.Add( "0.75" );
log.Info( "Options_fromXML: got only two nonlin points, added (0.75|0.75)" );
}
}
}
return true;
}
}
}