view FrmNewUnit.cs @ 19:a191d0655f55

Re #121: Migrate to AGPL license * Update all GTK# GUI files to AGPL license * Include AGPL license and remove GPL/LGPL documents * Fix copyright dates where they're known
author IBBoard <dev@ibboard.co.uk>
date Sat, 15 Aug 2009 10:46:08 +0000
parents 6b2c8e55564a
children 7055b24cfc79 4a33b3012100
line source
1 // This file (FrmNewUnit.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 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.
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;
12 namespace IBBoard.WarFoundry.GTK
13 {
14 public partial class FrmNewUnit : Gtk.Dialog
15 {
16 private ILog logger = LogManager.GetLogger(typeof(FrmNewUnit));
18 private UnitType unitType;
19 private Army unitArmy;
21 public FrmNewUnit(Race race, Category cat, Army army)
22 {
23 this.Build();
24 unitArmy = army;
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);
36 foreach (UnitType type in types)
37 {
38 logger.DebugFormat("Adding unit type {0}", type.Name);
39 store.AppendValues(type);
40 }
42 lstUnitTypes.Model = store;
43 lstUnitTypes.Selection.Changed+= new EventHandler(OnSelectionChanged);
44 }
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 }
52 protected virtual void OnSelectionChanged(object o, EventArgs e)
53 {
54 SetSelectUnitEnabledVal();
55 }
57 public UnitType SelectedUnit
58 {
59 get { return unitType; }
60 }
62 private UnitType GetSelectedUnitType()
63 {
64 TreeModel model;
65 TreeIter iter;
66 lstUnitTypes.Selection.GetSelected (out model, out iter);
67 UnitType toReturn = null;
69 if (model!=null)
70 {
71 toReturn = (UnitType) model.GetValue(iter, 0);
72 }
74 return toReturn;
75 }
77 private void SetSelectUnitEnabledVal()
78 {
79 UnitType type = GetSelectedUnitType();
81 if (type!=null)
82 {
83 buttonOk.Sensitive = true;
84 List<FailedUnitRequirement> fails = unitArmy.CanAddUnitType(type);
85 lblNewUnitWarning.Visible = (fails != null);
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 }
100 protected virtual void OnButtonCancelActivated (object sender, System.EventArgs e)
101 {
102 Respond(ResponseType.Cancel);
103 }
105 protected virtual void OnRowActivated (object o, Gtk.RowActivatedArgs args)
106 {
107 unitType = GetSelectedUnitType();
108 Respond(ResponseType.Ok);
109 }
111 protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
112 {
113 unitType = GetSelectedUnitType();
114 Respond(ResponseType.Ok);
115 }
116 }
117 }