Improve UI layout, add flags enum editor to PropertyGrid

This commit is contained in:
John Gross
2018-07-24 12:51:13 -07:00
parent 14e16a959f
commit 5bf91f1891
7 changed files with 221 additions and 26 deletions

View File

@@ -5,6 +5,7 @@ using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using Torch; using Torch;
using Torch.Collections; using Torch.Collections;
using Torch.Views;
using VRage.Game; using VRage.Game;
using VRage.Library.Utils; using VRage.Library.Utils;
using VRage.Serialization; using VRage.Serialization;
@@ -202,7 +203,7 @@ namespace Torch.Server.ViewModels
public bool EnableSpiders { get => _settings.EnableSpiders; set => SetValue(ref _settings.EnableSpiders, value); } public bool EnableSpiders { get => _settings.EnableSpiders; set => SetValue(ref _settings.EnableSpiders, value); }
[Torch.Views.Display(Name = "Block Type World Limits", GroupName = "Block Limits")] [Torch.Views.Display(Name = "Block Type World Limits", GroupName = "Block Limits")]
public SerializableDictionary<string, short> BlockTypeLimits; public Dictionary<string, short> BlockTypeLimits { get => _settings.BlockTypeLimits.Dictionary; set => SetValue(x => _settings.BlockTypeLimits.Dictionary = x, value); }
[Torch.Views.Display(Description = "Enables scripter role for administration.", Name = "Enable Scripter Role", GroupName = "Others")] [Torch.Views.Display(Description = "Enables scripter role for administration.", Name = "Enable Scripter Role", GroupName = "Others")]
public bool EnableScripterRole { get => _settings.EnableScripterRole; set => SetValue(ref _settings.EnableScripterRole, value); } public bool EnableScripterRole { get => _settings.EnableScripterRole; set => SetValue(ref _settings.EnableScripterRole, value); }
@@ -235,7 +236,7 @@ namespace Torch.Server.ViewModels
public bool TrashRemovalEnabled { get => _settings.TrashRemovalEnabled; set => SetValue(ref _settings.TrashRemovalEnabled, value); } public bool TrashRemovalEnabled { get => _settings.TrashRemovalEnabled; set => SetValue(ref _settings.TrashRemovalEnabled, value); }
[Torch.Views.Display(Description = "Defines flags for trash removal system.", Name = "Trash Removal Flags", GroupName = "Trash Removal")] [Torch.Views.Display(Description = "Defines flags for trash removal system.", Name = "Trash Removal Flags", GroupName = "Trash Removal")]
public int TrashFlagsValue { get => _settings.TrashFlagsValue; set => SetValue(ref _settings.TrashFlagsValue, value); } public MyTrashRemovalFlags TrashFlagsValue { get => (MyTrashRemovalFlags)_settings.TrashFlagsValue; set => SetValue(ref _settings.TrashFlagsValue, (int)value); }
[Torch.Views.Display(Description = "Defines block count threshold for trash removal system.", Name = "Block Count Threshold", GroupName = "Trash Removal")] [Torch.Views.Display(Description = "Defines block count threshold for trash removal system.", Name = "Block Count Threshold", GroupName = "Trash Removal")]
public int BlockCountThreshold { get => _settings.BlockCountThreshold; set => SetValue(ref _settings.BlockCountThreshold, value); } public int BlockCountThreshold { get => _settings.BlockCountThreshold; set => SetValue(ref _settings.BlockCountThreshold, value); }

View File

