comparison FrmNewUnit.cs @ 0:1bb28f84d567

Initial commit of WarFoundry code
author IBBoard <dev@ibboard.co.uk>
date Fri, 19 Dec 2008 15:57:51 +0000
parents
children 65279b85446f
comparison
equal deleted inserted replaced
-1:000000000000 0:1bb28f84d567
1 // FrmNewUnit.cs
2 //
3 // Copyright (C) 2007 IBBoard
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License version 2.1 of the License as published by the Free
8 // Software Foundation.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 //
19 //
20
21 using System;
22 using System.Collections.Generic;
23 using IBBoard.WarFoundry.API.Objects;
24 using IBBoard.WarFoundry.API.Requirements;
25 using Gtk;
26 using log4net;
27
28 namespace IBBoard.WarFoundry
29 {
30 public partial class FrmNewUnit : Gtk.Dialog
31 {
32 private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
33
34 private UnitType unitType;
35 private Army unitArmy;
36
37 public FrmNewUnit(Race race, Category cat, Army army)
38 {
39 this.Build();
40 unitArmy = army;
41
42 TreeViewColumn unitTypeColumn = new TreeViewColumn ();
43 unitTypeColumn.Title = "Unit Type";
44 CellRendererText unitTypeCell = new CellRendererText ();
45 unitTypeColumn.PackStart(unitTypeCell, true);
46 lstUnitTypes.AppendColumn(unitTypeColumn);
47 unitTypeColumn.SetCellDataFunc (unitTypeCell, new TreeCellDataFunc(RenderUnitTypeName));
48 ListStore store = new ListStore(typeof(UnitType));
49 UnitType[] types = race.GetUnitTypes(cat);
50 logger.DebugFormat("Listing {0} unit types in {1}", types.Length, cat.Name);
51
52 foreach (UnitType type in types)
53 {
54 logger.DebugFormat("Adding unit type {0}", type.Name);
55 store.AppendValues(type);
56 }
57
58 lstUnitTypes.Model = store;
59 lstUnitTypes.Selection.Changed+= new EventHandler(OnSelectionChanged);
60 }
61
62 private void RenderUnitTypeName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
63 {
64 UnitType type = (UnitType)model.GetValue(iter, 0);
65 (cell as CellRendererText).Text = type.Name;
66 }
67
68 protected virtual void OnSelectionChanged(object o, EventArgs e)
69 {
70 SetSelectUnitEnabledVal();
71 }
72
73 public UnitType SelectedUnit
74 {
75 get { return unitType; }
76 }
77
78 private UnitType GetSelectedUnitType()
79 {
80 TreeModel model;
81 TreeIter iter;
82 lstUnitTypes.Selection.GetSelected (out model, out iter);
83 UnitType toReturn = null;
84
85 if (model!=null)
86 {
87 toReturn = (UnitType) model.GetValue(iter, 0);
88 }
89
90 return toReturn;
91 }
92
93 private void SetSelectUnitEnabledVal()
94 {
95 UnitType type = GetSelectedUnitType();
96
97 if (type!=null)
98 {
99 buttonOk.Sensitive = true;
100 List<FailedUnitRequirement> fails = unitArmy.CanAddUnitType(type);
101 lblNewUnitWarning.Visible = (fails != null);
102
103 if (fails.Count > 0)
104 {
105 //FIXME: currently only show the first error
106 lblNewUnitWarning.Text = fails[0].Description;
107 }
108 }
109 else
110 {
111 buttonOk.Sensitive = false;
112 lblNewUnitWarning.Visible = false;
113 }
114 }
115
116 protected virtual void OnButtonCancelActivated (object sender, System.EventArgs e)
117 {
118 Respond(ResponseType.Cancel);
119 }
120
121 protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args)
122 {
123 unitType = GetSelectedUnitType();
124 Respond(ResponseType.Ok);
125 }
126
127 protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
128 {
129 unitType = GetSelectedUnitType();
130 Respond(ResponseType.Ok);
131 }
132 }
133 }