Mercurial > repos > IBBoard.WarFoundry.GUI.WinForms
annotate FrmEditUnitEquipment.cs @ 78:431cc3ace26c
Fixes #187: Error when adding equipment with minNum > unit size
* Separate out methods to ensure that the percentages and numbers are kept correct and that they're not messed up when editing percentage equipment items
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Oct 2009 13:39:50 +0000 |
parents | 712915de47fd |
children | c243b043aa62 |
rev | line source |
---|---|
36 | 1 // This file (FrmEditUnitEquipment.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2009 IBBoard. |
2 // | |
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. | |
4 | |
5 using System; | |
6 using System.Drawing; | |
7 using System.ComponentModel; | |
8 using System.Windows.Forms; | |
9 using IBBoard.Commands; | |
10 using IBBoard.WarFoundry.API; | |
11 using IBBoard.WarFoundry.API.Commands; | |
12 using IBBoard.WarFoundry.API.Objects; | |
13 | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
14 namespace IBBoard.WarFoundry.GUI.WinForms |
36 | 15 { |
16 /// <summary> | |
17 /// Summary description for FrmEditUnitEquipment. | |
18 /// </summary> | |
19 public class FrmEditUnitEquipment : System.Windows.Forms.Form | |
20 { | |
21 private Unit unit; | |
22 private UnitEquipmentItem equipItem; | |
23 private CommandStack commandStack; | |
24 private System.Windows.Forms.Label lblAmount; | |
25 private System.Windows.Forms.Button bttnCancel; | |
26 private System.Windows.Forms.Button bttnOkay; | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
27 private IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl equipmentAmount; |
36 | 28 /// <summary> |
29 /// Required designer variable. | |
30 /// </summary> | |
31 private System.ComponentModel.Container components = null; | |
32 | |
33 public FrmEditUnitEquipment(Unit unit, UnitEquipmentItem equipItem, CommandStack stack) | |
34 { | |
35 commandStack = stack; | |
36 this.unit = unit; | |
37 this.equipItem = equipItem; | |
38 InitializeComponent(); | |
39 this.Text = equipItem.Name + " for " + unit.Name; | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
40 equipmentAmount.SetUnit(unit); |
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
41 equipmentAmount.SetUnitEquipmentItem(equipItem); |
36 | 42 bttnOkay.Enabled = false; |
43 } | |
44 | |
45 /// <summary> | |
46 /// Clean up any resources being used. | |
47 /// </summary> | |
48 protected override void Dispose( bool disposing ) | |
49 { | |
50 if( disposing ) | |
51 { | |
52 if(components != null) | |
34
3ceb0efd261f
Re #118: Allow equipment amounts of "ratio" equipment to be define as absolute or ratio amounts
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
53 { |
36 | 54 components.Dispose(); |
55 } | |
56 } | |
57 base.Dispose( disposing ); | |
58 } | |
59 | |
60 #region Windows Form Designer generated code | |
61 /// <summary> | |
62 /// Required method for Designer support - do not modify | |
63 /// the contents of this method with the code editor. | |
64 /// </summary> | |
65 private void InitializeComponent() | |
66 { | |
67 this.lblAmount = new System.Windows.Forms.Label(); | |
68 this.bttnCancel = new System.Windows.Forms.Button(); | |
69 this.bttnOkay = new System.Windows.Forms.Button(); | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
70 this.equipmentAmount = new IBBoard.WarFoundry.GUI.WinForms.UI.EquipmentAmountControl(); |
36 | 71 this.SuspendLayout(); |
72 // | |
73 // lblAmount | |
74 // | |
75 this.lblAmount.Location = new System.Drawing.Point(8, 8); | |
76 this.lblAmount.Name = "lblAmount"; | |
77 this.lblAmount.Size = new System.Drawing.Size(72, 23); | |
78 this.lblAmount.TabIndex = 2; | |
79 this.lblAmount.Text = "amount"; | |
80 this.lblAmount.TextAlign = System.Drawing.ContentAlignment.TopRight; | |
81 // | |
82 // bttnCancel | |
83 // | |
84 this.bttnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; | |
85 this.bttnCancel.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
86 this.bttnCancel.Location = new System.Drawing.Point(153, 95); |
36 | 87 this.bttnCancel.Name = "bttnCancel"; |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
88 this.bttnCancel.Size = new System.Drawing.Size(75, 23); |
36 | 89 this.bttnCancel.TabIndex = 3; |
90 this.bttnCancel.Text = "cancel"; | |
91 this.bttnCancel.Click += new System.EventHandler(this.bttnCancel_Click); | |
92 // | |
93 // bttnOkay | |
94 // | |
95 this.bttnOkay.Enabled = false; | |
96 this.bttnOkay.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
97 this.bttnOkay.Location = new System.Drawing.Point(12, 95); |
36 | 98 this.bttnOkay.Name = "bttnOkay"; |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
99 this.bttnOkay.Size = new System.Drawing.Size(75, 23); |
36 | 100 this.bttnOkay.TabIndex = 4; |
101 this.bttnOkay.Text = "okay"; | |
102 this.bttnOkay.Click += new System.EventHandler(this.bttnOkay_Click); | |
103 // | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
104 // equipmentAmount |
36 | 105 // |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
106 this.equipmentAmount.Location = new System.Drawing.Point(88, 5); |
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
107 this.equipmentAmount.Name = "equipmentAmount"; |
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
108 this.equipmentAmount.Size = new System.Drawing.Size(155, 77); |
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
109 this.equipmentAmount.TabIndex = 5; |
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
110 this.equipmentAmount.ValueChanged += new System.EventHandler(this.equipmentAmount_ValueChanged); |
36 | 111 // |
112 // FrmEditUnitEquipment | |
113 // | |
114 this.AcceptButton = this.bttnOkay; | |
115 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); | |
116 this.CancelButton = this.bttnCancel; | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
117 this.ClientSize = new System.Drawing.Size(240, 130); |
36 | 118 this.ControlBox = false; |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
119 this.Controls.Add(this.equipmentAmount); |
36 | 120 this.Controls.Add(this.bttnOkay); |
121 this.Controls.Add(this.bttnCancel); | |
122 this.Controls.Add(this.lblAmount); | |
123 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; | |
124 this.Name = "FrmEditUnitEquipment"; | |
125 this.ShowInTaskbar = false; | |
126 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; | |
127 this.Text = "FrmEditUnitEquipment"; | |
128 this.ResumeLayout(false); | |
129 | |
130 } | |
131 #endregion | |
132 | |
133 private void bttnOkay_Click(object sender, System.EventArgs e) | |
134 { | |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
135 if (SetValue()) |
36 | 136 { |
137 this.Close(); | |
138 } | |
139 } | |
140 | |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
141 private bool SetValue() |
36 | 142 { |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
143 bool setValue = true; |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
144 double amount = equipmentAmount.EquipmentAmount; |
36 | 145 |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
146 if (amount == 0) |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
147 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
148 if (equipItem.IsRequired) |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
149 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
150 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); |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
151 setValue = false; |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
152 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
153 else |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
154 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
155 DialogResult dr = MessageBox.Show(this, "This will remove the item from the unit. Continue?", "Confirm remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1); |
36 | 156 |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
157 if (dr != DialogResult.Yes) |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
158 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
159 setValue = false; |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
160 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
161 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
162 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
163 |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
164 double oldAmount = unit.GetEquipmentAmount(equipItem); |
36 | 165 |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
166 if (setValue && oldAmount != amount) |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
167 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
168 if (equipmentAmount.IsRatioEquipmentAmount) |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
169 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
170 commandStack.Execute(new SetUnitEquipmentRatioAmountCommand(unit, equipItem, amount)); |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
171 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
172 else |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
173 { |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
174 commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, equipItem, (int)amount)); |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
175 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
176 } |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
177 |
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
178 return setValue; |
36 | 179 } |
180 | |
181 private void bttnCancel_Click(object sender, System.EventArgs e) | |
182 { | |
183 this.Close(); | |
184 } | |
185 | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
186 private void setOkayButton() |
36 | 187 { |
43
7ace7d2249ac
Fixes #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
188 double equipAmount = equipmentAmount.EquipmentAmount; |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
189 bttnOkay.Enabled = equipAmount > 0 || equipAmount == WarFoundryCore.INFINITY || !equipItem.IsRequired; |
36 | 190 } |
191 | |
42
d06c2e390a14
Re #117: Add percentage and number boxes to equipment item dialogs
IBBoard <dev@ibboard.co.uk>
parents:
36
diff
changeset
|
192 private void equipmentAmount_ValueChanged(object sender, EventArgs e) |
36 | 193 { |
194 setOkayButton(); | |
195 } | |
196 } | |
197 } |