@@ -26,7 +26,7 @@
</Grid.RowDefinitions> </Grid.RowDefinitions>
<DockPanel Grid.Row="0"> <DockPanel Grid.Row="0">
<Label Content="World:" DockPanel.Dock="Left" /> <Label Content="World:" DockPanel.Dock="Left" />
<Button Content="Import World Config" Margin="3" DockPanel.Dock="Right" Click="ImportConfig_OnClick" ToolTip="Override the DS config with the one from the selected world." IsEnabled="{Binding ElementName=WorldList, Path=Items.Count, Mode=OneWay}"/> <Button Content="Import World Config" Margin="3" Padding="3" DockPanel.Dock="Right" Click="ImportConfig_OnClick" ToolTip="Override the DS config with the one from the selected world." IsEnabled="{Binding ElementName=WorldList, Path=Items.Count, Mode=OneWay}"/>
<ComboBox x:Name="WorldList" ItemsSource="{Binding Worlds}" SelectedItem="{Binding SelectedWorld}" Margin="3" <ComboBox x:Name="WorldList" ItemsSource="{Binding Worlds}" SelectedItem="{Binding SelectedWorld}" Margin="3"
SelectionChanged="Selector_OnSelectionChanged"> SelectionChanged="Selector_OnSelectionChanged">
<ComboBox.ItemTemplate> <ComboBox.ItemTemplate>
@@ -49,16 +49,20 @@
</DockPanel> </DockPanel>
<Grid Grid.Row="1"> <Grid Grid.Row="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" /> <ColumnDefinition Width="7*" />
<ColumnDefinition Width="2*" /> <ColumnDefinition Width="10*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid Grid.Column="0"> <Grid Grid.Column="0">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition /> <RowDefinition />
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />
</Grid.RowDefinitions> </Grid.RowDefinitions>
<StackPanel Orientation="Horizontal"> <Grid>
<StackPanel Margin="3" DockPanel.Dock="Left"> <Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="3" DockPanel.Dock="Left">
<Label Content="Server Name" /> <Label Content="Server Name" />
<TextBox Text="{Binding ServerName}" Margin="3,0,3,3" Width="160" /> <TextBox Text="{Binding ServerName}" Margin="3,0,3,3" Width="160" />
<Label Content="World Name" /> <Label Content="World Name" />
@@ -82,7 +86,7 @@
</StackPanel> </StackPanel>
<CheckBox IsChecked="{Binding PauseGameWhenEmpty}" Content="Pause When Empty" Margin="3" /> <CheckBox IsChecked="{Binding PauseGameWhenEmpty}" Content="Pause When Empty" Margin="3" />
</StackPanel> </StackPanel>
<StackPanel Margin="3"> <StackPanel Grid.Column="1" Margin="3">
<Label Content="Mods" /> <Label Content="Mods" />
<TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto" <TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto"
Style="{StaticResource ValidatedTextBox}"> Style="{StaticResource ValidatedTextBox}">
@@ -114,7 +118,7 @@
</TextBox.Text> </TextBox.Text>
</TextBox> </TextBox>
</StackPanel> </StackPanel>
</StackPanel> </Grid>
<Button Grid.Row="1" Content="Save Config" Margin="3" Click="Save_OnClick" /> <Button Grid.Row="1" Content="Save Config" Margin="3" Click="Save_OnClick" />
</Grid> </Grid>
<views:PropertyGrid Grid.Column="1" Margin="3" DataContext="{Binding SessionSettings}" IgnoreDisplay ="True" /> <views:PropertyGrid Grid.Column="1" Margin="3" DataContext="{Binding SessionSettings}" IgnoreDisplay ="True" />

View File

@@ -273,6 +273,9 @@
<Compile Include="Views\EmbeddedCollectionEditor.xaml.cs"> <Compile Include="Views\EmbeddedCollectionEditor.xaml.cs">
<DependentUpon>EmbeddedCollectionEditor.xaml</DependentUpon> <DependentUpon>EmbeddedCollectionEditor.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Views\FlagsEditor.xaml.cs">
<DependentUpon>FlagsEditor.xaml</DependentUpon>
</Compile>
<Compile Include="Views\ObjectCollectionEditor.xaml.cs"> <Compile Include="Views\ObjectCollectionEditor.xaml.cs">
<DependentUpon>ObjectCollectionEditor.xaml</DependentUpon> <DependentUpon>ObjectCollectionEditor.xaml</DependentUpon>
</Compile> </Compile>
@@ -307,6 +310,10 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>
</Page> </Page>
<Page Include="Views\FlagsEditor.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Views\ObjectCollectionEditor.xaml"> <Page Include="Views\ObjectCollectionEditor.xaml">
<SubType>Designer</SubType> <SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator> <Generator>MSBuild:Compile</Generator>

View File

@@ -0,0 +1,30 @@
<Window x:Class="Torch.Views.FlagsEditorDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Flag Editor" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl x:Name="Items">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="Cancel" Margin="5" Click="Cancel_OnClick" />
<Button Grid.Column="1" Content="OK" Margin="5" Click="Ok_OnClick" />
</Grid>
</Grid>
</Window>

View File

