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

View File

@@ -218,7 +218,7 @@ namespace Barotrauma
if (value is bool)
{
GUITickBox propertyTickBox = new GUITickBox(new Rectangle(10, y, 18, 18), objectProperty.Name,
GUITickBox propertyTickBox = new GUITickBox(new Rectangle(10, y, 18, boxHeight), objectProperty.Name,
Alignment.Left, editingHUD);
propertyTickBox.Font = GUI.SmallFont;
@@ -227,6 +227,24 @@ namespace Barotrauma
propertyTickBox.UserData = objectProperty;
propertyTickBox.OnSelected = EnterProperty;
}
else if (value.GetType().IsEnum)
{
new GUITextBlock(new Rectangle(0, y, 100, 18), objectProperty.Name, "", Alignment.TopLeft, Alignment.Left, editingHUD, false, GUI.SmallFont);
GUIDropDown enumDropDown = new GUIDropDown(new Rectangle(180, y, 250, boxHeight), "", "", editingHUD);
foreach (object enumValue in Enum.GetValues(value.GetType()))
{
var enumTextBlock = new GUITextBlock(new Rectangle(0, 0, 200, 25), enumValue.ToString(), "", enumDropDown);
enumTextBlock.UserData = enumValue;
}
enumDropDown.OnSelected += (selected, val) =>
{
objectProperty.TrySetValue(val);
return true;
};
enumDropDown.SelectItem(value);
}
else
{
new GUITextBlock(new Rectangle(0, y, 100, 18), objectProperty.Name, "", Alignment.TopLeft, Alignment.Left, editingHUD, false, GUI.SmallFont);

View File

@@ -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;
}