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
@@ -1591,29 +1591,11 @@ namespace Barotrauma
} }
public static void Load(XElement element, Submarine submarine) public static void Load(XElement element, Submarine submarine)
{ {
string rectString = element.GetAttributeString("rect", "0,0,0,0"); Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
string[] rectValues = rectString.Split(',');
Rectangle rect = Rectangle.Empty;
if (rectValues.Length==4)
{
rect = new Rectangle(
int.Parse(rectValues[0]),
int.Parse(rectValues[1]),
int.Parse(rectValues[2]),
int.Parse(rectValues[3]));
}
else
{
rect = new Rectangle(
int.Parse(rectValues[0]),
int.Parse(rectValues[1]),
0, 0);
}
string name = element.Attribute("name").Value; string name = element.Attribute("name").Value;
foreach (MapEntityPrefab ep in MapEntityPrefab.list) foreach (MapEntityPrefab ep in MapEntityPrefab.list)
{ {
ItemPrefab ip = ep as ItemPrefab; ItemPrefab ip = ep as ItemPrefab;
@@ -1621,7 +1603,7 @@ namespace Barotrauma
if (ip.Name != name && (ip.Aliases == null || !ip.Aliases.Contains(name))) continue; if (ip.Name != name && (ip.Aliases == null || !ip.Aliases.Contains(name))) continue;
if (rect.Width==0 && rect.Height==0) if (rect.Width == 0 && rect.Height == 0)
{ {
rect.Width = (int)ip.Size.X; rect.Width = (int)ip.Size.X;
rect.Height = (int)ip.Size.Y; rect.Height = (int)ip.Size.Y;
@@ -654,17 +654,11 @@ namespace Barotrauma
if (element.Attribute("rect") != null) if (element.Attribute("rect") != null)
{ {
string rectString = element.GetAttributeString("rect", "0,0,0,0"); rect = element.GetAttributeRect("rect", Rectangle.Empty);
string[] rectValues = rectString.Split(',');
rect = new Rectangle(
int.Parse(rectValues[0]),
int.Parse(rectValues[1]),
int.Parse(rectValues[2]),
int.Parse(rectValues[3]));
} }
else else
{ {
//backwards compatibility
rect = new Rectangle( rect = new Rectangle(
int.Parse(element.Attribute("x").Value), int.Parse(element.Attribute("x").Value),
int.Parse(element.Attribute("y").Value), int.Parse(element.Attribute("y").Value),
@@ -708,20 +708,13 @@ namespace Barotrauma
public static void Load(XElement element, Submarine submarine) public static void Load(XElement element, Submarine submarine)
{ {
Rectangle rect = Rectangle.Empty; Rectangle rect = Rectangle.Empty;
if (element.Attribute("rect") != null) if (element.Attribute("rect") != null)
{ {
string rectString = element.GetAttributeString("rect", "0,0,0,0"); rect = element.GetAttributeRect("rect", Rectangle.Empty);
string[] rectValues = rectString.Split(',');
rect = new Rectangle(
int.Parse(rectValues[0]),
int.Parse(rectValues[1]),
int.Parse(rectValues[2]),
int.Parse(rectValues[3]));
} }
else else
{ {
//backwards compatibility
rect = new Rectangle( rect = new Rectangle(
int.Parse(element.Attribute("x").Value), int.Parse(element.Attribute("x").Value),
int.Parse(element.Attribute("y").Value), int.Parse(element.Attribute("y").Value),
@@ -824,14 +824,7 @@ namespace Barotrauma
public static void Load(XElement element, Submarine submarine) public static void Load(XElement element, Submarine submarine)
{ {
string rectString = element.GetAttributeString("rect", "0,0,0,0"); Rectangle rect = element.GetAttributeRect("rect", Rectangle.Empty);
string[] rectValues = rectString.Split(',');
Rectangle rect = new Rectangle(
int.Parse(rectValues[0]),
int.Parse(rectValues[1]),
int.Parse(rectValues[2]),
int.Parse(rectValues[3]));
string name = element.Attribute("name").Value; string name = element.Attribute("name").Value;
@@ -176,7 +176,7 @@ namespace Barotrauma
propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value)); propertyInfo.SetValue(obj, XMLExtensions.ParseColor(value));
break; break;
case "rectangle": case "rectangle":
propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value)); propertyInfo.SetValue(obj, XMLExtensions.ParseRect(value, true));
break; break;
} }
@@ -213,6 +213,15 @@ namespace Barotrauma
return ParseVector4(val); 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) public static string ElementInnerText(this XElement el)
{ {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
@@ -337,13 +346,13 @@ namespace Barotrauma
return new Color(components[0], components[1], components[2], components[3]); 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(','); string[] strComponents = stringRect.Split(',');
if (strComponents.Length < 3) 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); return new Rectangle(0, 0, 0, 0);
} }