| 1 | // This file (UnitDisplayWidget.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2009 IBBoard. |
|---|
| 2 | // |
|---|
| 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. |
|---|
| 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 | |
|---|
| 13 | namespace IBBoard.WarFoundry.GTK.Widgets |
|---|
| 14 | { |
|---|
| 15 | [System.ComponentModel.Category("WarFoundry GTK# GUI")] |
|---|
| 16 | [System.ComponentModel.ToolboxItem(true)] |
|---|
| 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 | CellRendererText renderer = new CellRendererText(); |
|---|
| 47 | unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); |
|---|
| 48 | |
|---|
| 49 | TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); |
|---|
| 50 | Stat[] stats = unit.UnitStatsArray; |
|---|
| 51 | |
|---|
| 52 | int length = stats.Length; |
|---|
| 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 | (cell as CellRendererText).Text = u.GetStatValue(column.Title); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | public IBBoard.WarFoundry.API.Objects.Unit Unit |
|---|
| 87 | { |
|---|
| 88 | get { return unit; } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue) |
|---|
| 92 | { |
|---|
| 93 | unitName.Text = newValue; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) |
|---|
| 97 | { |
|---|
| 98 | unitSize.Value = newValue; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args) |
|---|
| 102 | { |
|---|
| 103 | SetNewUnitSize(); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | [GLib.ConnectBefore ()] |
|---|
| 107 | protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args) |
|---|
| 108 | { |
|---|
| 109 | if (args.Event.Key == Gdk.Key.Return) |
|---|
| 110 | { |
|---|
| 111 | SetNewUnitSize(); |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | private void SetNewUnitSize() |
|---|
| 116 | { |
|---|
| 117 | if (unitSize.Value!=unit.Size) |
|---|
| 118 | { |
|---|
| 119 | SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); |
|---|
| 120 | stack.Execute(cmd); |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args) |
|---|
| 125 | { |
|---|
| 126 | SetNewUnitName(); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | [GLib.ConnectBefore ()] |
|---|
| 130 | protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args) |
|---|
| 131 | { |
|---|
| 132 | if (args.Event.Key == Gdk.Key.Return) |
|---|
| 133 | { |
|---|
| 134 | SetNewUnitName(); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | private void SetNewUnitName() |
|---|
| 139 | { |
|---|
| 140 | if (unitName.Text!=unit.Name) |
|---|
| 141 | { |
|---|
| 142 | SetNameCommand cmd = new SetNameCommand(unit, unitName.Text); |
|---|
| 143 | stack.Execute(cmd); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | } |
|---|