corrade-vassal – Blame information for rev 16

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