5
|
1 // This file (FrmNewUnit.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2009 IBBoard.
|
0
|
2 //
|
5
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
|
0
|
4
|
|
5 using System;
|
|
6 using System.Collections.Generic;
|
|
7 using IBBoard.WarFoundry.API.Objects;
|
|
8 using IBBoard.WarFoundry.API.Requirements;
|
|
9 using Gtk;
|
|
10 using log4net;
|
|
11
|
5
|
12 namespace IBBoard.WarFoundry.GTK
|
0
|
13 {
|
|
14 public partial class FrmNewUnit : Gtk.Dialog
|
|
15 {
|
|
16 private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
|
|
17
|
|
18 private UnitType unitType;
|
|
19 private Army unitArmy;
|
|
20
|
|
21 public FrmNewUnit(Race race, Category cat, Army army)
|
|
22 {
|
|
23 this.Build();
|
|
24 unitArmy = army;
|
|
25
|
|
26 TreeViewColumn unitTypeColumn = new TreeViewColumn ();
|
|
27 unitTypeColumn.Title = "Unit Type";
|
|
28 CellRendererText unitTypeCell = new CellRendererText ();
|
|
29 unitTypeColumn.PackStart(unitTypeCell, true);
|
|
30 lstUnitTypes.AppendColumn(unitTypeColumn);
|
|
31 unitTypeColumn.SetCellDataFunc (unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
|
|
32 ListStore store = new ListStore(typeof(UnitType));
|
|
33 UnitType[] types = race.GetUnitTypes(cat);
|
|
34 logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
|
|
35
|
|
36 foreach (UnitType type in types)
|
|
37 {
|
|
38 logger.DebugFormat("Adding unit type {0}", type.Name);
|
|
39 store.AppendValues(type);
|
|
40 }
|
|
41
|
|
42 lstUnitTypes.Model = store;
|
|
43 lstUnitTypes.Selection.Changed+= new EventHandler(OnSelectionChanged);
|
|
44 }
|
|
45
|
|
46 private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
|
|
47 {
|
|
48 UnitType type = (UnitType)model.GetValue(iter, 0);
|
|
49 (cell as CellRendererText).Text = type.Name;
|
|
50 }
|
|
51
|
|
52 protected virtual void OnSelectionChanged(object o, EventArgs e)
|
|
53 {
|
|
54 SetSelectUnitEnabledVal();
|
|
55 }
|
|
56
|
|
57 public UnitType SelectedUnit
|
|
58 {
|
|
59 get { return unitType; }
|
|
60 }
|
|
61
|
|
62 private UnitType GetSelectedUnitType()
|
|
63 {
|
|
64 TreeModel model;
|
|
65 TreeIter iter;
|
|
66 lstUnitTypes.Selection.GetSelected (out model, out iter);
|
|
67 UnitType toReturn = null;
|
|
68
|
|
69 if (model!=null)
|
|
70 {
|
|
71 toReturn = (UnitType) model.GetValue(iter, 0);
|
|
72 }
|
|
73
|
|
74 return toReturn;
|
|
75 }
|
|
76
|
|
77 private void SetSelectUnitEnabledVal()
|
|
78 {
|
|
79 UnitType type = GetSelectedUnitType();
|
|
80
|
|
81 if (type!=null)
|
|
82 {
|
|
83 buttonOk.Sensitive = true;
|
|
84 List<FailedUnitRequirement> fails = unitArmy.CanAddUnitType(type);
|
|
85 lblNewUnitWarning.Visible = (fails != null);
|
|
86
|
|
87 if (fails.Count > 0)
|
|
88 {
|
|
89 //FIXME: currently only show the first error
|
|
90 lblNewUnitWarning.Text = fails[0].Description;
|
|
91 }
|
|
92 }
|
|
93 else
|
|
94 {
|
|
95 buttonOk.Sensitive = false;
|
|
96 lblNewUnitWarning.Visible = false;
|
|
97 }
|
|
98 }
|
|
99
|
|
100 protected virtual void OnButtonCancelActivated (object sender, System.EventArgs e)
|
|
101 {
|
|
102 Respond(ResponseType.Cancel);
|
|
103 }
|
|
104
|
|
105 protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args)
|
|
106 {
|
|
107 unitType = GetSelectedUnitType();
|
|
108 Respond(ResponseType.Ok);
|
|
109 }
|
|
110
|
|
111 protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
|
|
112 {
|
|
113 unitType = GetSelectedUnitType();
|
|
114 Respond(ResponseType.Ok);
|
|
115 }
|
|
116 }
|
|
117 }
|