Files
SabreTools/SabreTools.DatItems/Control.cs

364 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2020-09-02 23:02:06 -07:00
using System.Linq;
2020-09-07 22:00:02 -07:00
using System.Xml.Serialization;
2020-09-02 23:02:06 -07:00
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-09-02 23:02:06 -07:00
using Newtonsoft.Json;
2020-09-07 22:00:02 -07:00
using Newtonsoft.Json.Converters;
2020-09-02 23:02:06 -07:00
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
2020-09-02 23:02:06 -07:00
{
/// <summary>
/// Represents control for an input
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("control"), XmlRoot("control")]
2020-09-02 23:02:06 -07:00
public class Control : DatItem
{
#region Fields
/// <summary>
/// General type of input
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[JsonConverter(typeof(StringEnumConverter))]
[XmlElement("type")]
public ControlType ControlType { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ControlTypeSpecified { get { return ControlType != ControlType.NULL; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Player which the input belongs to
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("player", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("player")]
public long? Player { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool PlayerSpecified { get { return Player != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Total number of buttons
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("buttons", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("buttons")]
public long? Buttons { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ButtonsSpecified { get { return Buttons != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Total number of non-optional buttons
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("reqbuttons", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("reqbuttons")]
public long? RequiredButtons { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool RequiredButtonsSpecified { get { return RequiredButtons != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Analog minimum value
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("minimum")]
public long? Minimum { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool MinimumSpecified { get { return Minimum != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Analog maximum value
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("maximum")]
public long? Maximum { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool MaximumSpecified { get { return Maximum != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Default analog sensitivity
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("sensitivity", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("sensitivity")]
public long? Sensitivity { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool SensitivitySpecified { get { return Sensitivity != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Default analog keydelta
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("keydelta", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:00:02 -07:00
[XmlElement("keydelta")]
public long? KeyDelta { get; set; }
2020-09-02 23:02:06 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool KeyDeltaSpecified { get { return KeyDelta != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Default analog reverse setting
2020-09-02 23:02:06 -07:00
/// </summary>
[JsonProperty("reverse", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:19:37 -07:00
[XmlElement("reverse")]
2020-09-02 23:02:06 -07:00
public bool? Reverse { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ReverseSpecified { get { return Reverse != null; } }
2020-09-02 23:02:06 -07:00
/// <summary>
/// First set of ways
/// </summary>
[JsonProperty("ways", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:19:37 -07:00
[XmlElement("ways")]
public string Ways { get; set; }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Second set of ways
/// </summary>
[JsonProperty("ways2", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:19:37 -07:00
[XmlElement("ways2")]
public string Ways2 { get; set; }
2020-09-02 23:02:06 -07:00
/// <summary>
/// Third set of ways
/// </summary>
[JsonProperty("ways3", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 22:19:37 -07:00
[XmlElement("ways3")]
public string Ways3 { get; set; }
2020-09-02 23:02:06 -07:00
#endregion
#region Accessors
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void SetFields(
Dictionary<DatItemField, string> datItemMappings,
Dictionary<MachineField, string> machineMappings)
2020-09-02 23:02:06 -07:00
{
// Set base fields
2020-12-13 13:22:06 -08:00
base.SetFields(datItemMappings, machineMappings);
2020-09-02 23:02:06 -07:00
// Handle Control-specific fields
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Type))
ControlType = datItemMappings[DatItemField.Control_Type].AsControlType();
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Player))
Player = Utilities.CleanLong(datItemMappings[DatItemField.Control_Player]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Buttons))
Buttons = Utilities.CleanLong(datItemMappings[DatItemField.Control_Buttons]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_RequiredButtons))
RequiredButtons = Utilities.CleanLong(datItemMappings[DatItemField.Control_RequiredButtons]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Minimum))
Minimum = Utilities.CleanLong(datItemMappings[DatItemField.Control_Minimum]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Maximum))
Maximum = Utilities.CleanLong(datItemMappings[DatItemField.Control_Maximum]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Sensitivity))
Sensitivity = Utilities.CleanLong(datItemMappings[DatItemField.Control_Sensitivity]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_KeyDelta))
KeyDelta = Utilities.CleanLong(datItemMappings[DatItemField.Control_KeyDelta]);
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Reverse))
Reverse = datItemMappings[DatItemField.Control_Reverse].AsYesNo();
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Ways))
Ways = datItemMappings[DatItemField.Control_Ways];
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Ways2))
Ways2 = datItemMappings[DatItemField.Control_Ways2];
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemMappings.Keys.Contains(DatItemField.Control_Ways3))
Ways3 = datItemMappings[DatItemField.Control_Ways3];
2020-09-02 23:02:06 -07:00
}
#endregion
#region Constructors
/// <summary>
/// Create a default, empty Control object
/// </summary>
public Control()
{
ItemType = ItemType.Control;
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Control()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
ControlType = this.ControlType,
Player = this.Player,
Buttons = this.Buttons,
RequiredButtons = this.RequiredButtons,
2020-09-02 23:02:06 -07:00
Minimum = this.Minimum,
Maximum = this.Maximum,
Sensitivity = this.Sensitivity,
KeyDelta = this.KeyDelta,
Reverse = this.Reverse,
Ways = this.Ways,
Ways2 = this.Ways2,
Ways3 = this.Ways3,
};
}
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
// If we don't have a Control, return false
if (ItemType != other.ItemType)
return false;
// Otherwise, treat it as a Control
Control newOther = other as Control;
// If the Control information matches
return (ControlType == newOther.ControlType
&& Player == newOther.Player
&& Buttons == newOther.Buttons
&& RequiredButtons == newOther.RequiredButtons
2020-09-02 23:02:06 -07:00
&& Minimum == newOther.Minimum
&& Maximum == newOther.Maximum
&& Sensitivity == newOther.Sensitivity
&& KeyDelta == newOther.KeyDelta
&& Reverse == newOther.Reverse
&& Ways == newOther.Ways
&& Ways2 == newOther.Ways2
&& Ways3 == newOther.Ways3);
}
#endregion
#region Filtering
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void RemoveFields(
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 23:02:06 -07:00
{
// Remove common fields first
2020-12-13 13:22:06 -08:00
base.RemoveFields(datItemFields, machineFields);
2020-09-02 23:02:06 -07:00
// Remove the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Type))
ControlType = ControlType.NULL;
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Player))
2020-09-02 23:02:06 -07:00
Player = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Buttons))
2020-09-02 23:02:06 -07:00
Buttons = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_RequiredButtons))
RequiredButtons = null;
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Minimum))
2020-09-02 23:02:06 -07:00
Minimum = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Maximum))
2020-09-02 23:02:06 -07:00
Maximum = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Sensitivity))
2020-09-02 23:02:06 -07:00
Sensitivity = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_KeyDelta))
2020-09-02 23:02:06 -07:00
KeyDelta = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Reverse))
2020-09-02 23:02:06 -07:00
Reverse = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways))
2020-09-02 23:02:06 -07:00
Ways = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways2))
2020-09-02 23:02:06 -07:00
Ways2 = null;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways3))
2020-09-02 23:02:06 -07:00
Ways3 = null;
}
#endregion
#region Sorting and Merging
2020-12-13 13:22:06 -08:00
/// <inheritdoc/>
public override void ReplaceFields(
DatItem item,
List<DatItemField> datItemFields,
List<MachineField> machineFields)
2020-09-02 23:02:06 -07:00
{
// Replace common fields first
2020-12-13 13:22:06 -08:00
base.ReplaceFields(item, datItemFields, machineFields);
2020-09-02 23:02:06 -07:00
// If we don't have a Control to replace from, ignore specific fields
if (item.ItemType != ItemType.Control)
return;
// Cast for easier access
Control newItem = item as Control;
// Replace the fields
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Type))
2020-09-02 23:02:06 -07:00
ControlType = newItem.ControlType;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Player))
2020-09-02 23:02:06 -07:00
Player = newItem.Player;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Buttons))
2020-09-02 23:02:06 -07:00
Buttons = newItem.Buttons;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_RequiredButtons))
RequiredButtons = newItem.RequiredButtons;
2020-09-02 23:02:06 -07:00
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Minimum))
2020-09-02 23:02:06 -07:00
Minimum = newItem.Minimum;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Maximum))
2020-09-02 23:02:06 -07:00
Maximum = newItem.Maximum;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Sensitivity))
2020-09-02 23:02:06 -07:00
Sensitivity = newItem.Sensitivity;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_KeyDelta))
2020-09-02 23:02:06 -07:00
KeyDelta = newItem.KeyDelta;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Reverse))
2020-09-02 23:02:06 -07:00
Reverse = newItem.Reverse;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways))
2020-09-02 23:02:06 -07:00
Ways = newItem.Ways;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways2))
2020-09-02 23:02:06 -07:00
Ways2 = newItem.Ways2;
2020-12-13 13:22:06 -08:00
if (datItemFields.Contains(DatItemField.Control_Ways3))
2020-09-02 23:02:06 -07:00
Ways3 = newItem.Ways3;
}
#endregion
}
}