2019-01-30 06:36:56 +00:00
|
|
|
|
using System.Xml;
|
2019-02-01 17:19:20 +00:00
|
|
|
|
using RobloxFiles.DataTypes;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2021-02-25 20:09:54 +00:00
|
|
|
|
namespace RobloxFiles.Tokens
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public class Vector2Token : IXmlPropertyToken, IAttributeToken<Vector2>
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public string XmlPropertyToken => "Vector2";
|
|
|
|
|
private static readonly string[] XmlCoords = new string[2] { "X", "Y" };
|
|
|
|
|
|
|
|
|
|
public AttributeType AttributeType => AttributeType.Vector2;
|
|
|
|
|
public Vector2 ReadAttribute(Attribute attr) => ReadVector2(attr);
|
|
|
|
|
public void WriteAttribute(Attribute attr, Vector2 value) => WriteVector2(attr, value);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
2019-02-01 18:40:39 +00:00
|
|
|
|
public static Vector2 ReadVector2(XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
|
|
|
|
float[] xy = new float[2];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
{
|
2021-02-25 20:09:54 +00:00
|
|
|
|
string key = XmlCoords[i];
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var coord = token[key];
|
|
|
|
|
string text = coord.InnerText;
|
2019-05-17 12:08:06 +00:00
|
|
|
|
xy[i] = Formatting.ParseFloat(text);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Vector2(xy);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public static void WriteVector2(XmlDocument doc, XmlNode node, Vector2 value)
|
|
|
|
|
{
|
|
|
|
|
XmlElement x = doc.CreateElement("X");
|
|
|
|
|
x.InnerText = value.X.ToInvariantString();
|
|
|
|
|
node.AppendChild(x);
|
|
|
|
|
|
|
|
|
|
XmlElement y = doc.CreateElement("Y");
|
|
|
|
|
y.InnerText = value.Y.ToInvariantString();
|
|
|
|
|
node.AppendChild(y);
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 20:09:54 +00:00
|
|
|
|
public static Vector2 ReadVector2(Attribute attr)
|
|
|
|
|
{
|
|
|
|
|
float x = attr.ReadFloat(),
|
|
|
|
|
y = attr.ReadFloat();
|
|
|
|
|
|
|
|
|
|
return new Vector2(x, y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void WriteVector2(Attribute attr, Vector2 value)
|
|
|
|
|
{
|
|
|
|
|
attr.WriteFloat(value.X);
|
|
|
|
|
attr.WriteFloat(value.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 12:08:06 +00:00
|
|
|
|
public bool ReadProperty(Property property, XmlNode token)
|
2019-01-30 06:36:56 +00:00
|
|
|
|
{
|
2019-02-01 18:40:39 +00:00
|
|
|
|
Vector2 result = ReadVector2(token);
|
|
|
|
|
bool success = (result != null);
|
2019-01-30 06:36:56 +00:00
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
property.Type = PropertyType.Vector2;
|
2019-02-01 18:40:39 +00:00
|
|
|
|
property.Value = result;
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return success;
|
|
|
|
|
}
|
2019-05-17 12:08:06 +00:00
|
|
|
|
|
|
|
|
|
public void WriteProperty(Property prop, XmlDocument doc, XmlNode node)
|
|
|
|
|
{
|
2019-06-30 22:01:19 +00:00
|
|
|
|
Vector2 value = prop.CastValue<Vector2>();
|
2019-05-17 12:08:06 +00:00
|
|
|
|
WriteVector2(doc, node, value);
|
|
|
|
|
}
|
2019-01-30 06:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|