@@ -0,0 +1,85 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using NLog;
using Torch.Collections;
namespace Torch.Views
{
/// <summary>
/// Interaction logic for FlagsEditorDialog.xaml
/// </summary>
public partial class FlagsEditorDialog : Window
{
public FlagsEditorDialog()
{
InitializeComponent();
}
private List<Flag> _flags;
private PropertyInfo _property;
private object _obj;
public void EditEnum(PropertyInfo prop, object obj)
{
if (!prop.PropertyType.IsEnum || prop.PropertyType.GetCustomAttribute<FlagsAttribute>() == null)
throw new ArgumentException("Type is not a flags enum");
_property = prop;
_obj = obj;
_flags = new List<Flag>();
var initial = (int)Convert.ChangeType(prop.GetValue(obj), typeof(int));
foreach (var value in Enum.GetValues(prop.PropertyType))
{
var val = (int)Convert.ChangeType(value, typeof(int));
_flags.Add(new Flag
{
Name = Enum.GetName(prop.PropertyType, value),
Value = val,
IsChecked = (initial & val) > 0
});
}
Items.ItemsSource = _flags;
ShowDialog();
}
private void Cancel_OnClick(object sender, RoutedEventArgs e)
{
Close();
}
private void Ok_OnClick(object sender, RoutedEventArgs e)
{
var final = 0;
foreach (var item in _flags)
{
if (item.IsChecked)
final |= item.Value;
}
_property.SetValue(_obj, Enum.ToObject(_property.PropertyType, final));
Close();
}
private class Flag
{
public bool IsChecked { get; set; }
public string Name { get; set; }
public int Value { get; set; }
}
}
}

View File

