comparison 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
comparison
equal deleted inserted replaced
46:1576f669b3eb 47:2f2e46cd4171
19 /// </summary> 19 /// </summary>
20 public class FrmReplaceUnitEquipment : System.Windows.Forms.Form 20 public class FrmReplaceUnitEquipment : System.Windows.Forms.Form
21 { 21 {
22 private Unit unit; 22 private Unit unit;
23 private UnitEquipmentItem oldItem; 23 private UnitEquipmentItem oldItem;
24 private bool required;
25 private CommandStack commandStack; 24 private CommandStack commandStack;
26
27 private System.Windows.Forms.NumericUpDown numEquipAmount;
28 private System.Windows.Forms.Label lblPercent;
29 private System.Windows.Forms.Label lblAmount; 25 private System.Windows.Forms.Label lblAmount;
30 private System.Windows.Forms.Button bttnCancel; 26 private System.Windows.Forms.Button bttnCancel;
31 private System.Windows.Forms.Button bttnOkay; 27 private System.Windows.Forms.Button bttnOkay;
32 private System.Windows.Forms.CheckBox cbEquipAll;
33 private System.Windows.Forms.Label lblItem; 28 private System.Windows.Forms.Label lblItem;
34 private System.Windows.Forms.ListBox listItems; 29 private System.Windows.Forms.ListBox listItems;
30 private IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl equipmentAmount;
35 /// <summary> 31 /// <summary>
36 /// Required designer variable. 32 /// Required designer variable.
37 /// </summary> 33 /// </summary>
38 private System.ComponentModel.Container components = null; 34 private System.ComponentModel.Container components = null;
39 35
44 oldItem = item; 40 oldItem = item;
45 InitializeComponent(); 41 InitializeComponent();
46 this.Text = "Replace " + item.EquipmentItem.Name + " with new equipment"; 42 this.Text = "Replace " + item.EquipmentItem.Name + " with new equipment";
47 object[] items = Arrays.Subtract(unit.UnitType.GetEquipmentItemsByExclusionGroup(item.MutexGroup), unit.GetEquipment()); 43 object[] items = Arrays.Subtract(unit.UnitType.GetEquipmentItemsByExclusionGroup(item.MutexGroup), unit.GetEquipment());
48 listItems.Items.AddRange(items); 44 listItems.Items.AddRange(items);
45 equipmentAmount.SetUnit(unit);
49 } 46 }
50 47
51 /// <summary> 48 /// <summary>
52 /// Clean up any resources being used. 49 /// Clean up any resources being used.
53 /// </summary> 50 /// </summary>
68 /// Required method for Designer support - do not modify 65 /// Required method for Designer support - do not modify
69 /// the contents of this method with the code editor. 66 /// the contents of this method with the code editor.
70 /// </summary> 67 /// </summary>
71 private void InitializeComponent() 68 private void InitializeComponent()
72 { 69 {
73 this.numEquipAmount = new System.Windows.Forms.NumericUpDown();
74 this.lblPercent = new System.Windows.Forms.Label();
75 this.lblAmount = new System.Windows.Forms.Label(); 70 this.lblAmount = new System.Windows.Forms.Label();
76 this.bttnCancel = new System.Windows.Forms.Button(); 71 this.bttnCancel = new System.Windows.Forms.Button();
77 this.bttnOkay = new System.Windows.Forms.Button(); 72 this.bttnOkay = new System.Windows.Forms.Button();
78 this.cbEquipAll = new System.Windows.Forms.CheckBox();
79 this.lblItem = new System.Windows.Forms.Label(); 73 this.lblItem = new System.Windows.Forms.Label();
80 this.listItems = new System.Windows.Forms.ListBox(); 74 this.listItems = new System.Windows.Forms.ListBox();
81 ((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).BeginInit(); 75 this.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl();
82 this.SuspendLayout(); 76 this.SuspendLayout();
83 //
84 // numEquipAmount
85 //
86 this.numEquipAmount.Enabled = false;
87 this.numEquipAmount.Location = new System.Drawing.Point(88, 80);
88 this.numEquipAmount.Name = "numEquipAmount";
89 this.numEquipAmount.Size = new System.Drawing.Size(144, 20);
90 this.numEquipAmount.TabIndex = 0;
91 this.numEquipAmount.ValueChanged += new System.EventHandler(this.numEquipAmount_ValueChanged);
92 //
93 // lblPercent
94 //
95 this.lblPercent.Location = new System.Drawing.Point(208, 80);
96 this.lblPercent.Name = "lblPercent";
97 this.lblPercent.Size = new System.Drawing.Size(16, 16);
98 this.lblPercent.TabIndex = 1;
99 this.lblPercent.Text = "%";
100 this.lblPercent.Visible = false;
101 // 77 //
102 // lblAmount 78 // lblAmount
103 // 79 //
104 this.lblAmount.Location = new System.Drawing.Point(8, 80); 80 this.lblAmount.Location = new System.Drawing.Point(8, 80);
105 this.lblAmount.Name = "lblAmount"; 81 this.lblAmount.Name = "lblAmount";
110 // 86 //
111 // bttnCancel 87 // bttnCancel
112 // 88 //
113 this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; 89 this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
114 this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System; 90 this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System;
115 this.bttnCancel.Location = new System.Drawing.Point(160, 136); 91 this.bttnCancel.Location = new System.Drawing.Point(159, 184);
116 this.bttnCancel.Name = "bttnCancel"; 92 this.bttnCancel.Name = "bttnCancel";
93 this.bttnCancel.Size = new System.Drawing.Size(75, 23);
117 this.bttnCancel.TabIndex = 3; 94 this.bttnCancel.TabIndex = 3;
118 this.bttnCancel.Text = "cancel"; 95 this.bttnCancel.Text = "cancel";
119 this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click); 96 this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click);
120 // 97 //
121 // bttnOkay 98 // bttnOkay
122 // 99 //
123 this.bttnOkay.Enabled = false; 100 this.bttnOkay.Enabled = false;
124 this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System; 101 this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System;
125 this.bttnOkay.Location = new System.Drawing.Point(8, 136); 102 this.bttnOkay.Location = new System.Drawing.Point(11, 184);
126 this.bttnOkay.Name = "bttnOkay"; 103 this.bttnOkay.Name = "bttnOkay";
104 this.bttnOkay.Size = new System.Drawing.Size(75, 23);
127 this.bttnOkay.TabIndex = 4; 105 this.bttnOkay.TabIndex = 4;
128 this.bttnOkay.Text = "okay"; 106 this.bttnOkay.Text = "okay";
129 this.bttnOkay.Click += new System.EventHandler(this.bttnOkay_Click); 107 this.bttnOkay.Click += new System.EventHandler(this.bttnOkay_Click);
130 //
131 // cbEquipAll
132 //
133 this.cbEquipAll.Enabled = false;
134 this.cbEquipAll.FlatStyle = System.Windows.Forms.FlatStyle.System;
135 this.cbEquipAll.Location = new System.Drawing.Point(88, 104);
136 this.cbEquipAll.Name = "cbEquipAll";
137 this.cbEquipAll.TabIndex = 5;
138 this.cbEquipAll.Text = "equip all";
139 this.cbEquipAll.CheckedChanged += new System.EventHandler(this.cbEquipAll_CheckedChanged);
140 // 108 //
141 // lblItem 109 // lblItem
142 // 110 //
143 this.lblItem.Location = new System.Drawing.Point(0, 8); 111 this.lblItem.Location = new System.Drawing.Point(0, 8);
144 this.lblItem.Name = "lblItem"; 112 this.lblItem.Name = "lblItem";
147 this.lblItem.Text = "item"; 115 this.lblItem.Text = "item";
148 this.lblItem.TextAlign = System.Drawing.ContentAlignment.TopRight; 116 this.lblItem.TextAlign = System.Drawing.ContentAlignment.TopRight;
149 // 117 //
150 // listItems 118 // listItems
151 // 119 //
152 this.listItems.Location = new System.Drawing.Point(88, 8); 120 this.listItems.Location = new System.Drawing.Point(86, 8);
153 this.listItems.Name = "listItems"; 121 this.listItems.Name = "listItems";
154 this.listItems.Size = new System.Drawing.Size(144, 69); 122 this.listItems.Size = new System.Drawing.Size(144, 69);
155 this.listItems.TabIndex = 7; 123 this.listItems.TabIndex = 7;
156 this.listItems.SelectedIndexChanged += new System.EventHandler(this.listItems_SelectedIndexChanged); 124 this.listItems.SelectedIndexChanged += new System.EventHandler(this.listItems_SelectedIndexChanged);
157 // 125 //
126 // equipmentAmount
127 //
128 this.equipmentAmount.Location = new System.Drawing.Point(86, 83);
129 this.equipmentAmount.Name = "equipmentAmount";
130 this.equipmentAmount.Size = new System.Drawing.Size(155, 77);
131 this.equipmentAmount.TabIndex = 8;
132 //
158 // FrmReplaceUnitEquipment 133 // FrmReplaceUnitEquipment
159 // 134 //
160 this.AcceptButton = this.bttnOkay;
161 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 135 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
162 this.CancelButton = this.bttnCancel; 136 this.ClientSize = new System.Drawing.Size(246, 219);
163 this.ClientSize = new System.Drawing.Size(240, 164);
164 this.ControlBox = false; 137 this.ControlBox = false;
138 this.Controls.Add(this.equipmentAmount);
165 this.Controls.Add(this.listItems); 139 this.Controls.Add(this.listItems);
166 this.Controls.Add(this.lblItem); 140 this.Controls.Add(this.lblItem);
167 this.Controls.Add(this.cbEquipAll);
168 this.Controls.Add(this.bttnOkay); 141 this.Controls.Add(this.bttnOkay);
169 this.Controls.Add(this.bttnCancel); 142 this.Controls.Add(this.bttnCancel);
170 this.Controls.Add(this.lblAmount); 143 this.Controls.Add(this.lblAmount);
171 this.Controls.Add(this.numEquipAmount);
172 this.Controls.Add(this.lblPercent);
173 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 144 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
174 this.Name = "FrmReplaceUnitEquipment"; 145 this.Name = "FrmReplaceUnitEquipment";
175 this.ShowInTaskbar = false; 146 this.ShowInTaskbar = false;
176 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; 147 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
177 this.Text = "FrmEditUnitEquipment"; 148 this.Text = "FrmEditUnitEquipment";
178 ((System.ComponentModel.ISupportInitialize)(this.numEquipAmount)).EndInit();
179 this.ResumeLayout(false); 149 this.ResumeLayout(false);
180 150
181 } 151 }
182 #endregion 152 #endregion
183 153
184 private void bttnOkay_Click(object sender, System.EventArgs e) 154 private void bttnOkay_Click(object sender, System.EventArgs e)
185 { 155 {
186 setValue(); 156 SetValue();
187 this.Close(); 157 this.Close();
188 } 158 }
189 159
190 private void setValue() 160 private void SetValue()
191 { 161 {
192 UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem; 162 UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
193 163 bool setValue = true;
194 if (cbEquipAll.Checked) 164 double amount = equipmentAmount.EquipmentAmount;
165
166 if (amount == 0)
195 { 167 {
196 commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, WarFoundryCore.INFINITY)); 168 if (equipItem.IsRequired)
169 {
170 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);
171 setValue = false;
172 }
173 else
174 {
175 DialogResult dr = MessageBox.Show(this, "This will remove the item from the unit. Continue?", "Confirm remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
176
177 if (dr != DialogResult.Yes)
178 {
179 setValue = false;
180 }
181 }
197 } 182 }
198 else 183
184 double oldAmount = unit.GetEquipmentAmount(equipItem);
185
186 if (setValue && oldAmount != amount)
199 { 187 {
200 if (equipItem.IsRatioLimit) 188 if (equipmentAmount.IsRatioEquipmentAmount)
201 { 189 {
202 commandStack.Execute(new ReplaceUnitEquipmentWithRatioAmountItemCommand(unit, oldItem, equipItem, (double)numEquipAmount.Value)); 190 commandStack.Execute(new ReplaceUnitEquipmentWithRatioAmountItemCommand(unit, oldItem, equipItem, amount));
203 } 191 }
204 else 192 else
205 { 193 {
206 commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, (int)numEquipAmount.Value)); 194 commandStack.Execute(new ReplaceUnitEquipmentWithNumericAmountItemCommand(unit, oldItem, equipItem, (int)amount));
207 } 195 }
208 } 196 }
209 } 197 }
210 198
211 private void bttnCancel_Click(object sender, System.EventArgs e) 199 private void bttnCancel_Click(object sender, System.EventArgs e)
212 { 200 {
213 this.Close(); 201 this.Close();
214 } 202 }
215 203
216 private void cbEquipAll_CheckedChanged(object sender, System.EventArgs e)
217 {
218 numEquipAmount.Enabled = ((UnitEquipmentItem)listItems.SelectedItem).MinNumber != WarFoundryCore.INFINITY && !cbEquipAll.Checked;
219 setOkayButton();
220 }
221
222 private void listItems_SelectedIndexChanged(object sender, System.EventArgs e) 204 private void listItems_SelectedIndexChanged(object sender, System.EventArgs e)
223 { 205 {
224 refreshNumber(); 206 equipmentAmount.SetUnitEquipmentItem((UnitEquipmentItem)listItems.SelectedItem);
225 setOkayButton(); 207 SetOkayButton();
226 } 208 }
227 209
228 private void refreshNumber() 210 private void SetOkayButton()
229 { 211 {
230 if (listItems.SelectedIndex > -1) 212 double equipAmount = equipmentAmount.EquipmentAmount;
231 { 213 bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !((UnitEquipmentItem)listItems.SelectedItem).IsRequired;
232 UnitEquipmentItem equipItem = (UnitEquipmentItem)listItems.SelectedItem;
233 required = equipItem.IsRequired;
234
235 if (equipItem.IsRatioLimit)
236 {
237 numEquipAmount.Minimum = (decimal)Math.Round(equipItem.MinNumber * 100.0, 2);
238 numEquipAmount.Maximum = (decimal)Math.Round(equipItem.MaxNumber * 100.0, 2);
239 numEquipAmount.Value = numEquipAmount.Minimum;
240 cbEquipAll.Enabled = false;
241 lblPercent.Visible = true;
242 numEquipAmount.Width = 120;
243 }
244 else
245 {
246 numEquipAmount.Minimum = (decimal)(equipItem.MinNumber != WarFoundryCore.INFINITY ? equipItem.MinNumber : unit.Size);
247 numEquipAmount.Maximum = (decimal)(equipItem.MaxNumber != WarFoundryCore.INFINITY ? equipItem.MaxNumber : unit.Size);
248 numEquipAmount.Value = numEquipAmount.Minimum;
249 cbEquipAll.Enabled = (equipItem.MaxNumber == WarFoundryCore.INFINITY && equipItem.MinNumber != WarFoundryCore.INFINITY);
250 cbEquipAll.Checked = equipItem.MinNumber == WarFoundryCore.INFINITY;
251 numEquipAmount.Width = 144;
252 }
253 }
254 else
255 {
256 numEquipAmount.Minimum = 0;
257 numEquipAmount.Value = 0;
258 numEquipAmount.Enabled = false;
259 cbEquipAll.Enabled = false;
260 }
261 }
262
263 private void setOkayButton()
264 {
265 bttnOkay.Enabled = listItems.SelectedIndex > -1 && (cbEquipAll.Checked || (numEquipAmount.Enabled && numEquipAmount.Value > 0));
266 } 214 }
267 215
268 private void numEquipAmount_ValueChanged(object sender, System.EventArgs e) 216 private void numEquipAmount_ValueChanged(object sender, System.EventArgs e)
269 { 217 {
270 setOkayButton(); 218 SetOkayButton();
271 } 219 }
272 } 220 }
273 } 221 }