diff FrmEditUnitEquipment.cs @ 42:d06c2e390a14

Re #117: Add percentage and number boxes to equipment item dialogs * Add new widget to "Edit equipment" dialog * Still needs "OK" button functionality re-implementing
author IBBoard <dev@ibboard.co.uk>
date Sun, 06 Sep 2009 19:58:39 +0000
parents 6ab7ddc038f9
children 7ace7d2249ac
line wrap: on
line diff
--- a/FrmEditUnitEquipment.cs	Sun Sep 06 19:31:49 2009 +0000
+++ b/FrmEditUnitEquipment.cs	Sun Sep 06 19:58:39 2009 +0000
@@ -12,7 +12,7 @@
 using IBBoard.WarFoundry.API.Commands;
 using IBBoard.WarFoundry.API.Objects;
 
-namespace IBBoard.WarFoundry
+namespace IBBoard.WarFoundry.GUI.WinForms
 {
 	/// <summary>
 	/// Summary description for FrmEditUnitEquipment.
@@ -21,15 +21,11 @@
 	{
 		private Unit unit;
 		private UnitEquipmentItem equipItem;
-		private double equipAmount;
 		private CommandStack commandStack;
-
-		private System.Windows.Forms.NumericUpDown numEquipAmount;
-		private System.Windows.Forms.Label lblPercent;
 		private System.Windows.Forms.Label lblAmount;
 		private System.Windows.Forms.Button bttnCancel;
 		private System.Windows.Forms.Button bttnOkay;
-		private System.Windows.Forms.CheckBox cbEquipAll;
+		private IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl equipmentAmount;
 		/// <summary>
 		/// Required designer variable.
 		/// </summary>
@@ -42,28 +38,9 @@
 			this.equipItem = equipItem;
 			InitializeComponent();
 			this.Text = equipItem.Name + " for " + unit.Name;
-			equipAmount = unit.GetEquipmentAmount(equipItem);
-
-			if (equipItem.IsRatioLimit)
-			{
-				numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinPercentage, 2);
-				numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxPercentage, 2);
-				numEquipAmount.Value = (decimal)Math.Round(equipAmount, 2);
-				cbEquipAll.Enabled = false;
-				lblPercent.Visible = true;
-				numEquipAmount.Width = 120;
-			}
-			else
-			{
-				numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != WarFoundryCore.INFINITY ? equipItem.MinNumber : unit.Size);
-				numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != WarFoundryCore.INFINITY ? equipItem.MaxNumber : unit.Size);
-				numEquipAmount.Value = (decimal)(equipAmount == WarFoundryCore.INFINITY ? unit.Size : equipAmount);
-				cbEquipAll.Checked = (equipAmount == WarFoundryCore.INFINITY);
-				cbEquipAll.Enabled = (equipItem.MaxNumber == WarFoundryCore.INFINITY && equipItem.MinNumber != WarFoundryCore.INFINITY);
-				numEquipAmount.Enabled = !cbEquipAll.Checked && equipItem.MinNumber != WarFoundryCore.INFINITY;
-				numEquipAmount.Width = 144;
-			}
-
+			equipmentAmount.SetUnit(unit);
+			equipmentAmount.SetUnitEquipmentItem(equipItem);
+			equipmentAmount.SetUnitEquipmentItemAmount(unit.GetEquipmentAmount(equipItem), unit.GetEquipmentAmountIsRatio(equipItem));
 			bttnOkay.Enabled = false;
 		}
 
