SCJMapper-V2/Devices/Options/DeviceTuningParameter.cs

398 lines
12 KiB
C#
Raw Normal View History

using System;
2014-10-26 22:11:57 +00:00
using System.Collections.Generic;
using System.Linq;
2014-10-26 22:11:57 +00:00
using System.Xml;
using System.Xml.Linq;
using SCJMapper_V2.Actions;
using SCJMapper_V2.Devices.Joystick;
namespace SCJMapper_V2.Devices.Options
2014-10-26 22:11:57 +00:00
{
/// <summary>
/// set of parameters to tune the Joystick
/// </summary>
public class DeviceTuningParameter : ICloneable
2014-10-26 22:11:57 +00:00
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod( ).DeclaringType );
private string m_nodetext = ""; // v_pitch - js1_x
private string m_action = ""; // v_pitch
private string m_cmdCtrl = ""; // js1_x, js1_y, js1_rotz ...
private string m_class = ""; // 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)
2014-10-26 22:11:57 +00:00
private string m_deviceName = "";
private bool m_isStrafe = false; // default
2014-10-26 22:11:57 +00:00
private bool m_expEnabled = false; // default
private string m_exponent = "1.000";
2014-10-26 22:11:57 +00:00
private bool m_ptsEnabled = false; // default
private List<string> m_PtsIn = new List<string>( );
private List<string> m_PtsOut = new List<string>( );
2014-10-26 22:11:57 +00:00
private bool m_invertEnabled = false; // default
private DeviceCls m_deviceRef = null; // Ref
private DeviceOptionParameter m_deviceoptionRef = null; // will be used only while editing the Options !!
/// <summary>
/// Clone this object
/// </summary>
/// <returns>A deep Clone of this object</returns>
public object Clone()
{
var dt = (DeviceTuningParameter)this.MemberwiseClone( ); // self and all value types
// more objects to deep copy
// --> NO cloning as this Ref will be overwritten when editing
dt.m_deviceoptionRef = null; // just reset
return dt;
}
/// <summary>
/// Check clone against This
/// </summary>
/// <param name="clone"></param>
/// <returns>True if the clone is identical but not a shallow copy</returns>
internal bool CheckClone( DeviceTuningParameter clone )
{
bool ret = true;
// object vars first
ret &= ( this.m_nodetext == clone.m_nodetext ); // immutable string - shallow copy is OK
ret &= ( this.m_action == clone.m_action ); // immutable string - shallow copy is OK
ret &= ( this.m_cmdCtrl == clone.m_cmdCtrl );// immutable string - shallow copy is OK
ret &= ( this.m_class == clone.m_class ); // immutable string - shallow copy is OK
ret &= ( this.m_devInstanceNo == clone.m_devInstanceNo );
ret &= ( this.m_option == clone.m_option );
ret &= ( this.m_deviceName == clone.m_deviceName );
ret &= ( this.m_isStrafe == clone.m_isStrafe );
ret &= ( this.m_expEnabled == clone.m_expEnabled );
ret &= ( this.m_exponent == clone.m_exponent );
ret &= ( this.m_ptsEnabled == clone.m_ptsEnabled );
ret &= ( this.m_PtsIn == clone.m_PtsIn );
ret &= ( this.m_PtsOut == clone.m_PtsOut );
ret &= ( this.m_invertEnabled == clone.m_invertEnabled );
ret &= ( this.m_deviceRef == clone.m_deviceRef );
// check m_deviceoptionRef
// --> NO check as this is assigned and used only while editing the Options
return ret;
}
2014-10-26 22:11:57 +00:00
public DeviceTuningParameter( string optName )
2014-10-26 22:11:57 +00:00
{
m_option = optName;
2014-10-26 22:11:57 +00:00
}
public DeviceTuningParameter( string optName, DeviceCls device )
{
m_option = optName;
GameDevice = device;
}
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_deviceRef; }
set {
m_deviceRef = value;
m_class = "";
m_devInstanceNo = -1;
if ( m_deviceRef == null ) return; // got a null device
m_class = m_deviceRef.DevClass;
m_devInstanceNo = m_deviceRef.XmlInstance;
}
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
2014-10-26 22:11:57 +00:00
{
get { return m_deviceName; }
set { m_deviceName = value; }
}
public string DeviceClass
2014-10-26 22:11:57 +00:00
{
get { return m_class; }
}
public string OptionName
{
get { return m_option; }
}
public string NodeText
{
get { return m_nodetext; }
set { m_nodetext = value; DecomposeCommand( ); }
2014-10-26 22:11:57 +00:00
}
public string CommandCtrl
2014-10-26 22:11:57 +00:00
{
get { return m_cmdCtrl; }
set { m_cmdCtrl = value; }
}
public bool IsStrafeCommand
{
get { return m_isStrafe; }
set { m_isStrafe = value; }
}
2014-10-26 22:11:57 +00:00
public DeviceOptionParameter DeviceoptionRef
2014-10-26 22:11:57 +00:00
{
get { return m_deviceoptionRef; }
set {
m_deviceoptionRef = value;
if ( m_deviceoptionRef != null )
m_deviceoptionRef.Action = m_action;
}
2014-10-26 22:11:57 +00:00
}
public bool InvertUsed
{
get { return m_invertEnabled; }
set { m_invertEnabled = value; }
2014-10-26 22:11:57 +00:00
}
public bool ExponentUsed
{
get { return m_expEnabled; }
set { m_expEnabled = value; }
}
public string Exponent
2014-10-26 22:11:57 +00:00
{
get { return m_exponent; }
set { m_exponent = value; }
}
public bool NonLinCurveUsed
{
get { return m_ptsEnabled; }
set { m_ptsEnabled = value; }
}
public List<string> NonLinCurvePtsIn
2014-10-26 22:11:57 +00:00
{
get { return m_PtsIn; }
set { m_PtsIn = value; }
}
public List<string> NonLinCurvePtsOut
2014-10-26 22:11:57 +00:00
{
get { return m_PtsOut; }
set { m_PtsOut = value; }
}
#endregion
/// <summary>
/// Reset all items that will be assigned dynamically while scanning the actions
/// - currently DeviceoptionRef, NodeText
/// </summary>
public void ResetDynamicItems()
{
// using the public property to ensure the complete processing of the assignment
// GameDevice = null;
DeviceoptionRef = null;
NodeText = "";
}
public void AssignDynamicItems( DeviceCls dev, DeviceOptionParameter devOptionRef, string nodeText )
{
// using the public property to ensure the complete processing of the assignment
NodeText = nodeText; // must be first - the content is used later for DeviceOptionRef assignment
GameDevice = dev;
DeviceoptionRef = devOptionRef;
}
2014-10-26 22:11:57 +00:00
/// <summary>
/// Derive values from a command (e.g. v_pitch - js1_x)
/// </summary>
private void DecomposeCommand()
2014-10-26 22:11:57 +00:00
{
// populate from input
// something like "v_pitch - js1_x" OR "v_pitch - xi_thumbl" OR "v_pitch - ximod+xi_thumbl+xi_mod"
string cmd = ActionTreeNode.CommandFromNodeText( NodeText );
m_action = ActionTreeNode.ActionFromNodeText( NodeText );
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_deviceRef.DevName;
}
else if ( cmd.Contains( "xi_thumbly" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbly";
m_deviceName = m_deviceRef.DevName;
}
else if ( cmd.Contains( "xi_thumbrx" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbrx";
m_deviceName = m_deviceRef.DevName;
}
else if ( cmd.Contains( "xi_thumbry" ) ) {
// gamepad
m_cmdCtrl = "xi_thumbry";
m_deviceName = m_deviceRef.DevName;
}
// assume joystick
else {
// get parts
m_cmdCtrl = JoystickCls.ActionFromJsCommand( cmd ); //js1_x -> x; js2_rotz -> rotz
m_deviceName = m_deviceRef.DevName;
}
2014-10-26 22:11:57 +00:00
}
}
/// <summary>
/// Rounds a string to 3 decimals (if it is a number..)
/// </summary>
/// <param name="valString">A value string</param>
/// <returns>A rounded value string - or the string if not a number</returns>
private string RoundString( string valString )
{
double d = 0;
if ( ( !string.IsNullOrEmpty( valString ) ) && double.TryParse( valString, out d ) ) {
return d.ToString( "0.000" );
}
else {
return valString;
}
}
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()
2014-10-26 22:11:57 +00:00
{
if ( ( /*SensitivityUsed ||*/ ExponentUsed || InvertUsed || NonLinCurveUsed ) == false ) return ""; // not used
if ( DevInstanceNo < 1 ) return ""; // no device to assign it to..
2014-10-26 22:11:57 +00:00
string tmp = "";
tmp += string.Format( "\t<options type=\"{0}\" instance=\"{1}\">\n", m_class, m_devInstanceNo.ToString( ) );
tmp += string.Format( "\t\t<{0} ", m_option );
2014-10-26 22:11:57 +00:00
if ( InvertUsed ) {
tmp += string.Format( "invert=\"1\" " );
2014-10-26 22:11:57 +00:00
}
/*
2014-10-26 22:11:57 +00:00
if ( SensitivityUsed ) {
tmp += string.Format( "sensitivity=\"{0}\" ", Sensitivity );
2014-10-26 22:11:57 +00:00
}
*/
2014-10-26 22:11:57 +00:00
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 ) {
2014-10-26 22:11:57 +00:00
// only exp used
tmp += string.Format( "exponent=\"{0}\" /> \n", Exponent );
}
else {
2014-10-26 22:11:57 +00:00
// neither exp or curve
tmp += string.Format( " /> \n" );// nothing...
2014-10-26 22:11:57 +00:00
}
tmp += string.Format( "\t</options>\n \n" );
2014-10-26 22:11:57 +00:00
return tmp;
}
/// <summary>
/// Read the options from the XML
/// can get only the 3 ones for Move Pitch, Yaw, Roll right now
2014-10-26 22:11:57 +00:00
/// </summary>
/// <param name="reader">A prepared XML reader</param>
/// <param name="instance">the Joystick instance number</param>
2014-10-26 22:11:57 +00:00
/// <returns></returns>
public Boolean Options_fromXML( XElement option, string type, int instance )
2014-10-26 22:11:57 +00:00
{
/*
<flight_move_pitch exponent="1.00" >
<nonlinearity_curve>
<point in="0.182" out="0.028"/>
<point in="0.629" out="0.235"/>
<point in="0.895" out="0.629"/>
</nonlinearity_curve>
</flight_move_pitch>
*/
m_class = type;
m_devInstanceNo = instance;
m_option = option.Name.LocalName;
2014-10-26 22:11:57 +00:00
// derive from flight_move_pitch || flight_move_yaw || flight_move_roll (nothing bad should arrive here)
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
string invert = (string)option.Attribute( "invert" );
if ( !string.IsNullOrEmpty(invert) ) {
InvertUsed = false;
if ( invert == "1" ) InvertUsed = true;
}
2014-10-26 22:11:57 +00:00
string exponent = (string)option.Attribute( "exponent" );
if ( !string.IsNullOrEmpty( exponent ) ) {
Exponent = RoundString( exponent );
ExponentUsed = true;
2014-10-26 22:11:57 +00:00
}
2014-10-26 22:11:57 +00:00
// we may have a nonlin curve...
XElement nlc = option.Element( "nonlinearity_curve" );
if ( nlc != null ) {
m_PtsIn.Clear( ); m_PtsOut.Clear( ); // reset pts
IEnumerable<XElement> points = from x in nlc.Elements( )
where ( x.Name == "point" )
select x;
foreach ( XElement point in points ) {
string ptIn = RoundString( (string)point.Attribute( "in" ) );
string ptOut = RoundString( (string)point.Attribute( "out" ) );
m_PtsIn.Add( ptIn ); m_PtsOut.Add( ptOut ); m_ptsEnabled = true;
2014-10-26 22:11:57 +00:00
}
}
// sanity check - we've have to have 3 pts here - else we subst
// add 2nd
if ( m_PtsIn.Count < 2 ) {
m_PtsIn.Add( "0.500" ); m_PtsOut.Add( "0.500" );
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.750" ); m_PtsOut.Add( "0.750" );
log.Info( "Options_fromXML: got only two nonlin points, added (0.75|0.75)" );
}
2014-10-26 22:11:57 +00:00
return true;
}
}
}