comparison FrmReplaceUnitEquipment.cs @ 0:7dd160dacb60

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