comparison Widgets/UnitDisplayWidget.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 // UnitDisplayWidget.cs
2 //
3 // Copyright (C) 2008 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 Gtk;
23 using IBBoard.Commands;
24 using IBBoard.Lang;
25 using IBBoard.WarFoundry.API;
26 using IBBoard.WarFoundry.API.Objects;
27 using IBBoard.WarFoundry.API.Commands;
28
29 namespace IBBoard.WarFoundry.Widgets
30 {
31 public partial class UnitDisplayWidget : Gtk.Bin
32 {
33 private IBBoard.WarFoundry.API.Objects.Unit unit;
34 private CommandStack stack;
35
36 public UnitDisplayWidget(IBBoard.WarFoundry.API.Objects.Unit sourceUnit, CommandStack commandStack)
37 {
38 this.Build();
39 stack = commandStack;
40 unit = sourceUnit;
41 unitName.Text = unit.Name;
42 unitSize.Value = unit.Size;
43 double max = unit.UnitType.MaxSize;
44
45 if (max == -1)
46 {
47 max = double.MaxValue;
48 }
49
50 unitSize.SetRange(unit.UnitType.MinSize, max);
51 unit.NameChanged+= new StringValChangedDelegate(UnitNameChanged);
52 unit.UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChanged);
53 SetStats();
54 }
55
56 private void SetStats()
57 {
58 //GameSystem system = unit.Army.GameSystem;
59 //SystemStats stats = system.StandardSystemStats;
60 Stats stats = unit.UnitType.UnitStats;
61 CellRendererText renderer = new CellRendererText();
62 unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName));
63
64 TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat);
65
66 int length = stats.StatCount;
67
68 for (int i = 0; i < length; i++)
69 {
70 unitStats.AppendColumn(stats[i].ParentSlot.Name, renderer, statFunc);
71 }
72
73 TreeStore model = new TreeStore(typeof(IBBoard.WarFoundry.API.Objects.Unit));
74 model.AppendValues(unit);
75 unitStats.Model = model;
76 }
77
78 private void RenderUnitName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
79 {
80 object o = model.GetValue(iter, 0);
81
82 if (o is IBBoard.WarFoundry.API.Objects.Unit)
83 {
84 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o;
85 (cell as CellRendererText).Text = u.UnitType.Name;
86 }
87 }
88
89 private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter)
90 {
91 object o = model.GetValue(iter, 0);
92
93 if (o is IBBoard.WarFoundry.API.Objects.Unit)
94 {
95 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o;
96 //TODO Check for correct value based on the column
97 (cell as CellRendererText).Text = u.UnitStats[column.Title].SlotValueString;
98 }
99 }
100
101 private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue)
102 {
103 unitName.Text = newValue;
104 }
105
106 private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue)
107 {
108 unitSize.Value = newValue;
109 }
110
111 protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args)
112 {
113 SetNewUnitSize();
114 }
115
116 [GLib.ConnectBefore ()]
117 protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args)
118 {
119 if (args.Event.Key == Gdk.Key.Return)
120 {
121 SetNewUnitSize();
122 }
123 }
124
125 private void SetNewUnitSize()
126 {
127 if (unitSize.Value!=unit.Size)
128 {
129 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value));
130 stack.Execute(cmd);
131 }
132 }
133
134 protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args)
135 {
136 SetNewUnitName();
137 }
138
139 [GLib.ConnectBefore ()]
140 protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args)
141 {
142 if (args.Event.Key == Gdk.Key.Return)
143 {
144 SetNewUnitName();
145 }
146 }
147
148 private void SetNewUnitName()
149 {
150 if (unitName.Text!=unit.Name)
151 {
152 SetNameCommand cmd = new SetNameCommand(unit, unitName.Text);
153 stack.Execute(cmd);
154 }
155 }
156 }
157 }