Mercurial > repos > IBBoard.WarFoundry.GUI.GTK
annotate Widgets/UnitDisplayWidget.cs @ 51:dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
* Start to implement Add functionality using a new abstraction concept that keeps the UI dumb and keeps the logic in a cross-toolkit controller
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 21 Aug 2010 20:00:03 +0000 |
parents | d29ad8445461 |
children | 4bad8cb3f889 |
rev | line source |
---|---|
19
a191d0655f55
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
1 // This file (UnitDisplayWidget.cs) is a part of the IBBoard.WarFoundry.GTK project and is copyright 2008, 2009 IBBoard. |
0 | 2 // |
19
a191d0655f55
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
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. |
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; | |
51
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
12 using IBBoard.WarFoundry.GUI.GTK.UIControl; |
0 | 13 |
5 | 14 namespace IBBoard.WarFoundry.GTK.Widgets |
0 | 15 { |
5 | 16 [System.ComponentModel.Category("WarFoundry GTK# GUI")] |
17 [System.ComponentModel.ToolboxItem(true)] | |
0 | 18 public partial class UnitDisplayWidget : Gtk.Bin |
19 { | |
20 private IBBoard.WarFoundry.API.Objects.Unit unit; | |
21 private CommandStack stack; | |
22 | |
23 public UnitDisplayWidget(IBBoard.WarFoundry.API.Objects.Unit sourceUnit, CommandStack commandStack) | |
24 { | |
25 this.Build(); | |
26 stack = commandStack; | |
27 unit = sourceUnit; | |
28 unitName.Text = unit.Name; | |
29 unitSize.Value = unit.Size; | |
48
23238d998535
Re #86: Complete initial GTK# UI
IBBoard <dev@ibboard.co.uk>
parents:
34
diff
changeset
|
30 notesView.Buffer.Text = unit.UnitType.Notes; |
0 | 31 double max = unit.UnitType.MaxSize; |
32 | |
33 if (max == -1) | |
34 { | |
35 max = double.MaxValue; | |
36 } | |
37 | |
38 unitSize.SetRange(unit.UnitType.MinSize, max); | |
39 unit.NameChanged+= new StringValChangedDelegate(UnitNameChanged); | |
40 unit.UnitSizeChanged+= new IntValChangedDelegate(UnitSizeChanged); | |
41 SetStats(); | |
13 | 42 } |
43 | |
44 private void SetStats() | |
45 { | |
46 //GameSystem system = unit.Army.GameSystem; | |
0 | 47 //SystemStats stats = system.StandardSystemStats; |
48 CellRendererText renderer = new CellRendererText(); | |
49 unitStats.AppendColumn(Translation.GetTranslation("UnitNameColumn", "Unit Type", null), renderer, new TreeCellDataFunc(RenderUnitName)); | |
50 | |
11
4e5e382fbd2e
Re #10 (Code readability) and Re #86 (Complete GTK# GUI):
IBBoard <dev@ibboard.co.uk>
parents:
5
diff
changeset
|
51 TreeCellDataFunc statFunc = new TreeCellDataFunc(RenderUnitStat); |
13 | 52 Stat[] stats = unit.UnitStatsArray; |
53 | |
54 int length = stats.Length; | |
55 | |
56 for (int i = 0; i < length; i++) | |
0 | 57 { |
34
a35c8be46006
Re #198: Add unit equipment slots
IBBoard <dev@ibboard.co.uk>
parents:
19
diff
changeset
|
58 unitStats.AppendColumn(stats[i].ParentSlotName, renderer, statFunc); |
13 | 59 } |
0 | 60 |
61 TreeStore model = new TreeStore(typeof(IBBoard.WarFoundry.API.Objects.Unit)); | |
13 | 62 model.AppendValues(unit); |
63 unitStats.Model = model; | |
0 | 64 } |
65 | |
66 private void RenderUnitName(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) | |
67 { | |
68 object o = model.GetValue(iter, 0); | |
69 | |
70 if (o is IBBoard.WarFoundry.API.Objects.Unit) | |
71 { | |
72 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; | |
73 (cell as CellRendererText).Text = u.UnitType.Name; | |
74 } | |
75 } | |
76 | |
77 private void RenderUnitStat(TreeViewColumn column, CellRenderer cell, TreeModel model, TreeIter iter) | |
78 { | |
79 object o = model.GetValue(iter, 0); | |
80 | |
81 if (o is IBBoard.WarFoundry.API.Objects.Unit) | |
82 { | |
83 IBBoard.WarFoundry.API.Objects.Unit u = (IBBoard.WarFoundry.API.Objects.Unit)o; | |
11
4e5e382fbd2e
Re #10 (Code readability) and Re #86 (Complete GTK# GUI):
IBBoard <dev@ibboard.co.uk>
parents:
5
diff
changeset
|
84 (cell as CellRendererText).Text = u.GetStatValue(column.Title); |
0 | 85 } |
86 } | |
15
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
87 |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
88 public IBBoard.WarFoundry.API.Objects.Unit Unit |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
89 { |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
90 get { return unit; } |
85db2c9a1546
Fixes #95: Can't re-open GTK# tabs
IBBoard <dev@ibboard.co.uk>
parents:
13
diff
changeset
|
91 } |
0 | 92 |
93 private void UnitNameChanged(WarFoundryObject obj, string oldValue, string newValue) | |
94 { | |
95 unitName.Text = newValue; | |
96 } | |
97 | |
98 private void UnitSizeChanged(WarFoundryObject obj, int oldValue, int newValue) | |
99 { | |
100 unitSize.Value = newValue; | |
101 } | |
102 | |
103 protected virtual void OnUnitSizeFocusOut (object o, Gtk.FocusOutEventArgs args) | |
104 { | |
105 SetNewUnitSize(); | |
106 } | |
107 | |
108 [GLib.ConnectBefore ()] | |
109 protected virtual void OnUnitSizeKeyPress (object o, Gtk.KeyPressEventArgs args) | |
110 { | |
51
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
111 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) |
0 | 112 { |
113 SetNewUnitSize(); | |
114 } | |
115 } | |
116 | |
117 private void SetNewUnitSize() | |
118 { | |
119 if (unitSize.Value!=unit.Size) | |
120 { | |
121 SetUnitSizeCommand cmd = new SetUnitSizeCommand(unit, (int)Math.Round(unitSize.Value)); | |
122 stack.Execute(cmd); | |
123 } | |
124 } | |
125 | |
126 protected virtual void OnUnitNameFocusOut (object o, Gtk.FocusOutEventArgs args) | |
127 { | |
128 SetNewUnitName(); | |
129 } | |
130 | |
131 [GLib.ConnectBefore ()] | |
132 protected virtual void OnUnitNameKeyPress (object o, Gtk.KeyPressEventArgs args) | |
133 { | |
49 | 134 if (args.Event.Key == Gdk.Key.Return || args.Event.Key == Gdk.Key.KP_Enter) |
0 | 135 { |
136 SetNewUnitName(); | |
137 } | |
138 } | |
139 | |
140 private void SetNewUnitName() | |
141 { | |
51
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
142 if (unitName.Text != unit.Name) |
0 | 143 { |
144 SetNameCommand cmd = new SetNameCommand(unit, unitName.Text); | |
145 stack.Execute(cmd); | |
146 } | |
147 } | |
51
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
148 |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
149 private void OnBttnAddEquipmentActivated(object sender, System.EventArgs e) |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
150 { |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
151 Console.WriteLine("Add button clicked"); |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
152 AddEquipment(); |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
153 } |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
154 |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
155 private void AddEquipment() |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
156 { |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
157 AddEquipmentUIControl addEquipment = new AddEquipmentUIControl(unit, stack); |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
158 addEquipment.Show(); |
dafbd432ca23
Re #60: Add UI to add/remove/edit weapons in GTK
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
159 } |
0 | 160 } |
161 } |