Files
Torch/Torch.Server/Views/Converters/DefinitionToIdConverter.cs
Westin Miller c188367749 Observable collection base code for those without a true backing collection.
Observable sorted dictionary
Grid view now displays blocks grouped by subtype.
Null propagation in entity view models because WPF.
2017-12-04 23:52:03 -08:00

40 lines
1.4 KiB
C#

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using Sandbox.Definitions;
using VRage.Game;
namespace Torch.Server.Views.Converters
{
public class DefinitionToIdConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// ReSharper disable once PossibleNullReferenceException
MyDefinitionId id = ((MyDefinitionBase) value).Id;
string typeName = id.TypeId.ToString();
if (typeName.StartsWith("MyObjectBuilder_"))
typeName = typeName.Substring("MyObjectBuilder_".Length);
string subtype = id.SubtypeName;
return string.IsNullOrWhiteSpace(subtype) ? typeName : $"{typeName}: {subtype}";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
// ReSharper disable once PossibleNullReferenceException
string[] parts = value.ToString().Split(':');
Type type;
try
{
type = Type.GetType(parts[0]);
}
catch
{
type = Type.GetType("MyObjectBuilder_" + parts[0]);
}
return MyDefinitionManager.Static.GetDefinition(
new MyDefinitionId(type, parts.Length > 1 ? parts[1].Trim() : ""));
}
}
}