ObjectProperties support enums, added a dropdown for enum properties to item editing UI.

This commit is contained in:
Joonas Rikkonen
2017-10-04 20:07:57 +03:00
parent 1ff2054ca8
commit 3aaf5cbae2
2 changed files with 41 additions and 10 deletions
@@ -72,8 +72,7 @@ namespace Barotrauma
}
else if (property.PropertyType == typeof(float))
{
float floatVal = 0.0f;
float floatVal;
if (float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out floatVal))
{
propertyInfo.SetValue(obj, floatVal, null);
@@ -81,16 +80,30 @@ namespace Barotrauma
}
else if (property.PropertyType == typeof(bool))
{
propertyInfo.SetValue(obj, (value.ToLowerInvariant() == "true"), null);
propertyInfo.SetValue(obj, value.ToLowerInvariant() == "true", null);
}
else if (property.PropertyType == typeof(int))
{
int intVal = 0;
int intVal;
if (int.TryParse(value, out intVal))
{
propertyInfo.SetValue(obj, intVal, null);
}
}
else if (property.PropertyType.IsEnum)
{
object enumVal;
try
{
enumVal = Enum.Parse(propertyInfo.PropertyType, value);
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value + " (not a valid " + propertyInfo.PropertyType + ")", e);
return false;
}
propertyInfo.SetValue(obj, enumVal);
}
else
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj + "\" to " + value);
@@ -105,12 +118,12 @@ namespace Barotrauma
public bool TrySetValue(object value)
{
if (value == null) return false;
if (property.PropertyType!= value.GetType())
{
DebugConsole.ThrowError("Failed to set the value of the property \""+Name+"\" of \""+obj.ToString()+"\" to "+value.ToString());
DebugConsole.ThrowError("(Non-matching type, should be "+property.PropertyType+" instead of " +value.GetType()+")");
if (property.PropertyType != value.GetType())
{
DebugConsole.ThrowError("Failed to set the value of the property \"" + Name + "\" of \"" + obj.ToString() + "\" to " + value.ToString());
DebugConsole.ThrowError("(Non-matching type, should be " + property.PropertyType + " instead of " + value.GetType() + ")");
return false;
}