view FrmSystem.cs @ 14:26f7f6d2b4f8

Implement most of the buttons on the stats tab and adjust button enabling on category tab.
author Tsudico
date Sat, 18 Dec 2010 21:04:51 -0600
parents 372f921c20fc
children 3a347947ecea
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 IBBoard.WarFoundry.API.Objects.Category currentCategory;
		private IBBoard.WarFoundry.API.Objects.SystemStats currentStats;
		
		private bool UpdateCategory
		{
			get
			{
				if(currentCategory == null)
				{
					return false;
				}
				if (currentCategory.Name == (string)this.listCategories.SelectedItem)
				{
					if (currentCategory.Name != this.txtCategoryName.Text)
					{
						return true;
					}
					if (currentCategory.ID != this.txtCategoryID.Text)
					{
						return true;
					}
					if (currentCategory.MinimumPoints > 0 && !this.cbPointMin.Checked)
					{
						return true;
					}
					else if (currentCategory.MinimumPoints != this.numPointMin.Value)
					{
						return true;
					}
					if (currentCategory.MaximumPoints < this.numPointMax.Maximum
						&& !this.cbPointMax.Checked)
					{
						return true;
					}
					else if (currentCategory.MaximumPoints != this.numPointMax.Value)
					{
						return true;
					}
					if (currentCategory.MinimumPercentage > 0 && !this.cbPercentMin.Checked)
					{
						return true;
					}
					else if (currentCategory.MinimumPercentage != this.numPercentMin.Value)
					{
						return true;
					}
					if (currentCategory.MaximumPercentage < this.numPercentMax.Maximum
						&& !this.cbPercentMax.Checked)
					{
						return true;
					}
					else if (currentCategory.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();
			updateSystemStatsList();
		}
		
		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;
			currentCategory = null;
		}
		
		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);
				}
			}
			else
			{
				this.btnCategoryRemove.Enabled = false;
			}
		}
		
		private void updateSystemStatsList()
		{
			this.lviewStats.Items.Clear();
			if (system.SystemStats.Length > 0)
			{
				for(int i = 0; i < system.SystemStats.Length; i++)
				{
					ListViewItem item = new ListViewItem();
					item.Text = system.SystemStats[i].ID;
					if(system.SystemStats[i].Equals(system.StandardSystemStats))
					{
						item.SubItems.Add('\u2713'+"");
					}
					this.lviewStats.Items.Add(item);
				}
			}
			this.lviewStats.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
			this.lviewStats.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.HeaderSize);
		}
		
		private void updateStatLinePreview(IBBoard.WarFoundry.API.Objects.SystemStats stats)
		{
			if(stats.SlotCount > 0)
			{
				this.lviewStatPreview.Items.Clear();
				this.lviewStatPreview.Columns.Clear();
				for(int i = 0; i < stats.SlotCount; i++)
				{
					System.Windows.Forms.ColumnHeader column = new ColumnHeader();
					column.Name = generateID(stats.StatSlots[i].Name);
					column.Text = stats.StatSlots[i].Name;
					column.TextAlign = HorizontalAlignment.Center;
					this.lviewStatPreview.Columns.Add(column);
				}
				this.lviewStatPreview.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
				this.lviewStatPreview.Items.Add(" ");
			}
		}
		
		private void updateStatNames(IBBoard.WarFoundry.API.Objects.SystemStats stats)
		{
			if (stats.SlotCount > 0)
			{
				this.listStatNames.Items.Clear();
				for (int i = 0; i < stats.SlotCount; i++)
				{
					this.listStatNames.Items.Add(stats.StatSlots[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)
		{
			currentCategory = null;
			foreach (IBBoard.WarFoundry.API.Objects.Category cat in system.Categories)
			{
				if (cat.Name == this.listCategories.SelectedItem)
				{
					currentCategory = cat;
					this.txtCategoryName.Text = currentCategory.Name;
					this.txtCategoryID.Text = currentCategory.ID;
					if (currentCategory.MinimumPoints > 0)
					{
						this.numPointMin.Value = currentCategory.MinimumPoints;
						this.cbPointMin.Checked = true;
						this.numPointMin.Enabled = true;
					}
					else
					{
						this.cbPointMin.Checked = false;
						this.numPointMin.Enabled = false;
					}
					if (currentCategory.MaximumPoints > 0
						&& currentCategory.MaximumPoints < this.numPercentMax.Maximum)
					{
						this.numPointMax.Value = currentCategory.MaximumPoints;
						this.cbPointMax.Checked = true;
						this.numPointMax.Enabled = true;
					}
					else
					{
						this.numPointMax.Value = this.numDefaultSize.Value;
						this.cbPointMax.Checked = false;
						this.numPointMax.Enabled = false;
					}
					if (currentCategory.MinimumPercentage > 0)
					{
						this.numPercentMin.Value = currentCategory.MinimumPercentage;
						this.cbPercentMin.Checked = true;
						this.numPercentMin.Enabled = true;
					}
					else
					{
						this.cbPercentMin.Checked = false;
						this.numPercentMin.Enabled = false;
					}
					if (currentCategory.MaximumPercentage < 100)
					{
						this.numPercentMax.Value = currentCategory.MaximumPercentage;
						this.cbPercentMax.Checked = true;
						this.numPercentMax.Enabled = true;
					}
					else
					{
						this.cbPercentMax.Checked = false;
						this.numPercentMax.Enabled = false;
					}
					this.btnCategoryRemove.Enabled = true;
					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();
					this.btnCategoryRemove.Enabled = false;
					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;
			}
			if(this.txtCategoryName.Text != string.Empty && this.txtCategoryID.Text != string.Empty)
			{
				this.btnCategoryAdd.Enabled = true;
			}
			else
			{
				this.btnCategoryAdd.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;
			}
		}

		private void btnCategoryApply_Click(object sender, EventArgs e)
		{
			currentCategory.Name = this.txtCategoryName.Text;
			currentCategory.ID = this.txtCategoryID.Text;
			if(this.cbPointMin.Checked)
			{
				currentCategory.MinimumPoints = (int)this.numPointMin.Value;
			}
			else
			{
				currentCategory.MinimumPoints = 0;
			}
			if (this.cbPointMax.Checked)
			{
				currentCategory.MaximumPoints = (int)this.numPointMax.Value;
			}
			else
			{
				currentCategory.MaximumPoints = IBBoard.WarFoundry.API.WarFoundryCore.INFINITY;
			}
			if (this.cbPercentMin.Checked)
			{
				currentCategory.MinimumPercentage = (int)this.numPercentMin.Value;
			}
			else
			{
				currentCategory.MinimumPercentage = 0;
			}
			if (this.cbPercentMax.Checked)
			{
				currentCategory.MinimumPercentage = (int)this.numPercentMax.Value;
			}
			else
			{
				currentCategory.MaximumPercentage = 100;
			}
			system.SetCategory(currentCategory);
			updateCategoryList();
			this.listCategories.ClearSelected();
		}

		void lviewStats_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			currentStats = null;
			foreach(IBBoard.WarFoundry.API.Objects.SystemStats stat in system.SystemStats)
			{
				ListView.SelectedListViewItemCollection items = this.lviewStats.SelectedItems;
				if(items.Count > 0)
				{
					this.btnStatsRemove.Enabled = true;
				}
				foreach(ListViewItem item in items)
				{
					if(stat.ID == item.Text)
					{
						currentStats = stat;
						if(currentStats.ID.Equals(system.StandardSystemStatsID))
						{
							this.btnStatsDefault.Enabled = false;
						}
						else
						{
							this.btnStatsDefault.Enabled = true;
						}
						this.txtStatLineID.Text = currentStats.ID;
						updateStatNames(currentStats);
						updateStatLinePreview(currentStats);
					}
				}
			}
		}

		private void btnStatsRemove_Click(object sender, EventArgs e)
		{
			system.RemoveSystemStats(currentStats.ID);
			this.btnStatsRemove.Enabled = false;
			updateSystemStatsList();
		}

		private void btnStatsDefault_Click(object sender, EventArgs e)
		{
			system.StandardSystemStatsID = this.lviewStats.SelectedItems[0].Text;
			updateSystemStatsList();
		}

		private void txtStatLineID_TextChanged(object sender, EventArgs e)
		{
			if(this.txtStatLineID.Text != string.Empty)
			{
				this.btnStatsAdd.Enabled = true;
			}
			else
			{
				this.btnStatsAdd.Enabled = false;
			}
		}

		private void txtStatName_TextChanged(object sender, EventArgs e)
		{
			if(this.txtStatName.Text != string.Empty)
			{
				this.btnStatListAdd.Enabled = true;
			}
			else
			{
				this.btnStatListAdd.Enabled = false;
			}
		}

		private void btnStatListAdd_Click(object sender, EventArgs e)
		{
			if(this.txtStatName.Text != string.Empty)
			{
				currentStats.AddStatSlot(this.txtStatName.Text);
			}
			updateStatNames(currentStats);
			updateStatLinePreview(currentStats);
		}

		private void listStatNames_SelectedIndexChanged(object sender, EventArgs e)
		{
			if(this.listStatNames.SelectedIndex >= 0)
			{
				this.btnStatListRemove.Enabled = true;
				if(this.listStatNames.SelectedIndex == 0)
				{
					this.btnStatListUp.Enabled = false;
				}
				else
				{
					this.btnStatListUp.Enabled = true;
				}
				if(this.listStatNames.SelectedIndex == this.listStatNames.Items.Count - 1)
				{
					this.btnStatListDown.Enabled = false;
				}
				else
				{
					this.btnStatListDown.Enabled = true;
				}
			}
			else
			{
				this.btnStatListRemove.Enabled = false;
				this.btnStatListUp.Enabled = false;
				this.btnStatListDown.Enabled = false;
			}
		}

		private void btnStatListRemove_Click(object sender, EventArgs e)
		{
			currentStats.RemoveStatSlot((string)this.listStatNames.SelectedItem);
			updateStatNames(currentStats);
			updateStatLinePreview(currentStats);
		}

		private void btnStatListUp_Click(object sender, EventArgs e)
		{
			int index = this.listStatNames.SelectedIndex;
			string[] slotList = new string[this.listStatNames.Items.Count];

			for (int i = 0; i < currentStats.SlotCount; i++)
			{
				slotList[i] = currentStats.StatSlots[i].Name;
			}
			for (int i = 0; i < slotList.Length; i++)
			{
				currentStats.RemoveStatSlot(slotList[i]);
			}

			string temp = slotList[index];
			slotList[index] = slotList[index - 1];
			slotList[index - 1] = temp;

			for (int i = 0; i < slotList.Length; i++)
			{
				currentStats.AddStatSlot(slotList[i]);
			}

			updateStatNames(currentStats);
			updateStatLinePreview(currentStats);
			this.listStatNames.SelectedIndex = index - 1;
		}

		private void btnStatListDown_Click(object sender, EventArgs e)
		{
			int index = this.listStatNames.SelectedIndex;
			string[] slotList = new string[this.listStatNames.Items.Count];

			for (int i = 0; i < currentStats.SlotCount; i++)
			{
				slotList[i] = currentStats.StatSlots[i].Name;
			}
			for (int i = 0; i < slotList.Length; i++)
			{
				currentStats.RemoveStatSlot(slotList[i]);
			}

			string temp = slotList[index];
			slotList[index] = slotList[index + 1];
			slotList[index + 1] = temp;

			for (int i = 0; i < slotList.Length; i++)
			{
				currentStats.AddStatSlot(slotList[i]);
			}

			updateStatNames(currentStats);
			updateStatLinePreview(currentStats);
			this.listStatNames.SelectedIndex = index + 1;
		}

		private void btnStatLineApply_Click(object sender, EventArgs e)
		{
			system.SetSystemStats(currentStats);
		}
	}
}