diff FrmNewUnit.cs @ 194:137ec6d45280

Re #33: Centralised requirement handling * Use central requirement handler to check each selection
author IBBoard <dev@ibboard.co.uk>
date Mon, 25 Apr 2011 12:58:50 +0000
parents 1bcd7ea857c4
children f9ef9b2db496
line wrap: on
line diff
--- a/FrmNewUnit.cs	Sun Apr 03 14:50:34 2011 +0000
+++ b/FrmNewUnit.cs	Mon Apr 25 12:58:50 2011 +0000
@@ -10,7 +10,8 @@
 using IBBoard.Lang;
 using IBBoard.Windows.Forms;
 using IBBoard.WarFoundry.API;
-using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Objects.Requirement;
 using IBBoard.Windows.Forms.I18N;
 
 namespace IBBoard.WarFoundry
@@ -30,7 +31,6 @@
 		private System.Windows.Forms.ImageList imageList;
 		private System.Windows.Forms.Label lblNewUnitWarning;
 		private System.Windows.Forms.Label lblWarningIcon;
-		private bool[] allowed;
 
 		public FrmNewUnit(Race race, Category cat, Army army)
 		{
@@ -38,12 +38,10 @@
 
 			ControlTranslator.TranslateControl(this, cat.Name);
 			units = race.GetUnitTypes(cat);
-			allowed = new bool[units.Length];
 			this.army = army;
 			
 			for (int i = 0; i<units.Length; i++)
 			{
-				allowed[i] = army.CanAddUnitType(units[i]).Count == 0;
 				lstUnits.Items.Add(units[i].Name);
 			}
 		}