@@ -89,32 +66,12 @@
 		/// </summary>
 		private void InitializeComponent()
 		{
-			this.numEquipAmount = new System.Windows.Forms.NumericUpDown();
-			this.lblPercent = new System.Windows.Forms.Label();
 			this.lblAmount = new System.Windows.Forms.Label();
 			this.bttnCancel = new System.Windows.Forms.Button();
 			this.bttnOkay = new System.Windows.Forms.Button();
-			this.cbEquipAll = new System.Windows.Forms.CheckBox();
-			((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).BeginInit();
+			this.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl();
 			this.SuspendLayout();
 			// 
-			// numEquipAmount
-			// 
-			this.numEquipAmount.Location = new System.Drawing.Point(88, 8);
-			this.numEquipAmount.Name = "numEquipAmount";
-			this.numEquipAmount.Size = new System.Drawing.Size(144, 20);
-			this.numEquipAmount.TabIndex = 0;
-			this.numEquipAmount.ValueChanged += new System.EventHandler(this.numEquipAmount_ValueChanged);
-			// 
-			// lblPercent
-			// 
-			this.lblPercent.Location = new System.Drawing.Point(208, 8);
-			this.lblPercent.Name = "lblPercent";
-			this.lblPercent.Size = new System.Drawing.Size(16, 16);
-			this.lblPercent.TabIndex = 1;
-			this.lblPercent.Text = "%";
-			this.lblPercent.Visible = false;
-			// 
 			// lblAmount
 			// 
 			this.lblAmount.Location = new System.Drawing.Point(8, 8);
@@ -128,8 +85,9 @@
 			// 
 			this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
 			this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.bttnCancel.Location = new System.Drawing.Point(160, 64);
+			this.bttnCancel.Location = new System.Drawing.Point(153, 95);
 			this.bttnCancel.Name = "bttnCancel";
+			this.bttnCancel.Size = new System.Drawing.Size(75, 23);
 			this.bttnCancel.TabIndex = 3;
 			this.bttnCancel.Text = "cancel";
 			this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
@@ -138,41 +96,37 @@
 			// 
 			this.bttnOkay.Enabled = false;
 			this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.bttnOkay.Location = new System.Drawing.Point(8, 64);
+			this.bttnOkay.Location = new System.Drawing.Point(12, 95);
 			this.bttnOkay.Name = "bttnOkay";
+			this.bttnOkay.Size = new System.Drawing.Size(75, 23);
 			this.bttnOkay.TabIndex = 4;
 			this.bttnOkay.Text = "okay";
 			this.bttnOkay.Click += new System.EventHandler(this.bttnOkay_Click);
 			// 
-			// cbEquipAll
+			// equipmentAmount
 			// 
-			this.cbEquipAll.Enabled = false;
-			this.cbEquipAll.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.cbEquipAll.Location = new System.Drawing.Point(88, 32);
-			this.cbEquipAll.Name = "cbEquipAll";
-			this.cbEquipAll.TabIndex = 5;
-			this.cbEquipAll.Text = "equip all";
-			this.cbEquipAll.CheckedChanged += new System.EventHandler(this.cbEquipAll_CheckedChanged);
+			this.equipmentAmount.Location = new System.Drawing.Point(88, 5);
+			this.equipmentAmount.Name = "equipmentAmount";
+			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
+			this.equipmentAmount.TabIndex = 5;
+			this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged);
 			// 
 			// FrmEditUnitEquipment
 			// 
 			this.AcceptButton = this.bttnOkay;
 			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
 			this.CancelButton = this.bttnCancel;
-			this.ClientSize = new System.Drawing.Size(240, 92);
+			this.ClientSize = new System.Drawing.Size(240, 130);
 			this.ControlBox = false;
-			this.Controls.Add(this.cbEquipAll);
+			this.Controls.Add(this.equipmentAmount);
 			this.Controls.Add(this.bttnOkay);
 			this.Controls.Add(this.bttnCancel);
 			this.Controls.Add(this.lblAmount);
-			this.Controls.Add(this.numEquipAmount);
-			this.Controls.Add(this.lblPercent);
 			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
 			this.Name = "FrmEditUnitEquipment";
 			this.ShowInTaskbar = false;
 			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
 			this.Text = "FrmEditUnitEquipment";
-			((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).EndInit();
 			this.ResumeLayout(false);
 
 		}
@@ -188,44 +142,45 @@
 
 		private bool setValue()
 		{
-			if ((!numEquipAmount.Enabled || numEquipAmount.Value == 0) && !cbEquipAll.Checked && unit.GetEquipmentAmount(equipItem) != 0)
-			{
-				if (equipItem.IsRequired)
-				{
-					MessageBox.Show(this, "This item is required and cannot be removed. It must have a quantity of at least one or be replaced by an alternative.", "Required item", MessageBoxButtons.OK, MessageBoxIcon.Warning);
-					return false;
-				}
-				else
-				{
-					DialogResult dr = MessageBox.Show(this, "This will remove the item from the unit. Continue?", "Confirm remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
+			return false;
+			//if (!bttnOkay.Enabled)
+			//{
+			//    if (equipItem.IsRequired)
+			//    {
+			//        MessageBox.Show(this, "This item is required and cannot be removed. It must have a quantity of at least one or be replaced by an alternative.", "Required item", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+			//        return false;
+			//    }
+			//    else
+			//    {
+			//        DialogResult dr = MessageBox.Show(this, "This will remove the item from the unit. Continue?", "Confirm remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
 
-					if (dr!=DialogResult.Yes)
-					{
-						return false;
-					}
-				}
-			}
+			//        if (dr!=DialogResult.Yes)
+			//        {
+			//            return false;
+			//        }
+			//    }
+			//}
 
-			if (cbEquipAll.Checked)
-			{
-				if (equipAmount != WarFoundryCore.INFINITY)
-				{
-					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, WarFoundryCore.INFINITY));
-				}
-			}
-			else if (numEquipAmount.Value != (decimal)equipAmount)
-			{
-				if (equipItem.IsRatioLimit)
-				{
-					commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, (double)numEquipAmount.Value));
-				}
-				else
-				{
-					commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)numEquipAmount.Value));
-				}
-			}
+			//if (cbEquipAll.Checked)
+			//{
+			//    if (equipAmount != WarFoundryCore.INFINITY)
+			//    {
+			//        commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, WarFoundryCore.INFINITY));
+			//    }
+			//}
+			//else if (numEquipAmount.Value != (decimal)equipAmount)
+			//{
+			//    if (equipItem.IsRatioLimit)
+			//    {
+			//        commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, (double)numEquipAmount.Value));
+			//    }
+			//    else
+			//    {
+			//        commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)numEquipAmount.Value));
+			//    }
+			//}
 
-			return true;
+			//return true;
 		}
 
 		private void bttnCancel_Click(object sender, System.EventArgs e)
@@ -233,18 +188,13 @@
 			this.Close();
 		}
 
-		private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e)
+		private void setOkayButton()
 		{
-			numEquipAmount.Enabled = !cbEquipAll.Checked && equipItem.MinNumber != WarFoundryCore.INFINITY;
-			setOkayButton();
+			double equipAmount = 0;// equipmentAmount.EquipmentAmount;
+			bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !equipItem.IsRequired;
 		}
 
-		private void setOkayButton()
-		{
-			bttnOkay.Enabled = (cbEquipAll.Checked || (numEquipAmount.Enabled && (numEquipAmount.Value > 0 || !equipItem.IsRequired)));
-		}
-
-		private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
+		private void equipmentAmount_ValueChanged(object sender, EventArgs e)
 		{
 			setOkayButton();
 		}