corrade-vassal – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | zed | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.Data; |
||
5 | using System.Drawing; |
||
6 | using System.IO; |
||
7 | using System.Linq; |
||
8 | using System.Text; |
||
9 | using System.Threading; |
||
10 | using System.Threading.Tasks; |
||
11 | using System.Windows.Forms; |
||
12 | using OpenMetaverse; |
||
13 | |||
14 | namespace Vassal |
||
15 | { |
||
16 | public partial class RegionEditForm : Form |
||
17 | { |
||
18 | private static RegionEditForm mainForm; |
||
19 | |||
20 | public RegionEditForm() |
||
21 | { |
||
22 | InitializeComponent(); |
||
23 | mainForm = this; |
||
24 | } |
||
25 | |||
26 | private void LoadRegionsRequested(object sender, EventArgs e) |
||
27 | { |
||
28 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
29 | { |
||
30 | switch (mainForm.LoadRegionsDialog.ShowDialog()) |
||
31 | { |
||
32 | case DialogResult.OK: |
||
33 | string file = mainForm.LoadRegionsDialog.FileName; |
||
34 | new Thread(() => |
||
35 | { |
||
36 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
37 | { |
||
38 | try |
||
39 | { |
||
40 | mainForm.StatusText.Text = @"loading regions..."; |
||
41 | mainForm.StatusProgress.Value = 0; |
||
42 | |||
43 | // import CSV regions |
||
44 | Vector3 localPosition; |
||
3 | eva | 45 | List<KeyValuePair<string, Vector3>> ConfiguredRegions = new List<KeyValuePair<string, Vector3>>( |
2 | zed | 46 | File.ReadAllLines(file) |
47 | .AsParallel() |
||
48 | .Select(o => new List<string>(Vassal.wasCSVToEnumerable(o))) |
||
49 | .Where(o => o.Count == 2) |
||
50 | .ToDictionary(o => o.First(), |
||
51 | p => |
||
52 | Vector3.TryParse(p.Last(), out localPosition) |
||
53 | ? localPosition |
||
3 | eva | 54 | : Vector3.Zero)); |
2 | zed | 55 | |
56 | Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => |
||
57 | { |
||
58 | Vassal.vassalForm.LoadedRegions.Items.Clear(); |
||
59 | Vassal.vassalForm.LoadedRegions.Items.AddRange( |
||
3 | eva | 60 | ConfiguredRegions.Select( |
2 | zed | 61 | o => (object) new ListViewItem {Text = o.Key, Tag = o.Value}) |
62 | .ToArray()); |
||
3 | eva | 63 | // Update batch restart grid view. |
64 | Vassal.vassalForm.BatchRestartGridView.Rows.Clear(); |
||
65 | foreach (KeyValuePair<string, Vector3> data in ConfiguredRegions) |
||
66 | { |
||
67 | Vassal.vassalForm.BatchRestartGridView.Rows.Add(data.Key, data.Value); |
||
68 | } |
||
2 | zed | 69 | })); |
70 | |||
71 | Regions.Items.Clear(); |
||
72 | Regions.Items.AddRange( |
||
3 | eva | 73 | ConfiguredRegions.Select(o => (object)new ListViewItem { Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o }) |
2 | zed | 74 | .ToArray()); |
75 | |||
76 | mainForm.StatusText.Text = @"regions loaded"; |
||
77 | mainForm.StatusProgress.Value = 100; |
||
78 | } |
||
79 | catch (Exception ex) |
||
80 | { |
||
81 | mainForm.StatusText.Text = ex.Message; |
||
82 | } |
||
83 | })); |
||
84 | }) |
||
85 | { IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
||
86 | break; |
||
87 | } |
||
88 | })); |
||
89 | } |
||
90 | |||
91 | private void SaveRegionsRequested(object sender, EventArgs e) |
||
92 | { |
||
93 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
94 | { |
||
95 | switch (mainForm.SaveRegionsDialog.ShowDialog()) |
||
96 | { |
||
97 | case DialogResult.OK: |
||
98 | string file = mainForm.SaveRegionsDialog.FileName; |
||
99 | new Thread(() => |
||
100 | { |
||
101 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
102 | { |
||
103 | try |
||
104 | { |
||
105 | mainForm.StatusText.Text = @"saving regions..."; |
||
106 | mainForm.StatusProgress.Value = 0; |
||
107 | |||
108 | // Save the regions to CSV. |
||
109 | using ( |
||
110 | StreamWriter streamWriter = |
||
111 | new StreamWriter(file, false, |
||
112 | Encoding.UTF8)) |
||
113 | { |
||
3 | eva | 114 | foreach ( |
115 | KeyValuePair<string, Vector3> region in |
||
116 | Regions.Items.Cast<ListViewItem>() |
||
117 | .Select(o => (KeyValuePair<string, Vector3>) o.Tag)) |
||
2 | zed | 118 | { |
119 | streamWriter.Write( |
||
120 | Vassal.wasEnumerableToCSV(new[] {region.Key, region.Value.ToString()})); |
||
121 | streamWriter.Write(Environment.NewLine); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | mainForm.StatusText.Text = @"regions saved"; |
||
126 | mainForm.StatusProgress.Value = 100; |
||
127 | } |
||
128 | catch (Exception ex) |
||
129 | { |
||
130 | mainForm.StatusText.Text = ex.Message; |
||
131 | } |
||
132 | })); |
||
133 | }) |
||
134 | { IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
||
135 | break; |
||
136 | } |
||
137 | })); |
||
138 | } |
||
139 | |||
140 | |||
141 | private void RegionEditShown(object sender, EventArgs e) |
||
142 | { |
||
143 | Regions.Items.Clear(); |
||
3 | eva | 144 | // Get all the regions if they exist. |
145 | if (File.Exists(Vassal.VASSAL_CONSTANTS.VASSAL_REGIONS)) |
||
146 | { |
||
147 | Vector3 localPosition; |
||
148 | List<KeyValuePair<string, Vector3>> ConfiguredRegions = new List<KeyValuePair<string, Vector3>>( |
||
149 | File.ReadAllLines(Vassal.VASSAL_CONSTANTS.VASSAL_REGIONS) |
||
150 | .Select(o => new List<string>(Vassal.wasCSVToEnumerable(o))) |
||
151 | .Where(o => o.Count == 2) |
||
152 | .ToDictionary(o => o.First(), |
||
153 | p => |
||
154 | Vector3.TryParse(p.Last(), out localPosition) |
||
155 | ? localPosition |
||
156 | : Vector3.Zero).OrderBy(o => o.Key).ToDictionary(o => o.Key, o => o.Value)); |
||
157 | Regions.Items.AddRange( |
||
158 | ConfiguredRegions.Select( |
||
159 | o => (object) new ListViewItem {Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o}) |
||
160 | .ToArray()); |
||
161 | } |
||
2 | zed | 162 | } |
163 | |||
164 | private void RequestAddRegion(object sender, EventArgs e) |
||
165 | { |
||
166 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
167 | { |
||
168 | if (string.IsNullOrEmpty(RegionName.Text)) |
||
169 | { |
||
170 | RegionName.BackColor = Color.MistyRose; |
||
171 | return; |
||
172 | } |
||
173 | Vector3 position; |
||
174 | if (!Vector3.TryParse(RegionPosition.Text, out position)) |
||
175 | { |
||
176 | RegionPosition.BackColor = Color.MistyRose; |
||
177 | return; |
||
178 | } |
||
179 | RegionName.BackColor = Color.Empty; |
||
180 | RegionPosition.BackColor = Color.Empty; |
||
181 | Regions.Items.Add(new ListViewItem |
||
182 | { |
||
183 | Text = RegionName.Text + @" " + @"(" + position + @")", |
||
184 | Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position) |
||
185 | }); |
||
3 | eva | 186 | |
187 | // Update drop down box. |
||
2 | zed | 188 | Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => |
189 | { |
||
190 | Vassal.vassalForm.LoadedRegions.Items.Add(new ListViewItem |
||
191 | { |
||
192 | Text = RegionName.Text, |
||
193 | Tag = position |
||
194 | }); |
||
3 | eva | 195 | // Add the row to the batch restart grid view. |
196 | Vassal.vassalForm.BatchRestartGridView.Rows.Add(RegionName.Text, position.ToString()); |
||
2 | zed | 197 | })); |
198 | })); |
||
199 | } |
||
200 | |||
201 | private void RequestRemoveRegion(object sender, EventArgs e) |
||
202 | { |
||
203 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
204 | { |
||
205 | ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
||
206 | if (listViewItem == null) |
||
207 | { |
||
208 | Regions.BackColor = Color.MistyRose; |
||
209 | return; |
||
210 | } |
||
3 | eva | 211 | |
2 | zed | 212 | int selectedItemIndex = Regions.SelectedIndex; |
3 | eva | 213 | if (selectedItemIndex == -1) |
214 | return; |
||
2 | zed | 215 | |
3 | eva | 216 | Regions.BackColor = Color.Empty; |
2 | zed | 217 | |
218 | Vassal.vassalForm.BeginInvoke((MethodInvoker)(() => |
||
219 | { |
||
220 | Vassal.vassalForm.LoadedRegions.Items.RemoveAt(selectedItemIndex); |
||
3 | eva | 221 | // Update the batch restart grid view. |
222 | Vassal.vassalForm.BatchRestartGridView.Rows.RemoveAt(selectedItemIndex); |
||
2 | zed | 223 | })); |
224 | |||
225 | Regions.Items.RemoveAt(Regions.SelectedIndex); |
||
226 | |||
227 | })); |
||
228 | } |
||
229 | |||
230 | private void RegionSettingChanged(object sender, EventArgs e) |
||
231 | { |
||
232 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
233 | { |
||
234 | ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
||
235 | if (listViewItem == null) |
||
236 | return; |
||
237 | |||
3 | eva | 238 | int selectedItemIndex = Regions.SelectedIndex; |
239 | if (selectedItemIndex == -1) |
||
240 | return; |
||
241 | |||
2 | zed | 242 | if (string.IsNullOrEmpty(RegionName.Text)) |
243 | { |
||
244 | RegionName.BackColor = Color.MistyRose; |
||
245 | RegionPosition.BackColor = Color.MistyRose; |
||
246 | return; |
||
247 | } |
||
248 | |||
249 | Vector3 position; |
||
250 | if (!Vector3.TryParse(RegionPosition.Text, out position)) |
||
251 | { |
||
252 | RegionPosition.BackColor = Color.MistyRose; |
||
253 | return; |
||
254 | } |
||
255 | |||
256 | RegionName.BackColor = Color.Empty; |
||
257 | RegionPosition.BackColor = Color.Empty; |
||
3 | eva | 258 | |
2 | zed | 259 | |
260 | Vassal.vassalForm.BeginInvoke((MethodInvoker)(() => |
||
261 | { |
||
3 | eva | 262 | if (Vassal.vassalForm.LoadedRegions.Items.Count > selectedItemIndex) |
2 | zed | 263 | { |
3 | eva | 264 | Vassal.vassalForm.LoadedRegions.Items[selectedItemIndex] = new ListViewItem |
265 | { |
||
266 | Text = RegionName.Text, |
||
267 | Tag = position |
||
268 | }; |
||
269 | } |
||
270 | // Update the batch restart grid view. |
||
271 | if (Vassal.vassalForm.BatchRestartGridView.Rows.Count > selectedItemIndex) |
||
272 | { |
||
273 | Vassal.vassalForm.BatchRestartGridView.Rows[selectedItemIndex].Cells["BatchRestartRegionName"] |
||
274 | .Value |
||
275 | = RegionName.Text; |
||
276 | Vassal.vassalForm.BatchRestartGridView.Rows[selectedItemIndex].Cells["BatchRestartPosition"] |
||
277 | .Value |
||
278 | = position.ToString(); |
||
279 | } |
||
2 | zed | 280 | })); |
281 | |||
282 | Regions.Items[selectedItemIndex] = new ListViewItem |
||
283 | { |
||
284 | Text = RegionName.Text + @" " + @"(" + position + @")", |
||
285 | Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position) |
||
286 | }; |
||
287 | |||
288 | })); |
||
289 | } |
||
290 | |||
291 | private void RegionSelected(object sender, EventArgs e) |
||
292 | { |
||
293 | mainForm.BeginInvoke((MethodInvoker)(() => |
||
294 | { |
||
295 | ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
||
296 | if (listViewItem == null) |
||
297 | return; |
||
3 | eva | 298 | |
299 | int selectedItemIndex = Regions.SelectedIndex; |
||
300 | if (selectedItemIndex == -1) |
||
301 | return; |
||
302 | |||
2 | zed | 303 | KeyValuePair<string, Vector3> region = (KeyValuePair<string, Vector3>) listViewItem.Tag; |
304 | RegionName.Text = region.Key; |
||
305 | RegionPosition.Text = region.Value.ToString(); |
||
306 | })); |
||
307 | } |
||
308 | } |
||
309 | } |