@@ -70,106 +68,106 @@
 		/// </summary>
 		private void InitializeComponent()
 		{
-			this.components = new System.ComponentModel.Container();
-			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmNewUnit));
-			this.lstUnits = new System.Windows.Forms.ListBox();
-			this.bttnCancel = new IBBoard.Windows.Forms.IBBButton();
-			this.lblUnitList = new IBBoard.Windows.Forms.IBBLabel();
-			this.bttnSelectUnit = new IBBoard.Windows.Forms.IBBButton();
-			this.lblNewUnitWarning = new System.Windows.Forms.Label();
-			this.imageList = new System.Windows.Forms.ImageList(this.components);
-			this.lblWarningIcon = new System.Windows.Forms.Label();
-			this.SuspendLayout();
-			// 
-			// lstUnits
-			// 
-			this.lstUnits.Location = new System.Drawing.Point(88, 8);
-			this.lstUnits.Name = "lstUnits";
-			this.lstUnits.Size = new System.Drawing.Size(272, 121);
-			this.lstUnits.TabIndex = 7;
-			this.lstUnits.SelectedIndexChanged += new System.EventHandler(this.lstUnits_SelectedIndexChanged);
-			this.lstUnits.DoubleClick += new System.EventHandler(this.lstUnits_DoubleClick);
-			this.lstUnits.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstUnits_MouseDown);
-			// 
-			// bttnCancel
-			// 
-			this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
-			this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.bttnCancel.Location = new System.Drawing.Point(8, 168);
-			this.bttnCancel.Name = "bttnCancel";
-			this.bttnCancel.Size = new System.Drawing.Size(80, 24);
-			this.bttnCancel.TabIndex = 6;
-			this.bttnCancel.Text = "Cancel";
-			this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
-			// 
-			// lblUnitList
-			// 
-			this.lblUnitList.Location = new System.Drawing.Point(0, 8);
-			this.lblUnitList.Name = "lblUnitList";
-			this.lblUnitList.Size = new System.Drawing.Size(88, 80);
-			this.lblUnitList.TabIndex = 5;
-			this.lblUnitList.Text = "unit list";
-			this.lblUnitList.TextAlign = System.Drawing.ContentAlignment.TopRight;
-			// 
-			// bttnSelectUnit
-			// 
-			this.bttnSelectUnit.Enabled = false;
-			this.bttnSelectUnit.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.bttnSelectUnit.Location = new System.Drawing.Point(256, 168);
-			this.bttnSelectUnit.Name = "bttnSelectUnit";
-			this.bttnSelectUnit.Size = new System.Drawing.Size(104, 24);
-			this.bttnSelectUnit.TabIndex = 4;
-			this.bttnSelectUnit.Text = "create unit";
-			this.bttnSelectUnit.Click += new System.EventHandler(this.bttnSelectUnit_Click);
-			// 
-			// lblNewUnitWarning
-			// 
-			this.lblNewUnitWarning.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
-			this.lblNewUnitWarning.ImageList = this.imageList;
-			this.lblNewUnitWarning.Location = new System.Drawing.Point(48, 136);
-			this.lblNewUnitWarning.Name = "lblNewUnitWarning";
-			this.lblNewUnitWarning.Size = new System.Drawing.Size(312, 32);
-			this.lblNewUnitWarning.TabIndex = 9;
-			this.lblNewUnitWarning.Text = "label1";
-			this.lblNewUnitWarning.TextAlign = System.Drawing.ContentAlignment.TopRight;
-			this.lblNewUnitWarning.Visible = false;
-			// 
-			// imageList
-			// 
-			this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
-			this.imageList.TransparentColor = System.Drawing.Color.Transparent;
-			this.imageList.Images.SetKeyName(0, "");
-			// 
-			// lblWarningIcon
-			// 
-			this.lblWarningIcon.Image = ((System.Drawing.Image)(resources.GetObject("lblWarningIcon.Image")));
-			this.lblWarningIcon.Location = new System.Drawing.Point(8, 136);
-			this.lblWarningIcon.Name = "lblWarningIcon";
-			this.lblWarningIcon.Size = new System.Drawing.Size(40, 32);
-			this.lblWarningIcon.TabIndex = 10;
-			this.lblWarningIcon.Visible = false;
-			// 
-			// FrmNewUnit
-			// 
-			this.AcceptButton = this.bttnSelectUnit;
-			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
-			this.CancelButton = this.bttnCancel;
-			this.ClientSize = new System.Drawing.Size(370, 196);
-			this.Controls.Add(this.lblWarningIcon);
-			this.Controls.Add(this.lblNewUnitWarning);
-			this.Controls.Add(this.bttnCancel);
-			this.Controls.Add(this.lblUnitList);
-			this.Controls.Add(this.bttnSelectUnit);
-			this.Controls.Add(this.lstUnits);
-			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
-			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
-			this.MaximizeBox = false;
-			this.MinimizeBox = false;
-			this.Name = "FrmNewUnit";
-			this.ShowInTaskbar = false;
-			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
-			this.Text = "FrmNewUni";
-			this.ResumeLayout(false);
+            this.components = new System.ComponentModel.Container();
+            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmNewUnit));
+            this.lstUnits = new System.Windows.Forms.ListBox();
+            this.bttnCancel = new IBBoard.Windows.Forms.IBBButton();
+            this.lblUnitList = new IBBoard.Windows.Forms.IBBLabel();
+            this.bttnSelectUnit = new IBBoard.Windows.Forms.IBBButton();
+            this.lblNewUnitWarning = new System.Windows.Forms.Label();
+            this.imageList = new System.Windows.Forms.ImageList(this.components);
+            this.lblWarningIcon = new System.Windows.Forms.Label();
+            this.SuspendLayout();
+            // 
+            // lstUnits
+            // 
+            this.lstUnits.Location = new System.Drawing.Point(88, 8);
+            this.lstUnits.Name = "lstUnits";
+            this.lstUnits.Size = new System.Drawing.Size(272, 121);
+            this.lstUnits.TabIndex = 7;
+            this.lstUnits.SelectedIndexChanged += new System.EventHandler(this.lstUnits_SelectedIndexChanged);
+            this.lstUnits.DoubleClick += new System.EventHandler(this.lstUnits_DoubleClick);
+            this.lstUnits.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstUnits_MouseDown);
+            // 
+            // bttnCancel
+            // 
+            this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
+            this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
+            this.bttnCancel.Location = new System.Drawing.Point(8, 168);
+            this.bttnCancel.Name = "bttnCancel";
+            this.bttnCancel.Size = new System.Drawing.Size(80, 24);
+            this.bttnCancel.TabIndex = 6;
+            this.bttnCancel.Text = "Cancel";
+            this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
+            // 
+            // lblUnitList
+            // 
+            this.lblUnitList.Location = new System.Drawing.Point(0, 8);
+            this.lblUnitList.Name = "lblUnitList";
+            this.lblUnitList.Size = new System.Drawing.Size(88, 80);
+            this.lblUnitList.TabIndex = 5;
+            this.lblUnitList.Text = "unit list";
+            this.lblUnitList.TextAlign = System.Drawing.ContentAlignment.TopRight;
+            // 
+            // bttnSelectUnit
+            // 
+            this.bttnSelectUnit.Enabled = false;
+            this.bttnSelectUnit.FlatStyle = System.Windows.Forms.FlatStyle.System;
+            this.bttnSelectUnit.Location = new System.Drawing.Point(256, 168);
+            this.bttnSelectUnit.Name = "bttnSelectUnit";
+            this.bttnSelectUnit.Size = new System.Drawing.Size(104, 24);
+            this.bttnSelectUnit.TabIndex = 4;
+            this.bttnSelectUnit.Text = "create unit";
+            this.bttnSelectUnit.Click += new System.EventHandler(this.bttnSelectUnit_Click);
+            // 
+            // lblNewUnitWarning
+            // 
+            this.lblNewUnitWarning.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
+            this.lblNewUnitWarning.ImageList = this.imageList;
+            this.lblNewUnitWarning.Location = new System.Drawing.Point(48, 136);
+            this.lblNewUnitWarning.Name = "lblNewUnitWarning";
+            this.lblNewUnitWarning.Size = new System.Drawing.Size(312, 32);
+            this.lblNewUnitWarning.TabIndex = 9;
+            this.lblNewUnitWarning.Text = "One or more unit requirements are not satisfied.";
+            this.lblNewUnitWarning.TextAlign = System.Drawing.ContentAlignment.TopRight;
+            this.lblNewUnitWarning.Visible = false;
+            // 
+            // imageList
+            // 
+            this.imageList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList.ImageStream")));
+            this.imageList.TransparentColor = System.Drawing.Color.Transparent;
+            this.imageList.Images.SetKeyName(0, "");
+            // 
+            // lblWarningIcon
+            // 
+            this.lblWarningIcon.Image = ((System.Drawing.Image)(resources.GetObject("lblWarningIcon.Image")));
+            this.lblWarningIcon.Location = new System.Drawing.Point(8, 136);
+            this.lblWarningIcon.Name = "lblWarningIcon";
+            this.lblWarningIcon.Size = new System.Drawing.Size(40, 32);
+            this.lblWarningIcon.TabIndex = 10;
+            this.lblWarningIcon.Visible = false;
+            // 
+            // FrmNewUnit
+            // 
+            this.AcceptButton = this.bttnSelectUnit;
+            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
+            this.CancelButton = this.bttnCancel;
+            this.ClientSize = new System.Drawing.Size(370, 196);
+            this.Controls.Add(this.lblWarningIcon);
+            this.Controls.Add(this.lblNewUnitWarning);
+            this.Controls.Add(this.bttnCancel);
+            this.Controls.Add(this.lblUnitList);
+            this.Controls.Add(this.bttnSelectUnit);
+            this.Controls.Add(this.lstUnits);
+            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
+            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
+            this.MaximizeBox = false;
+            this.MinimizeBox = false;
+            this.Name = "FrmNewUnit";
+            this.ShowInTaskbar = false;
+            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
+            this.Text = "FrmNewUni";
+            this.ResumeLayout(false);
 
 		}
 		#endregion
@@ -201,7 +199,7 @@
 			if (lstUnits.SelectedIndex>-1)
 			{
 				bttnSelectUnit.Enabled = true;
-				lblNewUnitWarning.Visible = !allowed[lstUnits.SelectedIndex];
+				lblNewUnitWarning.Visible = !Validates.AsOkay(RequirementHandler.AllowsAdding(SelectedUnit, army));
 				lblWarningIcon.Visible = lblNewUnitWarning.Visible;
 
 				if (lblNewUnitWarning.Visible)