Mercurial > repos > IBDev-IBBoard.WarFoundry.GUI.WinForms
annotate FrmUnit.cs @ 225:5233147ca7e4
Re #101: Make army names and sizes modifiable after creation
* Add form for editing name and size
* Add edit options from army tree
* Add edit options from Edit menu
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 29 Aug 2011 20:06:54 +0100 |
parents | eaa7b639d390 |
children | 72beddaffb71 |
rev | line source |
---|---|
193 | 1 // This file (FrmUnit.cs) is a part of the IBBoard.WarFoundry.GUI.WinForms project and is copyright 2007, 2008, 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.Data; | |
8 using System.Collections.Generic; | |
9 using System.ComponentModel; | |
10 using System.Windows.Forms; | |
11 using IBBoard.Commands; | |
12 using IBBoard.Lang; | |
13 using IBBoard.Windows.Forms; | |
14 using IBBoard.Windows.Forms.I18N; | |
15 using IBBoard.WarFoundry.API; | |
16 using IBBoard.WarFoundry.API.Commands; | |
17 using IBBoard.WarFoundry.API.Objects; | |
18 using IBBoard.WarFoundry.GUI.WinForms.UI; | |
19 using IBBoard.WarFoundry.GUI.WinForms.Util; | |
20 using log4net; | |
21 | |
22 namespace IBBoard.WarFoundry.GUI.WinForms | |
23 { | |
24 /// <summary> | |
25 /// Summary description for FrmUnit. | |
26 /// </summary> | |
27 public class FrmUnit : IBBoard.Windows.Forms.IBBForm | |
28 { | |
29 private static readonly ILog log = LogManager.GetLogger(typeof(FrmUnit)); | |
30 private static readonly int BORDER_WIDTH = 1; | |
31 private static readonly int NAME_COL_WIDTH_MULTIPLIER = 3; | |
32 private Unit unit; | |
33 private Dictionary<UnitEquipmentItem, UnitEquipmentChoice> equipmentChoices = new Dictionary<UnitEquipmentItem, UnitEquipmentChoice>(); | |
34 private Dictionary<string, DataGridView> DataGridViews = new Dictionary<string, DataGridView>(); | |
35 private CommandStack commandStack; | |
36 private System.Windows.Forms.TextBox tbUnitName; | |
37 private System.Windows.Forms.NumericUpDown unitSize; | |
38 private IBBLabel lblUnitSize; | |
39 private IBBButton bttnAddWeapon; | |
40 private IBBButton bttnRemoveWeapon; | |
41 private IBBLabel lblEquip; | |
42 private System.Windows.Forms.ListBox equipmentList; | |
43 private IBBButton bttnReplaceWeapon; | |
44 private IBBButton bttnEditWeapon; | |
45 private Label lblPoints; | |
46 private IBBLabel lblNotes; | |
47 private TextBox notes; | |
48 private ListBox abilitiesList; | |
49 private IBBLabel lblAbilities; | |
190 | 50 private FlowLayoutPanel statsPanel; |
51 public GameSystem CurrentGameSystem | |
52 { | |
53 get { return WarFoundryCore.CurrentGameSystem; } | |
54 set { WarFoundryCore.CurrentGameSystem = value; } | |
193 | 55 } |
56 | |
57 /// <summary> | |
58 /// Required designer variable. | |
59 /// </summary> | |
60 private System.ComponentModel.Container components = null; | |
61 | |
62 public FrmUnit(Unit toDisplay, CommandStack cmdStack) | |
63 { | |
64 unit = toDisplay; | |
65 commandStack = cmdStack; | |
66 // | |
67 // Required for Windows Form Designer support | |
68 // | |
69 InitializeComponent(); | |
70 TranslateForm(); | |
71 Translation.TranslationChanged += new MethodInvoker(TranslateForm); | |
72 unit.NameChanged += new StringValChangedDelegate(unit_NameChanged); | |
73 unit.UnitSizeChanged += new IntValChangedDelegate(unit_UnitSizeChanged); | |
74 unit.UnitEquipmentAmountChanged += new DoubleValChangedDelegate(unit_UnitEquipmentAmountChanged); | |
75 unit.PointsValueChanged += new DoubleValChangedDelegate(unit_PointsValueChanged); | |
76 | |
77 unitSize.Value = unit.Size; | |
78 unitSize.Maximum = (unit.UnitType.MaxSize == WarFoundryCore.INFINITY ? int.MaxValue : unit.UnitType.MaxSize); | |
79 unitSize.Minimum = unit.UnitType.MinSize; | |
80 unitSize.Enabled = (unitSize.Maximum != unitSize.Minimum); | |
81 | |
82 notes.Text = unit.UnitType.Notes; | |
83 abilitiesList.DataSource = new List<Ability>(unit.UnitType.GetRequiredAbilities()); | |
84 abilitiesList.DisplayMember = "Name"; | |
85 SetPointsValueText(); | |
86 SetStats(); | |
87 SetWeapons(); | |
88 } | |
89 | |
90 private void TranslateForm() | |
91 { | |
92 ControlTranslator.TranslateControl(this); | |
93 SetUnitName(); | |
94 RefreshUnitEquipment(); | |
95 } | |
96 | |
97 private void SetUnitName() | |
98 { | |
99 tbUnitName.Text = unit.Name; | |
100 Text = Translation.GetTranslation("FrmUnit", "{0} ({1})", unit.Name, unit.UnitType.Name); | |
101 } | |
102 | |
103 void unit_PointsValueChanged(WarFoundryObject obj, double oldValue, double newValue) | |
104 { | |
105 SetPointsValueText(); | |
106 } | |
107 | |
108 private void SetPointsValueText() | |
109 { | |
110 lblPoints.Text = Translation.GetTranslation("FrmUnitlblPoints", "(" + unit.Points + " pts)", unit.Points, CurrentGameSystem.GetPointsAbbrev(unit.Points)); | |
111 } | |
112 | |
113 private void SetStats() | |
114 { | |
115 Stat[][] stats = unit.UnitStatsArraysWithName; | |
116 string[] statsIDs = unit.UnitStatsArrayIDs; | |
117 int statsCount = stats.Length; | |
118 log.DebugFormat("Unit {0} has {1} stats arrays", unit.UnitType.Name, statsCount); | |
119 | |
120 for (int i = 0; i < statsCount; i++) | |
121 { | |
122 DataGridView statsGrid = GetDataGridView(statsIDs[i]); | |
123 DataTable dt = (DataTable)statsGrid.DataSource; | |
124 DataRow dr = dt.NewRow(); | |
125 dr.ItemArray = stats[i]; | |
126 log.DebugFormat("Add row to data table for {0}", statsIDs[i]); | |
127 dt.Rows.Add(dr); | |
128 statsGrid.ClearSelection(); | |
129 } | |
130 } | |
131 | |
132 private DataGridView GetDataGridView(string statsID) | |
133 { | |
134 DataGridView grid; | |
135 | |
136 if (DataGridViews.ContainsKey(statsID)) | |
137 { | |
138 grid = DictionaryUtils.GetValue(DataGridViews, statsID); | |
139 } | |
140 else | |
141 { | |
142 grid = CreateDataGridView(statsID); | |
143 DataGridViews[statsID] = grid; | |
144 } | |
145 | |
146 return grid; | |
147 } | |
148 | |
149 private DataGridView CreateDataGridView(string statsID) | |
150 { | |
151 log.DebugFormat("Create DataGridView for stats ID {0}", statsID); | |
152 SystemStats sysStats = unit.Race.GameSystem.GetSystemStatsForID(statsID); | |
153 StatSlot[] sysStatSlots = sysStats.StatSlots; | |
154 int statsCount = sysStatSlots.Length; | |
155 int statsWithNameCount = statsCount + 1; | |
156 StatSlot[] statsWithName = new StatSlot[statsWithNameCount]; | |
157 statsWithName[0] = new StatSlot("Name"); | |
158 sysStatSlots.CopyTo(statsWithName, 1); | |
159 DataTable dt = new DataTable(); | |
160 DataGridView statsGrid = CreateDataGridView(); | |
161 statsGrid.DataSource = dt; | |
162 int columnWidth = statsGrid.Width / (statsCount + NAME_COL_WIDTH_MULTIPLIER); | |
163 | |
164 for (int i = 0; i < statsWithNameCount; i++) | |
165 { | |
166 StatSlot stat = statsWithName[i]; | |
167 string slotName = stat.Name; | |
168 statsGrid.Columns.Add(CreateStatColumn(slotName, columnWidth)); | |
169 dt.Columns.Add(CreateDataColumn(slotName)); | |
170 } | |
171 | |
172 int otherStatsWidth = statsCount * columnWidth; | |
173 SetNameColumnWidth(statsGrid, otherStatsWidth); | |
174 | |
175 return statsGrid; | |
176 } | |
177 | |
178 private static DataGridViewColumn CreateStatColumn(string slotName, int columnWidth) | |
179 { | |
180 DataGridViewColumn col = new DataGridViewTextBoxColumn(); | |
181 col.Width = columnWidth; | |
182 col.Name = slotName; | |
183 col.HeaderText = slotName; | |
184 col.DataPropertyName = slotName; | |
185 col.SortMode = DataGridViewColumnSortMode.NotSortable; | |
186 col.CellTemplate = new StatsDataGridViewCell(); | |
187 col.HeaderCell.Style.WrapMode = DataGridViewTriState.False; | |
188 return col; | |
189 } | |
190 | |
191 private static void SetNameColumnWidth(DataGridView statsGrid, int otherStatsWidth) | |
192 { | |
193 | |
194 DataGridViewColumn nameColumn = statsGrid.Columns[0]; | |
195 nameColumn.HeaderText = Translation.GetTranslation("StatLineName", "name"); | |
196 nameColumn.Width = statsGrid.Width - otherStatsWidth; | |
197 } | |
198 | |
199 private static DataColumn CreateDataColumn(string slotName) | |
200 { | |
201 log.DebugFormat("Create column {0}", slotName); | |
202 DataColumn tempCol = new DataColumn(slotName, typeof(Stat)); | |
203 return tempCol; | |
204 } | |
205 | |
206 public DataGridView CreateDataGridView() | |
207 { | |
208 log.Debug("Create DataGridView widget"); | |
209 DataGridView statsGrid = new DataGridView(); | |
210 statsGrid.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
211 | System.Windows.Forms.AnchorStyles.Right))); | |
212 statsGrid.CausesValidation = false; | |
213 statsGrid.ReadOnly = true; | |
214 statsGrid.RowHeadersVisible = false; | |
215 statsGrid.Size = new System.Drawing.Size(600, 88); | |
216 statsGrid.TabStop = false; | |
217 statsGrid.AllowUserToAddRows = false; | |
218 statsGrid.ScrollBars = ScrollBars.None; | |
219 statsGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; | |
220 statsGrid.BorderStyle = BorderStyle.None; | |
221 statsGrid.BackgroundColor = SystemColors.Control; | |
222 statsGrid.RowsAdded += new DataGridViewRowsAddedEventHandler(statsGrid_RowsAdded); | |
223 statsPanel.Controls.Add(statsGrid); | |
224 statsGrid.Width = statsPanel.Width - (int)Math.Round(SystemInformation.VerticalScrollBarWidth * 1.4); | |
225 return statsGrid; | |
226 } | |
227 | |
228 private void statsGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) | |
229 { | |
230 if (sender is DataGridView) | |
231 { | |
232 SetGridHeight((DataGridView)sender); | |
233 } | |
234 } | |
235 | |
236 private static void SetGridHeight(DataGridView statsGrid) | |
237 { | |
238 DataGridViewRowCollection rows = statsGrid.Rows; | |
239 statsGrid.Height = statsGrid.Columns[0].HeaderCell.Size.Height + (rows.Count * rows[0].Height); | |
240 log.DebugFormat("Set height to {0} for grid of {1} rows", statsGrid.Height, rows.Count); | |
241 } | |
242 | |
243 private void SetWeapons() | |
244 { | |
245 foreach (UnitEquipmentItem item in unit.GetEquipment()) | |
246 { | |
247 equipmentList.Items.Add(GetEquipmentChoice(item)); | |
248 } | |
249 } | |
250 | |
251 private UnitEquipmentChoice GetEquipmentChoice(UnitEquipmentItem item) | |
252 { | |
253 UnitEquipmentChoice choice = null; | |
254 equipmentChoices.TryGetValue(item, out choice); | |
255 | |
256 if (choice == null) | |
257 { | |
258 choice = new UnitEquipmentChoice(Unit, item); | |
259 equipmentChoices[item] = choice; | |
260 } | |
261 | |
262 return choice; | |
263 } | |
264 | |
265 /// <summary> | |
266 /// Clean up any resources being used. | |
267 /// </summary> | |
268 protected override void Dispose(bool disposing) | |
269 { | |
270 //remove our leave events so that disposing doesn't trigger them | |
271 tbUnitName.Leave -= new System.EventHandler(this.tbUnitName_Leave); | |
272 unitSize.Leave -= new System.EventHandler(this.unitSize_Leave); | |
273 | |
274 if (disposing) | |
275 { | |
276 if (components != null) | |
277 { | |
278 components.Dispose(); | |
279 } | |
280 } | |
281 base.Dispose(disposing); | |
282 } | |
283 | |
284 #region Windows Form Designer generated code | |
285 /// <summary> | |
286 /// Required method for Designer support - do not modify | |
287 /// the contents of this method with the code editor. | |
288 /// </summary> | |
289 private void InitializeComponent() | |
290 { | |
291 this.tbUnitName = new System.Windows.Forms.TextBox(); | |
292 this.unitSize = new System.Windows.Forms.NumericUpDown(); | |
293 this.lblUnitSize = new IBBoard.Windows.Forms.IBBLabel(); | |
294 this.lblEquip = new IBBoard.Windows.Forms.IBBLabel(); | |
295 this.bttnAddWeapon = new IBBoard.Windows.Forms.IBBButton(); | |
296 this.bttnRemoveWeapon = new IBBoard.Windows.Forms.IBBButton(); | |
297 this.equipmentList = new System.Windows.Forms.ListBox(); | |
298 this.bttnReplaceWeapon = new IBBoard.Windows.Forms.IBBButton(); | |
299 this.bttnEditWeapon = new IBBoard.Windows.Forms.IBBButton(); | |
300 this.lblPoints = new System.Windows.Forms.Label(); | |
301 this.lblNotes = new IBBoard.Windows.Forms.IBBLabel(); | |
302 this.notes = new System.Windows.Forms.TextBox(); | |
303 this.abilitiesList = new System.Windows.Forms.ListBox(); | |
304 this.lblAbilities = new IBBoard.Windows.Forms.IBBLabel(); | |
305 this.statsPanel = new System.Windows.Forms.FlowLayoutPanel(); | |
306 ((System.ComponentModel.ISupportInitialize)(this.unitSize)).BeginInit(); | |
307 this.SuspendLayout(); | |
308 // | |
309 // tbUnitName | |
310 // | |
311 this.tbUnitName.Location = new System.Drawing.Point(8, 8); | |
312 this.tbUnitName.Name = "tbUnitName"; | |
313 this.tbUnitName.Size = new System.Drawing.Size(344, 20); | |
314 this.tbUnitName.TabIndex = 1; | |
315 this.tbUnitName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tbUnitName_KeyDown); | |
316 this.tbUnitName.Leave += new System.EventHandler(this.tbUnitName_Leave); | |
317 // | |
318 // unitSize | |
319 // | |
320 this.unitSize.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
321 this.unitSize.Location = new System.Drawing.Point(528, 8); | |
322 this.unitSize.Name = "unitSize"; | |
323 this.unitSize.Size = new System.Drawing.Size(80, 20); | |
324 this.unitSize.TabIndex = 1; | |
325 this.unitSize.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; | |
326 this.unitSize.Value = new decimal(new int[] { | |
327 1, | |
328 0, | |
329 0, | |
330 0}); | |
331 this.unitSize.Leave += new System.EventHandler(this.unitSize_Leave); | |
332 this.unitSize.KeyDown += new System.Windows.Forms.KeyEventHandler(this.unitSize_KeyDown); | |
333 // | |
334 // lblUnitSize | |
335 // | |
336 this.lblUnitSize.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
337 this.lblUnitSize.Location = new System.Drawing.Point(426, 8); | |
338 this.lblUnitSize.Name = "lblUnitSize"; | |
339 this.lblUnitSize.Size = new System.Drawing.Size(98, 23); | |
340 this.lblUnitSize.TabIndex = 0; | |
341 this.lblUnitSize.Text = "unit size"; | |
342 this.lblUnitSize.TextAlign = System.Drawing.ContentAlignment.TopRight; | |
343 // | |
344 // lblEquip | |
345 // | |
346 this.lblEquip.Location = new System.Drawing.Point(15, 126); | |
347 this.lblEquip.Name = "lblEquip"; | |
348 this.lblEquip.Size = new System.Drawing.Size(81, 108); | |
349 this.lblEquip.TabIndex = 3; | |
350 this.lblEquip.Text = "equipment"; | |
351 this.lblEquip.TextAlign = System.Drawing.ContentAlignment.TopRight; | |
352 // | |
353 // bttnAddWeapon | |
354 // | |
355 this.bttnAddWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
356 this.bttnAddWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
357 this.bttnAddWeapon.Location = new System.Drawing.Point(516, 126); | |
358 this.bttnAddWeapon.Name = "bttnAddWeapon"; | |
359 this.bttnAddWeapon.Size = new System.Drawing.Size(88, 22); | |
360 this.bttnAddWeapon.TabIndex = 4; | |
361 this.bttnAddWeapon.Text = "add"; | |
362 this.bttnAddWeapon.Click += new System.EventHandler(this.bttnAddWeapon_Click); | |
363 // | |
364 // bttnRemoveWeapon | |
365 // | |
366 this.bttnRemoveWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
367 this.bttnRemoveWeapon.Enabled = false; | |
368 this.bttnRemoveWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
369 this.bttnRemoveWeapon.Location = new System.Drawing.Point(516, 210); | |
370 this.bttnRemoveWeapon.Name = "bttnRemoveWeapon"; | |
371 this.bttnRemoveWeapon.Size = new System.Drawing.Size(88, 22); | |
372 this.bttnRemoveWeapon.TabIndex = 5; | |
373 this.bttnRemoveWeapon.Text = "remove"; | |
374 this.bttnRemoveWeapon.Click += new System.EventHandler(this.bttnRemoveWeapon_Click); | |
375 // | |
376 // equipmentList | |
377 // | |
378 this.equipmentList.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
379 | System.Windows.Forms.AnchorStyles.Right))); | |
380 this.equipmentList.Location = new System.Drawing.Point(102, 126); | |
381 this.equipmentList.Name = "equipmentList"; | |
382 this.equipmentList.Size = new System.Drawing.Size(408, 108); | |
383 this.equipmentList.TabIndex = 6; | |
384 this.equipmentList.SelectedIndexChanged += new System.EventHandler(this.equipmentList_SelectedIndexChanged); | |
385 this.equipmentList.DoubleClick += new System.EventHandler(this.equipmentList_DoubleClick); | |
386 // | |
387 // bttnReplaceWeapon | |
388 // | |
389 this.bttnReplaceWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
390 this.bttnReplaceWeapon.Enabled = false; | |
391 this.bttnReplaceWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
392 this.bttnReplaceWeapon.Location = new System.Drawing.Point(516, 182); | |
393 this.bttnReplaceWeapon.Name = "bttnReplaceWeapon"; | |
394 this.bttnReplaceWeapon.Size = new System.Drawing.Size(88, 22); | |
395 this.bttnReplaceWeapon.TabIndex = 10; | |
396 this.bttnReplaceWeapon.Text = "replace"; | |
397 this.bttnReplaceWeapon.Click += new System.EventHandler(this.bttnReplaceWeapon_Click); | |
398 // | |
399 // bttnEditWeapon | |
400 // | |
401 this.bttnEditWeapon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); | |
402 this.bttnEditWeapon.Enabled = false; | |
403 this.bttnEditWeapon.FlatStyle = System.Windows.Forms.FlatStyle.System; | |
404 this.bttnEditWeapon.Location = new System.Drawing.Point(516, 154); | |
405 this.bttnEditWeapon.Name = "bttnEditWeapon"; | |
406 this.bttnEditWeapon.Size = new System.Drawing.Size(88, 22); | |
407 this.bttnEditWeapon.TabIndex = 11; | |
408 this.bttnEditWeapon.Text = "edit"; | |
409 this.bttnEditWeapon.Click += new System.EventHandler(this.bttnEditWeapon_Click); | |
410 // | |
411 // lblPoints | |
412 // | |
413 this.lblPoints.Location = new System.Drawing.Point(358, 8); | |
414 this.lblPoints.Name = "lblPoints"; | |
415 this.lblPoints.Size = new System.Drawing.Size(77, 21); | |
416 this.lblPoints.TabIndex = 12; | |
417 this.lblPoints.Text = "(points)"; | |
418 // | |
419 // lblNotes | |
420 // | |
421 this.lblNotes.Location = new System.Drawing.Point(13, 317); | |
422 this.lblNotes.Name = "lblNotes"; | |
423 this.lblNotes.Size = new System.Drawing.Size(84, 62); | |
424 this.lblNotes.TabIndex = 13; | |
425 this.lblNotes.Text = "notes"; | |
426 this.lblNotes.TextAlign = System.Drawing.ContentAlignment.TopRight; | |
427 // | |
428 // notes | |
429 // | |
430 this.notes.Location = new System.Drawing.Point(102, 317); | |
431 this.notes.Multiline = true; | |
432 this.notes.Name = "notes"; | |
433 this.notes.ReadOnly = true; | |
434 this.notes.Size = new System.Drawing.Size(408, 62); | |
435 this.notes.TabIndex = 14; | |
436 // | |
437 // abilitiesList | |
438 // | |
439 this.abilitiesList.FormattingEnabled = true; | |
440 this.abilitiesList.Location = new System.Drawing.Point(102, 240); | |
441 this.abilitiesList.Name = "abilitiesList"; | |
442 this.abilitiesList.Size = new System.Drawing.Size(408, 69); | |
443 this.abilitiesList.TabIndex = 15; | |
444 // | |
445 // lblAbilities | |
446 // | |
447 this.lblAbilities.Location = new System.Drawing.Point(13, 240); | |
448 this.lblAbilities.Name = "lblAbilities"; | |
449 this.lblAbilities.Size = new System.Drawing.Size(84, 62); | |
450 this.lblAbilities.TabIndex = 16; | |
451 this.lblAbilities.Text = "abilities"; | |
452 this.lblAbilities.TextAlign = System.Drawing.ContentAlignment.TopRight; | |
453 // | |
454 // statsPanel | |
455 // | |
456 this.statsPanel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | |
457 | System.Windows.Forms.AnchorStyles.Right))); | |
458 this.statsPanel.AutoScroll = true; | |
459 this.statsPanel.Location = new System.Drawing.Point(8, 35); | |
460 this.statsPanel.Name = "statsPanel"; | |
461 this.statsPanel.Size = new System.Drawing.Size(600, 85); | |
462 this.statsPanel.TabIndex = 17; | |
463 // | |
464 // FrmUnit | |
465 // | |
466 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); | |
467 this.ClientSize = new System.Drawing.Size(616, 391); | |
468 this.Controls.Add(this.statsPanel); | |
469 this.Controls.Add(this.lblAbilities); | |
470 this.Controls.Add(this.abilitiesList); | |
471 this.Controls.Add(this.notes); | |
472 this.Controls.Add(this.lblNotes); | |
473 this.Controls.Add(this.lblPoints); | |
474 this.Controls.Add(this.bttnEditWeapon); | |
475 this.Controls.Add(this.bttnReplaceWeapon); | |
476 this.Controls.Add(this.equipmentList); | |
477 this.Controls.Add(this.bttnRemoveWeapon); | |
478 this.Controls.Add(this.bttnAddWeapon); | |
479 this.Controls.Add(this.lblEquip); | |
480 this.Controls.Add(this.lblUnitSize); | |
481 this.Controls.Add(this.unitSize); | |
482 this.Controls.Add(this.tbUnitName); | |
483 this.Name = "FrmUnit"; | |
484 this.ShowIcon = false; | |
485 this.ShowInTaskbar = false; | |
486 this.Text = "FrmUnit"; | |
487 this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmUnit_FormClosing); | |
488 ((System.ComponentModel.ISupportInitialize)(this.unitSize)).EndInit(); | |
489 this.ResumeLayout(false); | |
490 this.PerformLayout(); | |
491 | |
492 } | |
493 #endregion | |
494 | |
495 public Unit Unit | |
496 { | |
497 get { return unit; } | |
498 } | |
499 | |
500 private void tbUnitName_Leave(object sender, System.EventArgs e) | |
501 { | |
502 UpdateUnitName(); | |
503 } | |
504 | |
505 private void tbUnitName_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) | |
506 { | |
507 if (e.KeyCode == Keys.Enter) | |
508 { | |
509 UpdateUnitName(); | |
510 } | |
511 } | |
512 | |
513 private void UpdateUnitName() | |
514 { | |
515 if (unit.Name != tbUnitName.Text) | |
516 { | |
517 commandStack.Execute(new SetNameCommand(unit, tbUnitName.Text)); | |
518 } | |
519 } | |
520 | |
521 private void unitSize_Leave(object sender, System.EventArgs e) | |
522 { | |
523 UpdateUnitSize(); | |
524 } | |
525 | |
526 private void unitSize_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) | |
527 { | |
528 if (e.KeyCode == Keys.Enter) | |
529 { | |
530 UpdateUnitSize(); | |
531 } | |
532 } | |
533 | |
534 private void UpdateUnitSize() | |
535 { | |
536 if (unit.Size != unitSize.Value) | |
537 { | |
538 commandStack.Execute(new SetUnitSizeCommand(unit, (int) unitSize.Value)); | |
539 } | |
540 } | |
541 | |
542 private void unit_NameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
543 { | |
544 if (obj is Unit && obj.Equals(unit)) | |
545 { | |
546 SetUnitName(); | |
547 } | |
548 } | |
549 | |
550 private void unit_UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) | |
551 { | |
552 if (obj is Unit && obj.Equals(unit)) | |
553 { | |
554 unitSize.Value = newValue; | |
555 } | |
556 } | |
557 | |
558 private void RefreshUnitEquipment() | |
559 { | |
560 foreach (UnitEquipmentChoice choice in equipmentChoices.Values) | |
561 { | |
562 SetEquipmentListValue(choice); | |
563 } | |
564 } | |
565 | |
566 private void equipmentList_SelectedIndexChanged(object sender, System.EventArgs e) | |
567 { | |
568 SetButtonsEnabledState(); | |
569 } | |
570 | |
571 private void SetButtonsEnabledState() | |
572 { | |
573 | |
574 UnitEquipmentItem equipItem = GetSelectedUnitEquipmentItem(); | |
575 bttnReplaceWeapon.Enabled = (equipItem != null && equipItem.HasAlternatives()); | |
576 bttnEditWeapon.Enabled = (equipItem != null); | |
577 bttnRemoveWeapon.Enabled = (equipItem != null && !equipItem.IsRequired); | |
578 } | |
579 | |
580 private void unit_UnitEquipmentAmountChanged(WarFoundryObject obj, double oldValue, double newValue) | |
581 { | |
582 if (obj is UnitEquipmentItem) | |
583 { | |
584 UnitEquipmentItem equip = (UnitEquipmentItem) obj; | |
585 UnitEquipmentChoice equipChoice = GetEquipmentChoice(equip); | |
586 | |
587 if (newValue == 0) | |
588 { | |
589 equipmentList.Items.Remove(equipChoice); | |
590 } | |
591 else | |
592 { | |
593 SetEquipmentListValue(equipChoice); | |
594 } | |
595 } | |
596 } | |
597 | |
598 private void SetEquipmentListValue(UnitEquipmentChoice equipChoice) | |
599 { | |
600 int idx = equipmentList.Items.IndexOf(equipChoice); | |
601 | |
602 if (idx > -1) | |
603 { | |
604 equipmentList.Items[idx] = equipChoice; | |
605 } | |
606 else | |
607 { | |
608 equipmentList.Items.Add(equipChoice); | |
609 } | |
610 } | |
611 | |
612 private void EditWeapon() | |
613 { | |
614 UnitEquipmentItem item = GetSelectedUnitEquipmentItem(); | |
615 | |
616 if (item != null) | |
617 { | |
618 FrmEditUnitEquipment editEquip = new FrmEditUnitEquipment(Unit, item, commandStack); | |
619 editEquip.ShowDialog(this); | |
620 } | |
621 } | |
622 | |
623 private UnitEquipmentItem GetSelectedUnitEquipmentItem() | |
624 { | |
625 UnitEquipmentChoice selectedItem = GetSelectedUnitEquipmentChoice(); | |
626 UnitEquipmentItem equipItem = null; | |
627 | |
628 if (selectedItem!=null) | |
629 { | |
630 equipItem = selectedItem.Item; | |
631 } | |
632 | |
633 return equipItem; | |
634 } | |
635 | |
636 private UnitEquipmentChoice GetSelectedUnitEquipmentChoice() | |
637 { | |
638 return (UnitEquipmentChoice) equipmentList.SelectedItem; | |
639 } | |
640 | |
641 private void bttnEditWeapon_Click(object sender, System.EventArgs e) | |
642 { | |
643 EditWeapon(); | |
644 } | |
645 | |
646 private void equipmentList_DoubleClick(object sender, System.EventArgs e) | |
647 { | |
648 EditWeapon(); | |
649 } | |
650 | |
651 private void AddWeapon() | |
652 { | |
653 FrmNewUnitEquipment newEquip = new FrmNewUnitEquipment(Unit, commandStack); | |
654 newEquip.ShowDialog(this); | |
655 } | |
656 | |
657 private void bttnAddWeapon_Click(object sender, System.EventArgs e) | |
658 { | |
659 AddWeapon(); | |
660 } | |
661 | |
662 private void RemoveWeapon() | |
663 { | |
664 commandStack.Execute(new SetUnitEquipmentNumericAmountCommand(unit, GetSelectedUnitEquipmentItem(), 0)); | |
665 } | |
666 | |
667 private void bttnRemoveWeapon_Click(object sender, System.EventArgs e) | |
668 { | |
669 RemoveWeapon(); | |
670 } | |
671 | |
672 private void bttnReplaceWeapon_Click(object sender, System.EventArgs e) | |
673 { | |
674 FrmReplaceUnitEquipment replace = new FrmReplaceUnitEquipment(unit, GetSelectedUnitEquipmentItem(), commandStack); | |
675 replace.ShowDialog(this); | |
676 } | |
677 | |
678 private void FrmUnit_FormClosing(object sender, FormClosingEventArgs e) | |
679 { | |
680 UpdateUnitName(); | |
681 UpdateUnitSize(); | |
682 } | |
683 } | |
27
526fefefb16b
Fixes #91: Fix WinForms rendering of unit stats
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
684 } |