corrade-vassal – Rev 3

Subversion Repositories:
Rev:
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;
                                    List<KeyValuePair<string, Vector3>> ConfiguredRegions = new List<KeyValuePair<string, Vector3>>(
                                        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(
                                            ConfiguredRegions.Select(
                                                o => (object) new ListViewItem {Text = o.Key, Tag = o.Value})
                                                .ToArray());
                                        // Update batch restart grid view.
                                        Vassal.vassalForm.BatchRestartGridView.Rows.Clear();
                                        foreach (KeyValuePair<string, Vector3> data in ConfiguredRegions)
                                        {
                                            Vassal.vassalForm.BatchRestartGridView.Rows.Add(data.Key, data.Value);
                                        }
                                    }));

                                    Regions.Items.Clear();
                                    Regions.Items.AddRange(
                                        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
                                                Regions.Items.Cast<ListViewItem>()
                                                    .Select(o => (KeyValuePair<string, Vector3>) o.Tag))
                                        {
                                            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();
            // Get all the regions if they exist.
            if (File.Exists(Vassal.VASSAL_CONSTANTS.VASSAL_REGIONS))
            {
                Vector3 localPosition;
                List<KeyValuePair<string, Vector3>> ConfiguredRegions = new List<KeyValuePair<string, Vector3>>(
                    File.ReadAllLines(Vassal.VASSAL_CONSTANTS.VASSAL_REGIONS)
                        .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).OrderBy(o => o.Key).ToDictionary(o => o.Key, o => o.Value));
                Regions.Items.AddRange(
                    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)
                });

                // Update drop down box.
                Vassal.vassalForm.BeginInvoke((MethodInvoker) (() =>
                {
                    Vassal.vassalForm.LoadedRegions.Items.Add(new ListViewItem
                    {
                        Text = RegionName.Text,
                        Tag = position
                    });
                    // Add the row to the batch restart grid view.
                    Vassal.vassalForm.BatchRestartGridView.Rows.Add(RegionName.Text, position.ToString());
                }));
            }));
        }

        private void RequestRemoveRegion(object sender, EventArgs e)
        {
            mainForm.BeginInvoke((MethodInvoker)(() =>
            {
                ListViewItem listViewItem = Regions.SelectedItem as ListViewItem;
                if (listViewItem == null)
                {
                    Regions.BackColor = Color.MistyRose;
                    return;
                }

                int selectedItemIndex = Regions.SelectedIndex;
                if (selectedItemIndex == -1)
                    return;

                Regions.BackColor = Color.Empty;
                
                Vassal.vassalForm.BeginInvoke((MethodInvoker)(() =>
                {
                    Vassal.vassalForm.LoadedRegions.Items.RemoveAt(selectedItemIndex);
                    // Update the batch restart grid view.
                    Vassal.vassalForm.BatchRestartGridView.Rows.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;

                int selectedItemIndex = Regions.SelectedIndex;
                if (selectedItemIndex == -1)
                    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;
                

                Vassal.vassalForm.BeginInvoke((MethodInvoker)(() =>
                {
                    if (Vassal.vassalForm.LoadedRegions.Items.Count > selectedItemIndex)
                    {
                        Vassal.vassalForm.LoadedRegions.Items[selectedItemIndex] = new ListViewItem
                        {
                            Text = RegionName.Text,
                            Tag = position
                        };
                    }
                    // Update the batch restart grid view.
                    if (Vassal.vassalForm.BatchRestartGridView.Rows.Count > selectedItemIndex)
                    {
                        Vassal.vassalForm.BatchRestartGridView.Rows[selectedItemIndex].Cells["BatchRestartRegionName"]
                            .Value
                            = RegionName.Text;
                        Vassal.vassalForm.BatchRestartGridView.Rows[selectedItemIndex].Cells["BatchRestartPosition"]
                            .Value
                            = position.ToString();
                    }
                }));

                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;

                int selectedItemIndex = Regions.SelectedIndex;
                if (selectedItemIndex == -1)
                    return;

                KeyValuePair<string, Vector3> region = (KeyValuePair<string, Vector3>) listViewItem.Tag;
                RegionName.Text = region.Key;
                RegionPosition.Text = region.Value.ToString();
            }));
        }
    }
}