diff FrmReplaceUnitEquipment.cs @ 47:2f2e46cd4171

Fixes #148: "Replace equipment" dialog still uses old widgets * Update dialog to use new widget Also: * Rename some methods to match naming conventions
author IBBoard <dev@ibboard.co.uk>
date Sun, 13 Sep 2009 18:51:39 +0000
parents 1576f669b3eb
children ff80ed9ac50f
line wrap: on
line diff
--- a/FrmReplaceUnitEquipment.cs	Sun Sep 13 17:33:36 2009 +0000
+++ b/FrmReplaceUnitEquipment.cs	Sun Sep 13 18:51:39 2009 +0000
@@ -21,17 +21,13 @@
 	{
 		private Unit unit;
 		private UnitEquipmentItem oldItem;
-		private bool required;
 		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 System.Windows.Forms.Label lblItem;
 		private System.Windows.Forms.ListBox listItems;
+		private IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl equipmentAmount;
 		/// <summary>
 		/// Required designer variable.
 		/// </summary>
@@ -46,6 +42,7 @@
 			this.Text = "Replace " + item.EquipmentItem.Name + " with new equipment";
 			object[] items = Arrays.Subtract(unit.UnitType.GetEquipmentItemsByExclusionGroup(item.MutexGroup), unit.GetEquipment());
 			listItems.Items.AddRange(items);
+			equipmentAmount.SetUnit(unit);
 		}
 
 		/// <summary>
@@ -70,35 +67,14 @@
 		/// </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();
 			this.lblItem = new System.Windows.Forms.Label();
 			this.listItems = new System.Windows.Forms.ListBox();
-			((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).BeginInit();
+			this.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl();
 			this.SuspendLayout();
 			// 
-			// numEquipAmount
-			// 
-			this.numEquipAmount.Enabled = false;
-			this.numEquipAmount.Location = new System.Drawing.Point(88, 80);
-			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, 80);
-			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, 80);
@@ -112,8 +88,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, 136);
+			this.bttnCancel.Location = new System.Drawing.Point(159, 184);
 			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);
@@ -122,22 +99,13 @@
 			// 
 			this.bttnOkay.Enabled = false;
 			this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.bttnOkay.Location = new System.Drawing.Point(8, 136);
+			this.bttnOkay.Location = new System.Drawing.Point(11, 184);
 			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
-			// 
-			this.cbEquipAll.Enabled = false;
-			this.cbEquipAll.FlatStyle = System.Windows.Forms.FlatStyle.System;
-			this.cbEquipAll.Location = new System.Drawing.Point(88, 104);
-			this.cbEquipAll.Name = "cbEquipAll";
-			this.cbEquipAll.TabIndex = 5;
-			this.cbEquipAll.Text = "equip all";
-			this.cbEquipAll.CheckedChanged += new System.EventHandler(this.cbEquipAll_CheckedChanged);
-			// 
 			// lblItem
 			// 
 			this.lblItem.Location = new System.Drawing.Point(0, 8);
@@ -149,33 +117,35 @@
 			// 
 			// listItems
 			// 
-			this.listItems.Location = new System.Drawing.Point(88, 8);
+			this.listItems.Location = new System.Drawing.Point(86, 8);
 			this.listItems.Name = "listItems";
 			this.listItems.Size = new System.Drawing.Size(144, 69);
 			this.listItems.TabIndex = 7;
 			this.listItems.SelectedIndexChanged += new System.EventHandler(this.listItems_SelectedIndexChanged);
 			// 
+			// equipmentAmount
+			// 
+			this.equipmentAmount.Location = new System.Drawing.Point(86, 83);
+			this.equipmentAmount.Name = "equipmentAmount";
+			this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
+			this.equipmentAmount.TabIndex = 8;
+			// 
 			// FrmReplaceUnitEquipment
 			// 
-			this.AcceptButton = this.bttnOkay;
 			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
