From 3aaf5cbae25cc5765805f6e835b1d8659da9cb3e Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 4 Oct 2017 20:07:57 +0300 Subject: [PATCH] ObjectProperties support enums, added a dropdown for enum properties to item editing UI. --- .../BarotraumaClient/Source/Items/Item.cs | 20 +++++++++++- .../BarotraumaShared/Source/Properties.cs | 31 +++++++++++++------ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/Items/Item.cs b/Barotrauma/BarotraumaClient/Source/Items/Item.cs index ab3ad9eea..597c80af1 100644 --- a/Barotrauma/BarotraumaClient/Source/Items/Item.cs +++ b/Barotrauma/BarotraumaClient/Source/Items/Item.cs @@ -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); diff --git a/Barotrauma/BarotraumaShared/Source/Properties.cs b/Barotrauma/BarotraumaShared/Source/Properties.cs index 076da9f4f..9cea99346 100644 --- a/Barotrauma/BarotraumaShared/Source/Properties.cs +++ b/Barotrauma/BarotraumaShared/Source/Properties.cs @@ -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; }