corrade-vassal – Blame information for rev 2

Subversion Repositories:
Rev:
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;
45 Vassal.ConfiguredRegions =
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
54 : Vector3.Zero);
55  
56 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() =>
57 {
58 Vassal.vassalForm.LoadedRegions.Items.Clear();
59 Vassal.vassalForm.LoadedRegions.Items.AddRange(
60 Vassal.ConfiguredRegions.Select(
61 o => (object) new ListViewItem {Text = o.Key, Tag = o.Value})
62 .ToArray());
63 }));
64  
65 Regions.Items.Clear();
66 Regions.Items.AddRange(
67 Vassal.ConfiguredRegions.Select(o => (object)new ListViewItem { Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o })
68 .ToArray());
69  
70 mainForm.StatusText.Text = @"regions loaded";
71 mainForm.StatusProgress.Value = 100;
72 }
73 catch (Exception ex)
74 {
75 mainForm.StatusText.Text = ex.Message;
76 }
77 }));
78 })
79 { IsBackground = true, Priority = ThreadPriority.Normal }.Start();
80 break;
81 }
82 }));
83 }
84  
85 private void SaveRegionsRequested(object sender, EventArgs e)
86 {
87 mainForm.BeginInvoke((MethodInvoker)(() =>
88 {
89 switch (mainForm.SaveRegionsDialog.ShowDialog())
90 {
91 case DialogResult.OK:
92 string file = mainForm.SaveRegionsDialog.FileName;
93 new Thread(() =>
94 {
95 mainForm.BeginInvoke((MethodInvoker)(() =>
96 {
97 try
98 {
99 mainForm.StatusText.Text = @"saving regions...";
100 mainForm.StatusProgress.Value = 0;
101  
102 // Save the regions to CSV.
103 using (
104 StreamWriter streamWriter =
105 new StreamWriter(file, false,
106 Encoding.UTF8))
107 {
108 foreach (KeyValuePair<string, Vector3> region in Vassal.ConfiguredRegions)
109 {
110 streamWriter.Write(
111 Vassal.wasEnumerableToCSV(new[] {region.Key, region.Value.ToString()}));
112 streamWriter.Write(Environment.NewLine);
113 }
114 }
115  
116 mainForm.StatusText.Text = @"regions saved";
117 mainForm.StatusProgress.Value = 100;
118 }
119 catch (Exception ex)
120 {
121 mainForm.StatusText.Text = ex.Message;
122 }
123 }));
124 })
125 { IsBackground = true, Priority = ThreadPriority.Normal }.Start();
126 break;
127 }
128 }));
129 }
130  
131  
132 private void RegionEditShown(object sender, EventArgs e)
133 {
134 Regions.Items.Clear();
135 Regions.Items.AddRange(
136 Vassal.ConfiguredRegions.Select(o => (object)new ListViewItem { Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o })
137 .ToArray());
138 }
139  
140 private void RequestAddRegion(object sender, EventArgs e)
141 {
142 mainForm.BeginInvoke((MethodInvoker)(() =>
143 {
144 if (string.IsNullOrEmpty(RegionName.Text))
145 {
146 RegionName.BackColor = Color.MistyRose;
147 return;
148 }
149 Vector3 position;
150 if (!Vector3.TryParse(RegionPosition.Text, out position))
151 {
152 RegionPosition.BackColor = Color.MistyRose;
153 return;
154 }
155 RegionName.BackColor = Color.Empty;
156 RegionPosition.BackColor = Color.Empty;
157 Regions.Items.Add(new ListViewItem
158 {
159 Text = RegionName.Text + @" " + @"(" + position + @")",
160 Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position)
161 });
162 Vassal.ConfiguredRegions.Add(RegionName.Text, position);
163 Vassal.vassalForm.BeginInvoke((MethodInvoker) (() =>
164 {
165 Vassal.vassalForm.LoadedRegions.Items.Add(new ListViewItem
166 {
167 Text = RegionName.Text,
168 Tag = position
169 });
170 }));
171 }));
172 }
173  
174 private void RequestRemoveRegion(object sender, EventArgs e)
175 {
176 mainForm.BeginInvoke((MethodInvoker)(() =>
177 {
178 ListViewItem listViewItem = Regions.SelectedItem as ListViewItem;
179 if (listViewItem == null)
180 {
181 Regions.BackColor = Color.MistyRose;
182 return;
183 }
184 Regions.BackColor = Color.Empty;
185 int selectedItemIndex = Regions.SelectedIndex;
186  
187 Vassal.ConfiguredRegions.Remove(RegionName.Text);
188  
189 Vassal.vassalForm.BeginInvoke((MethodInvoker)(() =>
190 {
191 Vassal.vassalForm.LoadedRegions.Items.RemoveAt(selectedItemIndex);
192 }));
193  
194 Regions.Items.RemoveAt(Regions.SelectedIndex);
195  
196 }));
197 }
198  
199 private void RegionSettingChanged(object sender, EventArgs e)
200 {
201 mainForm.BeginInvoke((MethodInvoker)(() =>
202 {
203 ListViewItem listViewItem = Regions.SelectedItem as ListViewItem;
204 if (listViewItem == null)
205 return;
206  
207 if (string.IsNullOrEmpty(RegionName.Text))
208 {
209 RegionName.BackColor = Color.MistyRose;
210 RegionPosition.BackColor = Color.MistyRose;
211 return;
212 }
213  
214 Vector3 position;
215 if (!Vector3.TryParse(RegionPosition.Text, out position))
216 {
217 RegionPosition.BackColor = Color.MistyRose;
218 return;
219 }
220  
221 RegionName.BackColor = Color.Empty;
222 RegionPosition.BackColor = Color.Empty;
223 int selectedItemIndex = Regions.SelectedIndex;
224  
225 Vassal.ConfiguredRegions.Remove(RegionName.Text);
226 Vassal.ConfiguredRegions.Add(RegionName.Text, position);
227 Vassal.vassalForm.BeginInvoke((MethodInvoker)(() =>
228 {
229 Vassal.vassalForm.LoadedRegions.Items[selectedItemIndex] = new ListViewItem
230 {
231 Text = RegionName.Text,
232 Tag = position
233 };
234 }));
235  
236 Regions.Items[selectedItemIndex] = new ListViewItem
237 {
238 Text = RegionName.Text + @" " + @"(" + position + @")",
239 Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position)
240 };
241  
242 }));
243 }
244  
245 private void RegionSelected(object sender, EventArgs e)
246 {
247 mainForm.BeginInvoke((MethodInvoker)(() =>
248 {
249 ListViewItem listViewItem = Regions.SelectedItem as ListViewItem;
250 if (listViewItem == null)
251 return;
252 KeyValuePair<string, Vector3> region = (KeyValuePair<string, Vector3>) listViewItem.Tag;
253 RegionName.Text = region.Key;
254 RegionPosition.Text = region.Value.ToString();
255 }));
256 }
257 }
258 }