/Vassal/Vassal/RegionEditForm.cs |
@@ -0,0 +1,258 @@ |
using System; |
using System.Collections.Generic; |
using System.ComponentModel; |
using System.Data; |
using System.Drawing; |
using System.IO; |
using System.Linq; |
using System.Text; |
using System.Threading; |
using System.Threading.Tasks; |
using System.Windows.Forms; |
using OpenMetaverse; |
|
namespace Vassal |
{ |
public partial class RegionEditForm : Form |
{ |
private static RegionEditForm mainForm; |
|
public RegionEditForm() |
{ |
InitializeComponent(); |
mainForm = this; |
} |
|
private void LoadRegionsRequested(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
switch (mainForm.LoadRegionsDialog.ShowDialog()) |
{ |
case DialogResult.OK: |
string file = mainForm.LoadRegionsDialog.FileName; |
new Thread(() => |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
try |
{ |
mainForm.StatusText.Text = @"loading regions..."; |
mainForm.StatusProgress.Value = 0; |
|
// import CSV regions |
Vector3 localPosition; |
Vassal.ConfiguredRegions = |
File.ReadAllLines(file) |
.AsParallel() |
.Select(o => new List<string>(Vassal.wasCSVToEnumerable(o))) |
.Where(o => o.Count == 2) |
.ToDictionary(o => o.First(), |
p => |
Vector3.TryParse(p.Last(), out localPosition) |
? localPosition |
: Vector3.Zero); |
|
Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => |
{ |
Vassal.vassalForm.LoadedRegions.Items.Clear(); |
Vassal.vassalForm.LoadedRegions.Items.AddRange( |
Vassal.ConfiguredRegions.Select( |
o => (object) new ListViewItem {Text = o.Key, Tag = o.Value}) |
.ToArray()); |
})); |
|
Regions.Items.Clear(); |
Regions.Items.AddRange( |
Vassal.ConfiguredRegions.Select(o => (object)new ListViewItem { Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o }) |
.ToArray()); |
|
mainForm.StatusText.Text = @"regions loaded"; |
mainForm.StatusProgress.Value = 100; |
} |
catch (Exception ex) |
{ |
mainForm.StatusText.Text = ex.Message; |
} |
})); |
}) |
{ IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
break; |
} |
})); |
} |
|
private void SaveRegionsRequested(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
switch (mainForm.SaveRegionsDialog.ShowDialog()) |
{ |
case DialogResult.OK: |
string file = mainForm.SaveRegionsDialog.FileName; |
new Thread(() => |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
try |
{ |
mainForm.StatusText.Text = @"saving regions..."; |
mainForm.StatusProgress.Value = 0; |
|
// Save the regions to CSV. |
using ( |
StreamWriter streamWriter = |
new StreamWriter(file, false, |
Encoding.UTF8)) |
{ |
foreach (KeyValuePair<string, Vector3> region in Vassal.ConfiguredRegions) |
{ |
streamWriter.Write( |
Vassal.wasEnumerableToCSV(new[] {region.Key, region.Value.ToString()})); |
streamWriter.Write(Environment.NewLine); |
} |
} |
|
mainForm.StatusText.Text = @"regions saved"; |
mainForm.StatusProgress.Value = 100; |
} |
catch (Exception ex) |
{ |
mainForm.StatusText.Text = ex.Message; |
} |
})); |
}) |
{ IsBackground = true, Priority = ThreadPriority.Normal }.Start(); |
break; |
} |
})); |
} |
|
|
private void RegionEditShown(object sender, EventArgs e) |
{ |
Regions.Items.Clear(); |
Regions.Items.AddRange( |
Vassal.ConfiguredRegions.Select(o => (object)new ListViewItem { Text = o.Key + @" " + @"(" + o.Value.ToString() + @")", Tag = o }) |
.ToArray()); |
} |
|
private void RequestAddRegion(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
if (string.IsNullOrEmpty(RegionName.Text)) |
{ |
RegionName.BackColor = Color.MistyRose; |
return; |
} |
Vector3 position; |
if (!Vector3.TryParse(RegionPosition.Text, out position)) |
{ |
RegionPosition.BackColor = Color.MistyRose; |
return; |
} |
RegionName.BackColor = Color.Empty; |
RegionPosition.BackColor = Color.Empty; |
Regions.Items.Add(new ListViewItem |
{ |
Text = RegionName.Text + @" " + @"(" + position + @")", |
Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position) |
}); |
Vassal.ConfiguredRegions.Add(RegionName.Text, position); |
Vassal.vassalForm.BeginInvoke((MethodInvoker) (() => |
{ |
Vassal.vassalForm.LoadedRegions.Items.Add(new ListViewItem |
{ |
Text = RegionName.Text, |
Tag = position |
}); |
})); |
})); |
} |
|
private void RequestRemoveRegion(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
if (listViewItem == null) |
{ |
Regions.BackColor = Color.MistyRose; |
return; |
} |
Regions.BackColor = Color.Empty; |
int selectedItemIndex = Regions.SelectedIndex; |
|
Vassal.ConfiguredRegions.Remove(RegionName.Text); |
|
Vassal.vassalForm.BeginInvoke((MethodInvoker)(() => |
{ |
Vassal.vassalForm.LoadedRegions.Items.RemoveAt(selectedItemIndex); |
})); |
|
Regions.Items.RemoveAt(Regions.SelectedIndex); |
|
})); |
} |
|
private void RegionSettingChanged(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
if (listViewItem == null) |
return; |
|
if (string.IsNullOrEmpty(RegionName.Text)) |
{ |
RegionName.BackColor = Color.MistyRose; |
RegionPosition.BackColor = Color.MistyRose; |
return; |
} |
|
Vector3 position; |
if (!Vector3.TryParse(RegionPosition.Text, out position)) |
{ |
RegionPosition.BackColor = Color.MistyRose; |
return; |
} |
|
RegionName.BackColor = Color.Empty; |
RegionPosition.BackColor = Color.Empty; |
int selectedItemIndex = Regions.SelectedIndex; |
|
Vassal.ConfiguredRegions.Remove(RegionName.Text); |
Vassal.ConfiguredRegions.Add(RegionName.Text, position); |
Vassal.vassalForm.BeginInvoke((MethodInvoker)(() => |
{ |
Vassal.vassalForm.LoadedRegions.Items[selectedItemIndex] = new ListViewItem |
{ |
Text = RegionName.Text, |
Tag = position |
}; |
})); |
|
Regions.Items[selectedItemIndex] = new ListViewItem |
{ |
Text = RegionName.Text + @" " + @"(" + position + @")", |
Tag = new KeyValuePair<string, Vector3>(RegionName.Text, position) |
}; |
|
})); |
} |
|
private void RegionSelected(object sender, EventArgs e) |
{ |
mainForm.BeginInvoke((MethodInvoker)(() => |
{ |
ListViewItem listViewItem = Regions.SelectedItem as ListViewItem; |
if (listViewItem == null) |
return; |
KeyValuePair<string, Vector3> region = (KeyValuePair<string, Vector3>) listViewItem.Tag; |
RegionName.Text = region.Key; |
RegionPosition.Text = region.Value.ToString(); |
})); |
} |
} |
} |