view FrmSystem.cs @ 12:6284c091b132

Merge local with IBBoard
author Tsudico
date Fri, 17 Dec 2010 22:14:43 -0600
parents 43d88f50c712
children 372f921c20fc
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IBBoard.Lang;
using IBBoard.Windows.Forms;
using IBBoard.Windows.Forms.I18N;

namespace IBBoard.WarFoundry.Forge.WinForms
{
	public partial class FrmSystem : IBBoard.Windows.Forms.IBBForm
	{
		private IBBoard.WarFoundry.API.Objects.GameSystem system;
		
		private bool UpdateCategory
		{
			get
			{
				if(this.listCategories.SelectedIndex >= 0)
				{
					foreach(IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
					{
						if(cat.Name == this.listCategories.SelectedItem)
						{
							if(cat.Name != this.txtCategoryName.Text)
							{
								return true;
							}
							if(cat.ID != this.txtCategoryID.Text)
							{
								return true;
							}
							if(cat.MinimumPoints > 0 && !this.cbPointMin.Checked)
							{
								return true;
							}
							else if(cat.MinimumPoints != this.numPointMin.Value)
							{
								return true;
							}
							if(cat.MaximumPoints < this.numPointMax.Maximum && !this.cbPointMax.Checked)
							{
								return true;
							}
							else if(cat.MaximumPoints != this.numPointMax.Value)
							{
								return true;
							}
							if(cat.MinimumPercentage > 0 && !this.cbPercentMin.Checked)
							{
								return true;
							}
							else if(cat.MinimumPercentage != this.numPercentMin.Value)
							{
								return true;
							}
							if(cat.MaximumPercentage < this.numPercentMax.Maximum && !this.cbPercentMax.Checked)
							{
								return true;
							}
							else if(cat.MaximumPercentage != this.numPercentMax.Value)
							{
								return true;
							}
						}
					}
				}
				return false;
			}
		}
		
		public FrmSystem(IBBoard.WarFoundry.API.Objects.GameSystem loadSystem)
		{
			InitializeComponent();
			system = loadSystem;
			this.txtSystemName.Text = system.Name;
			this.txtSystemId.Text = system.ID;
			this.numDefaultSize.Value = system.SystemArmyDefaultSize;
			this.numPointMax.Value = this.numDefaultSize.Value;
			if(system.AllowAllies)
			{
				this.radSystemAlliesYes.Checked = true;
			}
			else
			{
				this.radSystemAlliesNo.Checked = true;
			}
			if (system.WarnOnError)
			{
				this.radSystemWarnYes.Checked = true;
			}
			else
			{
				this.radSystemWarnNo.Checked = true;
			}
			updateCategoryList();
		}
		
		private string generateID(string name)
		{
			string newId = String.Empty;

			MatchCollection id_parts = Regex.Matches(name, @"[A-Z\d]");
			foreach (Match part in id_parts)
			{
				newId += part.ToString();
			}

			if (newId.Length < 3)
			{
				newId = name.ToLower().Replace(" ", "");
			}

			return newId.ToLower();
		}
		
		private void clearCategory()
		{
			this.txtCategoryName.Text = string.Empty;
			this.txtCategoryID.Text = string.Empty;
			this.cbPointMin.Checked = false;
			this.cbPointMax.Checked = false;
			this.cbPercentMin.Checked = false;
			this.cbPercentMax.Checked = false;
			this.numPointMin.Value = 0;
			this.numPointMin.Enabled = false;
			this.numPointMax.Value = this.numDefaultSize.Value;
			this.numPointMax.Enabled = false;
			this.numPercentMin.Value = 0;
			this.numPercentMin.Enabled = false;
			this.numPercentMax.Value = 100;
			this.numPercentMax.Enabled = false;
		}
		
		private void updateCategoryList()
		{
			if (system.Categories.Length > 0)
			{
				this.listCategories.Items.Clear();
				for (int i = 0; i < system.Categories.Length; i++)
				{
					this.listCategories.Items.Add(system.Categories[i].Name);
				}
			}
		}

		private void btnSystemClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void btnGenerateSysId_Click(object sender, EventArgs e)
		{
			this.txtSystemId.Text = generateID(this.txtSystemName.Text);
		}

		private void listCategories_SelectedIndexChanged(object sender, EventArgs e)
		{
			foreach (IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
			{
				if (cat.Name == this.listCategories.SelectedItem)
				{
					this.txtCategoryName.Text = cat.Name;
					this.txtCategoryID.Text = cat.ID;
					if (cat.MinimumPoints > 0)
					{
						this.numPointMin.Value = cat.MinimumPoints;
						this.cbPointMin.Checked = true;
						this.numPointMin.Enabled = true;
					}
					else
					{
						this.numPointMin.Enabled = false;
					}
					if (cat.MaximumPoints > 0 && cat.MaximumPoints < this.numPercentMax.Maximum)
					{
						this.numPointMax.Value = cat.MaximumPoints;
						this.cbPointMax.Checked = true;
						this.numPointMax.Enabled = true;
					}
					else
					{
						this.numPointMax.Value = this.numDefaultSize.Value;
						this.numPointMax.Enabled = false;
					}
					if (cat.MinimumPercentage > 0)
					{
						this.numPercentMin.Value = cat.MinimumPercentage;
						this.cbPercentMin.Checked = true;
						this.numPercentMin.Enabled = true;
					}
					else
					{
						this.numPercentMin.Enabled = false;
					}
					if (cat.MaximumPercentage < 100)
					{
						this.numPercentMax.Value = cat.MaximumPercentage;
						this.cbPercentMax.Checked = true;
						this.numPercentMax.Enabled = true;
					}
					else
					{
						this.numPercentMax.Enabled = false;
					}
					this.btnCategoryApply.Enabled = false;
					break;
				}
			}
			if(this.listCategories.SelectedIndex == 0)
			{
				this.btnCategoryUp.Enabled = false;
			}
			else
			{
				this.btnCategoryUp.Enabled = true;
			}
			if (this.listCategories.SelectedIndex == this.listCategories.Items.Count - 1)
			{
				this.btnCategoryDown.Enabled = false;
			}
			else
			{
				this.btnCategoryDown.Enabled = true;
			}
		}

		private void btnCategoryAdd_Click(object sender, EventArgs e)
		{
			if(this.txtCategoryName.Text == string.Empty)
			{
				MessageBox.Show("Category must have a name", "Category Error");
				return;
			}
			if (this.txtCategoryID.Text == string.Empty)
			{
				MessageBox.Show("Category must have an ID", "Category Error");
				return;
			}
			IBBoard.WarFoundry.API.Objects.Category cat = new IBBoard.WarFoundry.API.Objects.Category(
					this.txtCategoryID.Text,
					this.txtCategoryName.Text
					);
			
			cat.MinimumPoints = (int)this.numPointMin.Value;
			cat.MaximumPoints = (int)this.numPointMax.Value;
			cat.MinimumPercentage = (int)this.numPercentMin.Value;
			cat.MaximumPercentage = (int)this.numPercentMax.Value;
			
			system.AddCategory(cat);
			updateCategoryList();
			clearCategory();
		}

		private void btnCategoryRemove_Click(object sender, EventArgs e)
		{
			foreach(IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
			{
				if(cat.Name == this.listCategories.SelectedItem)
				{
					system.RemoveCategory(cat.ID);
					this.listCategories.ClearSelected();
					break;
				}
			}
			updateCategoryList();
		}

		private void btnCategoryUp_Click(object sender, EventArgs e)
		{
			int index = this.listCategories.SelectedIndex;
			IBBoard.WarFoundry.API.Objects.Category[] catList = new IBBoard.WarFoundry.API.Objects.Category[this.listCategories.Items.Count];
			
			for(int i = 0; i < system.Categories.Length; i++)
			{
				catList[i] = system.Categories[i];
			}
			foreach(IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
			{
				system.RemoveCategory(cat.ID);
			}
			
			IBBoard.WarFoundry.API.Objects.Category temp = catList[index];
			catList[index] = catList[index - 1];
			catList[index - 1] = temp;
			
			for(int i = catList.Length - 1; i >= 0; i--)
			{
				system.AddCategory(catList[i]);
			}
			
			updateCategoryList();
			this.listCategories.SelectedIndex = index - 1;
		}

		private void btnCategoryDown_Click(object sender, EventArgs e)
		{
			int index = this.listCategories.SelectedIndex;
			IBBoard.WarFoundry.API.Objects.Category[] catList = new IBBoard.WarFoundry.API.Objects.Category[this.listCategories.Items.Count];

			for (int i = 0; i < system.Categories.Length; i++)
			{
				catList[i] = system.Categories[i];
			}
			foreach (IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
			{
				system.RemoveCategory(cat.ID);
			}

			IBBoard.WarFoundry.API.Objects.Category temp = catList[index];
			catList[index] = catList[index + 1];
			catList[index + 1] = temp;

			for (int i = catList.Length - 1; i >= 0; i--)
			{
				system.AddCategory(catList[i]);
			}

			updateCategoryList();
			this.listCategories.SelectedIndex = index + 1;
		}

		private void txtCategoryName_TextChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void txtCategoryID_TextChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void btnGenerateCatID_Click(object sender, EventArgs e)
		{
			this.txtCategoryID.Text = generateID(this.txtCategoryName.Text);
		}

		private void cbPointMin_CheckedChanged(object sender, EventArgs e)
		{
			if(this.cbPointMin.Checked)
			{
				this.numPointMin.Enabled = true;
			}
			else
			{
				this.numPointMin.Enabled = false;
			}
			if(this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void cbPointMax_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbPointMax.Checked)
			{
				this.numPointMax.Enabled = true;
			}
			else
			{
				this.numPointMax.Enabled = false;
			}
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void cbPercentMin_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbPercentMin.Checked)
			{
				this.numPercentMin.Enabled = true;
			}
			else
			{
				this.numPercentMin.Enabled = false;
			}
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void cbPercentMax_CheckedChanged(object sender, EventArgs e)
		{
			if (this.cbPercentMax.Checked)
			{
				this.numPercentMax.Enabled = true;
			}
			else
			{
				this.numPercentMax.Enabled = false;
			}
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void numPointMin_ValueChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void numPointMax_ValueChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void numPercentMin_ValueChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}

		private void numPercentMax_ValueChanged(object sender, EventArgs e)
		{
			if (this.UpdateCategory)
			{
				this.btnCategoryApply.Enabled = true;
			}
			else
			{
				this.btnCategoryApply.Enabled = false;
			}
		}
	}
}