-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathConfigControl.xaml.cs
More file actions
217 lines (188 loc) · 7.89 KB
/
ConfigControl.xaml.cs
File metadata and controls
217 lines (188 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Threading;
using Torch.API.Managers;
using Torch.Server.Annotations;
using Torch.Server.Managers;
using Torch.Server.ViewModels;
using VRage.Game.ModAPI;
using VRage.Serialization;
namespace Torch.Server.Views
{
/// <summary>
/// Interaction logic for ConfigControl.xaml
/// </summary>
public partial class ConfigControl : UserControl, INotifyPropertyChanged
{
private InstanceManager _instanceManager;
private bool _configValid;
public bool ConfigValid { get => _configValid; private set { _configValid = value; OnPropertyChanged(); } }
private List<BindingExpression> _bindingExpressions = new List<BindingExpression>();
private TorchServer _server;
public ConfigControl(TorchServer server)
{
InitializeComponent();
_server = server;
_instanceManager = TorchBase.Instance.Managers.GetManager<InstanceManager>();
_instanceManager.InstanceLoaded += _instanceManager_InstanceLoaded;
DataContext = _instanceManager.DedicatedConfig;
TorchSettings.DataContext = (TorchConfig)TorchBase.Instance.Config;
// Gets called once all children are loaded
Dispatcher.BeginInvoke(DispatcherPriority.Loaded, new Action(ApplyStyles));
_server.PropertyChanged += (sender, args) =>
{
if (args.PropertyName == nameof(TorchServer.CanRun))
{
Dispatcher.Invoke(SetReadOnly);
}
};
}
private void CheckValid()
{
ConfigValid = !_bindingExpressions.Any(x => x.HasError);
}
private void ApplyStyles()
{
foreach (var textbox in GetAllChildren<TextBox>(this))
{
textbox.Style = (Style)Resources["ValidatedTextBox"];
var binding = textbox.GetBindingExpression(TextBox.TextProperty);
if (binding == null)
continue;
_bindingExpressions.Add(binding);
textbox.TextChanged += (sender, args) =>
{
binding.UpdateSource();
CheckValid();
};
textbox.LostKeyboardFocus += (sender, args) =>
{
if (binding.HasError)
binding.UpdateTarget();
CheckValid();
};
CheckValid();
}
}
private IEnumerable<T> GetAllChildren<T>(DependencyObject control) where T : DependencyObject
{
var children = LogicalTreeHelper.GetChildren(control).OfType<DependencyObject>();
foreach (var child in children)
{
if (child is T t)
yield return t;
foreach (var grandChild in GetAllChildren<T>(child))
yield return grandChild;
}
}
private void _instanceManager_InstanceLoaded(ConfigDedicatedViewModel obj)
{
Dispatcher.Invoke(() => DataContext = obj);
}
private void Save_OnClick(object sender, RoutedEventArgs e)
{
_instanceManager.SaveConfig();
((ITorchConfig)TorchSettings.DataContext).Save();
}
private void ImportConfig_OnClick(object sender, RoutedEventArgs e)
{
_instanceManager.ImportSelectedWorldConfig();
}
private void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
//The control doesn't update the binding before firing the event.
if (e.AddedItems.Count > 0)
{
var result = MessageBoxResult.Yes; //MessageBox.Show("Do you want to import the session settings from the selected world?", "Import Config", MessageBoxButton.YesNo);
var world = (WorldViewModel)e.AddedItems[0];
_instanceManager.SelectWorld(world.WorldPath, result != MessageBoxResult.Yes);
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private void NewWorld_OnClick(object sender, RoutedEventArgs e)
{
var c = new WorldGeneratorDialog(_instanceManager);
c.Show();
}
private void RoleEdit_Onlick(object sender, RoutedEventArgs e)
{
//var w = new RoleEditor(_instanceManager.DedicatedConfig.SelectedWorld);
//w.Show();
var d = new RoleEditor();
var w = _instanceManager.DedicatedConfig.SelectedWorld;
if(w.Checkpoint.PromotedUsers == null) {
w.Checkpoint.PromotedUsers = new VRage.Serialization.SerializableDictionary<ulong, MyPromoteLevel>();
}
if (w == null)
{
MessageBox.Show("A world is not selected.");
return;
}
if (w.Checkpoint.PromotedUsers == null)
w.Checkpoint.PromotedUsers = new SerializableDictionary<ulong, MyPromoteLevel>();
d.Edit(w.Checkpoint.PromotedUsers.Dictionary);
_instanceManager.DedicatedConfig.Administrators = w.Checkpoint.PromotedUsers.Dictionary.Where(k => k.Value >= MyPromoteLevel.Admin).Select(k => k.Key.ToString()).ToList();
}
private void SetReadOnly()
{
foreach (var textbox in GetAllChildren<TextBox>(this))
{
// Don't make inherently read-only textboxes editable
// (e.g., AnalyticsToken with ReadOnly=true in DisplayAttribute)
if (!_server.CanRun)
textbox.IsReadOnly = true;
// Only make editable if it wasn't marked as inherently read-only
// We use Tag to track this - PropertyGrid doesn't set Tag on TextBoxes
else if (textbox.Tag as string != "InherentlyReadOnly")
{
// Check if this is the first time and textbox is already read-only
// If so, mark it as inherently read-only
if (textbox.IsReadOnly && textbox.Tag == null)
textbox.Tag = "InherentlyReadOnly";
else
textbox.IsReadOnly = false;
}
}
foreach (var button in GetAllChildren<Button>(this))
{
button.IsEnabled = _server.CanRun;
}
foreach (var comboBox in GetAllChildren<ComboBox>(this))
{
comboBox.IsEnabled = _server.CanRun;
}
foreach (var slider in GetAllChildren<Slider>(this))
{
slider.IsEnabled = _server.CanRun;
}
foreach (var checkbox in GetAllChildren<CheckBox>(this))
{
checkbox.IsEnabled = _server.CanRun;
}
foreach (var toggleButton in GetAllChildren<ToggleButton>(this))
{
toggleButton.IsEnabled = _server.CanRun;
}
foreach (var radioButton in GetAllChildren<RadioButton>(this))
{
radioButton.IsEnabled = _server.CanRun;
}
foreach (var listBox in GetAllChildren<ListBox>(this))
{
listBox.IsEnabled = _server.CanRun;
}
}
}
}