view FrmSystem.cs @ 6:d63df495cf5a

Implemented initial support for adding and editing categories.
author Tsudico
date Mon, 13 Dec 2010 22:38:01 -0600
parents 866d0093bb11
children 0dadaa315430
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;
		
		public FrmSystem(IBBoard.WarFoundry.API.Objects.GameSystem loadSystem)
		{
			InitializeComponent();
			system = loadSystem;
			this.txtSystemName.Text = system.Name;
			this.txtSystemId.Text = system.ID;
			if(system.AllowAllies)
			{
				this.radSystemAlliesYes.Checked = true;
			}
			else
			{
				this.radSystemAlliesNo.Checked = true;
			}
			if (system.WarnOnError)
			{
				this.radSystemWarnYes.Checked = true;
			}
			else
			{
				this.radSystemWarnNo.Checked = true;
			}
			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 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 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 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);

			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 btnCategoryEdit_Click(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;
					}
					if(cat.MaximumPoints > 0 && cat.MaximumPoints < this.numPointMax.Maximum)
					{	
						this.numPointMax.Value = cat.MaximumPoints;
					}
					if(cat.MinimumPercentage >= 0)
					{
						this.numPercentMin.Value = cat.MinimumPercentage;
					}
					if(cat.MaximumPercentage <= 100)
					{
						this.numPercentMax.Value = cat.MaximumPercentage;
					}
				}
			}
		}

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