Add block limit editor, various UI tweaks
This commit is contained in:
@@ -10,6 +10,9 @@ using NLog.Targets;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
/// <summary>
|
||||
/// NLog target that writes to a <see cref="FlowDocument"/>.
|
||||
/// </summary>
|
||||
[Target("flowDocument")]
|
||||
public sealed class FlowDocumentTarget : TargetWithLayout
|
||||
{
|
||||
|
@@ -86,8 +86,6 @@ quit";
|
||||
public void Run()
|
||||
{
|
||||
_server = new TorchServer(_config);
|
||||
try
|
||||
{
|
||||
var init = Task.Run(() => _server.Init());
|
||||
if (!_config.NoGui)
|
||||
{
|
||||
@@ -105,13 +103,6 @@ quit";
|
||||
_server.Start();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (_server.IsRunning)
|
||||
_server.Stop();
|
||||
_server.Destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private TorchConfig InitConfig()
|
||||
{
|
||||
|
@@ -1,20 +1,31 @@
|
||||
using NLog;
|
||||
using Sandbox;
|
||||
using Sandbox.Game.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
#region
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using NLog;
|
||||
using Sandbox;
|
||||
using Sandbox.Engine.Networking;
|
||||
using Sandbox.Game.Multiplayer;
|
||||
using Sandbox.Game.World;
|
||||
using Torch.API;
|
||||
using Torch.API.Managers;
|
||||
using Torch.API.Session;
|
||||
using Torch.Server.Managers;
|
||||
using Torch.Utils;
|
||||
using VRage.Game;
|
||||
using VRage;
|
||||
using VRage.Dedicated;
|
||||
using VRage.GameServices;
|
||||
using VRage.Steam;
|
||||
using Timer = System.Threading.Timer;
|
||||
|
||||
#endregion
|
||||
|
||||
#pragma warning disable 618
|
||||
|
||||
@@ -22,76 +33,14 @@ namespace Torch.Server
|
||||
{
|
||||
public class TorchServer : TorchBase, ITorchServer
|
||||
{
|
||||
//public MyConfigDedicated<MyObjectBuilder_SessionSettings> DedicatedConfig { get; set; }
|
||||
/// <inheritdoc />
|
||||
public float SimulationRatio
|
||||
{
|
||||
get => _simRatio;
|
||||
set
|
||||
{
|
||||
_simRatio = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TimeSpan ElapsedPlayTime
|
||||
{
|
||||
get => _elapsedPlayTime;
|
||||
set
|
||||
{
|
||||
_elapsedPlayTime = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Thread GameThread { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public ServerState State
|
||||
{
|
||||
get => _state;
|
||||
private set
|
||||
{
|
||||
_state = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsRunning
|
||||
{
|
||||
get => _isRunning;
|
||||
set
|
||||
{
|
||||
_isRunning = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private bool _canRun;
|
||||
public bool CanRun { get => _canRun; set => SetValue(ref _canRun, value); }
|
||||
|
||||
private bool _hasRun;
|
||||
|
||||
public event Action<ITorchServer> Initialized;
|
||||
|
||||
/// <inheritdoc />
|
||||
public InstanceManager DedicatedInstance { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string InstanceName => Config?.InstanceName;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string InstancePath => Config?.InstancePath;
|
||||
|
||||
private bool _isRunning;
|
||||
private ServerState _state;
|
||||
private TimeSpan _elapsedPlayTime;
|
||||
private bool _hasRun;
|
||||
private bool _isRunning;
|
||||
private float _simRatio;
|
||||
private Timer _watchdog;
|
||||
private ServerState _state;
|
||||
private Stopwatch _uptime;
|
||||
private Timer _watchdog;
|
||||
|
||||
/// <inheritdoc />
|
||||
public TorchServer(TorchConfig config = null)
|
||||
@@ -102,22 +51,50 @@ namespace Torch.Server
|
||||
Config = config ?? new TorchConfig();
|
||||
|
||||
var sessionManager = Managers.GetManager<ITorchSessionManager>();
|
||||
sessionManager.AddFactory((x) => new MultiplayerManagerDedicated(this));
|
||||
sessionManager.AddFactory(x => new MultiplayerManagerDedicated(this));
|
||||
}
|
||||
|
||||
//public MyConfigDedicated<MyObjectBuilder_SessionSettings> DedicatedConfig { get; set; }
|
||||
/// <inheritdoc />
|
||||
public float SimulationRatio { get => _simRatio; set => SetValue(ref _simRatio, value); }
|
||||
|
||||
/// <inheritdoc />
|
||||
public TimeSpan ElapsedPlayTime { get => _elapsedPlayTime; set => SetValue(ref _elapsedPlayTime, value); }
|
||||
|
||||
/// <inheritdoc />
|
||||
public Thread GameThread { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsRunning { get => _isRunning; set => SetValue(ref _isRunning, value); }
|
||||
|
||||
public bool CanRun { get => _canRun; set => SetValue(ref _canRun, value); }
|
||||
|
||||
/// <inheritdoc />
|
||||
public InstanceManager DedicatedInstance { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public string InstanceName => Config?.InstanceName;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override uint SteamAppId => 244850;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override string SteamAppName => "SpaceEngineersDedicated";
|
||||
|
||||
/// <inheritdoc />
|
||||
public ServerState State { get => _state; private set => SetValue(ref _state, value); }
|
||||
|
||||
public event Action<ITorchServer> Initialized;
|
||||
|
||||
/// <inheritdoc />
|
||||
public string InstancePath => Config?.InstancePath;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Init()
|
||||
{
|
||||
Log.Info("Initializing server");
|
||||
Sandbox.Engine.Platform.Game.IsDedicated = true;
|
||||
MySandboxGame.IsDedicated = true;
|
||||
base.Init();
|
||||
|
||||
Managers.GetManager<ITorchSessionManager>().SessionStateChanged += OnSessionStateChanged;
|
||||
GetManager<InstanceManager>().LoadInstance(Config.InstancePath);
|
||||
CanRun = true;
|
||||
@@ -125,15 +102,6 @@ namespace Torch.Server
|
||||
Log.Info($"Initialized server '{Config.InstanceName}' at '{Config.InstancePath}'");
|
||||
}
|
||||
|
||||
private void OnSessionStateChanged(ITorchSession session, TorchSessionState newState)
|
||||
{
|
||||
if (newState == TorchSessionState.Unloading || newState == TorchSessionState.Unloaded)
|
||||
{
|
||||
_watchdog?.Dispose();
|
||||
_watchdog = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Start()
|
||||
{
|
||||
@@ -200,21 +168,25 @@ namespace Torch.Server
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSessionStateChanged(ITorchSession session, TorchSessionState newState)
|
||||
{
|
||||
if (newState == TorchSessionState.Unloading || newState == TorchSessionState.Unloaded)
|
||||
{
|
||||
_watchdog?.Dispose();
|
||||
_watchdog = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Init(object gameInstance)
|
||||
{
|
||||
base.Init(gameInstance);
|
||||
var game = gameInstance as MySandboxGame;
|
||||
if (game != null && MySession.Static != null)
|
||||
{
|
||||
State = ServerState.Running;
|
||||
// SteamServerAPI.Instance.GameServer.SetKeyValue("SM", "Torch");
|
||||
}
|
||||
else
|
||||
{
|
||||
State = ServerState.Stopped;
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Update()
|
||||
@@ -250,11 +222,9 @@ namespace Torch.Server
|
||||
throw new TimeoutException($"Server watchdog detected that the server was frozen for at least {((TorchServer)state).Config.TickTimeout} seconds.");
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Log.Debug("Server watchdog responded");
|
||||
}
|
||||
}
|
||||
|
||||
private static string DumpFrozenThread(Thread thread, int traces = 3, int pause = 5000)
|
||||
{
|
||||
@@ -267,6 +237,7 @@ namespace Torch.Server
|
||||
stacks.Add(dump);
|
||||
Thread.Sleep(pause);
|
||||
}
|
||||
|
||||
string commonPrefix = StringUtils.CommonSuffix(stacks);
|
||||
// Advance prefix to include the line terminator.
|
||||
commonPrefix = commonPrefix.Substring(commonPrefix.IndexOf('\n') + 1);
|
||||
@@ -280,6 +251,7 @@ namespace Torch.Server
|
||||
result.AppendLine($"Suffix {i}");
|
||||
result.AppendLine(stacks[i].Substring(0, stacks[i].Length - commonPrefix.Length));
|
||||
}
|
||||
|
||||
return result.ToString();
|
||||
}
|
||||
|
||||
@@ -293,6 +265,7 @@ namespace Torch.Server
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
var stack = new StackTrace(thread, true);
|
||||
try
|
||||
{
|
||||
@@ -302,6 +275,7 @@ namespace Torch.Server
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
return stack;
|
||||
}
|
||||
|
||||
|
@@ -50,15 +50,14 @@
|
||||
</DockPanel>
|
||||
<Grid Grid.Row="1">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition Width="3*" />
|
||||
<ColumnDefinition Width="2*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid Grid.Column="0">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<ScrollViewer Grid.Row="0" Margin="3">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<StackPanel Margin="3" DockPanel.Dock="Left">
|
||||
<Label Content="Server Name" />
|
||||
@@ -86,7 +85,8 @@
|
||||
</StackPanel>
|
||||
<StackPanel Margin="3">
|
||||
<Label Content="Mods" />
|
||||
<TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto" Style="{StaticResource ValidatedTextBox}">
|
||||
<TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto"
|
||||
Style="{StaticResource ValidatedTextBox}">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Mods" UpdateSourceTrigger="PropertyChanged"
|
||||
ValidatesOnDataErrors="True" NotifyOnValidationError="True"
|
||||
@@ -98,10 +98,12 @@
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
<Label Content="Administrators" />
|
||||
<TextBox Text="{Binding Administrators, Converter={StaticResource ListConverterString}}" Margin="3"
|
||||
<TextBox Text="{Binding Administrators, Converter={StaticResource ListConverterString}}"
|
||||
Margin="3"
|
||||
Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto" />
|
||||
<Label Content="Banned Players" />
|
||||
<TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto" Style="{StaticResource ValidatedTextBox}">
|
||||
<TextBox Margin="3" Height="100" AcceptsReturn="true" VerticalScrollBarVisibility="Auto"
|
||||
Style="{StaticResource ValidatedTextBox}">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Banned" UpdateSourceTrigger="PropertyChanged"
|
||||
ValidatesOnDataErrors="True" NotifyOnValidationError="True"
|
||||
@@ -114,10 +116,11 @@
|
||||
</TextBox>
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
</ScrollViewer>
|
||||
<Button Grid.Row="1" Content="Save Config" Margin="3" Click="Save_OnClick" />
|
||||
</Grid>
|
||||
<views:PropertyGrid Grid.Column="1" Margin="3" DataContext="{Binding SessionSettings}" />
|
||||
<GridSplitter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Stretch" ShowsPreview="True"
|
||||
Width="2" Background="Gray" />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</UserControl>
|
@@ -14,8 +14,8 @@
|
||||
<Grid Margin="3">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<StackPanel Grid.Row="0">
|
||||
<Label Content="{Binding FullName}" FontSize="16" />
|
||||
@@ -25,7 +25,7 @@
|
||||
</StackPanel>
|
||||
<Label Content="Properties"/>
|
||||
</StackPanel>
|
||||
<Expander Grid.Row="1" Header="Block Properties">
|
||||
<Expander Grid.Row="1" Header="Block Properties" IsExpanded="true">
|
||||
<ListView ItemsSource="{Binding Properties}" Margin="3" IsEnabled="True">
|
||||
<ListView.ItemTemplate>
|
||||
<DataTemplate>
|
||||
|
@@ -16,8 +16,8 @@
|
||||
</UserControl.Resources>
|
||||
<Grid>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition />
|
||||
<ColumnDefinition MinWidth="300" Width="Auto" />
|
||||
<ColumnDefinition Width="5*"/>
|
||||
<ColumnDefinition Width="5*" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<Grid Grid.Column="0">
|
||||
<Grid.RowDefinitions>
|
||||
@@ -92,5 +92,6 @@
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
<Frame Grid.Column="1" x:Name="EditorFrame" Margin="3" NavigationUIVisibility="Hidden" />
|
||||
<GridSplitter Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Stretch" ShowsPreview="True" Width="2" Background="Gray"/>
|
||||
</Grid>
|
||||
</UserControl>
|
@@ -55,13 +55,18 @@
|
||||
<TextBlock Text="{Binding ElapsedPlayTime, StringFormat=Uptime: {0:g}}"/>
|
||||
</Label.Content>
|
||||
</Label>
|
||||
<Label x:Name="LabelPlayers">
|
||||
<Label.Content>
|
||||
<TextBlock ></TextBlock>
|
||||
</Label.Content>
|
||||
</Label>
|
||||
</StackPanel>
|
||||
<TabControl Grid.Row="2" Height="Auto" x:Name="TabControl" Margin="5,10,5,5">
|
||||
<TabItem Header="Log">
|
||||
<RichTextBox x:Name="ConsoleText" VerticalScrollBarVisibility="Visible" FontFamily="Consolas"/>
|
||||
</TabItem>
|
||||
<TabItem Header="Configuration">
|
||||
<Grid IsEnabled="{Binding IsRunning, Converter={StaticResource InverseBool}}">
|
||||
<Grid IsEnabled="{Binding CanRun}">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition/>
|
||||
@@ -72,7 +77,7 @@
|
||||
<ColumnDefinition/>
|
||||
</Grid.ColumnDefinitions>
|
||||
</Grid>
|
||||
<views:ConfigControl Grid.Row="1" x:Name="ConfigControl" Margin="3" DockPanel.Dock="Bottom" IsEnabled="{Binding IsRunning, Converter={StaticResource InverseBool}}"/>
|
||||
<views:ConfigControl Grid.Row="1" x:Name="ConfigControl" Margin="3" DockPanel.Dock="Bottom" IsEnabled="{Binding CanRun}"/>
|
||||
</Grid>
|
||||
</TabItem>
|
||||
<TabItem Header="Chat/Players">
|
||||
|
@@ -16,9 +16,9 @@ using System.Windows.Shapes;
|
||||
using NLog;
|
||||
using Sandbox;
|
||||
using Torch.API;
|
||||
using Torch.API.Managers;
|
||||
using Torch.Server.Managers;
|
||||
using MessageBoxResult = System.Windows.MessageBoxResult;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Torch.Server
|
||||
{
|
||||
@@ -93,7 +93,7 @@ namespace Torch.Server
|
||||
if (_server?.State == ServerState.Running)
|
||||
_server.Stop();
|
||||
|
||||
Environment.Exit(0);
|
||||
Process.GetCurrentProcess().Kill();
|
||||
}
|
||||
|
||||
private void BtnRestart_Click(object sender, RoutedEventArgs e)
|
||||
@@ -109,7 +109,7 @@ namespace Torch.Server
|
||||
return;
|
||||
|
||||
_config.InstancePath = name;
|
||||
_server.GetManager<InstanceManager>().LoadInstance(_config.InstancePath);
|
||||
_server.Managers.GetManager<InstanceManager>().LoadInstance(_config.InstancePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -257,6 +257,9 @@
|
||||
<Compile Include="Views\CollectionEditor.xaml.cs">
|
||||
<DependentUpon>CollectionEditor.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Views\DictionaryEditor.xaml.cs">
|
||||
<DependentUpon>DictionaryEditor.xaml</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Views\PropertyGrid.xaml.cs">
|
||||
<DependentUpon>PropertyGrid.xaml</DependentUpon>
|
||||
</Compile>
|
||||
@@ -277,6 +280,10 @@
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Include="Views\DictionaryEditor.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
</Page>
|
||||
<Page Include="Views\PropertyGrid.xaml">
|
||||
<Generator>MSBuild:Compile</Generator>
|
||||
<SubType>Designer</SubType>
|
||||
|
32
Torch/Views/DictionaryEditor.xaml
Normal file
32
Torch/Views/DictionaryEditor.xaml
Normal file
@@ -0,0 +1,32 @@
|
||||
<Window x:Class="Torch.Views.DictionaryEditorDialog"
|
||||
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"
|
||||
xmlns:views="clr-namespace:Torch.Views"
|
||||
xmlns:objectModel="clr-namespace:System.Collections.ObjectModel;assembly=System"
|
||||
mc:Ignorable="d"
|
||||
Title="DictionaryEditorDialog" Height="300" Width="300">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition />
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<DataGrid x:Name="ItemGrid" AutoGenerateColumns="false" CanUserAddRows="true" Grid.Row="0">
|
||||
<DataGrid.Columns>
|
||||
<DataGridTextColumn Width="5*" Header="Key" Binding="{Binding Key}"/>
|
||||
<DataGridTextColumn Width="5*" Header="Value" Binding="{Binding Value}"/>
|
||||
</DataGrid.Columns>
|
||||
</DataGrid>
|
||||
<Button Grid.Row="1" Content="Add New" Margin="5" Click="AddNew_OnClick"></Button>
|
||||
<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>
|
109
Torch/Views/DictionaryEditor.xaml.cs
Normal file
109
Torch/Views/DictionaryEditor.xaml.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
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 DictionaryEditorDialog.xaml
|
||||
/// </summary>
|
||||
public partial class DictionaryEditorDialog : Window
|
||||
{
|
||||
public DictionaryEditorDialog()
|
||||
{
|
||||
InitializeComponent();
|
||||
DataContext = Items;
|
||||
}
|
||||
|
||||
public ObservableCollection<IDictionaryItem> Items { get; } = new ObservableCollection<IDictionaryItem>();
|
||||
private Type _itemType;
|
||||
|
||||
private Action _commitChanges;
|
||||
|
||||
public void Edit(IDictionary dict)
|
||||
{
|
||||
Items.Clear();
|
||||
var dictType = dict.GetType();
|
||||
_itemType = typeof(DictionaryItem<,>).MakeGenericType(dictType.GenericTypeArguments[0], dictType.GenericTypeArguments[1]);
|
||||
|
||||
foreach (var key in dict.Keys)
|
||||
{
|
||||
Items.Add((IDictionaryItem)Activator.CreateInstance(_itemType, key, dict[key]));
|
||||
}
|
||||
|
||||
ItemGrid.ItemsSource = Items;
|
||||
|
||||
_commitChanges = () =>
|
||||
{
|
||||
dict.Clear();
|
||||
foreach (var item in Items)
|
||||
{
|
||||
dict[item.Key] = item.Value;
|
||||
}
|
||||
};
|
||||
|
||||
Show();
|
||||
}
|
||||
|
||||
private void Cancel_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
|
||||
private void Ok_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_commitChanges?.Invoke();
|
||||
Close();
|
||||
}
|
||||
|
||||
public interface IDictionaryItem
|
||||
{
|
||||
object Key { get; set; }
|
||||
object Value { get; set; }
|
||||
}
|
||||
|
||||
public class DictionaryItem<TKey, TValue> : ViewModel, IDictionaryItem
|
||||
{
|
||||
private TKey _key;
|
||||
private TValue _value;
|
||||
|
||||
object IDictionaryItem.Key { get => _key; set => SetValue(ref _key, (TKey)value); }
|
||||
object IDictionaryItem.Value { get => _value; set => SetValue(ref _value, (TValue)value); }
|
||||
|
||||
public TKey Key { get => _key; set => SetValue(ref _key, value); }
|
||||
public TValue Value { get => _value; set => SetValue(ref _value, value); }
|
||||
|
||||
public DictionaryItem()
|
||||
{
|
||||
_key = default(TKey);
|
||||
_value = default(TValue);
|
||||
}
|
||||
|
||||
public DictionaryItem(TKey key, TValue value)
|
||||
{
|
||||
_key = key;
|
||||
_value = value;
|
||||
}
|
||||
}
|
||||
|
||||
private void AddNew_OnClick(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Items.Add((IDictionaryItem)Activator.CreateInstance(_itemType));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
@@ -15,6 +16,7 @@ using System.Windows.Media.Imaging;
|
||||
using System.Windows.Navigation;
|
||||
using System.Windows.Shapes;
|
||||
using VRage.Game;
|
||||
using VRage.Serialization;
|
||||
|
||||
namespace Torch.Views
|
||||
{
|
||||
@@ -52,8 +54,8 @@ namespace Torch.Views
|
||||
var properties = t.GetProperties(BindingFlags.Instance | BindingFlags.Public);
|
||||
|
||||
var grid = new Grid();
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition());
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition());
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
|
||||
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
||||
|
||||
var curRow = 0;
|
||||
foreach (var property in properties.OrderBy(x => x.Name))
|
||||
@@ -64,6 +66,7 @@ namespace Torch.Views
|
||||
grid.RowDefinitions.Add(new RowDefinition());
|
||||
|
||||
var displayName = property.GetCustomAttribute<DisplayAttribute>()?.Name;
|
||||
var propertyType = property.PropertyType;
|
||||
|
||||
var text = new TextBlock
|
||||
{
|
||||
@@ -86,12 +89,12 @@ namespace Torch.Views
|
||||
};
|
||||
valueControl.SetBinding(TextBlock.TextProperty, binding);
|
||||
}
|
||||
else if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(bool?))
|
||||
else if (propertyType == typeof(bool) || propertyType == typeof(bool?))
|
||||
{
|
||||
valueControl = new CheckBox();
|
||||
valueControl.SetBinding(CheckBox.IsCheckedProperty, property.Name);
|
||||
}
|
||||
else if (property.PropertyType.IsEnum)
|
||||
else if (propertyType.IsEnum)
|
||||
{
|
||||
valueControl = new ComboBox
|
||||
{
|
||||
@@ -99,6 +102,28 @@ namespace Torch.Views
|
||||
};
|
||||
valueControl.SetBinding(ComboBox.SelectedItemProperty, property.Name);
|
||||
}
|
||||
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||
{
|
||||
var button = new Button
|
||||
{
|
||||
Content = "Edit Collection"
|
||||
};
|
||||
button.SetBinding(Button.DataContextProperty, property.Name);
|
||||
button.Click += (sender, args) => EditDictionary(((Button)sender).DataContext);
|
||||
|
||||
valueControl = button;
|
||||
}
|
||||
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(SerializableDictionary<,>))
|
||||
{
|
||||
var button = new Button
|
||||
{
|
||||
Content = "Edit Collection"
|
||||
};
|
||||
button.SetBinding(Button.DataContextProperty, $"{property.Name}.Dictionary");
|
||||
button.Click += (sender, args) => EditDictionary(((Button)sender).DataContext);
|
||||
|
||||
valueControl = button;
|
||||
}
|
||||
else
|
||||
{
|
||||
valueControl = new TextBox();
|
||||
@@ -118,6 +143,12 @@ namespace Torch.Views
|
||||
return grid;
|
||||
}
|
||||
|
||||
private void EditDictionary(object dict)
|
||||
{
|
||||
var dic = (IDictionary)dict;
|
||||
new DictionaryEditorDialog().Edit(dic);
|
||||
}
|
||||
|
||||
private void UpdateFilter(object sender, TextChangedEventArgs e)
|
||||
{
|
||||
var filterText = ((TextBox)sender).Text;
|
||||
|
Reference in New Issue
Block a user