Files
SabreTools/SabreTools.Library/DatItems/Control.cs

431 lines
14 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
using SabreTools.Library.Filtering;
using SabreTools.Library.Tools;
using Newtonsoft.Json;
2020-09-07 22:00:02 -07:00
using Newtonsoft.Json.Converters;
2020-09-02 23:02:06 -07:00
namespace SabreTools.Library.DatItems
{
/// <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
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="mappings">Mappings dictionary</param>
public override void SetFields(Dictionary<Field, string> mappings)
{
// Set base fields
base.SetFields(mappings);
// Handle Control-specific fields
if (mappings.Keys.Contains(Field.DatItem_Control_Type))
ControlType = mappings[Field.DatItem_Control_Type].AsControlType();
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Player))
Player = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Player]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Buttons))
Buttons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Buttons]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_RequiredButtons))
RequiredButtons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_RequiredButtons]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Minimum))
Minimum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Minimum]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Maximum))
Maximum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Maximum]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Sensitivity))
Sensitivity = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Sensitivity]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_KeyDelta))
KeyDelta = Sanitizer.CleanLong(mappings[Field.DatItem_Control_KeyDelta]);
2020-09-02 23:02:06 -07:00
if (mappings.Keys.Contains(Field.DatItem_Control_Reverse))
Reverse = mappings[Field.DatItem_Control_Reverse].AsYesNo();
if (mappings.Keys.Contains(Field.DatItem_Control_Ways))
Ways = mappings[Field.DatItem_Control_Ways];
if (mappings.Keys.Contains(Field.DatItem_Control_Ways2))
Ways2 = mappings[Field.DatItem_Control_Ways2];
if (mappings.Keys.Contains(Field.DatItem_Control_Ways3))
Ways3 = mappings[Field.DatItem_Control_Ways3];
}
#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
/// <summary>
/// Check to see if a DatItem passes the filter
/// </summary>
/// <param name="filter">Filter to check against</param>
/// <returns>True if the item passed the filter, false otherwise</returns>
public override bool PassesFilter(Filter filter)
{
// Check common fields first
if (!base.PassesFilter(filter))
return false;
// Filter on control type
if (filter.DatItem_Control_Type.MatchesPositive(ControlType.NULL, ControlType) == false)
2020-09-02 23:02:06 -07:00
return false;
if (filter.DatItem_Control_Type.MatchesNegative(ControlType.NULL, ControlType) == true)
2020-09-02 23:02:06 -07:00
return false;
// Filter on player
if (!filter.PassLongFilter(filter.DatItem_Control_Player, Player))
2020-09-02 23:02:06 -07:00
return false;
// Filter on buttons
if (!filter.PassLongFilter(filter.DatItem_Control_Buttons, Buttons))
2020-09-02 23:02:06 -07:00
return false;
// Filter on reqbuttons
if (!filter.PassLongFilter(filter.DatItem_Control_ReqButtons, RequiredButtons))
2020-09-02 23:02:06 -07:00
return false;
// Filter on minimum
if (!filter.PassLongFilter(filter.DatItem_Control_Minimum, Minimum))
2020-09-02 23:02:06 -07:00
return false;
// Filter on maximum
if (!filter.PassLongFilter(filter.DatItem_Control_Maximum, Maximum))
2020-09-02 23:02:06 -07:00
return false;
// Filter on sensitivity
if (!filter.PassLongFilter(filter.DatItem_Control_Sensitivity, Sensitivity))
2020-09-02 23:02:06 -07:00
return false;
// Filter on keydelta
if (!filter.PassLongFilter(filter.DatItem_Control_KeyDelta, KeyDelta))
2020-09-02 23:02:06 -07:00
return false;
// Filter on reverse
if (!filter.PassBoolFilter(filter.DatItem_Control_Reverse, Reverse))
2020-09-02 23:02:06 -07:00
return false;
// Filter on ways
if (!filter.PassStringFilter(filter.DatItem_Control_Ways, Ways))
2020-09-02 23:02:06 -07:00
return false;
// Filter on ways2
if (!filter.PassStringFilter(filter.DatItem_Control_Ways2, Ways2))
2020-09-02 23:02:06 -07:00
return false;
// Filter on ways3
if (!filter.PassStringFilter(filter.DatItem_Control_Ways3, Ways3))
2020-09-02 23:02:06 -07:00
return false;
return true;
}
/// <summary>
/// Remove fields from the DatItem
/// </summary>
/// <param name="fields">List of Fields to remove</param>
public override void RemoveFields(List<Field> fields)
{
// Remove common fields first
base.RemoveFields(fields);
// Remove the fields
if (fields.Contains(Field.DatItem_Control_Type))
ControlType = ControlType.NULL;
2020-09-02 23:02:06 -07:00
if (fields.Contains(Field.DatItem_Control_Player))
Player = null;
if (fields.Contains(Field.DatItem_Control_Buttons))
Buttons = null;
if (fields.Contains(Field.DatItem_Control_RequiredButtons))
RequiredButtons = null;
2020-09-02 23:02:06 -07:00
if (fields.Contains(Field.DatItem_Control_Minimum))
Minimum = null;
if (fields.Contains(Field.DatItem_Control_Maximum))
Maximum = null;
if (fields.Contains(Field.DatItem_Control_Sensitivity))
Sensitivity = null;
if (fields.Contains(Field.DatItem_Control_KeyDelta))
KeyDelta = null;
if (fields.Contains(Field.DatItem_Control_Reverse))
Reverse = null;
if (fields.Contains(Field.DatItem_Control_Ways))
Ways = null;
if (fields.Contains(Field.DatItem_Control_Ways2))
Ways2 = null;
if (fields.Contains(Field.DatItem_Control_Ways3))
Ways3 = null;
}
#endregion
#region Sorting and Merging
/// <summary>
/// Replace fields from another item
/// </summary>
/// <param name="item">DatItem to pull new information from</param>
/// <param name="fields">List of Fields representing what should be updated</param>
public override void ReplaceFields(DatItem item, List<Field> fields)
{
// Replace common fields first
base.ReplaceFields(item, fields);
// 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
if (fields.Contains(Field.DatItem_Control_Type))
ControlType = newItem.ControlType;
if (fields.Contains(Field.DatItem_Control_Player))
Player = newItem.Player;
if (fields.Contains(Field.DatItem_Control_Buttons))
Buttons = newItem.Buttons;
if (fields.Contains(Field.DatItem_Control_RequiredButtons))
RequiredButtons = newItem.RequiredButtons;
2020-09-02 23:02:06 -07:00
if (fields.Contains(Field.DatItem_Control_Minimum))
Minimum = newItem.Minimum;
if (fields.Contains(Field.DatItem_Control_Maximum))
Maximum = newItem.Maximum;
if (fields.Contains(Field.DatItem_Control_Sensitivity))
Sensitivity = newItem.Sensitivity;
if (fields.Contains(Field.DatItem_Control_KeyDelta))
KeyDelta = newItem.KeyDelta;
if (fields.Contains(Field.DatItem_Control_Reverse))
Reverse = newItem.Reverse;
if (fields.Contains(Field.DatItem_Control_Ways))
Ways = newItem.Ways;
if (fields.Contains(Field.DatItem_Control_Ways2))
Ways2 = newItem.Ways2;
if (fields.Contains(Field.DatItem_Control_Ways3))
Ways3 = newItem.Ways3;
}
#endregion
}
}