Skip to content

Commit

Permalink
Show object type enum and description in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Rikux3 committed Mar 18, 2020
1 parent 36be7bd commit 38d8921
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
47 changes: 47 additions & 0 deletions OpenKh.Tools.ObjentryEditor/Converters/EnumDescriptionConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;

namespace OpenKh.Tools.ObjentryEditor.Converters
{
public class EnumDescriptionConverter : IValueConverter
{
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);

if (attribArray.Length == 0)
return enumObj.ToString();
else
{
DescriptionAttribute attrib = null;

foreach (var att in attribArray)
{
if (att is DescriptionAttribute)
attrib = att as DescriptionAttribute;
}

if (attrib != null)
return attrib.Description;

return enumObj.ToString();
}
}

object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
Enum myEnum = (Enum)value;
string description = GetEnumDescription(myEnum);
return description;
}

object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Empty;
}
}
}
10 changes: 9 additions & 1 deletion OpenKh.Tools.ObjentryEditor/ViewModels/ObjentryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ public ushort ObjectId
}

public ushort Unknown02 { get => Objentry.Unknown02; set => Objentry.Unknown02 = value; }
public Objentry.Type ObjectType { get => Objentry.ObjectType; set => Objentry.ObjectType = value; }
public Objentry.Type ObjectType
{
get => Objentry.ObjectType;
set
{
Objentry.ObjectType = value;
OnPropertyChanged(nameof(ObjectType));
}
}
public byte Unknown05 { get => Objentry.Unknown05; set => Objentry.Unknown05 = value; }
public byte Unknown06 { get => Objentry.Unknown06; set => Objentry.Unknown06 = value; }
public byte WeaponJoint { get => Objentry.WeaponJoint; set => Objentry.WeaponJoint = value; }
Expand Down
11 changes: 10 additions & 1 deletion OpenKh.Tools.ObjentryEditor/Views/ObjentryView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
xmlns:controls="clr-namespace:Xe.Tools.Wpf.Controls;assembly=Xe.Tools.Wpf"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:bhv="clr-namespace:OpenKh.Tools.ObjentryEditor.Behavior"
xmlns:conv="clr-namespace:OpenKh.Tools.ObjentryEditor.Converters"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="800">
<UserControl.Resources>
<Thickness x:Key="LabelMargin" Left="0" Top="0" Right="5" Bottom="0" />
<conv:EnumDescriptionConverter x:Key="EnumDescConverter" />
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
Expand Down Expand Up @@ -76,7 +78,14 @@
<TextBox Text="{Binding SelectedItem.Unknown02, UpdateSourceTrigger=PropertyChanged}"/>

<TextBlock Text="Type" Margin="{StaticResource LabelMargin}"/>
<ComboBox ItemsSource="{Binding ObjEntryTypes}" SelectedValue="{Binding SelectedItem.ObjectType}" DisplayMemberPath="Name" SelectedValuePath="Value"/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0" ItemsSource="{Binding ObjEntryTypes}" SelectedValue="{Binding SelectedItem.ObjectType}" DisplayMemberPath="Name" SelectedValuePath="Value" Margin="{StaticResource LabelMargin}"/>
<TextBox Grid.Column="1" IsEnabled="False" Text="{Binding Path=SelectedItem.ObjectType, Converter={StaticResource EnumDescConverter}, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

<TextBlock Text="Unknown05" Margin="{StaticResource LabelMargin}"/>
<TextBox Text="{Binding SelectedItem.Unknown05, UpdateSourceTrigger=PropertyChanged}"/>
Expand Down

0 comments on commit 38d8921

Please sign in to comment.