@@ -9,7 +9,7 @@
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/>
<RowDefinition/> <RowDefinition/>
<RowDefinition Height="Auto"/> <RowDefinition Height="52px"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid Grid.Row="0"> <Grid Grid.Row="0">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@@ -20,6 +20,8 @@
<TextBox Name="TbFilter" Grid.Column="1" Margin="3" TextChanged="UpdateFilter" IsEnabled="False"/> <TextBox Name="TbFilter" Grid.Column="1" Margin="3" TextChanged="UpdateFilter" IsEnabled="False"/>
</Grid> </Grid>
<ScrollViewer Grid.Row="1" x:Name="ScrollViewer"/> <ScrollViewer Grid.Row="1" x:Name="ScrollViewer"/>
<TextBlock x:Name="TbDescription" Grid.Row="2" MinHeight="18" TextWrapping="Wrap"/> <TextBlock x:Name="TbDescription" Grid.Row="2" TextWrapping="Wrap" Background="DarkGray" Padding="2"/>
<GridSplitter Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top" ShowsPreview="True"
Height="2" />
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -67,6 +67,7 @@ namespace Torch.Views
var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public); var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
var grid = new Grid(); var grid = new Grid();
grid.MouseMove += Grid_MouseMove;
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
@@ -123,7 +124,8 @@ namespace Torch.Views
if (property.GetGetMethod() == null) if (property.GetGetMethod() == null)
continue; continue;
grid.RowDefinitions.Add(new RowDefinition()); var def = new RowDefinition();
grid.RowDefinitions.Add(def);
var descriptor = descriptors[property]; var descriptor = descriptors[property];
var displayName = descriptor?.Name; var displayName = descriptor?.Name;
@@ -138,8 +140,7 @@ namespace Torch.Views
text.SetValue(Grid.ColumnProperty, 0); text.SetValue(Grid.ColumnProperty, 0);
text.SetValue(Grid.RowProperty, curRow); text.SetValue(Grid.RowProperty, curRow);
text.Margin = new Thickness(3); text.Margin = new Thickness(3);
text.Tag = $"{text.Text}: {descriptor?.Description}"; def.Tag = new Tuple<string, string>(text.Text, descriptor?.Description);
text.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged;
//if (descriptor?.Enabled == false) //if (descriptor?.Enabled == false)
// text.IsEnabled = false; // text.IsEnabled = false;
grid.Children.Add(text); grid.Children.Add(text);
@@ -166,11 +167,27 @@ namespace Torch.Views
} }
else if (propertyType.IsEnum) else if (propertyType.IsEnum)
{ {
valueControl = new ComboBox var isFlags = propertyType.GetCustomAttribute<FlagsAttribute>() != null;
{
ItemsSource = Enum.GetValues(property.PropertyType) if (isFlags)
}; {
valueControl.SetBinding(ComboBox.SelectedItemProperty, property.Name); var button = new Button
{
Content = "Edit Flags"
};
button.SetBinding(Button.DataContextProperty, property.Name);
button.Click += EditFlags;
valueControl = button;
}
else
{
valueControl = new ComboBox
{
ItemsSource = Enum.GetValues(property.PropertyType)
};
valueControl.SetBinding(ComboBox.SelectedItemProperty, property.Name);
}
} }
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>)) else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{ {
@@ -246,7 +263,6 @@ namespace Torch.Views
valueControl.VerticalAlignment = VerticalAlignment.Center; valueControl.VerticalAlignment = VerticalAlignment.Center;
valueControl.SetValue(Grid.ColumnProperty, 1); valueControl.SetValue(Grid.ColumnProperty, 1);
valueControl.SetValue(Grid.RowProperty, curRow); valueControl.SetValue(Grid.RowProperty, curRow);
valueControl.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged;
if (descriptor?.Enabled == false) if (descriptor?.Enabled == false)
valueControl.IsEnabled = false; valueControl.IsEnabled = false;
grid.Children.Add(valueControl); grid.Children.Add(valueControl);
@@ -259,32 +275,82 @@ namespace Torch.Views
return grid; return grid;
} }
private void Text_IsMouseDirectlyOverChanged(object sender, DependencyPropertyChangedEventArgs e) private int _lastActiveRow;
private void Grid_MouseMove(object sender, MouseEventArgs e)
{ {
TbDescription.Text = (sender as FrameworkElement)?.Tag?.ToString() ?? string.Empty; var grid = (Grid)sender;
var mousePoint = e.GetPosition(grid);
var heightSum = grid.RowDefinitions[0].ActualHeight;
var activeRow = 0;
while (heightSum < mousePoint.Y && activeRow < grid.RowDefinitions.Count)
{
heightSum += grid.RowDefinitions[activeRow].ActualHeight;
activeRow++;
}
if (activeRow > grid.RowDefinitions.Count - 1 || activeRow == _lastActiveRow)
return;
_lastActiveRow = activeRow;
var tag = (Tuple<string, string>)grid.RowDefinitions[activeRow].Tag;
TbDescription.Inlines.Clear();
TbDescription.Inlines.Add(new Run(tag?.Item1 ?? "?") {FontWeight = FontWeights.Bold});
TbDescription.Inlines.Add(new Run($"{Environment.NewLine}{tag?.Item2 ?? "No description."}"));
}
private void EditFlags(object sender, RoutedEventArgs e)
{
var btn = (Button)sender;
var obj = DataContext;
var propName = btn.GetBindingExpression(DataContextProperty).ParentBinding.Path.Path;
var propInfo = DataContext.GetType().GetProperty(propName);
new FlagsEditorDialog
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Window.GetWindow(this)
}.EditEnum(propInfo, obj);
} }
private void EditDictionary(object dict) private void EditDictionary(object dict)
{ {
var dic = (IDictionary)dict; var dic = (IDictionary)dict;
new DictionaryEditorDialog().Edit(dic); new DictionaryEditorDialog
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Window.GetWindow(this)
}.Edit(dic);
} }
private void EditPrimitiveCollection(object collection, string title = "Collection Editor") private void EditPrimitiveCollection(object collection, string title = "Collection Editor")
{ {
var c = (ICollection)collection; var c = (ICollection)collection;
new CollectionEditor().Edit(c, title); new CollectionEditor
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Window.GetWindow(this)
}.Edit(c, title);
} }
private void EditObjectCollection(object collection, string title = "Collection Editor") private void EditObjectCollection(object collection, string title = "Collection Editor")
{ {
var c = (ICollection)collection; var c = (ICollection)collection;
new ObjectCollectionEditor().Edit(c, title); new ObjectCollectionEditor
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Window.GetWindow(this)
}.Edit(c, title);
} }
private void EditObject(object o, string title = "Edit Object") private void EditObject(object o, string title = "Edit Object")
{ {
new ObjectEditor().Edit(o, title); new ObjectEditor
{
WindowStartupLocation = WindowStartupLocation.CenterOwner,
Owner = Window.GetWindow(this)
}.Edit(o, title);
} }
private void UpdateFilter(object sender, TextChangedEventArgs e) private void UpdateFilter(object sender, TextChangedEventArgs e)