Replaced manual rect parsing in a bunch of places with an extension method

This commit is contained in:
Joonas Rikkonen
2017-11-14 21:09:07 +02:00
parent 913a102591
commit d072713936
6 changed files with 24 additions and 53 deletions
@@ -176,7 +176,7 @@ namespace Barotrauma
propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value));
break;
case "rectangle":
propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value));
propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value, true));
break;
}
@@ -213,6 +213,15 @@ namespace Barotrauma
return ParseVector4(val);
}
public static Rectangle GetAttributeRect(this XElement element, string name, Rectangle defaultValue)
{
if (element == null || element.Attribute(name) == null) return defaultValue;
string val = element.Attribute(name).Value;
return ParseRect(val, false);
}
public static string ElementInnerText(this XElement el)
{
StringBuilder str = new StringBuilder();
@@ -337,13 +346,13 @@ namespace Barotrauma
return new Color(components[0], components[1], components[2], components[3]);
}
public static Rectangle ParseRect(string stringColor, bool errorMessages = true)
public static Rectangle ParseRect(string stringRect, bool requireSize, bool errorMessages = true)
{
string[] strComponents = stringColor.Split(',');
if (strComponents.Length < 3)
string[] strComponents = stringRect.Split(',');
if ((strComponents.Length < 3 && requireSize) || strComponents.Length < 2)
{
if (errorMessages) DebugConsole.ThrowError("Failed to parse the string \"" + stringColor + "\" to Color");
if (errorMessages) DebugConsole.ThrowError("Failed to parse the string \"" + stringRect + "\" to Rectangle");
return new Rectangle(0, 0, 0, 0);
}