corrade-vassal – Diff between revs 2 and 3

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