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