-			this.CancelButton = this.bttnCancel;
-			this.ClientSize = new System.Drawing.Size(240, 164);
+			this.ClientSize = new System.Drawing.Size(246, 219);
 			this.ControlBox = false;
+			this.Controls.Add(this.equipmentAmount);
 			this.Controls.Add(this.listItems);
 			this.Controls.Add(this.lblItem);
-			this.Controls.Add(this.cbEquipAll);
 			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 = "FrmReplaceUnitEquipment";
 			this.ShowInTaskbar = false;
 			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
 			this.Text = "FrmEditUnitEquipment";
-			((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).EndInit();
 			this.ResumeLayout(false);
 
 		}
@@ -183,27 +153,45 @@
 
 		private void bttnOkay_Click(object sender, System.EventArgs e)
 		{
-			setValue();
+			SetValue();
 			this.Close();
 		}
 
-		private void setValue()
+		private void SetValue()
 		{
 			UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
+			bool setValue = true;
+			double amount = equipmentAmount.EquipmentAmount;
 
-			if (cbEquipAll.Checked)
+			if (amount == 0)
 			{
-                commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, WarFoundryCore.INFINITY));
-			}
-			else
-			{
-                if (equipItem.IsRatioLimit)
+				if (equipItem.IsRequired)
 				{
-                    commandStack.Execute(new ReplaceUnitEquipmentWithRatioAmountItemCommand(unit, oldItem, equipItem, (double)numEquipAmount.Value));
+					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);
+					setValue = false;
 				}
 				else
 				{
-					commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, (int)numEquipAmount.Value));
+					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)
+					{
+						setValue = false;
+					}
+				}
+			}
+
+			double oldAmount = unit.GetEquipmentAmount(equipItem);
+
+			if (setValue && oldAmount != amount)
+			{
+				if (equipmentAmount.IsRatioEquipmentAmount)
+				{
+					commandStack.Execute(new ReplaceUnitEquipmentWithRatioAmountItemCommand(unit, oldItem, equipItem, amount));
+				}
+				else
+				{
+					commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, (int)amount));
 				}
 			}
 		}
@@ -213,61 +201,21 @@
 			this.Close();
 		}
 
-		private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e)
-		{
-			numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).MinNumber != WarFoundryCore.INFINITY && !cbEquipAll.Checked;
-			setOkayButton();
-		}
-
 		private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
 		{
-			refreshNumber();
-			setOkayButton();
+			equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
+			SetOkayButton();
 		}
 
-		private void refreshNumber()
+		private void SetOkayButton()
 		{
-			if (listItems.SelectedIndex > -1)
-			{
-				UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
-				required = equipItem.IsRequired;
-
-                if (equipItem.IsRatioLimit)
-				{
-                    numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinNumber * 100.0, 2);
-                    numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxNumber * 100.0, 2);
-					numEquipAmount.Value = numEquipAmount.Minimum;
-					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 = numEquipAmount.Minimum;
-                    cbEquipAll.Enabled = (equipItem.MaxNumber == WarFoundryCore.INFINITY && equipItem.MinNumber != WarFoundryCore.INFINITY);
-                    cbEquipAll.Checked = equipItem.MinNumber == WarFoundryCore.INFINITY;
-					numEquipAmount.Width = 144;
-				}
-			}
-			else
-			{
-				numEquipAmount.Minimum = 0;
-				numEquipAmount.Value = 0;
-				numEquipAmount.Enabled = false;
-				cbEquipAll.Enabled = false;
-			}
-		}
-
-		private void setOkayButton()
-		{
-			bttnOkay.Enabled = listItems.SelectedIndex > -1 && (cbEquipAll.Checked || (numEquipAmount.Enabled && numEquipAmount.Value > 0));
+			double equipAmount = equipmentAmount.EquipmentAmount;
+			bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !((UnitEquipmentItem)listItems.SelectedItem).IsRequired;
 		}
 
 		private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
 		{			
-			setOkayButton();
+			SetOkayButton();
 		}
 	}
 }